addressManager.js
13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
import {API_HOST, SERVICE_HOST} from '../../libs/config';
import {GET, POST} from '../../libs/request';
import objectAssign from '../../vendors/object-assign';
import {
logEvent,
YB_PAGE_OPEN_L,
} from '../../libs/analytics.js'
var modeEdit = "modeEdit";
var modeSelect = "modeSelect";
var modeChange = "modeChange";
var modeAdd = "modeAdd"
var app = getApp();
let PV_ID = new Date().getTime() + '';
Page({
data: {
items: [],
order_code: "",
showContainer: false,
hiddenOpt: false,
currentMode: modeSelect,
modeEdit: modeEdit,
modeSelect: modeSelect,
modeChange: modeChange,
from_page_name: '',
from_page_param: '',
current_page_name: 'addressManager',
current_page_param: '',
//data for edit page
editeAddressPage: {
currentEditMode: modeAdd,
_consignee: "",
_mobile: "",
_detailAddress: "",
_area_code: 0,
_address: "",
_id: "",},
currentSelectedIndex: -1,
addType: 'none' // normal or wechat
},
onLoad:function(options){
// 生命周期函数--监听页面加载
new app.WeToast;
this.data.currentMode = modeEdit;
if (options.currentMode == modeEdit || options.currentMode == modeSelect || options.currentMode == modeChange){
this.data.currentMode = options.currentMode
wx.setNavigationBarTitle({
title: "管理地址"
})
if (this.data.currentMode != modeEdit) {
if (this.data.currentMode == modeChange) {
this.data.order_code = options.order_code;
}
this.setData({
hiddenOpt: options.fromPage == "statement"
})
}
}
let from_page_name = options.page_name ? options.page_name : '';
let from_page_param = options.page_param ? options.page_param : '';
this.setData({ from_page_param, from_page_name });
var pages = getCurrentPages()
var currentPage = pages[pages.length - 1]
var url = currentPage.route
let params = {
PAGE_NAME: this.data.current_page_name,
PAGE_PARAM: this.data.current_page_param,
FROM_PAGE_NAME: this.data.from_page_name,
FROM_PAGE_PARAM: this.data.from_page_param,
PV_ID: PV_ID,
PAGE_PATH: url
};
logEvent(YB_PAGE_OPEN_L, params);
let address_id = options.address_id == undefined || options.address_id == "" ? 0 : options.address_id;
address_id = decodeURIComponent(address_id);
let addType = options.addType ? options.addType : 'none';
this.setData({
currentMode: this.data.currentMode,
address_id,
addType,
})
this.getAddressList();
if (addType == 'normal') {
this.addButtonTapped();
} else if (addType == 'wechat') {
this.addFromWechatButtonTapped();
}
},
onReady:function(){
// 生命周期函数--监听页面初次渲染完成
},
onShow:function(){
// 生命周期函数--监听页面显示
},
onHide:function(){
// 生命周期函数--监听页面隐藏
},
onUnload:function(){
// 生命周期函数--监听页面卸载
if (this.data.items.length == 0 && this.data.currentMode == modeSelect) {
let prePage = getCurrentPages();
if (prePage.length > 1 && prePage[prePage.length - 2].selectComplete != undefined) {
prePage[prePage.length - 2].selectComplete(null);
}
}
},
onPullDownRefresh: function() {
// 页面相关事件处理函数--监听用户下拉动作
let params = {
PAGE_NAME: this.data.current_page_name,
PAGE_PARAM: this.data.current_page_param,
FROM_PAGE_NAME: this.data.from_page_name,
FROM_PAGE_PARAM: this.data.from_page_param,
PV_ID: PV_ID,
};
logEvent(YB_PAGE_OPEN_L, params);
},
onReachBottom: function() {
// 页面上拉触底事件的处理函数
},
selectAddress: function(e) {
let index = e.currentTarget.dataset.index;
if (this.data.currentMode == modeSelect) {
let prePage = getCurrentPages();
if (prePage.length > 1 && prePage[prePage.length - 2].selectComplete != undefined) {
let json = this.data.items[index];
prePage[prePage.length - 2].selectComplete(json);
}
wx.navigateBack({
delta: 1
})
}
else if (this.data.currentMode == modeChange) {
var _self = this;
var addressID = this.data.items[index].address_id;
wx.showModal({
title: '提示',
content: '关联订单的收货地址将一起修改且不可修改第二次,请确认是否修改',
cancelText: '返回',
confirmText: '确认修改',
confirmColor: '#000000',
success: function(res) {
if (res.confirm) {
let param = {
method: "app.SpaceOrders.updateDeliveryAddress",
order_code: _self.data.order_code,
uid: app.getUid(),
address_id: addressID
}
GET(API_HOST, param)
.then((json) => {
if (json && json.code && json.code == 200) {
wx.navigateBack({
delta: 1
})
}
})
.catch(error => {
})
}
else {
wx.navigateBack({
delta: 1
});
}
}
})
}
},
setDefaultButtonTapped: function(e) {
let id = e.currentTarget.dataset.id;
let param = {
method: "app.address.setdefault",
uid: app.getUid(),
id: id
};
let that = this;
POST(API_HOST, param).then((json) => {
let newItems = that.data.items;
newItems.map((item, index) => {
if (id == item.address_id) {
item.is_default = 'Y';
} else {
item.is_default = 'N';
}
});
that.setData({
items: newItems
});
}).catch((e) => {
})
},
editButtonTapped: function(e) {
// console.log(e)
let id = e.currentTarget.dataset.id;
let consignee = e.currentTarget.dataset.consignee;
let mobile = e.currentTarget.dataset.mobile;
let area_code = e.currentTarget.dataset.area_code;
let address = e.currentTarget.dataset.address;
let detailAddress = e.currentTarget.dataset.detail_address;
let newEditeAddressPage = objectAssign(this.data.editeAddressPage, {
_consignee: consignee,
_mobile: mobile,
_area_code: area_code,
_address: address,
_detailAddress: detailAddress,
_page_name: '编辑地址',
_id: id,
currentEditMode: modeEdit,});
var timestamp = Date.parse(new Date());
try {
wx.setStorageSync(timestamp + '', newEditeAddressPage);
} catch (e) {
}
wx.navigateTo({
url: './addressEditer/addressEditer?' + 'timestamp=' + timestamp ,
})
},
deleteButtonTapped: function(e) {
let id = e.currentTarget.dataset.id;
let index = e.currentTarget.dataset.index;
let that = this;
wx.showModal({
title: '提示',
content: '确认删除收货地址?',
cancelText: '取消',
confirmText: '确认',
confirmColor: '#000000',
success: function (res) {
if (res.confirm) {
let param = {
method: "app.address.del",
uid: app.getUid(),
id: id
};
POST(API_HOST, param).then((json) => {
that.getAddressList();
if (that.data.currentSelectedIndex == index) {
let prePage = getCurrentPages();
if (prePage.length > 1 && prePage[prePage.length - 2].selectComplete != undefined) {
prePage[prePage.length - 2].selectComplete(null);
}
that.setData({
currentSelectedIndex: -1,
})
}
}).catch((e) => {
})
}
else {
}
}
})
},
addButtonTapped: function() {
if (app.globalData.userInfo.uid == 0 || app.globalData.userInfo.uid == undefined){
//未登录状态
wx.showModal({
content: '请登录后再新增地址',
confirmText:'确定',
showCancel:false,
success:function(res){
if (res.confirm){
let pages = getCurrentPages();
if (pages && pages.length > 1 && pages[pages.length - 2]){
let lastPage = pages[pages.length -2];
if (lastPage.route ='pages/userCenter/userCenter'){
wx.navigateBack({
delta:1
})
}
}else{
wx.switchTab({
url: '../userCenter/userCenter',
})
}
}
}
})
return;
}
let newEditeAddressPage = objectAssign(this.data.editeAddressPage, {
currentEditMode: modeAdd,
_consignee: "",
_mobile: "",
_detailAddress: "",
_area_code: 0,
_address: "",
_id: "",
_page_name: '新增地址',
});
var timestamp = Date.parse(new Date());
try {
wx.setStorageSync(timestamp + '', newEditeAddressPage);
} catch (e) {
}
wx.navigateTo({
url: './addressEditer/addressEditer?' + 'timestamp=' + timestamp,
})
},
addFromWechatButtonTapped: function() {
if (wx.getSetting) {
let that = this;
wx.getSetting({
success(res) {
if (!res.authSetting['scope.address']) {
wx.authorize({
scope: 'scope.address',
success() {
that.chooseAddress();
},
fail(error) {
if (res.authSetting['scope.address'] == false) {
wx.showModal({
title: '',
content: '需要打开小程序的设置,重新授权获取您的收货地址',
confirmText: "去开启",
confirmColor: "#000000",
success: function (res) {
if (res.confirm) {
wx.openSetting({
success: function (res) {
}
});
}
}
});
}
},
})
} else {
that.chooseAddress();
}
}
})
} else {
wx.showModal({
title: '提示',
content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
})
}
},
chooseAddress: function() {
let that = this;
wx.chooseAddress({
success: function (res) {
let param = {
method: "app.address.sync",
uid: app.getUid(),
userName: res.userName,
postalCode: res.postalCode,
provinceName: res.provinceName,
cityName: res.cityName,
countyName: res.countyName,
detailInfo: res.detailInfo,
nationalCode: res.nationalCode,
telNumber: res.telNumber,
};
GET(API_HOST, param).then((json) => {
// console.log(json)
if (json.code == 200) {
that.getAddressList();
} else {
that.wetoast.toast({
title: json.message,
titleClassName: 'wetoast-title',
duration: 1500
});
}
}).catch((e) => {
})
}
})
},
/**
* 获取地址列表
*/
getAddressList: function() {
let param = {
method: 'app.address.gethidden',
uid: app.getUid()
};
GET(API_HOST, param)
.then(json => {
// console.log(json)
if (json && json.code && json.code == 200) {
this.data.items = json.data
let currentSelectedIndex = -1;
this.data.items.map((item, index) => {
if (this.data.address_id == item.address_id) {
currentSelectedIndex = index;
}
});
//删除或更新地址后需要通知上一个页面刷新
//修复删除或更新被选择的地址后,返回上一个页面,页面地址没有变化的bug 可能订单创建会出现失败的情况
if (currentSelectedIndex == -1) {
let prePage = getCurrentPages();
if (prePage.length > 1 && prePage[prePage.length - 2].selectComplete != undefined) {
prePage[prePage.length - 2].selectComplete(null);
}
//防止编辑地址页面回来后由于接口反响快导致页面层级关系不对,定位不到statement页面的bug
if (prePage.length > 2 && prePage[prePage.length - 3].selectComplete != undefined) {
prePage[prePage.length - 3].selectComplete(null);
}
}else {
let prePage = getCurrentPages();
if (prePage.length > 1 && prePage[prePage.length - 2].selectComplete != undefined) {
prePage[prePage.length - 2].selectComplete(this.data.items[currentSelectedIndex]);
}
//防止编辑地址页面回来后由于接口反响快导致页面层级关系不对,定位不到statement页面的bug
if (prePage.length > 2 && prePage[prePage.length - 3].selectComplete != undefined) {
prePage[prePage.length - 3].selectComplete(this.data.items[currentSelectedIndex]);
}
}
this.setData({
items: this.data.items,
showContainer: this.data.items.length > 0,
currentSelectedIndex
})
}
})
.catch(error => {
// console.log(error)
});
},
})