addressManager.js
9.48 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
import AddressService from './addressService.js'
import Yas from '../../../utils/yas';
var modeEdit = "modeEdit";
var modeSelect = "modeSelect";
var modeChange = "modeChange";
var modeAdd = "modeAdd"
var app = getApp();
let PV_ID = new Date().getTime() + '';
let yas;
Page({
data: {
items: [],
order_code: "",
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: "",
_is_default: "N",
},
currentSelectedIndex: -1,
addType: 'none' // normal or wechat
},
onLoad:function(options){
yas = new Yas(this);
yas.pageOpenReport();
// 生命周期函数--监听页面加载
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();
}
},
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 that = this;
var addressID = this.data.items[index].address_id;
let api = new AddressService().yohoApi();
wx.showModal({
title: '提示',
content: '关联订单的收货地址将一起修改且不可修改第二次,请确认是否修改',
cancelText: '返回',
confirmText: '确认修改',
confirmColor: '#000000',
success: function(res) {
if (res.confirm) {
let params = {
order_code: that.data.order_code,
address_id: addressID
}
api.delAddressRequest(params)
.then(data => {
if (data) {
wx.navigateBack({
delta: 1
})
}
})
.catch(error => {
})
}
else {
wx.navigateBack({
delta: 1
});
}
}
})
}
},
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 is_default = e.currentTarget.dataset.is_default;
let newEditeAddressPage = Object.assign(this.data.editeAddressPage, {
_consignee: consignee,
_mobile: mobile,
_area_code: area_code,
_address: address,
_detailAddress: detailAddress,
_page_name: '',
_id: id,
currentEditMode: modeEdit,
_is_default: is_default,
});
var timestamp = Date.parse(new Date());
try {
wx.setStorageSync(timestamp + '', newEditeAddressPage);
} catch (e) {
}
wx.navigateTo({
url: './addressEditer/addressEditer?' + 'timestamp=' + timestamp ,
})
},
addButtonTapped: function() {
if (this.data.items.length >= 5) {
wx.showToast({
title: '地址不能超过5个',
icon: 'none',
duration: 1500,
})
return;
}
// 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 = Object.assign(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,
})
},
/**
* 获取地址列表
*/
getAddressList: function() {
let api = new AddressService().yohoApi();
api.fetchAddressList()
.then(data => {
if (data) {
this.data.items = 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,
currentSelectedIndex
})
}
})
.catch(error => {
console.error(error);
})
},
})