// pages/addAddress/addAddress.js const getReq = require('./../../config.js').getReq let appInstance = getApp() Page({ /** * 页面的初始数据 */ data: { checked: true, areas: [], tree: [], value: [0, 0, 0], oldval: [0, 0, 0], citys: [], districts: [], animation_flag: false, province: '', city: '', district: '', name: '', mob_phone: '', address: '' }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { this.getDatas() }, toggleChecked() { this.setData({ checked: !this.data.checked }) }, bindChange: function (e) { const val = e.detail.value if (this.data.value[0] != val[0]) { val[1] = 0 val[2] = 0 } else { //若省份column未做滑动,地级市做了滑动则定位区县第一位 if (this.data.value[1] != val[1]) { val[2] = 0 } } this.setData({ value: val, citys: this.data.tree[val[0]].children, districts: this.data.tree[val[0]].children[val[1]].children }) }, getDatas() { wx.showLoading({ title: '加载中', }) var self = this getReq({ act: 'app_update', op: 'area', curpage: 1 }, function (res) { wx.hideLoading() if (res.code == 200) { let tree = self.arrayToTree(res.datas.areas) self.setData({ areas: res.datas.areas, tree, citys: tree[0].children, districts: tree[0].children[0].children }) } else { wx.showToast({ icon: 'none', title: res.message, duration: 2000 }) } }) }, arrayToTree(array) { let result = [], hash = {}, children = 'children' array.forEach((item, index) => { hash[item.aid] = item }) array.forEach((item, index) => { let pid = item.pid if (pid == 0) { result.push(item) } else { let hashvp = hash[item.pid] if (!hashvp[children]) { hashvp[children] = [] } hashvp[children].push(item) } }) return result }, animation_flag(e) { let flag = e.currentTarget.dataset.flag let animation_flag = flag == 'true' ? true : false let val = this.data.oldval this.setData({ animation_flag, value: val, citys: this.data.tree[val[0]].children, districts: this.data.tree[val[0]].children[val[1]].children }) }, setAddress() { let val = this.data.value let province = this.data.tree[val[0]] let city = province.children[val[1]] let district = province.children[val[1]].children[val[2]] this.setData({ animation_flag: false, oldval: val, province, city, district }) }, commmitAddress() { let self = this let name = this.trim(this.data.name) let phone = this.trim(this.data.mob_phone) let address = this.trim(this.data.address) let province = this.data.province if (!name) { wx.showToast({ icon: 'none', title: "请填写您的收货人名称", duration: 1500 }) return } if (!(/^1(3|4|5|7|8)\d{9}$/.test(phone))) { wx.showToast({ icon: 'none', title: "请填写您的手机号", duration: 1500 }) return } if (!province) { wx.showToast({ icon: 'none', title: "请选择您的收货地址", duration: 1500 }) return } if (!address) { wx.showToast({ icon: 'none', title: "请填写的您的详细收货地址", duration: 1500 }) return } getReq({ act: 'member_address', op: 'address_add', curpage: 1, true_name: name, address, mob_phone: phone, area_id: this.data.district.aid }, function (res) { wx.hideLoading() if (res.code == 200) { let address_id = res.datas.address_id let is_default = self.data.checked ? 1 : 0 if (is_default) { let area_info = self.data.province.n + self.data.city.n + self.data.district.n appInstance.checkDefaultAddress({ true_name: name, mob_phone: phone, area_info, address, address_id }) } getReq({ act: 'member_address', op: 'set_default', curpage: 1, address_id, is_default }, function (response) { wx.hideLoading() if (response.code == 200) { wx.navigateBack() } else { wx.showToast({ icon: 'none', title: response.message, duration: 2000 }) } }) } else { wx.showToast({ icon: 'none', title: res.message, duration: 2000 }) } }) }, trim(str) { str = str.replace(/\s+/g, "") return str }, bindKeyInput(e) { let valueType = e.currentTarget.dataset.value let value = e.detail.value switch (valueType) { case 'name': this.setData({ name: value }) break; case 'phone': this.setData({ mob_phone: value }) break; case 'address': this.setData({ address: value }) break; default: break; } }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { } })