// pages/order/address/addAddress.js const app = getApp(); Page({ /** * 页面的初始数据 */ data: { type: 0, // name: '', // 姓名 sex: '1', // 性别 iPhone: '', // 手机 address: {}, // 地址 newAddress: '选择地理位置', // tablet: '', // 门牌 isDefaullt: 0, // }, /** * 生命周期函数--监听页面加载 */ onLoad: function(options) { this.data.type = options.type; var title = ""; if (this.data.type == 0) { // 新增收货地址 title = "新增收货地址" this.setData({ sex_man: true }); } else if (this.data.type == 1) { title = "修改收货地址" this.data.address = JSON.parse(options.address); var sex_man = true; var sex_woman = false; var sex_secrecy = false; var swicth = false; if (this.data.address.isDefaullt) { swicth = true; } this.setData({ sex_man: sex_man, sex_woman: sex_woman, sex_secrecy: sex_secrecy, name: this.data.address.name, iPhone: this.data.address.receiveMobile, newAddress: this.data.address.receiveAddress, isSwitch: swicth, }); } wx.setNavigationBarTitle({ title: title //页面标题为路由参数 }) }, // 选择性别 radioChange: function(e) { this.setData({ sex: e.detail.value }); }, // 获取输入内容 getInputContent: function(e) { var index = e.currentTarget.dataset.index; var value = e.detail.value; if (index == 1) { // 姓名 this.data.name = value; } else if (index == 2) { //手机 this.data.iPhone = value; } else if (index == 3) { // 地址 this.data.newAddress = value; } else if (index == 4) { // 门牌号 this.data.tablet = value; } }, // 设置默认收货地址 switchChange: function(e) { if (e.detail.value) { this.data.isDefaullt = 1; } else { this.data.isDefaullt = 0; } }, // getLocation: function(e) { var that = this; wx.chooseLocation({ success: function(res) { console.log(res); if (res.errMsg == "chooseLocation:ok") { var address = res.address + res.name; that.setData({ newAddress: address }); } }, fail: function(res) { console.log(res); } }) }, // 保存收货地址 saveAddAddress: function() { var that = this; console.log(this.data.iPhone.length); if (this.data.iPhone.length == 0) { app.utils.alertErrorMsg("提示", "请输入手机"); return; } if (this.data.iPhone.length != 11) { app.utils.alertErrorMsg("提示", "手机输入错误"); return; } if (this.data.newAddress.length == 0) { app.utils.alertErrorMsg("提示", "请输入地址"); return; } wx.showLoading({ title: '保存中...', }); var address = this.data.newAddress; if (this.data.tablet.length != 0) { address = this.data.newAddress + "-" + this.data.tablet; } var id = ""; var method = "wxdc.save.distribution.address"; if (this.data.type == 0) { method = "wxdc.save.distribution.address"; } else { method = "wxdc.update.distribution.address"; id = this.data.address.id; } var params = { "method": method, "wid": app.wid, "openId": app.openId, "mobile": this.data.iPhone, "receiveMobile": this.data.iPhone, "receiveAddress": address, "isDefault": this.data.isDefaullt, "sex": this.data.sex, } var ignores = []; ignores.push("mobile"); ignores.push("receiveMobile"); ignores.push("receiveAddress"); ignores.push("sex"); ignores.push("isDefault"); if (id.length != 0) { params.id = id; ignores.push("id"); } if (this.data.name != "") { params.name = this.data.name; ignores.push("name"); } // 忽略签名参数 app.jsapi.api(app.globalData.appKey, app.globalData.appSecret, app.globalData.serverUrl).ajax(params, ignores, function(json) { var result = json.data; if (result.status == 1) { var pages = getCurrentPages(); var prevPage = pages[pages.length - 2] //上一个页面 prevPage.getLoadAddress(); wx.navigateBack({ }); } else { app.msg.showMsg("提示", result.message); } wx.hideLoading(); }, function(error) { wx.hideLoading(); app.msg.showMsg("提示", "网络中断,获取门店失败"); } ); }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function() { }, /** * 生命周期函数--监听页面显示 */ onShow: function() { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function() { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function() { }, /** * 用户点击右上角分享 */ onShareAppMessage: function() { } })