Authored by ccbikai(👎🏻🍜)

reset

@@ -24,10 +24,10 @@ const favicon = require('serve-favicon'); @@ -24,10 +24,10 @@ const favicon = require('serve-favicon');
24 const uuid = require('uuid'); 24 const uuid = require('uuid');
25 25
26 const pkg = require('./package.json'); 26 const pkg = require('./package.json');
27 -const CookieSession = require('client-sessions');  
28 -const MemcachedSession = require('yoho-express-session'); 27 +const cookieSession = require('client-sessions');
  28 +const memcachedSession = require('yoho-express-session');
29 const memcached = require('connect-memcached'); 29 const memcached = require('connect-memcached');
30 -const MemcachedStore = memcached(MemcachedSession); 30 +const MemcachedStore = memcached(memcachedSession);
31 const _ = require('lodash'); 31 const _ = require('lodash');
32 32
33 const app = express(); 33 const app = express();
@@ -76,7 +76,7 @@ app.use(bodyParser.urlencoded({ @@ -76,7 +76,7 @@ app.use(bodyParser.urlencoded({
76 app.use(cookieParser()); 76 app.use(cookieParser());
77 app.use(compression()); 77 app.use(compression());
78 78
79 -app.use(MemcachedSession({ 79 +app.use(memcachedSession({
80 proxy: true, 80 proxy: true,
81 resave: false, 81 resave: false,
82 saveUninitialized: true, 82 saveUninitialized: true,
@@ -87,9 +87,6 @@ app.use(MemcachedSession({ @@ -87,9 +87,6 @@ app.use(MemcachedSession({
87 domain: 'yohobuy.com', 87 domain: 'yohobuy.com',
88 httpOnly: false 88 httpOnly: false
89 }, 89 },
90 - // genid() {  
91 - // return uuid.v4() // use UUIDs for session IDs  
92 - // },  
93 store: new MemcachedStore({ 90 store: new MemcachedStore({
94 hosts: config.memcache.session, 91 hosts: config.memcache.session,
95 prefix: 'yohobuy_session:', 92 prefix: 'yohobuy_session:',
@@ -99,52 +96,43 @@ app.use(MemcachedSession({ @@ -99,52 +96,43 @@ app.use(MemcachedSession({
99 }) 96 })
100 })); 97 }));
101 98
102 -// app.use(CookieSession({  
103 -// requestKey: 'session2',  
104 -// cookieName: 'yohobuy_session_cookie',  
105 -// secret: '82dd7e724f2c6870472c89dfa43cf48d',  
106 -// domain: config.cookieDomain  
107 -// }));  
108 -  
109 -// app.use((req, res, next) => {  
110 -// if (req.session) {  
111 -// let sessionKeys = Object.keys(req.session || {});  
112 -// let backSessionKeys = Object.keys(req.session2.sessionBack || {});  
113 -  
114 -// if (backSessionKeys.length > sessionKeys.length) {  
115 -// let differences = _.difference(backSessionKeys, sessionKeys);  
116 -// _.forEach(differences, d => {  
117 -// req.session[d] = req.session2.sessionBack[d];  
118 -// });  
119 -// }  
120 -// req.session2.sessionBack = req.session;  
121 -// } else {  
122 -// req.session = new MemcachedSession.Session(req);  
123 -// req.session = _.assign(req.session, req.session2.sessionBack);  
124 -// }  
125 -  
126 -// if (typeof req.session.reset !== 'function') {  
127 -// req.session.reset = function() {  
128 -// req.session.destroy();  
129 -// req.session2.reset();  
130 -// };  
131 -// }  
132 -// next();  
133 -// }); 99 +app.use(cookieSession({
  100 + requestKey: 'session2',
  101 + cookieName: 'yohobuy_session_cookie',
  102 + secret: '82dd7e724f2c6870472c89dfa43cf48d',
  103 + domain: config.cookieDomain
  104 +}));
  105 +
  106 +app.use((req, res, next) => {
  107 + if (req.session) {
  108 + let sessionKeys = Object.keys(req.session || {});
  109 + let backSessionKeys = Object.keys(req.session2.sessionBack || {});
  110 +
  111 + if (backSessionKeys.length > sessionKeys.length) {
  112 + let differences = _.difference(backSessionKeys, sessionKeys);
  113 +
  114 + _.forEach(differences, d => {
  115 + req.session[d] = req.session2.sessionBack[d];
  116 + });
  117 + }
  118 + req.session2.sessionBack = req.session;
  119 + } else {
  120 + req.session = new memcachedSession.Session(req);
  121 + req.session = _.assign(req.session, req.session2.sessionBack);
  122 + }
  123 +
  124 + if (!req.sessionID) {
  125 + req.sessionID = uuid.v4();
  126 + }
  127 +
  128 + next();
  129 +});
134 130
135 131
136 app.use((req, res, next) => { 132 app.use((req, res, next) => {
137 req.user = {}; // 全局的用户数据 133 req.user = {}; // 全局的用户数据
138 req.yoho = {}; // req和res绑定yoho对象,用于传递全局数据, 如req.yoho.channel等 134 req.yoho = {}; // req和res绑定yoho对象,用于传递全局数据, 如req.yoho.channel等
139 req.app.locals.wap = app.locals.wap; // zookeper对象赋值 135 req.app.locals.wap = app.locals.wap; // zookeper对象赋值
140 - if (!req.session) {  
141 - req.session = {};  
142 - }  
143 - if (!req.session.id) {  
144 - req.session.id = uuid.v4();  
145 - }  
146 -  
147 - req.sessionID = req.session.id;  
148 136
149 next(); 137 next();
150 }); 138 });
@@ -143,6 +143,7 @@ const local = { @@ -143,6 +143,7 @@ const local = {
143 143
144 res.render('international', { 144 res.render('international', {
145 width750: true, 145 width750: true,
  146 +
146 // 返回的URL链接 147 // 返回的URL链接
147 backUrl: 'javascript:history.go(-1)', // eslint-disable-line 148 backUrl: 'javascript:history.go(-1)', // eslint-disable-line
148 loginInternational: true, // 模板中使用JS的标识 149 loginInternational: true, // 模板中使用JS的标识
@@ -179,6 +180,7 @@ const local = { @@ -179,6 +180,7 @@ const local = {
179 180
180 if (!errorCount) { 181 if (!errorCount) {
181 _.set(req.session, 'captchaValidCount', 5); 182 _.set(req.session, 'captchaValidCount', 5);
  183 +
182 // delete req.session.captcha; // 验证码 用过就扔 184 // delete req.session.captcha; // 验证码 用过就扔
183 req.session.captcha = null; 185 req.session.captcha = null;
184 jsonData.changeCaptcha = true; 186 jsonData.changeCaptcha = true;
@@ -235,7 +237,12 @@ const local = { @@ -235,7 +237,12 @@ const local = {
235 })(req, res, next); 237 })(req, res, next);
236 }, 238 },
237 logout: (req, res) => { 239 logout: (req, res) => {
238 - req.session.destroy(); 240 + if (req.session && req.session.destroy) {
  241 + req.session.destroy();
  242 + }
  243 + if (req.session2 && req.session2.reset) {
  244 + req.session2.reset();
  245 + }
239 246
240 res.clearCookie('_UID', { 247 res.clearCookie('_UID', {
241 domain: 'yohobuy.com' 248 domain: 'yohobuy.com'