session_auth.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. // 判断是否有权限查看决策大屏
  38. let showDataCollect = 0;
  39. if (projectData.data_collect) {
  40. if (sessionUser.is_admin) {
  41. showDataCollect = 1;
  42. } else {
  43. const grounpInfo = yield this.service.datacollectAudit.getGroupInfo(projectData.id, accountInfo.account_group);
  44. if (grounpInfo) {
  45. showDataCollect = 1;
  46. } else {
  47. const auditInfo = yield this.service.datacollectAudit.getDataByCondition({ pid: projectData.id, uid: accountInfo.id });
  48. if (auditInfo) {
  49. showDataCollect = 1;
  50. }
  51. }
  52. }
  53. }
  54. this.session.sessionProject.showDataCollect = showDataCollect;
  55. if (sessionUser.is_admin) {
  56. this.session.sessionProject.showBudget = true;
  57. } else {
  58. this.session.sessionProject.showBudget = yield this.service.budgetPermission.showBudget(sessionUser.accountId);
  59. }
  60. // 同步消息
  61. yield this.service.notify.syncNotifyData();
  62. // 同步系统维护信息
  63. yield this.service.maintain.syncMaintainData();
  64. if (this.session === null) {
  65. throw '系统维护中~';
  66. }
  67. // 对sub_menu项目默认打开页进行配置
  68. const path = yield this.service.settingShow.getDefaultPath(this.session.sessionProject.id);
  69. path && (this.curListUrl = path);
  70. } catch (error) {
  71. if (this.helper.isAjax(this.request)) {
  72. return this.body = {
  73. err: 2,
  74. msg: '登录信息异常,请重新登录',
  75. data: '',
  76. };
  77. } else if (this.session === null) {
  78. if (this.helper.isWap(this.request)) {
  79. this.session.wapTenderID = this.params.id ? this.params.id : null;
  80. return this.redirect('/wap/login?referer=' + this.url);
  81. }
  82. return this.redirect('/login?referer=' + this.url);
  83. }
  84. if (this.helper.isWap(this.request)) {
  85. this.session.wapTenderID = this.params.id ? this.params.id : null;
  86. return this.redirect('/wap/login?referer=' + this.url);
  87. }
  88. this.session.message = {
  89. type: messageType.ERROR,
  90. icon: 'exclamation-circle',
  91. message: '登录信息异常,请重新登录',
  92. };
  93. return this.redirect('/login?referer=' + this.url);
  94. }
  95. yield next;
  96. };
  97. };