user.js
1.35 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
/**
* 用户controller
* @author: feng.chen<feng.chen@yoho.cn>
* @date: 2017/04/13
*/
'use strict';
const Context = require('../common/context');
const UserModel = require('../service/user-model');
const config = global.yoho.config;
class UserController extends Context {
login(req, res, next) {
this.instance(UserModel).login(req.body.username, req.body.password).then(result => {
if (result.code === 200) {
this.syncSession(req, res, result.data);
return res.json({
code: 200,
data: {
name: result.data.account
}
});
} else {
return res.json({
code: 400,
message: '登录错误'
});
}
}).catch(next);
}
logout(req, res) {
delete res.session.LOGIN_UID;
delete res.session.USER;
res.clearCookie('USER_NAME');
return res.json({
code: 200,
data: '登出成功'
});
}
syncSession(req, res, user) {
req.session.USER = user;
req.session.LOGIN_UID = user.pid; // pid 为用户名
res.cookie('USER_NAME', user.account, {
domain: config.cookieDomain
});
}
}
module.exports = UserController;