// pages/addAddress/addAddress.js const getReq = require('../../config.js').getReq import trees from '../../areas.js' const app = getApp() Page({ /** * 页面的初始数据 */ data: { checked: false, areas: [], tree: [], value: [0, 0, 0], oldval: [0, 0, 0], citys: [], districts: [], animation_flag: false, name: '', mob_phone: '', address: '', area_info: '', area_id: '', isEdit: false, address_id: '', city_id: '', bindName: '', bindPhone: '', bindAddress: '', show: true }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { if (options.true_name) { this.setData({ name: options.true_name, bindName: options.true_name, mob_phone: options.mob_phone, bindPhone: options.mob_phone, address: options.address, bindAddress: options.address, area_info: options.area_info, area_id: options.area_id, isEdit: true, address_id: options.address_id, city_id: options.city_id }) } this.setData({ show: false, tree: trees, citys: trees[0].children, districts: trees[0].children[0].children }) // 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() { var self = this getReq(app,{ act: 'app_update', op: 'area', curpage: 1 }, function (res) { if (res.code == 200) { self.setData({ show: false, }) } 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]] let area_info = province.n + city.n + district.n let area_id = district.aid this.setData({ animation_flag: false, oldval: val, area_info, area_id }) }, commmitAddress() { let self = this let name = this.trim(this.data.name) if (!name) { wx.showToast({ icon: 'none', title: "请填写您的收货人名称", duration: 1500 }) return } let phone = this.trim(this.data.mob_phone) if (!(/^1(3|4|5|7|8)\d{9}$/.test(phone))) { wx.showToast({ icon: 'none', title: "请填写您的手机号", duration: 1500 }) return } let area_info = this.data.area_info if (!area_info) { wx.showToast({ icon: 'none', title: "请选择您的收货地址", duration: 1500 }) return } let address = this.trim(this.data.address) if (!address) { wx.showToast({ icon: 'none', title: "请填写的您的详细收货地址", duration: 1500 }) return } wx.showLoading({ title: '加载中', }) if (this.data.isEdit) { this.editAddress(name, this.data.address_id, this.data.city_id, this.data.area_id, this.data.area_info, address, phone) } else { this.addArress(name, address, phone, area_info); } }, addArress(name, address, phone, area_info) { let self = this; getReq(app,{ act: 'member_address', op: 'address_add', area_info, true_name: name, address, mob_phone: phone, area_id: this.data.area_id }, function (res) { wx.hideLoading() if (res.code == 200) { let address_id = res.datas.address_id let {area_id, city_id} = self.data self.setDefault(res, name, phone, area_info, address, address_id, area_id, city_id) } else { wx.showToast({ icon: 'none', title: res.message, duration: 2000 }) } }) }, editAddress(true_name, address_id, city_id, area_id, area_info, address, mob_phone) { let self = this; let params = { act: 'member_address', op: 'address_edit', true_name, address_id, city_id, area_id, area_info, address, mob_phone, } getReq(app,params, function (res) { if (res.code == 200) { self.setDefault(res, true_name, mob_phone, area_info, address, address_id, area_id, city_id) } }); }, setDefault(res, true_name, mob_phone, area_info, address, address_id, area_id, city_id) { let self = this; let is_default = self.data.checked ? 1 : 0 if (is_default) { let area_info = self.data.area_info app.checkDefaultAddress({ true_name, mob_phone, area_info, address, address_id, area_id, city_id }) } let params = { act: 'member_address', op: 'set_default', is_default, address_id } getReq(app,params, function (response) { wx.hideLoading() if (response.code == 200) { wx.navigateBack() } else { wx.showToast({ icon: 'none', title: response.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 () { } })