addressManager.js
17 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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
import {API_HOST, SERVICE_HOST} from '../../common/config';
import {GET, POST} from '../../common/request';
import objectAssign from '../../vendors/object-assign';
import {
logEvent,
YB_PAGE_OPEN_L,
} from '../../common/analytics.js'
var modeEdit = "modeEdit";
var modeSelect = "modeSelect";
var modeChange = "modeChange";
var modeAdd = "modeAdd"
var app = getApp();
let PV_ID = new Date().getTime() + '';
const APP_ADDRESS_PROVINCES = "app.address.provinces";
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: '',
showEditAddressPage: false,
//data for edit page
editeAddressPage: {
currentEditMode: modeAdd,
modeAdd: modeAdd,
_consignee: "",
_mobile: "",
_detailAddress: "",
_area_code: 0,
_address: "",
_id: "",},
},
onLoad:function(options){
// 生命周期函数--监听页面加载
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);
this.setData({
currentMode: this.data.currentMode,
address_id: options.address_id == undefined || options.address_id == "" ? 0 : options.address_id
})
},
onReady:function(){
// 生命周期函数--监听页面初次渲染完成
},
onShow:function(){
// 生命周期函数--监听页面显示
this.getAddressList();
},
onHide:function(){
// 生命周期函数--监听页面隐藏
},
onUnload:function(){
// 生命周期函数--监听页面卸载
},
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() {
// 页面上拉触底事件的处理函数
},
radioChange: function(e) {
if (this.data.currentMode == modeSelect) {
let prePage = getCurrentPages();
if (prePage.length > 1 && prePage[prePage.length - 2].selectComplete != undefined) {
let json = this.data.items[e.detail.value];
prePage[prePage.length - 2].selectComplete(json);
}
wx.navigateBack({
delta: 1
})
}
else if (this.data.currentMode == modeChange) {
var _self = this;
var addressID = this.data.items[e.detail.value].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
});
}
}
})
}
},
editButtonTapped: function(e) {
let id = e.target.dataset.id;
let consignee = e.target.dataset.consignee;
let mobile = e.target.dataset.mobile;
let area_code = e.target.dataset.area_code;
let address = e.target.dataset.address;
let detailAddress = e.target.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,});
this.setData({
showEditAddressPage: true,
editeAddressPage: newEditeAddressPage,
})
wx.setNavigationBarTitle({
title: "编辑地址"
});
this.editPageOnLoad();
},
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: "",
});
this.setData({
showEditAddressPage: true,
editeAddressPage: newEditeAddressPage,
})
wx.setNavigationBarTitle({
title: "新增地址"
});
this.editPageOnLoad();
},
/**
* 获取地址列表
*/
getAddressList: function() {
let param = {
method: 'app.address.gethidden',
uid: app.getUid()
};
GET(API_HOST, param)
.then(json => {
if (json && json.code && json.code == 200) {
this.data.items = json.data
this.setData({
items: this.data.items,
showContainer: this.data.items.length > 0
})
}
})
.catch(error => {
});
},
addDot: function (arr) {
if (arr instanceof Array) {
arr.map(val => {
if (val.caption.length > 4) {
val.captionDot = val.caption.slice(0, 4) + '...';
return val;
} else {
val.captionDot = val.caption;
return val;
}
})
}
},
hiddenPicker: function () {
let newEditeAddressPage = objectAssign(this.data.editeAddressPage, {
isShow: false, // 显示区域选择框
});
this.setData({
editeAddressPage: newEditeAddressPage,
});
},
editPageOnLoad: function() {
let newEditeAddressPage = objectAssign(this.data.editeAddressPage, {
isShow: false, // 显示区域选择框
showDistrict: true // 默认为省市区三级区域选择
});
this.setData({
editeAddressPage: newEditeAddressPage,
});
GET(API_HOST, {
method: APP_ADDRESS_PROVINCES,
id: "0"
}).then((province) => {
const firstProvince = province.data[0];
this.addDot(province.data);
/**
* 默认选择获取的省份第一个省份数据
*/
let newEditeAddressPage = objectAssign(this.data.editeAddressPage, {
proviceData: province.data,
'selectedProvince.index': 0,
'selectedProvince.id': firstProvince.id,
'selectedProvince.caption': firstProvince.caption,
});
this.setData({
editeAddressPage: newEditeAddressPage,
});
return (
GET(API_HOST,
{method: APP_ADDRESS_PROVINCES,
id: firstProvince.id})
);
}).then((city) => {
const firstCity = city.data[0];
this.addDot(city.data);
let newEditeAddressPage = objectAssign(this.data.editeAddressPage, {
cityData: city.data,
'selectedCity.index': 0,
'selectedCity.id': firstCity.id,
'selectedCity.caption': firstCity.caption,
});
this.setData({
editeAddressPage: newEditeAddressPage,
});
/**
* 省市二级则不请求区域
*/
if (this.data.editeAddressPage.showDistrict) {
return (
GET(API_HOST,
{method: APP_ADDRESS_PROVINCES,
id: firstCity.id})
);
} else {
let newEditeAddressPage = objectAssign(this.data.editeAddressPage, {
value: [0, 0]
});
this.setData({
editeAddressPage: newEditeAddressPage,
});
return;
}
}).then((district) => {
const firstDistrict = district.data[0];
this.addDot(district.data);
let newEditeAddressPage = objectAssign(this.data.editeAddressPage, {
value: [0, 0, 0],
districtData: district.data,
_area_code: firstDistrict.id,
'selectedDistrict.index': 0,
'selectedDistrict.id': firstDistrict.id,
'selectedDistrict.caption': firstDistrict.caption,
'tempAddress': this.data.editeAddressPage.proviceData[0].caption + ' ' + this.data.editeAddressPage.cityData[0].caption + ' ' + district.data[0].caption
});
this.setData({
editeAddressPage: newEditeAddressPage,
});
}).catch((e) => {
// console.log(e)
});
},
onInputChanged:function(e) {
let id = e.target.dataset.id;
let val = e.detail.value;
if (id == "txtConsignee") {
this.data.editeAddressPage._consignee = val;
}
else if (id == "txtMobile") {
this.data.editeAddressPage._mobile = val;
}
else if (id == "txtAddress") {
this.data.editeAddressPage._detailAddress = val;
}
},
onDeleteButtonTapped: function() {
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: that.data.editeAddressPage._id
};
POST(API_HOST, param).then((json) => {
that.setData({
showEditAddressPage: false,
});
that.getAddressList();
}).catch((e) => {
// console.log(e);
})
}
else {
}
}
})
},
/**
* 保存按钮点击事件
*/
onSubmitButtonTapped: function() {
if (this.data.editeAddressPage.currentEditMode == modeAdd) {
let param = {
method: "app.address.add",
consignee: this.data.editeAddressPage._consignee,
mobile: this.data.editeAddressPage._mobile,
area_code: this.data.editeAddressPage._area_code,
address: this.data.editeAddressPage._detailAddress,
uid: app.getUid()
};
POST(API_HOST, param).then((json) => {
this.setData({
showEditAddressPage: false,
});
this.getAddressList();
}).catch((e) => {
// console.log(e);
})
}
else if (this.data.editeAddressPage.currentEditMode == modeEdit) {
let param = {
method: "app.address.update",
consignee: this.data.editeAddressPage._consignee,
mobile: this.data.editeAddressPage._mobile,
area_code: this.data.editeAddressPage._area_code,
address: this.data.editeAddressPage._detailAddress,
uid: app.getUid(),
id: this.data.editeAddressPage._id
};
POST(API_HOST, param).then((json) => {
this.setData({
showEditAddressPage: false
});
this.getAddressList();
}).catch((e) => {
// console.log(e);
})
}
},
/**
* 页面选址触发事件
*/
choosearea: function (e) {
let that = this;
if (that.data.editeAddressPage._address === "") {
let newEditeAddressPage = objectAssign(that.data.editeAddressPage, {
isShow: true,
_address: that.data.editeAddressPage.tempAddress,
});
that.setData({
editeAddressPage: newEditeAddressPage,
});
}
else {
let newEditeAddressPage = objectAssign(that.data.editeAddressPage, {
isShow: true,
});
that.setData({
editeAddressPage: newEditeAddressPage,
});
}
},
/**
* 滑动事件
*/
bindChange: function (e) {
const current_value = e.detail.value, _data = this.data.editeAddressPage;
if (current_value.length > 2) {
if (this.data.editeAddressPage.value[0] !== current_value[0] && this.data.editeAddressPage.value[1] === current_value[1] && this.data.editeAddressPage.value[2] === current_value[2]) {
// 滑动省份
current_value[1] = 0;
current_value[2] = 0;
GET(API_HOST,
{method: APP_ADDRESS_PROVINCES,
id: _data.proviceData[current_value[0]].id}).then((city) => {
this.addDot(city.data);
let newEditeAddressPage = objectAssign(this.data.editeAddressPage, {
cityData: city.data,
value: current_value,
});
this.setData({
editeAddressPage: newEditeAddressPage,
});
return (
GET(API_HOST,
{method: APP_ADDRESS_PROVINCES, id: city.data[0].id})
);
}).then((district) => {
if (district.data.length > 0) {
this.addDot(district.data);
let newEditeAddressPage = objectAssign(this.data.editeAddressPage, {
districtData: district.data,
'value[0]': current_value[0],
'value[1]': 0,
'value[2]': 0,
_address: this.data.editeAddressPage.proviceData[current_value[0]].caption +
' ' +
this.data.editeAddressPage.cityData[0].caption +
' ' +
district.data[0].caption
});
this.setData({
editeAddressPage: newEditeAddressPage,
});
//保存当前districtid
this.data.editeAddressPage._area_code = district.data[0].id;
}
}).catch((e) => {
// console.log(e);
})
} else if (this.data.editeAddressPage.value[0] === current_value[0] && this.data.editeAddressPage.value[1] !== current_value[1] && this.data.editeAddressPage.value[2] === current_value[2]) {
// 滑动城市
GET(API_HOST,
{method: APP_ADDRESS_PROVINCES,
id: _data.cityData[current_value[1]].id}).then((district) => {
if (district.data.length > 0) {
this.addDot(district.data);
current_value[2] = 0;
let newEditeAddressPage = objectAssign(this.data.editeAddressPage, {
districtData: district.data,
value: current_value,
_address: this.data.editeAddressPage.proviceData[current_value[0]].caption +
' ' +
this.data.editeAddressPage.cityData[current_value[1]].caption +
' ' +
district.data[0].caption
});
this.setData({
editeAddressPage: newEditeAddressPage,
});
//保存当前districtid
this.data.editeAddressPage._area_code = district.data[0].id;
}
}).catch((e) => {
// console.log(e);
})
} else if (this.data.editeAddressPage.value[0] === current_value[0] && this.data.editeAddressPage.value[1] === current_value[1] && this.data.editeAddressPage.value[2] !== current_value[2]) {
// 滑动地区
let newEditeAddressPage = objectAssign(this.data.editeAddressPage, {
value: current_value,
_address: this.data.editeAddressPage.proviceData[current_value[0]].caption +
' ' +
this.data.editeAddressPage.cityData[current_value[1]].caption +
' ' +
this.data.editeAddressPage.districtData[current_value[2]].caption
});
this.setData({
editeAddressPage: newEditeAddressPage,
});
//保存当前districtid
this.data.editeAddressPage._area_code = this.data.editeAddressPage.districtData[current_value[2]].id;
}
}
}
})