user.js
2.08 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
const _ = require('lodash');
const authcode = require('../../utils/authcode');
const cookie = global.yoho.cookie;
const config = global.yoho.config;
module.exports = (req, res, next) => {
if (!req.yoho.isApp) {
if (req.session && _.isNumber(req.session.LOGIN_UID_)) {
// 调用接口传参时切勿使用toString获得字符串
req.user = {
uid: _.parseInt(req.session.LOGIN_UID_),
sessionKey: req.session.SESSION_KEY,
appSessionType: req.session.SESSION_TYPE
};
let userData = _.get(req.session, 'USER', {});
_.merge(req.user, userData);
}
if (!req.user.uid && req.cookies._UID) {
let sessionKey = req.cookies._SESSION_KEY &&
authcode(req.cookies._SESSION_KEY, '_SESSION_KEY', 2592000000);
req.user = {
uid: _.parseInt(cookie.getUid(req)),
sessionKey,
appSessionType: req.session.SESSION_TYPE
};
}
} else if (!req.user.uid &&
req.cookies.app_uid &&
req.cookies.app_uid !== '0' &&
req.cookies.app_session_key &&
req.cookies.app_version &&
req.cookies.app_client_type) {
// 调用接口传参时切勿使用toString获得字符串
req.user = {
uid: _.parseInt(req.cookies.app_uid),
sessionKey: req.cookies.app_session_key,
appVersion: req.cookies.app_version || config.appVersion,
appSessionType: req.cookies.app_client_type
};
} else if (!req.user.uid &&
req.headers['app-uid'] &&
req.headers['app-session-key'] &&
req.headers['app-version'] &&
req.headers['app-client-type']) {
// 调用接口传参时切勿使用toString获得字符串
req.user = {
uid: _.parseInt(req.headers['app-uid']),
sessionKey: req.headers['app-session-key'],
appVersion: req.headers['app-version'] || config.appVersion,
appSessionType: req.headers['app-client-type']
};
}
res.locals.isLogin = Boolean(req.user.uid); // 用户是否登录
if (_.get(req, 'user.appSessionType') === 'miniapp') { // miniapp环境下调用接口使用web的version
req.user.appVersion = config.appVersion;
}
next();
};