user.js
2.63 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
'use strict';
const _ = require('lodash');
const cookie = global.yoho.cookie;
const authcode = require('../../utils/authcode');
module.exports = () => {
return (req, res, next) => {
// 从 SESSION 中获取到当前登录用户的 UID
if (req.session && _.isNumber(req.session.LOGIN_UID)) {
// 调用接口传参时切勿使用toString获得字符串
req.user.uid = {
toString: () => {
return req.session.LOGIN_UID;
},
sessionKey: req.session.SESSION_KEY
};
let userData = _.get(req.session, 'USER', {});
_.merge(req.user, userData);
}
// session 没有读取到的时候,从 cookie 读取 UID
if (!req.user.uid && req.cookies._UID) {
let sessionKey = req.cookies._SESSION_KEY && authcode(req.cookies._SESSION_KEY, '_SESSION_KEY', 2592000000);
// 调用接口传参时切勿使用toString获得字符串
req.user.uid = {
toString: () => {
return cookie.getUid(req);
},
sessionKey
};
}
if (!req.user.uid && req.cookies.app_uid && req.cookies.app_session_key) {
// 调用接口传参时切勿使用toString获得字符串
req.user.uid = {
toString: () => {
return req.cookies.app_uid;
},
sessionKey: req.cookies.app_session_key,
appVersion: req.cookies.app_version || void 0,
appSessionType: req.cookies.app_client_type || void 0
};
}
if (!req.user.uid &&
(req.query.uid || req.cookies.app_uid) &&
(req.query.client_type || req.cookies.app_client_type) &&
(req.query.app_version || req.cookies.app_version)) {
let uid = req.query.uid || req.cookies.app_uid;
req.query.uid = {
toString: () => {
return uid;
},
appVersion: req.query.app_version || req.cookies.app_version || void 0,
appSessionType: req.query.client_type || req.cookies.app_client_type || void 0
};
res.cookie('app_uid', req.query.uid.toString(), {
domain: 'm.yohobuy.com'
});
res.cookie('app_client_type', req.query.uid.appSessionType, {
domain: 'm.yohobuy.com'
});
res.cookie('app_version', req.query.uid.appVersion, {
domain: 'm.yohobuy.com'
});
}
next();
};
};