user.js
3.01 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
/**
* 用户controller
* @author: leo <qi.li@yoho.cn>
* @date: 2017/06/26
*/
'use strict';
const _ = require('lodash');
const UserModel = require('../models/user');
const authYoho = require('../../../utils/authYoho');
const INVALID_SESSION = '用户SESSION信息缺失, 请重新验证';
const GET_USER_INFO_SUCCESS = '获取用户信息成功';
const userController = {
/**
* 获取用户信息
* @param req
* @param res
* @param next
*/
userInfo(req, res, next) {
const mobile = _.get(req.session, 'smsSend.mobile', '');
if (!mobile) {
res.json({
code: 403,
message: INVALID_SESSION
});
}
req.ctx(UserModel).getUser(mobile)
.then(result => {
let userInfo = {};
if (result.length) {
let user = result[0];
// 用户ID
_.set(req.session, 'user.id', user.id);
_.set(req.session, 'user.phone', user.user_phone);
res.json({
code: 200,
data: {
id: user.id,
name: user.user_name,
mobile: user.user_phone,
avatar: user.user_avatar
},
message: GET_USER_INFO_SUCCESS
});
} else {
let name = '用户' + mobile.substr(7);
let avatar = 'http://img12.static.yhbimg.com/' +
'imserver/2016/11/17/11/02bc3c3de856432175c01d937342a1f2ce.jpg';
userInfo = {
name,
avatar,
mobile
};
req.ctx(UserModel).createUser(userInfo)
.then((id) => {
// 用户ID
_.set(req.session, 'user.id', id);
_.set(req.session, 'user.phone', mobile);
res.json({
code: 200,
data: _.assign(userInfo, {id}),
message: GET_USER_INFO_SUCCESS
});
})
.catch(next);
}
})
.catch(next);
},
/**
* YOHO授权登录
* @param req
* @param res
*/
yohoAuth(req, res) {
const refer = req.session.auth_refer;
const yh_sign = req.query.yh_sign.toLowerCase();
const lastSign = authYoho.sign(req.query).toLowerCase();
if (yh_sign !== lastSign) {
return res.json({
code: 401,
message: 'yh_sign签名验证错误'
});
}
_.set(req.session, 'yh_auth_id', req.query.yh_uid);
res.redirect(refer);
}
};
module.exports = userController;