Authored by 毕凯

Merge branch 'gray' of git.yoho.cn:fe/yohobuywap-node into gray

@@ -108,67 +108,7 @@ app.use(bodyParser.urlencoded({ @@ -108,67 +108,7 @@ app.use(bodyParser.urlencoded({
108 app.use(cookieParser()); 108 app.use(cookieParser());
109 app.use(compression()); 109 app.use(compression());
110 110
111 -const sessionStore = new MemcachedStore({  
112 - hosts: config.memcache.session,  
113 - prefix: 'yohobuy_session:',  
114 - reconnect: 5000,  
115 - timeout: 1000,  
116 - retries: 0  
117 -});  
118 -  
119 -app.use(memcachedSession({  
120 - proxy: true,  
121 - resave: false,  
122 - saveUninitialized: true,  
123 - unset: 'destroy',  
124 - secret: '82dd7e724f2c6870472c89dfa43cf48d',  
125 - name: 'yohobuy_session',  
126 - cookie: {  
127 - domain: 'yohobuy.com',  
128 - httpOnly: false  
129 - },  
130 - store: sessionStore  
131 -}));  
132 -  
133 -app.use(cookieSession({  
134 - requestKey: 'session2',  
135 - cookieName: 'yohobuy_session_cookie',  
136 - secret: '82dd7e724f2c6870472c89dfa43cf48d',  
137 - cookie: {  
138 - domain: 'yohobuy.com',  
139 - ephemeral: true  
140 - }  
141 -}));  
142 -  
143 -app.use((req, res, next) => {  
144 - if (req.session) {  
145 - let sessionKeys = Object.keys(req.session || {});  
146 - let backSessionKeys = Object.keys(req.session2.sessionBack || {});  
147 -  
148 - if (backSessionKeys.length > sessionKeys.length) {  
149 - let differences = _.difference(backSessionKeys, sessionKeys);  
150 -  
151 - _.forEach(differences, d => {  
152 - req.session[d] = req.session2.sessionBack[d];  
153 - });  
154 - }  
155 - req.session2.sessionBack = req.session;  
156 - } else {  
157 - req.session = new memcachedSession.Session(req);  
158 - req.session.cookie = new memcachedSession.Cookie({  
159 - domain: 'yohobuy.com',  
160 - httpOnly: false  
161 - });  
162 - req.session = _.assign(req.session, req.session2.sessionBack);  
163 - }  
164 -  
165 - if (!req.sessionID) {  
166 - req.sessionID = uuid.v4();  
167 - }  
168 -  
169 - next();  
170 -});  
171 - 111 +require('./doraemon/middleware/yoho-session')(app);
172 112
173 app.use((req, res, next) => { 113 app.use((req, res, next) => {
174 req.user = {}; // 全局的用户数据 114 req.user = {}; // 全局的用户数据
  1 +const config = global.yoho.config;
  2 +const memcachedSession = require('yoho-express-session');
  3 +const _ = require('lodash');
  4 +const uuid = require('uuid');
  5 +
  6 +const cookieSession = require('client-sessions');
  7 +const memcached = require('connect-memcached');
  8 +const MemcachedStore = memcached(memcachedSession);
  9 +
  10 +/**
  11 + * 该中间件主要把 express-session 和 client-session 集中起来处理,如果 memcached 出错了,使用 cookie session
  12 + * @param opts.backSession cookieSession 的键名
  13 + * @returns {function(*=, *=, *)}
  14 + */
  15 +function yohoSession(opts) {
  16 + return (req, res, next) => {
  17 + let notUseMemcached = _.get(req.app.locals.wap, 'session.removeMemcached', false);
  18 +
  19 + opts.backSession = opts.backSession || 'session2';
  20 +
  21 + if (req.session && !notUseMemcached) {
  22 + req.sessionError = false;
  23 + } else {
  24 + // 重建 session
  25 + res.emit('sessionError');
  26 + req.sessionError = true;
  27 +
  28 + req.sessionID = uuid.v4();
  29 + req.session = new memcachedSession.Session(req, req[opts.backSession].sessionBack);
  30 + req.session.cookie = new memcachedSession.Cookie({
  31 + domain: 'yohobuy.com',
  32 + httpOnly: false
  33 + });
  34 + }
  35 +
  36 + Object.defineProperty(req.session, 'reset', {
  37 + configurable: true,
  38 + enumerable: false,
  39 + value: function() {
  40 + req.session.destroy();
  41 + req[opts.backSession].reset();
  42 + },
  43 + writable: false
  44 + });
  45 +
  46 + // 备份数据
  47 + req[opts.backSession].sessionBack = req.session;
  48 +
  49 + next();
  50 + };
  51 +}
  52 +
  53 +module.exports = (app) => {
  54 + app.use(memcachedSession({ // eslint-disable-line
  55 + proxy: true,
  56 + resave: false,
  57 + saveUninitialized: true,
  58 + unset: 'destroy',
  59 + secret: '82dd7e724f2c6870472c89dfa43cf48d',
  60 + name: 'yohobuy_session',
  61 + cookie: {
  62 + domain: 'yohobuy.com',
  63 + httpOnly: false
  64 + },
  65 + store: new MemcachedStore({
  66 + hosts: config.memcache.session,
  67 + prefix: 'yohobuy_session:',
  68 + reconnect: 5000,
  69 + timeout: 1000,
  70 + retries: 0
  71 + })
  72 + }));
  73 +
  74 + app.use(cookieSession({ // eslint-disable-line
  75 + requestKey: 'session2',
  76 + cookieName: 'yohobuy_session_cookie',
  77 + secret: '82dd7e724f2c6870472c89dfa43cf48d',
  78 + cookie: {
  79 + domain: 'yohobuy.com',
  80 + ephemeral: true
  81 + }
  82 + }));
  83 +
  84 + app.use(yohoSession({
  85 + backSession: 'session2'
  86 + }));
  87 +};