address.js
5.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
/**
* Created by PhpStorm.
* User: Targaryen
* Date: 2016/8/17
* Time: 13:41
*/
'use strict';
const utils = '../../../utils';
const api = global.yoho.API;
const _ = require('lodash');
const camelCase = global.yoho.camelCase;
const decodeURIComponentExt = require(`${utils}/string-code`).decodeURIComponentExt;
module.exports = class extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
}
/**
* 从接口获取地址列表
* @param params
*/
_getAddressData(params) {
return this.get({
data: {
method: 'app.address.gethidden',
uid: params.uid
},
param: {code: 200}
});
}
/**
* 获取三级地址列表数据
* @param params
* @returns {*}
* @private
*/
_getThrAddressData(params) {
return this.get({
data: {
method: 'app.address.getlist',
uid: params.uid
},
param: {code: 200, cache: true}
});
}
/**
* 调用接口保存地址数据
* @param params
* @private
*/
_saveAddressData(params) {
return this.get({
data: {
method: params.id ? 'app.address.update' : 'app.address.add',
id: decodeURIComponentExt(params.id),
address: params.address,
area_code: params.area_code,
consignee: params.consignee,
email: params.email,
mobile: params.mobile,
zip_code: params.zip_code,
uid: params.uid
}
});
}
/**
* 调用接口设置默认地址
* @param params
* @private
*/
_setDefaultAddress(params) {
return this.get({
data: {
method: 'app.address.setdefault',
uid: params.uid,
id: decodeURIComponentExt(params.id)
}
});
}
/**
* 调用接口删除地址
* @param params
* @private
*/
_delAddressData(params) {
return this.get({
data: {
method: 'app.address.del',
id: decodeURIComponentExt(params.id),
uid: params.uid
}
});
}
/**
* 地址管理页面
* @param params
*/
address(params) {
let apiList = [
this._getAddressData(params)
];
if (params.id) { // 如果有 ID 是编辑地址页面
params.id = decodeURIComponentExt(params.id);
apiList.push(this._getThrAddressData(params));
}
return api.all(apiList).then(result => {
let selfAddress = _.get(result, '[0].data', []);
let editAddress = {};
_.forEach(selfAddress, (value) => {
if (value.area.length > 11) {
value.showArea = value.area.substr(0, 5) + '...' + value.area.substr(-5);
} else {
value.showArea = value.area;
}
if (params.id && value.address_id === params.id) {
editAddress = value;
}
value.address_id = encodeURIComponent(value.address_id);
});
return {
address: camelCase(params.id ? editAddress : selfAddress),
addressList: camelCase(_.get(result, '[1].data', [])),
navTitle: params.id ? '编辑地址' : '添加地址'
};
});
}
/**
* 保存地址数据
* @param params
*/
saveAddress(params) {
return this._saveAddressData(params).then(result => {
return result;
});
}
/**
* 设置默认地址
* @param params
*/
setDefaultAddress(params) {
return this._setDefaultAddress(params).then(result => {
return result;
});
}
/**
* 删除地址
* @param params
*/
delAddress(params) {
return this._delAddressData(params).then(result => {
return result;
});
}
/**
* 三级地址数据
* @param params
*/
locationList(params) {
return this._getThrAddressData(params).then(result => {
if (result.data) {
return {
addressList: result.data
};
}
});
}
/**
* 新版地址选择获取地址
*/
newGetAddress(params) {
return this.get({
data: {
method: 'app.address.provinces',
id: _.parseInt(params.id) || 0
},
param: {code: 200, cache: true}
}).then(result => {
return result && result.data;
});
}
/**
* 订单详情选择地址
* @param params
*/
chooseAddress(orderCode, addressId, uid) {
return this.get({
data: {
method: 'app.SpaceOrders.updateDeliveryAddress',
uid: uid,
order_code: orderCode,
address_id: decodeURIComponentExt(addressId)
}
}).then((result) => {
return result;
});
}
};