Authored by 陈峰

用户接口携带sessionid参数

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