user-controller.js
3.28 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
/**
* 用户controller
* @author: feng.chen<feng.chen@yoho.cn>
* @date: 2017/04/13
*/
'use strict';
const Context = require('../common/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 => {
let user = allResult[0];
let sess = allResult[1];
this.userService.getShops(user.pid).then(result => {
if (result.code === 200) {
let currentShop = _.first(result.data);
this.syncSession({req, res}, Object.assign(user, {
shops: result.data,
currentShop: currentShop
}), sess);
console.log(result.data)
return res.json({
code: 200,
data: {
name: user.account,
email: user.email,
createDate: user.create_date,
shops: _.map(result.data, shop => {
return {
id: shop.id,
shopName: shop.shopName
};
}),
currentShop: {
shopName: currentShop.shopName,
id: currentShop.id
}
}
});
} else {
return res.json(result);
}
});
}, err => {
return res.json(err);
}).catch(next);
}
logout(req, res) {
delete req.session.USER;
delete req.session.LOGIN_UID;
delete req.session.CURRENT_SHOP;
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: '登出成功'
});
}
syncSession(context, user, sess) {
context.req.session.USER = user;
context.req.session.LOGIN_UID = user.pid; // pid 为用户名
_.each(sess, (v, k) => {
context.res.cookie(k, v, {
path: '/',
domain: '.yohobuy.com',
httpOnly: true,
overwrite: false,
encode: val => val
});
});
context.res.cookie('_isLogin', true, {
path: '/'
});
context.res.cookie('_sign', user.currentShop.id, {
path: '/'
});
}
}
module.exports = UserController;