addressEditer.js
11.9 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
import AddressService from '../addressService.js'
import user from '../../../../utils/user';
import Yas from '../../../../utils/yas';
var modeEdit = "modeEdit";
var modeSelect = "modeSelect";
var modeChange = "modeChange";
var modeAdd = "modeAdd"
var app = getApp();
const APP_ADDRESS_PROVINCES = "app.address.provinces";
let yas;
Page({
/**
* 页面的初始数据
*/
data: {
addressTitle: '',
editeAddressPage: {
currentEditMode: modeAdd,
_consignee: "",
_mobile: "",
_detailAddress: "",
_area_code: 0,
_address: "",
_id: "",
_is_default: "N",
},
originAddressList: [],
submitBtnEnable: false,
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
yas = new Yas(this);
yas.pageOpenReport();
let timestamp = options.timestamp;
let value = user.getYHStorageSync(timestamp, 'addressEditer');
let addressTitle = (value && (value.currentEditMode === 'modeEdit')) ? '编辑地址' : '添加地址';
let address = value && value._address ? value._address : '';
let originAddressList = address ? address.split(" ") : [];
this.setData({
editeAddressPage: value,
originAddressList,
addressTitle,
});
this.refreshSubmitBtnState();
try {
wx.setStorageSync(timestamp, null);//清除内存
} catch (e) {
}
},
onShow() {
},
editPageOnLoad: function (level,id) {
let params = {
fromPage: 'addressEditer',
id: id
};
let that = this;
let api = new AddressService().yohoApi();
return new Promise(function (resolve, reject) {
api.fetchAddressProvinces(params)
.then(json => {
if (json) {
let value = json;
let newEditeAddressPage = that.data.editeAddressPage;
let key = 'address_' + level;
newEditeAddressPage[key] = value;
//比对_area_code 解决 “其他” 被选中后,后台自动过滤掉address文字的返回但是_area_code没过滤的情况,导致我们显示上就没有选中 “其他”
if (level == 3) {
let list = value;
for (var i = 0; i < list.length; i++) {
let item = list[i];
if (item.id == newEditeAddressPage._area_code) {
newEditeAddressPage['level3Select'] = item;
break;
}
}
}
that.setData({
editeAddressPage: newEditeAddressPage,
});
resolve(json);
}
})
.catch(error => {
reject(error);
})
});
},
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;
}
this.refreshSubmitBtnState();
},
defaultButtonTapped: function(e) {
let editeAddressPage = this.data.editeAddressPage;
if (editeAddressPage._is_default == 'Y') {
editeAddressPage._is_default = 'N';
} else {
editeAddressPage._is_default = 'Y';
}
this.setData({
editeAddressPage
});
},
onDeleteButtonTapped: function () {
let that = this;
wx.showModal({
title: '提示',
content: '确认删除收货地址?',
cancelText: '取消',
confirmText: '确认',
confirmColor: '#000000',
success: function (res) {
if (res.confirm) {
let params = {
id: that.data.editeAddressPage._id,
};
let api = new AddressService().yohoApi();
api.delAddressRequest(params)
.then(data => {
let prePage = getCurrentPages();
if (prePage.length > 1 && prePage[prePage.length - 2].getAddressList != undefined) {
prePage[prePage.length - 2].getAddressList();
}
wx.navigateBack({
delta: 1
})
})
.catch(error => {
})
}
}
})
},
/**
* 保存按钮点击事件
*/
onSubmitButtonTapped: function () {
this.hiddenPicker();
if (!this.data.submitBtnEnable) {
return;
}
var reg = /^1\d{10}$/;//校验11位数字
if (!reg.test(this.data.editeAddressPage._mobile)) {
wx.showToast({
title: '请输入正确的手机号',
icon: 'none',
duration: 1500
})
return;
}
if (this.data.editeAddressPage.currentEditMode == modeAdd) {
let params = {
consignee: this.data.editeAddressPage._consignee,
mobile: this.data.editeAddressPage._mobile,
area_code: this.data.editeAddressPage._area_code,
address: this.data.editeAddressPage._detailAddress,
is_default:this.data.editeAddressPage._is_default
};
let api = new AddressService().yohoApi();
api.addAddressRequest(params)
.then(data => {
let prePage = getCurrentPages();
if (prePage.length > 1 && prePage[prePage.length - 2].getAddressList != undefined) {
prePage[prePage.length - 2].getAddressList();
}
wx.navigateBack({
delta: 1
})
})
.catch(error => {
wx.showToast({
title: json.message,
icon: 'none',
duration: 1500,
})
})
}
else if (this.data.editeAddressPage.currentEditMode == modeEdit) {
let params = {
consignee: this.data.editeAddressPage._consignee,
mobile: this.data.editeAddressPage._mobile,
area_code: this.data.editeAddressPage._area_code,
address: this.data.editeAddressPage._detailAddress,
is_default:this.data.editeAddressPage._is_default,
id: this.data.editeAddressPage._id
};
let api = new AddressService().yohoApi();
api.updateAddressRequest(params)
.then(data => {
let prePage = getCurrentPages();
if (prePage.length > 1 && prePage[prePage.length - 2].getAddressList != undefined) {
prePage[prePage.length - 2].getAddressList();
}
wx.navigateBack({
delta: 1
})
})
.catch(error => {
wx.showToast({
title: json.message,
icon: 'none',
duration: 1500,
})
})
}
},
/**
* 页面选址触发事件
*/
choosearea: function (e) {
let that = this;
this.editPageOnLoad(0,0).then(json => {
that.editData(0);
let newEditeAddressPage0 = that.data.editeAddressPage;
let item0 = newEditeAddressPage0.level0Select ? newEditeAddressPage0.level0Select : '';
let item_id_0 = item0 ? item0.id : '';
if (!item_id_0){
newEditeAddressPage0.isShow = true;
newEditeAddressPage0.currentLevel = 0;
that.setData({
editeAddressPage: newEditeAddressPage0,
});
return;
}
that.editPageOnLoad(1, item_id_0).then(json => {
that.editData(1);
let newEditeAddressPage1 = that.data.editeAddressPage;
let item1 = newEditeAddressPage1.level1Select ? newEditeAddressPage1.level1Select : '';
let item_id_1 = item1 ? item1.id : '';
if (!item_id_1) {
newEditeAddressPage1.isShow = true;
newEditeAddressPage0.currentLevel = 1;
that.setData({
editeAddressPage: newEditeAddressPage1,
});
return;
}
that.editPageOnLoad(2, item_id_1).then(json => {
that.editData(2);
let newEditeAddressPage2 = that.data.editeAddressPage;
let item2 = newEditeAddressPage2.level2Select ? newEditeAddressPage2.level2Select : '';
let item_id_2 = item2 ? item2.id : '';
if (!item_id_2) {
newEditeAddressPage2.isShow = true;
newEditeAddressPage0.currentLevel = 2;
that.setData({
editeAddressPage: newEditeAddressPage2,
});
return;
}
that.editPageOnLoad(3, item_id_2).then(json => {
that.editData(3);
let newEditeAddressPage3 = that.data.editeAddressPage;
newEditeAddressPage3.isShow = true;
newEditeAddressPage0.currentLevel = 3;
that.setData({
editeAddressPage: newEditeAddressPage3,
});
});
});
});
});
},
editData: function (level){
let newEditeAddressPage = this.data.editeAddressPage;
let key = 'address_' + level;
if (this.data.originAddressList.length > 0 && this.data.originAddressList.length > parseInt(level)) {
let adsressItem = this.data.originAddressList[parseInt(level)];
let list = newEditeAddressPage[key];
for (var i = 0; i < list.length; i++) {
let item = list[i];
if (item.caption == adsressItem) {
newEditeAddressPage['level' + level + 'Select'] = item;
break;
}
}
this.setData({
editeAddressPage: newEditeAddressPage,
});
}
},
updateAddress: function (event) {
let fliterSelectIndex = parseInt(event.detail.fliterSelectIndex);
let rowIndex = parseInt(event.detail.rowIndex);
let editeAddressPage = this.data.editeAddressPage;
for(var i = 0 ; i < 4 ; i++) {
if (i > fliterSelectIndex){
let key1 = 'address_' + i;
delete editeAddressPage[key1];
let key2 = 'level' + i + 'Select';
delete editeAddressPage[key2];
}
}
editeAddressPage.currentLevel = fliterSelectIndex;
let that = this;
let list = editeAddressPage['address_' + fliterSelectIndex];
let item = list[rowIndex]
let curIndex = fliterSelectIndex+1;
this.editPageOnLoad(curIndex, item.id).then(json => {
let key = 'level' + fliterSelectIndex + 'Select';
editeAddressPage[key] = item;
editeAddressPage.currentLevel = curIndex;
that.setData({
editeAddressPage: editeAddressPage,
});
});
},
selectAddressFinish: function (event) {
let rowIndex = parseInt(event.detail);
let editeAddressPage = this.data.editeAddressPage;
let list = editeAddressPage['address_3'];
let item = list[rowIndex]
let key = 'level' + 3 + 'Select';
editeAddressPage[key] = item;
editeAddressPage.currentLevel = 3;
let _address = editeAddressPage.level0Select.caption + ' ' + editeAddressPage.level1Select.caption + ' ' + editeAddressPage.level2Select.caption + ' ' + editeAddressPage.level3Select.caption;
editeAddressPage._address = _address;
editeAddressPage._area_code = item.id;
let originAddressList = _address ? _address.split(" ") : [];
this.setData({
editeAddressPage: editeAddressPage,
originAddressList,
});
this.refreshSubmitBtnState();
this.hiddenPicker();
},
refreshSubmitBtnState: function () {
let consignee = this.data.editeAddressPage._consignee;
let mobile = this.data.editeAddressPage._mobile;
let area_code = this.data.editeAddressPage._area_code;
let address = this.data.editeAddressPage._detailAddress;
let submitBtnEnable = this.data.submitBtnEnable;
if (consignee.length && mobile.length && area_code.length && address.length) {
submitBtnEnable = true;
} else {
submitBtnEnable = false;
}
this.setData({
submitBtnEnable,
})
},
didTouchCancelButton: function () {
this.hiddenPicker();
},
hiddenPicker: function () {
let newEditeAddressPage = Object.assign(this.data.editeAddressPage, {
isShow: false, // 显示区域选择框
});
this.setData({
editeAddressPage: newEditeAddressPage,
});
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
},
})