Sanakey
2020-10-13 245dd6f1b9a206019ba1680b5214d3bbcd870d72
提交 | 用户 | age
d98d05 1 // pages/address/address.js
S 2 // var commonCityData = require('../../utils/city.js')
3 //获取应用实例
4 var utils = require("../../utils/util.js");
245dd6 5 var app = getApp();
S 6 const chooseLocation = requirePlugin('chooseLocation');
d98d05 7 Page({
S 8   data: {
9     region: ['请选择', '请选择', '请选择'],
10     customItem: '全部',
11     addressData:{},
12     isShowPostcode:false
13   },
14   bindCancel: function () {
15     wx.navigateBack()
16   },
17
18   // 保存
19   bindSave: function (e) {
20     console.log(e)
21     var that = this;
22     var linkMan = e.detail.value.linkMan;
23     var street = e.detail.value.street;
24     var mobile = e.detail.value.mobile;
25     var code = e.detail.value.code || '';
245dd6 26     // var provincename = e.detail.value.area[0];
S 27     // var cityname = e.detail.value.area[1];
28     // var countyname = e.detail.value.area[2];
29     let location = this.data.location;
d98d05 30     // if (linkMan == "") {
S 31     //   wx.showModal({
32     //     title: '提示',
33     //     content: '请填写联系人姓名',
34     //     showCancel: false
35     //   })
36     //   return
37     // }
38     // if (mobile == "") {
39     //   wx.showModal({
40     //     title: '提示',
41     //     content: '请填写手机号码',
42     //     showCancel: false
43     //   })
44     //   return
45     // }
245dd6 46     // if (this.data.region[0] == "请选择") {
d98d05 47     //   wx.showModal({
S 48     //     title: '提示',
245dd6 49     //     content: '请选择地区',
d98d05 50     //     showCancel: false
S 51     //   })
52     //   return
53     // }
245dd6 54     if (!location) {
S 55       wx.showModal({
56         title: '提示',
57         content: '请选择收货地址',
58         showCancel: false
59       })
60       return
61     }
62     if (!location.longitude||!location.latitude) {
63       wx.showModal({
64         title: '提示',
65         content: '请完善收货地址',
66         showCancel: false
67       })
68       return
69     }
d98d05 70
S 71     wx.showLoading()
72     wx.request({
73       url: utils.getUrl('/shopping/account.do?m=updateaddress'),
74       header: {
75         "Cookie": "JSESSIONID=" + wx.getStorageSync('sessionID')
76       },
77       // method:'POST',
78       data: {
79         paymentaddress: this.data.edit ? "" : this.data.seqid,
80         ac: this.data.edit ? "new" :"edit",
81         linkman: linkMan,
82         telephone: mobile,
245dd6 83         // provincename: provincename,
S 84         // cityname: cityname,
85         // countyname: countyname,
86         address:location.address,
87         longitude:location.longitude,
88         latitude:location.latitude,
89         addressname:location.name,
d98d05 90         street: street,
S 91         postcode: code,
92       },
93       success: res => {
94
95         wx.hideLoading();
96         if (res.data.error) {
97           utils.showModal(res.data.error.linkman || res.data.error.telephone || res.data.error.street||res.data.error.warning,'提示',false);
98           return false;
99         }
100         wx.navigateBack()
101       },
102       fail: function (errmsg) {
103         utils.requestFail(errmsg,'/shopping/account.do');
104       }
105     })
106
107
108   },
109
110   onLoad: function (options) {
111     this.setData({
112       seqid: options.seqid,
113       isShowPostcode: app.globalData.isShowPostcode
114     })
115     console.log(options);
116     if (options.edit){
117       this.setData({
118         edit: options.edit
245dd6 119       })
S 120     }
121     if (options.hidebtn) {
122       this.setData({
123         hidebtn: options.hidebtn,
d98d05 124       })
S 125     }
126
127     if(!options.edit){
128       this.getAddressData();
129     }
130     console.log(this.data.addressData);
131
132   },
133
134   getAddressData(){
135     wx.showLoading();
136     wx.request({
137       url: utils.getUrl('/shopping/account.do?m=getAddressBySeq'),
138       header: {
139         "Cookie": "JSESSIONID=" + wx.getStorageSync('sessionID')
140       },
141       data: {
142         paymentaddress: this.data.seqid,
143         ac: "edit"
144       },
145       success: res => {
146         wx.hideLoading();
147         if(!utils.requestError(res)){
148           return false;
149         }
150         this.setData({
151           ["addressData.linkMan"]: res.data.list.linkman,
152           ["addressData.mobile"]: res.data.list.telephone,
153           ["addressData.street"]: res.data.list.street,
154           ["addressData.code"]: res.data.list.postcode,
245dd6 155           // region: [res.data.list.provincename, res.data.list.cityname, res.data.list.countyname]
S 156           ["location.address"]: res.data.list.address,
157           ["location.longitude"]: res.data.list.longitude,
158           ["location.latitude"]: res.data.list.latitude,
159           ["location.name"]: res.data.list.addressname,
d98d05 160         })
S 161
162       },
163       fail: function (errmsg) {
164         utils.requestFail(errmsg,'/shopping/account.do');
165       }
166     })
167   },
168
169
170   bindRegionChange: function (e) {
171     console.log('picker发送选择改变,携带值为', e.detail.value)
172     this.setData({
173       region: e.detail.value
174     })
175   },
176
177   //从微信读取
178   readFromWx: function () {
179     let that = this;
180     wx.chooseAddress({
181       success: function (res) {
182         let provinceName = res.provinceName;
183         let cityName = res.cityName;
184         let diatrictName = res.countyName;
185         let retSelIdx = 0;
186
187         that.setData({
188           wxaddress: res,
189           region: [res.provinceName, res.cityName, res.countyName]
190         });
191       }
192     })
193   },
194   // 删除地址
195   deleteAddress(){
196
197     wx.showModal({
198       title: '提示',
199       content: '确定删除所选项吗?',
200       success: res => {
201         if (res.confirm) {
202           wx.showLoading();
203           wx.request({
204             url: utils.getUrl('/shopping/account.do?m=deladdress'),
205             header: {
206               "Cookie": "JSESSIONID=" + wx.getStorageSync('sessionID')
207             },
208             data: {
209               paymentaddress: this.data.seqid,
210             },
211             success: res => {
212               wx.hideLoading();
213               if(!utils.requestError(res)){
214                 return false;
215               }
216               wx.navigateBack()
217             },
218             fail: function (errmsg) {
219               utils.requestFail(errmsg,'/shopping/account.do');
220             }
221           })
222         } else if (res.cancel) {
223           console.log('用户点击取消')
224         }
225       }
226     })
227
228   },
245dd6 229   selectLocation(){
S 230     const key = app.globalData.TengXunMapLocationServiceKey;
231     if (!key) {
232       wx.showModal({
233         title: '提示',
234         content: '请配置腾讯地图服务所需要的key',
235         showCancel:false
236       })
237       return;
238     }
239     const referer = 'miniApp';
240     wx.navigateTo({
241       url: 'plugin://chooseLocation/index?key=' + key + '&referer=' + referer
242       //  + '&location=' + location + '&category=' + category
243     });
244   },
245   onShow(){
246     // app.fromUserIdOnChange(()=>{
247     //   this.getAddressData();
248     // })
249     const location = chooseLocation.getLocation(); // 如果点击确认选点按钮,则返回选点结果对象,否则返回null
250     console.log(location);
251     if (location) {
252       this.setData({
253         location
254       })
255     }
256     
257   },
258   onUnload () {
259     // 页面卸载时设置插件选点数据为null,防止再次进入页面,geLocation返回的是上次选点结果
260     chooseLocation.setLocation(null);
261   }
d98d05 262 })