// pages/editAddress/editAddress.js const request = require("../../utils/request.js"); Page({ /** * 页面的初始数据 */ data: { districtData: {}, multiArray: [[], [], []],//picker窗口显示数据 multiIndex: [0, 0, 0], districtArray: [[], [], []],//实际显示数据 districtIndex: [0, 0, 0], districtId: null, name: '', phone: '', detailAddress: '', defaultAddress: false, userAddressId: null,//修改 editAdressData: {}, delViewStatus: false, }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { this.getDistrictData().then(res=>{ if(options.userAddressId) { wx.setNavigationBarTitle({ title: '编辑收货地址' }) this.setData({ userAddressId: options.userAddressId }) this.getAddressDetail(); } }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { }, /** * 用户点击右上角分享 */ onShareAppMessage() { }, deleteFun() { this.setData({ delViewStatus: true }) }, cancelDel() { this.setData({ delViewStatus: false }) }, sureDel() { let that = this; request.postData( 'app/user/address/remove', {userAddressId: that.data.userAddressId}, res => { wx.navigateBack({ delta: 1 }) } ) }, getAddressDetail() { let that = this; request.getData( 'app/user/address/detail',{userAddressId: that.data.userAddressId}, res => { that.data.districtArray[1] = that.data.multiArray[1] = that.findarr(res.data.data.districtList[0].districtId, that.data.districtData.cityList); that.data.districtArray[2] = that.data.multiArray[2] = that.findarr(res.data.data.districtList[1].districtId, that.data.districtData.districtList); that.data.districtIndex[0] = that.data.multiIndex[0] = that.findIndex(res.data.data.districtList[0].districtId, that.data.districtArray[0]); that.data.districtIndex[1] = that.data.multiIndex[1] = that.findIndex(res.data.data.districtList[1].districtId, that.data.districtArray[1]); that.data.districtIndex[2] = that.data.multiIndex[2] = that.findIndex(res.data.data.districtList[2].districtId, that.data.districtArray[2]); let data = { name: res.data.data.name, phone: res.data.data.phone, districtId: res.data.data.districtList[2].districtId, detailAddress: res.data.data.detailAddress, defaultAddress: res.data.data.defaultAddress, multiArray: that.data.multiArray, districtArray: that.data.districtArray, districtIndex: that.data.districtIndex, multiIndex: that.data.multiIndex }; that.setData(data) } ) }, toSave() { let that = this; if(that.data.name == '') { wx.showToast({ title: '请输入姓名', icon: 'none', duration: 2000 }) return }else if(that.data.phone == '') { wx.showToast({ title: '请输入手机号码', icon: 'none', duration: 2000 }) return }else if(that.data.detailAddress == '') { wx.showToast({ title: '请输入详细地址', icon: 'none', duration: 2000 }) return } let addressObject if(that.data.userAddressId) { addressObject = { userAddressId: that.data.userAddressId, name: that.data.name, phone: that.data.phone, districtId: that.data.districtId, detailAddress: that.data.detailAddress, defaultAddress: that.data.defaultAddress } request.postData( 'app/user/address/update', addressObject, res => { console.log(res.data.data) wx.navigateBack({ delta: 1 }) } ) }else{ addressObject = { name: that.data.name, phone: that.data.phone, districtId: that.data.districtId, detailAddress: that.data.detailAddress, defaultAddress: that.data.defaultAddress } request.postData( 'app/user/address/create', addressObject, res => { console.log(res.data.data) wx.navigateBack({ delta: 1 }) } ) } console.log(addressObject) }, switchDefaultAddressChange(event) { this.setData({ defaultAddress: event.detail.value }) }, getDetailAddressInput(event) { this.setData({ detailAddress: event.detail.value }) }, getPhoneInput(event) { this.setData({ phone: event.detail.value }) }, getNameInput(event) { this.setData({ name: event.detail.value }) }, findIndex(districtId, arr) { for(let index=0; index<=arr.length; index++) { if(arr[index].districtId == districtId) { return index } } }, findarr(pid, arr) { var obj = [] arr.forEach((val)=> { if(val.pid == pid) { obj.push(val) } }) return obj }, getDistrictData() { let that = this; return new Promise((resolve, reject) => { var districtData = wx.getStorageSync('districtData') that.data.multiArray[0] = districtData.provinceList; that.data.multiArray[1] = that.findarr(that.data.multiArray[0][0].districtId, districtData.cityList); that.data.multiArray[2] = that.findarr(that.data.multiArray[1][0].districtId, districtData.districtList); that.setData({ districtData: districtData, multiArray: that.data.multiArray, districtArray: that.data.multiArray, }) resolve(districtData) }) }, // getDistrictData() { // let that = this; // return new Promise((resolve, reject) => { // request.getData( // 'app/district/list',{}, // res => { // that.data.multiArray[0] = res.data.data.provinceList; // that.data.multiArray[1] = that.findarr(that.data.multiArray[0][0].districtId, res.data.data.cityList); // that.data.multiArray[2] = that.findarr(that.data.multiArray[1][0].districtId, res.data.data.districtList); // that.setData({ // districtData: res.data.data, // multiArray: that.data.multiArray, // districtArray: that.data.multiArray, // }) // resolve(res) // } // ) // }) // }, bindMultiPickerChange: function (e) { // console.log('picker发送选择改变,携带值为', e.detail.value) let districtId = this.data.multiArray[2][e.detail.value[2]].districtId; this.setData({ districtArray: this.data.multiArray, districtIndex: e.detail.value, districtId: districtId }) }, bindMultiPickerColumnChange: function (e) { let that = this; // console.log('修改的列为', e.detail.column, ',值为', e.detail.value); var data = { multiArray: that.data.multiArray, multiIndex: that.data.multiIndex }; data.multiIndex[e.detail.column] = e.detail.value; switch (e.detail.column) { case 0: data.multiArray[1] = that.findarr(data.multiArray[0][e.detail.value].districtId, that.data.districtData.cityList); data.multiArray[2] = that.findarr(data.multiArray[1][0].districtId, that.data.districtData.districtList); data.multiIndex[1] = 0; data.multiIndex[2] = 0; break; case 1: data.multiArray[2] = that.findarr(data.multiArray[1][e.detail.value].districtId, that.data.districtData.districtList); data.multiIndex[2] = 0; break; } // console.log(data.multiIndex); that.setData(data); }, })