Authored by ccbikai

增加 req.user 对象

@@ -20,6 +20,7 @@ const favicon = require('serve-favicon'); @@ -20,6 +20,7 @@ const favicon = require('serve-favicon');
20 const session = require('yoho-express-session'); 20 const session = require('yoho-express-session');
21 const memcached = require('yoho-connect-memcached'); 21 const memcached = require('yoho-connect-memcached');
22 const uuid = require('uuid'); 22 const uuid = require('uuid');
  23 +const _ = require('lodash');
23 const pkg = require('./package.json'); 24 const pkg = require('./package.json');
24 25
25 const app = express(); 26 const app = express();
@@ -51,11 +52,7 @@ app.use(session({ @@ -51,11 +52,7 @@ app.use(session({
51 return uuid.v4(); // 兼容 PHP SESSION 52 return uuid.v4(); // 兼容 PHP SESSION
52 }, 53 },
53 cookie: { 54 cookie: {
54 - domain: 'yohobuy.com',  
55 - path: '/',  
56 - httpOnly: true,  
57 - secure: false,  
58 - maxAge: null 55 + domain: 'yohobuy.com'
59 }, 56 },
60 store: new MemcachedStore({ 57 store: new MemcachedStore({
61 hosts: config.memcache.session, 58 hosts: config.memcache.session,
@@ -64,6 +61,16 @@ app.use(session({ @@ -64,6 +61,16 @@ app.use(session({
64 }) 61 })
65 })); 62 }));
66 63
  64 +app.use((req, res, next) => {
  65 + req.user = {};
  66 +
  67 + // 从 PHP 写的 SESSION 中获取到当前登录用户的 UID
  68 + if (req.session && _.isNumber(req.session._LOGIN_UID)) {
  69 + req.user.uid = req.session._LOGIN_UID;
  70 + }
  71 + next();
  72 +});
  73 +
67 // dispatcher 74 // dispatcher
68 require('./dispatch')(app); 75 require('./dispatch')(app);
69 76
@@ -8,7 +8,6 @@ const _ = require('lodash'); @@ -8,7 +8,6 @@ const _ = require('lodash');
8 const channelModel = require('../models/channel'); 8 const channelModel = require('../models/channel');
9 const helpers = require(`${global.library}/helpers`); 9 const helpers = require(`${global.library}/helpers`);
10 const log = require(`${global.library}/logger`); 10 const log = require(`${global.library}/logger`);
11 -const cookie = require(`${global.library}/cookie`);  
12 11
13 const renderData = { 12 const renderData = {
14 module: 'channel', 13 module: 'channel',
@@ -42,7 +41,7 @@ const channelLogger = (err, res) => { @@ -42,7 +41,7 @@ const channelLogger = (err, res) => {
42 const channelPage = (req, res, data) => { 41 const channelPage = (req, res, data) => {
43 channelModel.getChannelDate({ 42 channelModel.getChannelDate({
44 gender: data.gender, 43 gender: data.gender,
45 - uid: cookie.getUid(req) 44 + uid: req.user.uid
46 }).then(result => { 45 }).then(result => {
47 res.render('channel', Object.assign(renderData, data, result)); 46 res.render('channel', Object.assign(renderData, data, result));
48 }).catch((err) => { 47 }).catch((err) => {