user-address-save.js 13.4 KB
Newer Older
D
devil_gong 已提交
1 2 3
const app = getApp();
Page({
  data: {
D
devil 已提交
4
    params: null,
5 6 7 8
    data_list_loding_status: 1,
    data_list_loding_msg: '',
    editor_path_type: '',
    address_data: null,
D
devil 已提交
9 10 11
    province_list: [],
    city_list: [],
    county_list: [],
12 13 14 15
    province_id: null,
    city_id: null,
    county_id: null,
    idcard_images_data: {},
D
devil_gong 已提交
16 17 18 19 20

    default_province: "请选择省",
    default_city: "请选择市",
    default_county: "请选择区/县",

D
devil_gong 已提交
21 22 23
    province_value: null,
    city_value: null,
    county_value: null,
24 25 26 27 28

    user_location_cache_key: app.data.cache_userlocation_key,
    user_location: null,

    form_submit_disabled_status: false,
29 30 31 32

    // 基础配置
    home_user_address_map_status: 0,
    home_user_address_idcard_status : 0,
D
devil_gong 已提交
33 34 35
  },

  onLoad(params) {
36
    this.setData({ params: params });
D
devil_gong 已提交
37 38
  },

39
  onReady: function () {
D
devil_gong 已提交
40 41 42 43 44 45 46
    if((this.data.params.id || null) == null)
    {
      var title = app.data.common_pages_title.user_address_save_add;
    } else {
      var title = app.data.common_pages_title.user_address_save_edit;
    }
    qq.setNavigationBarTitle({title: title});
47 48 49

    // 清除位置缓存信息
    qq.removeStorage({key: this.data.user_location_cache_key});
D
devil_gong 已提交
50 51 52
    this.init();
  },

53 54 55 56
  onShow() {
    this.user_location_init();
  },

57 58 59 60 61 62 63 64 65 66 67 68 69
  // 初始化配置
  init_config(status) {
    if((status || false) == true) {
      this.setData({
        home_user_address_map_status: app.get_config('config.home_user_address_map_status'),
        home_user_address_idcard_status: app.get_config('config.home_user_address_idcard_status')
      });
    } else {
      app.is_config(this, 'init_config');
    }
  },

  // 获取数据
D
devil_gong 已提交
70
  init() {
D
Devil 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84
    var user = app.get_user_info(this, "init");
    if (user != false) {
      // 用户未绑定用户则转到登录页面
      if (app.user_is_need_login(user)) {
        qq.redirectTo({
          url: "/pages/login/login?event_callback=init"
        });
        this.setData({
          data_list_loding_status: 2,
          data_list_loding_msg: '请先绑定手机号码',
        });
        return false;
      } else {
        this.get_province_list();
85
        this.get_data();
D
Devil 已提交
86 87 88 89 90 91
      }
    } else {
      this.setData({
        data_list_loding_status: 2,
        data_list_loding_msg: '请先授权用户信息',
      });
D
devil_gong 已提交
92 93 94
    }
  },

95 96
  // 获取数据
  get_data() {
D
devil_gong 已提交
97 98 99 100 101 102 103 104 105
    var self = this;
    qq.request({
      url: app.get_request_url("detail", "useraddress"),
      method: "POST",
      data: self.data.params,
      dataType: "json",
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      success: res => {
        if (res.data.code == 0) {
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
          var data = res.data.data || null;
          var ads_data = data.data || null;
          var idcard_images = {
            idcard_front: (ads_data == null) ? '' : ads_data.idcard_front || '',
            idcard_back: (ads_data == null) ? '' : ads_data.idcard_back || '',
          };
          self.setData({
            address_data: ads_data,
            idcard_images_data: idcard_images,
            editor_path_type: data.editor_path_type || '',
          });

          // 数据设置
          if(ads_data != null)
          {
D
devil_gong 已提交
121
            self.setData({
122 123 124
              province_id: ads_data.province || null,
              city_id: ads_data.city || null,
              county_id: ads_data.county || null,
D
devil_gong 已提交
125 126
            });

127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
            // 地理位置
            var lng = ads_data.lng || null;
            var lat = ads_data.lat || null;
            if (lng != null && lat != null)
            {
              self.setData({ user_location: {
                lng: lng,
                lat: lat,
                address: ads_data.address || '',
              }});
            }
          }
          
          // 获取城市、区县
          self.get_city_list();
          self.get_county_list();
D
devil_gong 已提交
143

144 145 146 147
          // 半秒后初始化数据
          setTimeout(function () {
            self.init_region_value();
          }, 500);
D
devil_gong 已提交
148
        } else {
D
Devil 已提交
149 150 151
          if (app.is_login_check(res.data)) {
            app.showToast(res.data.msg);
          }
D
devil_gong 已提交
152 153 154
        }
      },
      fail: () => {
155
        app.showToast("省份信息失败");
D
devil_gong 已提交
156 157 158 159
      }
    });
  },

160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
  // 地区数据初始化
  init_region_value() {
     this.setData({
      province_value: this.get_region_value("province_list", "province_id"),
      city_value: this.get_region_value("city_list", "city_id"),
      county_value: this.get_region_value("county_list", "county_id"),
    });
  },

  // 地区初始化匹配索引
  get_region_value(list, id) {
    var data = this.data[list];
    var data_id = this.data[id];
    var value = null;
    data.forEach((d, i) => {
      if (d.id == data_id) {
        value = i;
        return false;
      }
    });
    return value;
  },

  // 获取省份
D
devil_gong 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
  get_province_list() {
    var self = this;
    qq.request({
      url: app.get_request_url("index", "region"),
      method: "POST",
      data: {},
      dataType: "json",
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      success: res => {
        if (res.data.code == 0) {
          var data = res.data.data;
          self.setData({
            province_list: data
          });
        } else {
          app.showToast(res.data.msg);
        }
      },
      fail: () => {
203
        app.showToast("省份获取失败");
D
devil_gong 已提交
204 205 206 207
      }
    });
  },

208
  // 获取市
D
devil_gong 已提交
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
  get_city_list() {
    var self = this;
    if (self.data.province_id) {
      qq.request({
        url: app.get_request_url("index", "region"),
        method: "POST",
        data: {
          pid: self.data.province_id
        },
        dataType: "json",
        header: { 'content-type': 'application/x-www-form-urlencoded' },
        success: res => {
          if (res.data.code == 0) {
            var data = res.data.data;
            self.setData({
              city_list: data
            });
          } else {
            app.showToast(res.data.msg);
          }
        },
        fail: () => {
231
          app.showToast("城市获取失败");
D
devil_gong 已提交
232 233 234 235 236
        }
      });
    }
  },

237
  // 获取区/县
D
devil_gong 已提交
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
  get_county_list() {
    var self = this;
    if (self.data.city_id) {
      // 加载loding
      qq.request({
        url: app.get_request_url("index", "region"),
        method: "POST",
        data: {
          pid: self.data.city_id
        },
        dataType: "json",
        header: { 'content-type': 'application/x-www-form-urlencoded' },
        success: res => {
          if (res.data.code == 0) {
            var data = res.data.data;
            self.setData({
              county_list: data
            });
          } else {
            app.showToast(res.data.msg);
          }
        },
        fail: () => {
261
          app.showToast("区/县获取失败");
D
devil_gong 已提交
262 263 264 265 266
        }
      });
    }
  },

267 268 269 270 271
  // 省份事件
  select_province_event(e) {
    var index = e.detail.value || 0;
    if (index >= 0) {
      var data = this.data.province_list[index];
D
devil_gong 已提交
272
      this.setData({
273
        province_value: index,
D
devil_gong 已提交
274 275 276 277 278 279 280 281
        province_id: data.id,
        city_value: null,
        county_value: null,
        city_id: null,
        county_id: null
      });
      this.get_city_list();
    }
D
devil_gong 已提交
282 283
  },

284 285 286 287 288
  // 市事件
  select_city_event(e) {
    var index = e.detail.value || 0;
    if (index >= 0) {
      var data = this.data.city_list[index];
D
devil_gong 已提交
289
      this.setData({
290
        city_value: index,
D
devil_gong 已提交
291 292 293 294 295 296
        city_id: data.id,
        county_value: null,
        county_id: null
      });
      this.get_county_list();
    }
D
devil_gong 已提交
297 298
  },

299 300 301 302 303
  // 区/县事件
  select_county_event(e) {
    var index = e.detail.value || 0;
    if (index >= 0) {
      var data = this.data.county_list[index];
D
devil_gong 已提交
304
      this.setData({
305
        county_value: index,
D
devil_gong 已提交
306 307 308
        county_id: data.id
      });
    }
D
devil_gong 已提交
309 310
  },

311 312 313 314 315 316 317 318 319 320 321 322
  // 省市区未按照顺序选择提示
  region_select_error_event(e) {
    var value = e.currentTarget.dataset.value || null;
    if (value != null) {
      app.showToast(value);
    }
  },

  // 选择地理位置
  choose_location_event(e) {
    qq.navigateTo({
      url: '/pages/common/open-setting-location/open-setting-location'
D
devil_gong 已提交
323 324 325
    });
  },

326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
  // 地址信息初始化
  user_location_init() {
    var result = qq.getStorageSync(this.data.user_location_cache_key) || null;
    var data = null;
    if (result != null)
    {
      data = {
        name: result.name || null,
        address: result.address || null,
        lat: result.latitude || null,
        lng: result.longitude || null
      }
    }
    this.setData({user_location: data});
  },

  // 文件上传
  file_upload_event(e) {
    var form_name = e.currentTarget.dataset.value || null;
    if(form_name == null) {
      app.showToast('表单名称类型有误');
      return false;
    }

    var self = this;
    qq.chooseImage({
      count: 1,
      success(res) {
        var success = 0;
        var fail = 0;
        var length = res.tempFilePaths.length;
        var count = 0;
        self.upload_one_by_one(res.tempFilePaths, success, fail, count, length, form_name);
      }
    });
  },

  // 采用递归的方式上传多张
  upload_one_by_one(img_paths, success, fail, count, length, form_name) {
    var self = this;
    qq.uploadFile({
      url: app.get_request_url("index", "ueditor"),
      filePath: img_paths[count],
      name: 'upfile',
      formData: {
        action: 'uploadimage',
        path_type: self.data.editor_path_type
      },
      success: function (res) {
        success++;
        if (res.statusCode == 200) {
          var data = (typeof (res.data) == 'object') ? res.data : JSON.parse(res.data);
          if (data.code == 0 && (data.data.url || null) != null) {
            var temp_idcard_images_data = self.data.idcard_images_data || {};
            temp_idcard_images_data[form_name] = data.data.url;
            self.setData({ idcard_images_data: temp_idcard_images_data });
          } else {
            app.showToast(data.msg);
          }
        }
      },
      fail: function (e) {
        fail++;
      },
      complete: function (e) {
        count++; // 下一张
        if (count >= length) {
          // 上传完毕,作一下提示
          //app.showToast('上传成功' + success +'张', 'success');
        } else {
          // 递归调用,上传下一张
          self.upload_one_by_one(img_paths, success, fail, count, length, form_name);
        }
      }
    });
  },

  // 图片删除
  upload_delete_event(e) {
    var form_name = e.currentTarget.dataset.value || null;
    if(form_name == null) {
      app.showToast('表单名称类型有误');
      return false;
    }

    var self = this;
    qq.showModal({
      title: '温馨提示',
      content: '删除后不可恢复、继续吗?',
      success(res) {
        if (res.confirm) {
          var temp_idcard_images_data = self.data.idcard_images_data || {};
          temp_idcard_images_data[form_name] = '';
          self.setData({ idcard_images_data: temp_idcard_images_data });
        }
D
devil_gong 已提交
421 422 423 424
      }
    });
  },

D
Devil 已提交
425
  // 数据提交
D
devil_gong 已提交
426
  form_submit(e) {
427
    var self = this;
D
devil_gong 已提交
428 429 430 431 432
    // 表单数据
    var form_data = e.detail.value;

    // 数据校验
    var validation = [
433 434
      { fields: "name", msg: "请填写联系人" },
      { fields: "tel", msg: "请填写联系电话" },
D
devil_gong 已提交
435 436 437
      { fields: "province", msg: "请选择省份" },
      { fields: "city", msg: "请选择城市" },
      { fields: "county", msg: "请选择区县" },
438 439 440
      { fields: "address", msg: "请填写详细地址" },
      { fields: "lng", msg: "请选择地理位置" },
      { fields: "lat", msg: "请选择地理位置" }
D
devil_gong 已提交
441 442
    ];

443 444 445
    form_data["province"] = self.data.province_id;
    form_data["city"] = self.data.city_id;
    form_data["county"] = self.data.county_id;
D
devil_gong 已提交
446
    form_data["id"] = self.data.params.id || 0;
447 448 449
    form_data["is_default"] = form_data.is_default == true ? 1 : 0;
    form_data['idcard_front'] = self.data.idcard_images_data.idcard_front || '';
    form_data['idcard_back'] = self.data.idcard_images_data.idcard_back || '';
D
devil_gong 已提交
450

451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
    // 地理位置
    var lng = 0;
    var lat = 0;
    if((self.data.user_location || null) != null) {
      lng = self.data.user_location.lng || 0;
      lat = self.data.user_location.lat || 0;
    }
    if((self.data.address_data || null) != null) {
      if((lng || null) == null) {
        lng = self.data.address_data.lng || 0;
      }
      if((lat || null) == null) {
        lat = self.data.address_data.lat || 0;
      }
    }
    form_data["lng"] = lng;
    form_data["lat"] = lat;
D
devil_gong 已提交
468

469 470 471 472 473
    // 验证提交表单
    if (app.fields_check(form_data, validation)) {
      // 数据保存
      self.setData({ form_submit_disabled_status: true });
      qq.showLoading({ title: "处理中..." });
D
devil_gong 已提交
474 475 476 477 478 479 480 481 482 483
      qq.request({
        url: app.get_request_url("save", "useraddress"),
        method: "POST",
        data: form_data,
        dataType: "json",
        header: { 'content-type': 'application/x-www-form-urlencoded' },
        success: res => {
          qq.hideLoading();
          if (res.data.code == 0) {
            app.showToast(res.data.msg, "success");
484
            setTimeout(function () {
D
devil_gong 已提交
485 486 487
              qq.navigateBack();
            }, 1000);
          } else {
488
            self.setData({ form_submit_disabled_status: false });
D
Devil 已提交
489 490 491 492 493
            if (app.is_login_check(res.data)) {
              app.showToast(res.data.msg);
            } else {
              app.showToast('提交失败,请重试!');
            }
D
devil_gong 已提交
494 495 496
          }
        },
        fail: () => {
497
          self.setData({ form_submit_disabled_status: false });
D
devil_gong 已提交
498 499 500 501 502
          qq.hideLoading();
          app.showToast("服务器请求出错");
        }
      });
    }
D
Devil 已提交
503
  },
D
devil_gong 已提交
504
});