user-controller.js
5.77 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
/**
* 用户controller
* @author: feng.chen<feng.chen@yoho.cn>
* @date: 2017/04/13
*/
'use strict';
const Context = require('../framework/context');
const UserService = require('../service/user-service');
const _ = require('lodash');
class UserController extends Context {
constructor() {
super();
this.userService = this.instance(UserService);
}
login(req, res, next) {
Promise.all([
this.userService.login(req.body.username, req.body.password),
this.userService.shopLogin(req.body.username, req.body.password),
])
.then(
allResult => {
const user = allResult[0];
const sess = allResult[1];
let overdueInfo = '';
if (user.pwdExpirationDays > 0) {
overdueInfo = `您的密码即将在${user.pwdExpirationDays}天后过期`;
} else if (user.pwdExpirationDays !== null) {
overdueInfo =
user.pwdExpirationDays === 0 ? '您的密码已于今天过期' : `您的密码已过期${user.pwdExpirationDays}天`;
}
this.userService.getShops(user.pid).then(result => {
if (result.code === 200) {
const currentShop = _.first(result.data);
const needUpdate = user.pwdComplexRate === false;
this.userService
.switchShop({
shopId: currentShop.shopsId,
cookies: sess,
})
.then(shopSess => {
this.syncSession(
{ req, res },
Object.assign(user, {
shops: result.data,
}),
shopSess,
currentShop,
needUpdate
);
return res.json({
code: 200,
data: {
name: user.account,
email: user.email,
createDate: user.create_date,
shops: result.data,
currentShop,
overdueInfo,
pwdComplexRateDesc: user.pwdComplexRateDesc,
needUpdate,
token: user.token,
},
});
});
} else {
result.data.overdueInfo = overdueInfo;
return res.json(result);
}
});
},
err => {
req.session.isCaptcha = true;
res.cookie('_captcha', true, {
path: '/',
});
return res.json(
Object.assign(err, {
captcha: true,
})
);
}
)
.catch(next);
}
updatePwd(req, res) {
if (!req.user.uid) {
return res.status(401).json({
code: 401,
message: '抱歉,您暂未登录!',
data: {
refer: '/login',
},
});
}
if (!req.body.password) {
return res.json({
code: 400,
message: '请输入密码',
});
}
return this.userService.updatePwd(req.user.uid, req.body.password).then(result => {
if (result.code !== 200) {
return res.json(result);
}
delete req.session.needUpdate;
return res.json({
code: 200,
data: '密码修改成功',
});
});
}
logout(req, res) {
delete req.session.USER;
delete req.session.LOGIN_UID;
delete req.session.CURRENT_SHOP;
delete req.session.needUpdate;
res.clearCookie('PHPSESSID', {
domain: '.yohobuy.com',
});
res.clearCookie('connect.sid', {
domain: '.yohobuy.com',
});
res.clearCookie('_isLogin');
res.clearCookie('_sign');
res.clearCookie('yoho-shop');
res.clearCookie('yoho-shop.sig');
return res.json({
code: 200,
data: '登出成功',
});
}
config(req, res) {
const config = {
shopsFeDomain: '//shop-manage.yohobuy.com/oldshops',
};
if (global.env.Gray) {
Object.assign(config, {
shopsFeDomain: '//shopmanage.yohobuy.com/oldshops',
});
} else if (global.env.Production) {
Object.assign(config, {
shopsFeDomain: '//shopmanage.yohobuy.com/oldshops',
});
}
res.json(config);
}
switchShop(req, res) {
const shopId = req.body.shopId;
if (!shopId) {
return res.json({
code: 400,
message: '参数错误',
});
}
const shop = _.find(req.session.USER.shops, s => s.shopsId === shopId);
if (!shop) {
return res.json({
code: 400,
message: '不存在的店铺',
});
}
this.userService
.switchShop({
shopId,
cookies: {
PHPSESSID: encodeURIComponent(req.cookies.PHPSESSID),
'connect.sid': encodeURIComponent(req.cookies['connect.sid']),
},
})
.then(response => {
this.syncShopSession(
{
req,
res,
},
response
);
return res.json({
code: 200,
});
});
}
syncSession(context, user, sess, currentShop, needUpdate) {
delete context.req.session.isCaptcha;
context.res.clearCookie('_captcha');
context.req.session.USER = user;
context.req.session.LOGIN_UID = user.pid; // pid 为用户名
context.req.session.needUpdate = needUpdate;
this.syncShopSession(context, sess);
context.res.cookie('_isLogin', true, {
path: '/',
});
context.res.cookie('_sign', currentShop.shopsId, {
path: '/',
});
}
syncShopSession(context, sess) {
_.each(sess, (v, k) => {
context.res.cookie(k, v, {
path: '/',
domain: '.yohobuy.com',
httpOnly: true,
encode: val => val,
});
});
}
}
module.exports = UserController;