address.js
4.46 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
/**
* @author: jiangmin
* @date: 2016/07/05
*/
'use strict';
const mcHandler = require('../models/menu-crumb-handler');
const addressModel = require('../models/address');
const crypto = global.yoho.crypto;
const config = global.yoho.config;
const _ = require('lodash');
/**
* 默认地址
*/
const _setDefault = (id, uid) => {
return addressModel.setDefaultAddress(id, uid).then(result => result);
};
/**
* 收货地址页面加载
*/
const index = (req, res, next) => {
let uid = req.user.uid;
Promise.all([addressModel.getAddressDataAsync(uid, 20), mcHandler.getMeThumb()]).then(result => {
let resultData = result[0].data ? result[0].data : [];
let length = resultData.length > 7 ? 7 : resultData.length;
let reg = /(\d{3})\d{4}(\d{4})/;
let thumb = result[1];
for (let i = 0; i < length; i++) {
resultData[i].default = resultData[i].is_default === 'Y';
resultData[i].mobile = resultData[i].mobile.replace(reg, '$1****$2');
resultData[i].id = crypto.encryption(config.crypto.common, resultData[i].address_id + '');
}
resultData.leftLength = 7 - length;
resultData.length = length;
res.display('index', {
module: 'me',
page: 'address',
isMe: true,
content: {
nav: mcHandler.getMeCrumb('收货地址'),
navigation: mcHandler.getSideMenu('收货地址'),
banner: thumb,
address: true,
title: '收货地址',
data: resultData
}
});
}).catch(next);
};
const getAddressList = (req, res, next) => {
addressModel.getAddressDataAsync(req.user.uid, 20).then(result => {
if (result.data) {
let defaultAd = _.find(result.data, o => o.is_default === 'Y');
_.each(result.data, (d) => {
d.address_id = crypto.encryption(config.crypto.common, d.address_id + '');
});
defaultAd && (defaultAd.focus = true);
}
res.send(result);
}).catch(next);
};
/**
* 添加地址
*/
const addAddressData = (req, res, next) => {
let uid = req.user.uid;
let address = req.body.address;
let areaCode = req.body.area_code;
let consignee = req.body.consignee;
let mobile = req.body.mobile;
let phone = req.body.phone;
let isInit = req.body.init && req.body.init === 'true'; // 是否是初始地址
addressModel.addAddressData(uid, address, areaCode, consignee, mobile, phone, isInit).then(result => {
if (result.data && result.data.address_id) {
result.data.aid = crypto.encryption(config.crypto.common, result.data.address_id + '');
}
res.send(result);
}).catch(next);
};
/**
* 修改地址
*/
const updateAddressData = (req, res, next) => {
let id = crypto.decrypt(config.crypto.common, req.body.id);
let uid = req.user.uid;
let address = req.body.address;
let areaCode = req.body.area_code;
let consignee = req.body.consignee;
let mobile = req.body.mobile;
let phone = req.body.phone;
addressModel.updateAddressData(id, uid, address, areaCode, consignee, mobile, phone).then(result => {
res.send(result);
}).catch(next);
};
/**
* 删除地址
*/
const delAddressData = (req, res, next) => {
let id = crypto.decrypt(config.crypto.common, req.body.id);
let uid = req.user.uid;
addressModel.delAddressData(id, uid).then(result => {
res.send(result);
}).catch(next);
};
/**
* 设置默认地址
*/
const setDefaultAddress = (req, res) => {
let id = crypto.decrypt(config.crypto.common, req.body.id);
let uid = req.user.uid;
_setDefault(id, uid).then(result => {
res.send(result);
});
};
/**
* 获取地址信息
* @function getAddressData
* @return { Object }
*/
const getAddressData = (req, res) => {
addressModel.getAddressData(req.params.areaId).then(result => {
if (result.code === 200) {
result.data = result.data.map((item) => {
return {
is_support: item.is_support_express,
text: item.caption,
value: item.id
};
});
res.json(result);
} else {
res.json(result);
}
});
};
module.exports = {
index,
getAddressList,
addAddressData,
updateAddressData,
delAddressData,
setDefaultAddress,
getAddressData
};