list.js
10.2 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
import addressModel from '../../../models/home/address';
import wechatAddressUtil from '../../../utils/wechat-address';
import Yas from '../../../common/yas';
const PAGE_TYPE_ADD = 'add';
const PAGE_TYPE_SELECT = 'select';
const AREA_CACHE_KEY = 'address_provinces_cache';
let CACHE_AREA_LIST = {};
let yas;
const validateAddress = function(data) {
const errorTip = {
consignee: '请输入正确的收件人姓名',
area_code: '请选择省市区',
mobile: '请输入正确的手机号',
address: '请输入详细的收件地址'
};
const regx = {
consignee: /^([\u4e00-\u9fa5\w*]{1,20})$/,
area_code: /^[0-9]{6,9}$/,
mobile: /^\d{3}(\d{4}|\*{4})\d{4,11}$/
};
let pass = true;
let errorType;
data = data || {};
for (let i in data) {
if (data.hasOwnProperty(i) &&
regx[i] &&
!regx[i].test(data[i])) {
pass = false;
!errorType && (errorType = i);
}
}
if (data.hasOwnProperty('address') && !data.address) {
pass = false;
!errorType && (errorType = 'address');
}
if (!pass) {
wx.showToast({
title: errorTip[errorType] || '请输入正确的信息',
icon: 'none',
duration: 2000
});
}
return pass;
};
Page({
data: {
showEditAddrPage: false,
editAddrPageData: {},
areaPicker: false,
pickerProvice: [],
pickerCity: [],
pickerArea: [],
addressList: []
},
onLoad(options) {
this.loadOptions = options || {};
if (options.type === PAGE_TYPE_ADD) {
wx.setNavigationBarTitle({title: '新增地址'});
this.setData({
showEditAddrPage: true,
editAddrPageData: {}
});
return;
} else if (options.type === PAGE_TYPE_SELECT) {
options.id = decodeURIComponent(options.id);
this.setData({
selectedId: options.id
});
}
try {
let area = wx.getStorageSync(AREA_CACHE_KEY);
if (area) {
CACHE_AREA_LIST = JSON.parse(area);
}
} catch (e) {
console.log(JSON.stringify(e));
}
this.loadAddrList();
yas = new Yas();
yas.pageOpenReport();
},
loadAddrList() {
addressModel.addressList().then(res => {
if (res.code === 200) {
let list = res.data;
this.setData({
addressEmpty: !list || !list.length,
addressList: list
});
}
});
},
loadAreaList(id) {
if (CACHE_AREA_LIST[id]) {
return Promise.resolve(CACHE_AREA_LIST[id]);
}
return addressModel.areaList(id).then(res => {
if (res.code !== 200) {
return [];
}
CACHE_AREA_LIST[id] = res.data;
wx.setStorage({
key: AREA_CACHE_KEY,
data: JSON.stringify(CACHE_AREA_LIST)
});
return res.data || [];
});
},
getAreaInfo(areaCode, resData) {
resData = resData || {};
resData.value = resData.value || [];
resData.valueName = resData.valueName || [];
let id = 0;
let type = 'provice';
let select;
if (areaCode) {
areaCode = areaCode.toString();
switch (resData.value.length) {
case 1:
id = areaCode.substring(0, 2);
select = areaCode.substring(0, 4);
type = 'city';
break;
case 2:
id = areaCode.substring(0, 4);
select = areaCode.substring(0, 6);
type = 'area';
break;
case 3:
id = areaCode.substring(0, 6);
select = areaCode;
type = 'street';
break;
default:
select = areaCode.substring(0, 2);
break;
}
}
return this.loadAreaList(id).then(list => {
if (!list.length) {
resData.fail = true;
return resData;
}
let selectIndex;
list.map((val, key) => {
if (+val.id === +select) {
selectIndex = key;
}
});
if (typeof selectIndex === 'undefined') {
selectIndex = 0;
areaCode = list[0].id;
}
resData.value.push(selectIndex);
resData.valueName.push(list[selectIndex].caption || '');
resData[type] = list;
if (resData.value.length < 3) {
return this.getAreaInfo(areaCode, resData);
}
resData.areaCode = list[selectIndex].id;
return Promise.resolve(resData);
});
},
setDefaultAddr(e) {
const that = this;
let selectIndex = e.currentTarget.dataset.key;
let addressList = this.data.addressList,
addr = addressList[selectIndex];
if (addr) {
addressModel.setDefaultAddress(addr.address_id).then(res => {
if (res.code !== 200) {
return;
}
addressList.map(item => {
item.is_default = addr.address_id === item.address_id ? 'Y' : 'N';
});
that.setData({addressList});
});
}
},
chooseAddr(e) {
if (this.loadOptions.type !== PAGE_TYPE_SELECT) {
return;
}
let selectIndex = e.currentTarget.dataset.key;
this.backToPrePage(this.data.addressList[selectIndex]);
},
backToPrePage(address) {
let currentPages = getCurrentPages();
if (address && currentPages.length > 1 &&
currentPages[currentPages.length - 2].chooseAddressCallback) {
currentPages[currentPages.length - 2].chooseAddressCallback(address);
}
wx.navigateBack({delta: 1});
},
deleteAddr(e) {
const that = this;
let selectIndex = e.currentTarget.dataset.key;
let selectId = e.currentTarget.dataset.id;
if (!selectId) {
return;
}
wx.showModal({
title: '提示',
content: '确认删除收货地址?',
cancelText: '取消',
confirmText: '确认',
confirmColor: '#000',
success: function(confirm) {
confirm.confirm && addressModel.deleteAddress(selectId).then(res => {
if (res.code === 200) {
let addressList = that.data.addressList;
addressList.splice(selectIndex, 1);
that.setData({
showEditAddrPage: false,
addressList
});
}
});
}
});
},
showEditAddrPage(e) {
let selectIndex = e.currentTarget.dataset.key;
let address = this.data.addressList[selectIndex] || {};
wx.setNavigationBarTitle({title: address.address_id ? '编辑地址' : '新增地址'});
address.index = selectIndex;
this.setData({
showEditAddrPage: true,
editAddrPageData: address
});
this.editAddress = Object.assign({changed: false}, address);
},
saveAddr() {
const that = this;
let address = this.data.editAddrPageData;
if (this.editAddress && !this.editAddress.changed) {
return that.setData({showEditAddrPage: false});
}
if (validateAddress(address)) {
addressModel.saveAddressData({
id: address.address_id,
consignee: address.consignee,
area_code: address.area_code,
address: address.address,
mobile: address.mobile,
is_default: address.is_default
}).then(result => {
if (!result || result.code !== 200) {
return wx.showModal({
content: result.message || '保存失败,请稍后重试',
confirmText: '好的',
showCancel: false
});
}
let addressList = this.data.addressList;
if (address.address_id) {
addressList.map(item => {
if (item.address_id === address.address_id) {
Object.assign(item, result.data || address);
}
});
} else if (this.loadOptions.type === PAGE_TYPE_ADD) {
this.backToPrePage(result.data);
} else {
addressList.unshift(result.data);
}
that.setData({
addressEmpty: false,
showEditAddrPage: false,
addressList
});
});
}
},
addWechatAddr() {
wechatAddressUtil.choose(res => {
addressModel.saveWechatAddressData(res).then(result => {
if (result.code === 200) {
let addressList = this.data.addressList;
addressList.unshift(result.data);
this.setData({addressList});
} else {
wx.showToast({
title: result.message,
icon: 'none',
duration: 1500
});
}
});
});
},
hideAreaPicker() {
if (this.data.areaPicker) {
this.setData({areaPicker: false});
}
},
chooseArea() {
if (this.data.areaPicker) {
return;
}
let editAddrPageData = this.data.editAddrPageData;
this.getAreaInfo(editAddrPageData.area_code).then(res => {
res = res || {};
this.setData({
areaPicker: true,
pickerProvice: res.provice || [],
pickerCity: res.city || [],
pickerArea: res.area || [],
areaValue: res.value
});
});
},
areaPickerChange(e) {
const val = e.detail.value;
const info = this.data;
const oldCode = info.areaValue;
const pickerArr = ['pickerProvice', 'pickerCity', 'pickerArea'];
let obj = {};
let code;
for (let i = 0; i < pickerArr.length; i++) {
let select = info[pickerArr[i]][val[i]];
obj[pickerArr[i]] = select;
if (i < 2 && val[i] !== oldCode[i]) {
code = select.id;
}
}
let editData = info.editAddrPageData;
if (code) {
this.getAreaInfo(code).then(res => {
res = res || {};
editData.area = res.valueName.join(' ');
editData.area_code = res.areaCode;
this.setData({
editAddrPageData: editData,
pickerProvice: res.provice || [],
pickerCity: res.city || [],
pickerArea: res.area || [],
areaValue: res.value
});
});
} else {
editData.area = `${obj[pickerArr[0]].caption} ${obj[pickerArr[1]].caption} ${obj[pickerArr[2]].caption}`;
editData.area_code = obj[pickerArr[2]].id;
if (this.editAddress.area_code !== editData.area_code) {
this.editAddress.changed = true;
}
this.setData({editAddrPageData: editData});
}
},
onInputChanged(e) {
let name = e.currentTarget.dataset.name;
this.data.editAddrPageData = this.data.editAddrPageData || {};
this.data.editAddrPageData[name] = e.detail.value;
this.editAddress = this.editAddress || {};
if (this.editAddress[name] !== this.data.editAddrPageData[name]) {
this.editAddress.changed = true;
}
}
});