Authored by 李奇

修改

... ... @@ -76,26 +76,18 @@ try {
origin: config.corsAllowOrigin
}));
const user = require('./doraemon/middleware/user');
const setYohoData = require('./doraemon/middleware/set-yoho-data');
const errorHanlder = require('./doraemon/middleware/error-handler');
const setPageInfo = require('./doraemon/middleware/set-pageinfo');
const errorHandler = require('./doraemon/middleware/error-handler');
const devtools = require('./doraemon/middleware/devtools');
// YOHO 前置中间件
app.use(setYohoData());
app.use(user());
app.use(setPageInfo());
if (app.locals.devEnv) {
app.use(devtools());
}
require('./dispatch')(app);
app.all('*', errorHanlder.notFound()); // 404
app.all('*', errorHandler.notFound); // 404
// YOHO 后置中间件
app.use(errorHanlder.serverError());
app.use(errorHandler.serverError);
} catch (err) {
logger.error(err);
... ...
... ... @@ -5,7 +5,7 @@
*/
const express = require('express');
const router = express.Router(); // eslint-disable-line
const {auth} = require('../../middleware');
const auth = require('../../middleware/auth');
const article = require('./controllers/article');
router.post('/like', article.like);
... ...
/**
* 登录判断
* @author: xuqi<qi.xu@yoho.cn>
* @date: 2016/4/25
* @author: leo<qi.li@yoho.cn>
* @date: 2017/6/23
*/
'use strict';
const helpers = global.yoho.helpers;
const _ = require('lodash');
module.exports = (req, res, next) => {
if (!req.user.uid) {
const userId = _.get(req.session, 'user.id', 0);
if (!userId) {
if (req.xhr) {
return res.json({
code: 400,
message: '抱歉,您暂未登录!',
redirect: '/passport/login'
});
} else if (req.yoho.isApp) {
return next({
code: 401,
message: 'weblogin'
message: '抱歉,您暂未验证'
});
} else {
return res.redirect(helpers.urlFormat('/signin.html', {
refer: req.originalUrl
}));
return res.render('error/404');
}
}
... ...
/**
* 管理员判断
* @author: leo <qi.li@yoho.cn>
* @date: 2017/7/6
*/
'use strict';
const _ = require('lodash');
module.exports = (req, res, next) => {
const isAdmin = _.get(req.session, 'user.isAdmin', false);
if (!isAdmin) {
return res.json({
code: 401,
message: '抱歉,您没有管理员权限,请使用管理员账号登陆后重试'
});
}
next();
};
... ...
/**
* 404 错误
* @return {[type]}
* error处理
* @author: leo<qi.li@yoho.cn>
* @date: 2017/06/23
*/
const logger = global.yoho.logger;
const helpers = global.yoho.helpers;
const forceNoCache = (res) => {
if (res && !res.finished) {
res.set({
'Cache-Control': 'no-cache',
Pragma: 'no-cache',
Expires: (new Date(1900, 0, 1, 0, 0, 0, 0)).toUTCString()
});
}
};
const serverError = (err, req, res, next) => { // eslint-disable-line
logger.error(err);
exports.notFound = () => {
return (req, res) => {
forceNoCache(res);
res.status(404);
if (req.xhr) {
return res.json({
code: 404,
message: '抱歉,页面不存在!'
});
}
return res.render('error/404', {
module: 'common',
page: 'error',
title: '页面不存在 | Yoho!Buy有货 | 潮流购物逛不停',
pageFooter: true,
isErr: true
});
};
return res.status(500).json({
code: 500,
message: '服务器错误!'
});
};
/**
* 服务器错误
* @return {[type]}
*/
exports.serverError = () => {
return (err, req, res, next) => {
forceNoCache(res);
if (err && err.code === 401) {
logger.error(`401 error info:client_type=${req.query.client_type},req.user=${JSON.stringify(req.user)},req.query=${JSON.stringify(req.query)},cookies=${JSON.stringify(req.cookies)}`); // eslint-disable-line
if (req.xhr) {
return res.status(401).json(err);
} else if (req.yoho.isApp) {
if (err.lowVersion) {
return res.render('error/app-auth', {
module: 'common',
page: 'app-update',
localCss: true,
message: err.message
});
}
return res.render('error/app-auth', {
module: 'common',
page: 'app-redirect-login',
message: '验证失败,请登录',
localCss: true,
refer: err.refer
});
} else {
return res.redirect(helpers.urlFormat('/signin.html', {
refer: req.originalUrl
}));
}
}
logger.error(`error at path: ${req.url}`);
logger.error(err);
const notFound = (req, res) => {
res.status(404);
if (!res.headersSent) {
res.status(err.code || 500);
if (req.xhr) {
return res.json({
code: 404,
message: '请求路径不存在'
});
}
if (req.xhr) {
return res.json({
code: 500,
message: '服务器错误!'
});
}
res.render('error/404');
};
return res.render('error/500', {
err: err,
module: 'common',
page: 'error',
title: '服务器错误 | Yoho!Buy有货 | 潮流购物逛不停',
pageFooter: true,
isErr: true
});
}
next(err);
};
module.exports = {
notFound,
serverError
};
... ...
/**
* 设置页面的module,page默认值
* @author: 赵彪<bill.zhao@yoho.cn>
* @date: 2016/6/22
*/
'use strict';
module.exports = () => {
return (req, res, next) => {
if (!req.xhr) {
const arr = req.path.substring(1).split('/');
Object.assign(res.locals, {
module: arr[0],
page: arr[1]
});
}
next();
};
};
/**
* 设置 YOHO 数据
* @author: 赵彪<bill.zhao@yoho.cn>
* @date: 2016/6/16
*/
'use strict';
const _ = require('lodash');
/**
* 获取 IP
* @param {*} req
*/
const _getClientIp = req => {
let remoteIp = req.get('X-Forwarded-For') || req.get('X-Real-IP') || req.ip;
if (remoteIp.indexOf(',') > 0) {
let arr = remoteIp.split(',');
remoteIp = _.trim(arr[arr.length - 1]);
}
if (_.startsWith(remoteIp, '10.66.')) {
remoteIp = req.get('X-Real-IP');
}
return _.trim(remoteIp);
};
module.exports = () => {
return (req, res, next) => {
let yoho = {
pageChannel: {}
};
// IP 地址
yoho.clientIp = _getClientIp(req);
Object.assign(res.locals, yoho);
Object.assign(req.yoho, yoho);
res.locals.showHeader = true;
next();
};
};
'use strict';
const _ = require('lodash');
const cookie = global.yoho.cookie;
const authcode = require('../../utils/authcode');
module.exports = () => {
return (req, res, next) => {
// 从 SESSION 中获取到当前登录用户的 UID
if (req.session && _.isNumber(req.session.LOGIN_UID)) {
// 调用接口传参时切勿使用toString获得字符串
req.user.uid = {
toString: () => {
return _.parseInt(req.session.LOGIN_UID);
},
sessionKey: req.session.SESSION_KEY
};
let userData = _.get(req.session, 'USER', {});
_.merge(req.user, userData);
}
// session 没有读取到的时候,从 cookie 读取 UID
if (!req.user.uid && req.cookies._UID) {
let sessionKey = req.cookies._SESSION_KEY &&
authcode(req.cookies._SESSION_KEY, '_SESSION_KEY', 2592000000);
// 调用接口传参时切勿使用toString获得字符串
req.user.uid = {
toString: () => {
return _.parseInt(cookie.getUid(req));
},
sessionKey
};
}
next();
};
};