address.js
3.96 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
/**
* Created by PhpStorm.
* User: Targaryen
* Date: 2016/8/17
* Time: 13:41
*/
'use strict';
const api = global.yoho.API;
const _ = require('lodash');
const camelCase = global.yoho.camelCase;
const crypto = global.yoho.crypto;
/**
* 从接口获取地址列表
* @param params
*/
const _getAddressData = (params) => {
return api.get('', {
method: 'app.address.gethidden',
uid: params.uid
}, {code: 200});
};
/**
* 获取三级地址列表数据
* @param params
* @returns {*}
* @private
*/
const _getThrAddressData = (params) => {
return api.get('', {
method: 'app.address.getlist',
uid: params.uid
}, {code: 200, cache: true});
};
/**
* 调用接口保存地址数据
* @param params
* @private
*/
const _saveAddressData = (params) => {
return api.get('', {
method: params.id ? 'app.address.update' : 'app.address.add',
id: 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
*/
const _setDefaultAddress = (params) => {
return api.get('', {
method: 'app.address.setdefault',
uid: params.id,
id: params.id
});
};
/**
* 调用接口删除地址
* @param params
* @private
*/
const _delAddressData = (params) => {
return api.get('', {
method: 'app.address.del',
id: params.id,
uid: params.uid
});
};
/**
* 地址管理页面
* @param params
*/
const address = (params) => {
let apiList = [
_getAddressData(params)
];
if (params.id) { // 如果有 ID 是编辑地址页面
apiList.push(_getThrAddressData(params));
}
return api.all(apiList).then(result => {
let selfAddress = result[0];
if (selfAddress.data) {
/* 加密地址 ID */
_.forEach(selfAddress.data, (value, key) => {
selfAddress.data[key].address_id = encodeURIComponent(crypto.encryption(null, selfAddress.data[key].address_id));
});
/* 如果有 ID 是地址编辑页面 */
if (params.id) {
return {
address: camelCase(_.find(selfAddress.data, o => {
return decodeURIComponent(o.address_id) === params.id;
})),
addressList: camelCase(result[1] ? result[1].data : []),
navTitle: '编辑地址'
};
} else {
return {
address: camelCase(selfAddress.data),
addressList: camelCase(result[1] ? result[1].data : []),
navTitle: '添加地址'
};
}
}
});
};
/**
* 保存地址数据
* @param params
*/
const saveAddress = (params) => {
if (params.id) {
params.id = crypto.decrypt(null, params.id);
}
return _saveAddressData(params).then(result => {
return result;
});
};
/**
* 设置默认地址
* @param params
*/
const setDefaultAddress = (params) => {
if (params.id) {
params.id = crypto.encryption(null, params.id);
}
return _setDefaultAddress(params).then(result => {
return result;
});
};
/**
* 删除地址
* @param params
*/
const delAddress = (params) => {
if (params.id) {
params.id = crypto.decrypt(null, params.id);
}
return _delAddressData(params).then(result => {
return result;
});
};
/**
* 三级地址数据
* @param params
*/
const locationList = (params) => {
return _getThrAddressData(params).then(result => {
if (result.data) {
return {
addressList: result.data
};
}
});
};
module.exports = {
address,
saveAddress,
delAddress,
setDefaultAddress,
locationList
};