session_auth.js 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. 'use strict';
  2. // 加密类
  3. const crypto = require('crypto');
  4. const messageType = require('../const/message_type');
  5. module.exports = options => {
  6. /**
  7. * session判断中间件
  8. *
  9. * @param {function} next - 中间件继续执行的方法
  10. * @return {void}
  11. */
  12. return function* sessionAuth(next) {
  13. try {
  14. // 判断session
  15. const sessionUser = this.session.sessionUser;
  16. if (sessionUser === undefined) {
  17. throw '不存在session';
  18. }
  19. // 校验session
  20. if (sessionUser.account === undefined || sessionUser.loginTime === undefined) {
  21. throw '用户数据不完整';
  22. }
  23. // 校验session
  24. const sessionToken = crypto.createHmac('sha1', sessionUser.loginTime + '')
  25. .update(sessionUser.account).digest('hex').toString('base64');
  26. if (sessionToken !== sessionUser.sessionToken) {
  27. throw 'session数据错误';
  28. }
  29. // 获取用户新建标段权利
  30. const accountInfo = yield this.service.projectAccount.getDataById(this.session.sessionUser.accountId);
  31. this.session.sessionUser.permission = accountInfo !== undefined && accountInfo.permission !== '' ? JSON.parse(accountInfo.permission) : null;
  32. const projectData = yield this.service.project.getDataById(this.session.sessionProject.id);
  33. this.session.sessionProject.page_show = yield this.service.projectAccount.getPageShow(projectData.page_show);
  34. this.session.sessionProject.custom = projectData.custom;
  35. this.session.sessionProject.dataCollect = projectData.data_collect;
  36. this.session.sessionProject.customType = projectData.customType;
  37. this.session.sessionProject.funSet = projectData.fun_set ? JSON.parse(projectData.fun_set) : null;
  38. // 同步消息
  39. yield this.service.notify.syncNotifyData();
  40. // 同步系统维护信息
  41. yield this.service.maintain.syncMaintainData();
  42. if (this.session === null) {
  43. throw '系统维护中~';
  44. }
  45. // 对sub_menu项目默认打开页进行配置
  46. const path = yield this.service.settingShow.getDefaultPath(this.session.sessionProject.id);
  47. path && (this.curListUrl = path);
  48. // 针对非wap重定向,去掉wap
  49. if (this.method === 'GET' && this.url.match(/\/wap\//) && !this.helper.isMobile(this.request.header['user-agent'])) {
  50. const returnUrl = this.url.replace(/\/wap/g, '');
  51. this.redirect(returnUrl);
  52. }
  53. } catch (error) {
  54. console.log(error);
  55. this.log(error);
  56. if (this.helper.isAjax(this.request)) {
  57. return this.body = {
  58. err: 2,
  59. msg: '登录信息异常,请重新登录',
  60. data: '',
  61. };
  62. } else if (this.session === null) {
  63. if (this.helper.isWap(this.request)) {
  64. this.session.wapTenderID = this.params.id ? this.params.id : null;
  65. return this.redirect('/wap/login?referer=' + this.url);
  66. }
  67. return this.redirect('/login?referer=' + this.url);
  68. }
  69. if (this.helper.isWap(this.request)) {
  70. this.session.wapTenderID = this.params.id ? this.params.id : null;
  71. return this.redirect('/wap/login?referer=' + this.url);
  72. }
  73. this.session.message = {
  74. type: messageType.ERROR,
  75. icon: 'exclamation-circle',
  76. message: '登录信息异常,请重新登录',
  77. };
  78. return this.redirect('/login?referer=' + this.url);
  79. }
  80. yield next;
  81. };
  82. };