Authored by 陈峰

用户接口携带sessionid参数

@@ -4,6 +4,7 @@ @@ -4,6 +4,7 @@
4 */ 4 */
5 const headerModel = require('../models/header'); 5 const headerModel = require('../models/header');
6 const logger = global.yoho.logger; 6 const logger = global.yoho.logger;
  7 +const helpers = global.yoho.helpers;
7 8
8 const forceNoCache = (res) => { 9 const forceNoCache = (res) => {
9 if (res && !res.finished) { 10 if (res && !res.finished) {
@@ -47,10 +48,18 @@ exports.notFound = () => { @@ -47,10 +48,18 @@ exports.notFound = () => {
47 */ 48 */
48 exports.serverError = () => { 49 exports.serverError = () => {
49 return (err, req, res, next) => { 50 return (err, req, res, next) => {
50 - console.log(err);  
51 -  
52 forceNoCache(res); 51 forceNoCache(res);
53 52
  53 + if (err && err.code === 401) {
  54 + if (req.xhr) {
  55 + return res.json(err);
  56 + } else {
  57 + return res.redirect(helpers.urlFormat('/signin.html', {
  58 + refer: req.originalUrl
  59 + }));
  60 + }
  61 + }
  62 +
54 logger.error(`error at path: ${req.url}`); 63 logger.error(`error at path: ${req.url}`);
55 logger.error(err); 64 logger.error(err);
56 65
@@ -7,8 +7,13 @@ module.exports = () => { @@ -7,8 +7,13 @@ module.exports = () => {
7 return (req, res, next) => { 7 return (req, res, next) => {
8 // 从 SESSION 中获取到当前登录用户的 UID 8 // 从 SESSION 中获取到当前登录用户的 UID
9 if (req.session && _.isNumber(req.session.LOGIN_UID)) { 9 if (req.session && _.isNumber(req.session.LOGIN_UID)) {
10 - req.user.uid = req.session.LOGIN_UID;  
11 - 10 + // 不要使用 === 判断uid的值,如果需要判断使用 ==
  11 + req.user.uid = {
  12 + toString: () => {
  13 + return req.session.LOGIN_UID;
  14 + },
  15 + sessionId: req.cookies._session
  16 + };
12 let userData = _.get(req.session, 'USER', {}); 17 let userData = _.get(req.session, 'USER', {});
13 18
14 _.merge(req.user, userData); 19 _.merge(req.user, userData);
@@ -16,7 +21,13 @@ module.exports = () => { @@ -16,7 +21,13 @@ module.exports = () => {
16 21
17 // session 没有读取到的时候,从 cookie 读取 UID 22 // session 没有读取到的时候,从 cookie 读取 UID
18 if (!req.user.uid && req.cookies._UID) { 23 if (!req.user.uid && req.cookies._UID) {
19 - req.user.uid = cookie.getUid(req); 24 + // 不要使用 === 判断uid的值,如果需要判断使用 ==
  25 + req.user.uid = {
  26 + toString: () => {
  27 + return cookie.getUid(req);
  28 + },
  29 + sessionId: req.cookies._session
  30 + };
20 } 31 }
21 32
22 next(); 33 next();
  1 +/**
  2 + * 全局引用js
  3 + * @author: feng.chen<feng.chen@yoho.cn>
  4 + * @date: 2017/03/15
  5 + */
  6 +const $ = require('yoho-jquery');
  7 +const cookie = require('yoho-cookie');
  8 +var yoho = require('./yoho-app');
  9 +
  10 +// 初始化
  11 +
  12 +// 注册ajaxError处理服务端401状态
  13 +$(document).ajaxError((event, xhr) => {
  14 + if (xhr.status === 401) {
  15 + cookie.remove('_UID');
  16 + cookie.remove('_TOKEN');
  17 + if (yoho.isApp) {
  18 + yoho.goLogin(window.location.href);
  19 + } else {
  20 + window.location.href = `/signin.html?refer=${encodeURIComponent(window.location.href)}`;
  21 + }
  22 + }
  23 +});
@@ -22,7 +22,8 @@ shelljs.ls(path.join(__dirname, 'js/**/*.page.js')).forEach((f) => { @@ -22,7 +22,8 @@ shelljs.ls(path.join(__dirname, 'js/**/*.page.js')).forEach((f) => {
22 // 生成规则:module.page: './js/module/xx.page.js' 22 // 生成规则:module.page: './js/module/xx.page.js'
23 entries[`${dir[0]}.${dir[1].match(/(.*).page.js/)[1]}`] = `./js/${dir.join('/')}`; 23 entries[`${dir[0]}.${dir[1].match(/(.*).page.js/)[1]}`] = `./js/${dir.join('/')}`;
24 entries.libs = [ 24 entries.libs = [
25 - 'yoho-jquery' 25 + 'yoho-jquery',
  26 + './js/global.js' // 全局引用js
26 ]; 27 ];
27 }); 28 });
28 29