|
|
'use strict';
|
|
|
|
|
|
const Fn = require('lodash/fp');
|
|
|
const cookie = global.yoho.cookie;
|
|
|
const authcode = require(`${global.utils}/authcode`);
|
|
|
const Promise = require('bluebird');
|
|
|
const co = Promise.coroutine;
|
|
|
const config = require('../../config/common');
|
|
|
const cache = global.yoho.cache;
|
|
|
const logger = global.yoho.logger;
|
|
|
const crypto = global.yoho.crypto;
|
|
|
|
|
|
|
|
|
// const loginService = require('../../apps/passport/models/login-service');
|
|
|
// const _ = require('lodash');
|
|
|
// const cache = global.yoho.cache;
|
|
|
|
|
|
function decrypt(word) {
|
|
|
return authcode(word, '_SESSION_KEY', 0, 'decode');
|
|
|
}
|
...
|
...
|
@@ -25,54 +14,41 @@ module.exports = () => { |
|
|
// 获得原始请求 url
|
|
|
req.fullUrl = () => req.protocol + '://' + req.get('host') + req.originalUrl;
|
|
|
|
|
|
co(function *() {
|
|
|
// 个性化推荐id
|
|
|
if (req.cookies._PRID) {
|
|
|
req.user.prid = parseInt(`0${crypto.decrypt('', req.cookies._PRID)}`, 10);
|
|
|
}
|
|
|
|
|
|
// 始终从 session 中读取 uid
|
|
|
let uid = req.session.LOGIN_UID_;
|
|
|
|
|
|
if (uid && req.cookies._UID && req.cookies._SESSION_KEY) {
|
|
|
|
|
|
// memcached中session的key
|
|
|
let cKey = `${config.sessionMemcachedPrefix}${req.sessionID}`,
|
|
|
uid = 0;
|
|
|
let userInfo = req.cookies._UID.split('::');
|
|
|
|
|
|
yield cache.get(cKey).catch(err => {
|
|
|
logger.error(`product query save cache data fail:${err.toString()}`);
|
|
|
uid = cookie.getUid(req);
|
|
|
}).then((session) => {
|
|
|
if (session) {
|
|
|
uid = JSON.parse(session).LOGIN_UID_;
|
|
|
let getName = Fn.nth(0);
|
|
|
let getVip = Fn.nth(2);
|
|
|
let getToken = Fn.nth(3);
|
|
|
|
|
|
req.user.name = getName(userInfo); // 0
|
|
|
req.user.vip = getVip(userInfo); // 2
|
|
|
req.user.token = getToken(userInfo); // 3
|
|
|
req.user.isStudent = req.cookies.isStudent || 0;
|
|
|
|
|
|
req.user.uid = {
|
|
|
toString() {
|
|
|
return this.uid;
|
|
|
},
|
|
|
uid: uid,
|
|
|
sessionKey: decodeURIComponent(decrypt(req.cookies._SESSION_KEY)),
|
|
|
isValid() {
|
|
|
return this.uid && this.sessionKey;
|
|
|
}
|
|
|
});
|
|
|
|
|
|
// 个性化推荐id
|
|
|
if (req.cookies._PRID) {
|
|
|
req.user.prid = parseInt(`0${crypto.decrypt('', req.cookies._PRID)}`, 10);
|
|
|
}
|
|
|
|
|
|
// 从 cookie 读取 UID
|
|
|
if (uid && req.cookies._UID && req.cookies._SESSION_KEY) {
|
|
|
|
|
|
let uidCookie = req.cookies._UID.split('::');
|
|
|
|
|
|
let getName = Fn.nth(0);
|
|
|
let getVip = Fn.nth(2);
|
|
|
let getToken = Fn.nth(3);
|
|
|
|
|
|
req.user.name = getName(uidCookie); // 0
|
|
|
req.user.vip = getVip(uidCookie); // 2
|
|
|
req.user.token = getToken(uidCookie); // 3
|
|
|
req.user.isStudent = req.cookies.isStudent || 0;
|
|
|
|
|
|
req.user.uid = {
|
|
|
toString() {
|
|
|
return this.uid;
|
|
|
},
|
|
|
uid: uid,
|
|
|
sessionKey: decodeURIComponent(decrypt(req.cookies._SESSION_KEY)),
|
|
|
isValid() {
|
|
|
return this.uid && this.sessionKey;
|
|
|
}
|
|
|
};
|
|
|
|
|
|
}
|
|
|
next();
|
|
|
})();
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
next();
|
|
|
|
|
|
|
|
|
// 记住我
|
...
|
...
|
|