Authored by htoooth

refactor

... ... @@ -6,6 +6,7 @@
'use strict';
const helpers = global.yoho.helpers;
const config = global.yoho.config;
const service = require('../models/back-service');
const passportHelper = require('../models/passport-helper');
const simpleHeaderModel = require('../../../doraemon/models/simple-header');
... ... @@ -17,12 +18,15 @@ const _ = require('lodash');
const index = (req, res, next) => {
// 清除cookie
res.clearCookie('_UID', {
domain: 'yohobuy.com'
domain: config.cookieDomain
});
res.clearCookie('_TOKEN', {
domain: 'yohobuy.com'
domain: config.cookieDomain
});
// 清除 session
req.session = null;
service.indexPageDataAsync()
.then(result => {
res.render('back/index', Object.assign({
... ...
... ... @@ -70,7 +70,7 @@ const common = {
let refer = req.query.refer || req.get('Referer');
refer && res.cookie('refer', encodeURI(refer), {
domain: '.yohobuy.com'
domain: config.cookieDomain
});
next();
},
... ... @@ -98,12 +98,14 @@ const local = {
loginPage: (req, res) => {
// 清除cookie
res.clearCookie('_UID', {
domain: '.yohobuy.com'
domain: config.cookieDomain
});
res.clearCookie('_TOKEN', {
domain: '.yohobuy.com'
domain: config.cookieDomain
});
req.session = null;
let bindMobile = _.trim(req.query.bindMobile || '');
let bindArea = '+' + _.trim(req.query.bindArea || '86');
let areaArr = PassportHelper.getCountry();
... ... @@ -190,10 +192,10 @@ const local = {
req.session = null;
res.clearCookie('_UID', {
domain: 'yohobuy.com'
domain: config.cookieDomain
});
res.clearCookie('_TOKEN', {
domain: 'yohobuy.com'
res.clearCookie('TOKEN_', {
domain: config.cookieDomain
});
res.clearCookie('_SPK');
res.clearCookie('_g');
... ... @@ -366,7 +368,6 @@ const local = {
const wechat = {
login: (req, res, next) => {
req.session = req.session || {};
req.session.authState = uuid.v4();
return passport.authenticate('wechat', {
state: req.session.authState
... ... @@ -396,7 +397,6 @@ const wechat = {
const sina = {
login: (req, res, next) => {
req.session = req.session || {};
req.session.authState = uuid.v4();
return passport.authenticate('sina', {
state: req.session.authState
... ... @@ -426,8 +426,6 @@ const sina = {
const qq = {
login: (req, res, next) => {
req.session = req.session || {};
let type = req.query.type || 'yohobuy';
let authState = req.session.authState = (req.query.state || uuid.v4()) + '::' + type;
... ... @@ -489,7 +487,6 @@ const alipay = {
const douban = {
login: (req, res, next) => {
req.session = req.session || {};
req.session.authState = uuid.v4();
return passport.authenticate('douban', {
state: req.session.authState
... ... @@ -516,7 +513,6 @@ const douban = {
const renren = {
login: (req, res, next) => {
req.session = req.session || {};
req.session.authState = uuid.v4();
return passport.authenticate('renren', {
state: req.session.authState
... ...
... ... @@ -66,7 +66,7 @@ let checkPassword = (req, res, next) => {
*/
let index = (req, res, next) => {
// 设置注册有效时间30分钟, 防机器刷
req.session._REG_EXPIRE = Date.now() + 1800000;
req.session.REG_EXPIRE_ = Date.now() + 1800000;
let refer = req.query.refer;
let mobile = req.query.mobile;
... ... @@ -230,7 +230,7 @@ let mobileRegister = (req, res, next) => {
};
/* 判断是否是有效的注册方式,防注册机刷 */
let regExpireTime = req.session._REG_EXPIRE;
let regExpireTime = req.session.REG_EXPIRE_;
if (!regExpireTime || regExpireTime < Date.now()) {
data.message = '注册超时';
... ...
... ... @@ -6,6 +6,7 @@ const aes = require('./aes-pwd');
const cache = global.yoho.cache;
const sign = global.yoho.sign;
const cookie = global.yoho.cookie;
const config = global.yoho.config;
const Promise = require('bluebird');
... ... @@ -33,14 +34,11 @@ const syncUserSession = (uid, req, res) => {
let uidCookie = `${data.profile_name}::${encryptionUid}::${data.vip_info.title}::${token}`;
let isStudent = data.vip_info.is_student || 0;
req.session._TOKEN = token;
req.session._LOGIN_UID = uid;
res.cookie('_UID', uidCookie, {
domain: 'yohobuy.com'
domain: config.cookieDomain
});
res.cookie('isStudent', isStudent, {
domain: 'yohobuy.com'
domain: config.cookieDomain
});
// 购物车中商品的数量
... ... @@ -50,14 +48,17 @@ const syncUserSession = (uid, req, res) => {
_ac: 0,
_c: 1
}), {
domain: 'yohobuy.com'
domain: config.cookieDomain
});
}
req.session._TOKEN = token; // esline-disable-line
req.session._LOGIN_UID = uid; // esline-disable-line
req.session.TOKEN_ = token;
req.session.LOGIN_UID_ = uid;
res.cookie('_TOKEN', token, {
domain: 'yohobuy.com'
}); // esline-disable-line
domain: config.cookieDomain
});
}).catch(console.log);
};
... ... @@ -65,8 +66,15 @@ const rememberAccount = (accountInfo, req, res) => {
let aWeek = (new Date()).getTime() / 1000 + 504000; // 504000-一周
let rememKey = md5(md5(accountInfo.account + accountInfo.password + accountInfo.area));
res.cookie('isRemember', true, aWeek);
res.cookie('remem', rememKey, aWeek);
res.cookie('isRemember', true, {
maxAge: aWeek,
domain: config.cookieDomain
});
res.cookie('remem', rememKey, {
maxAge: aWeek,
domain: config.cookieDomain
});
if (!cache.get(rememKey)) {
cache.set(rememKey, accountInfo, aWeek);
}
... ...
... ... @@ -14,6 +14,7 @@ module.exports = {
appVersion: '4.9.0', // 调用api的版本
port: 6002,
siteUrl: 'http://www.yohobuy.com',
cookieDomain: '.yohobuy.com',
domains: {
favApi: 'http://192.168.102.31:8092/brower',
api: 'http://api.yoho.cn/',
... ...
... ... @@ -6,25 +6,22 @@ const cookie = global.yoho.cookie;
module.exports = () => {
return (req, res, next) => {
// session 没有读取到的时候,从 cookie 读取 UID
// 从 cookie 读取 UID
if (req.cookies._UID) {
req.user.uid = cookie.getUid(req);
let getVip = Fp.pipe(Fp.split('::'), Fp.nth(2));
let uidCookie = req.cookies._UID.split('::');
req.user.vip = getVip(req.cookies._UID);
}
let getName = Fp.nth(0);
let getVip = Fp.nth(2);
let getToken = Fp.nth(3);
// 用户是否学生
if (req.user.uid && req.cookies.isStudent) {
req.user.name = getName(uidCookie); //0
req.user.uid = cookie.getUid(req); //1
req.user.vip = getVip(uidCookie); //2
req.user.token = getToken(uidCookie); //3
req.user.isStudent = req.cookies.isStudent || 0;
}
// 从 SESSION 中获取到当前登录用户的 UID
// if (req.session && _.isNumber(req.session._LOGIN_UID)) {
// req.user.uid = req.session._LOGIN_UID;
// }
next();
};
};
... ...