session_auth.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. // todo 迁移至subProject内部
  39. // 判断是否有权限查看决策大屏
  40. // let showDataCollect = 0;
  41. // if (projectData.data_collect && this.session.sessionProject.page_show.openDataCollect) {
  42. // if (sessionUser.is_admin) {
  43. // showDataCollect = 1;
  44. // } else {
  45. // const auditInfo = yield this.service.datacollectAudit.getDataByCondition({ pid: projectData.id, uid: accountInfo.id });
  46. // if (auditInfo) {
  47. // showDataCollect = 1;
  48. // } else {
  49. // let companyInfo = null;
  50. // if (accountInfo.company_id) {
  51. // companyInfo = yield this.service.datacollectAudit.getDataByCondition({
  52. // pid: projectData.id,
  53. // company_id: accountInfo.company_id,
  54. // });
  55. // } else {
  56. // const cuInfo = yield this.service.constructionUnit.getDataByCondition({
  57. // pid: projectData.id,
  58. // name: accountInfo.company,
  59. // });
  60. // if (cuInfo) {
  61. // companyInfo = yield this.service.datacollectAudit.getDataByCondition({
  62. // pid: projectData.id,
  63. // company_id: cuInfo.id,
  64. // });
  65. // yield this.service.projectAccount.update({ company_id: cuInfo.id }, { id: accountInfo.id });
  66. // }
  67. // }
  68. // if (companyInfo) {
  69. // showDataCollect = 1;
  70. // } else {
  71. // const grounpInfo = yield this.service.datacollectAudit.getGroupInfo(projectData.id, accountInfo.account_group);
  72. // if (grounpInfo) {
  73. // showDataCollect = 1;
  74. // }
  75. // }
  76. // }
  77. // }
  78. // }
  79. // this.session.sessionProject.showDataCollect = showDataCollect;
  80. // // 判断是否有权限查看支付审批
  81. // let showPayment = 0;
  82. // if (sessionUser.is_admin) {
  83. // showPayment = this.session.sessionProject.page_show.openPayment ? 1 : 0;
  84. // } else {
  85. // if (this.session.sessionProject.page_show.openPayment) {
  86. // const auditInfo = yield this.service.subProjPermission.showPayment(sessionUser.accountId);
  87. // if (auditInfo) {
  88. // showPayment = 1;
  89. // }
  90. // }
  91. // }
  92. // this.session.sessionProject.showPayment = showPayment;
  93. // 同步消息
  94. yield this.service.notify.syncNotifyData();
  95. // 同步系统维护信息
  96. yield this.service.maintain.syncMaintainData();
  97. if (this.session === null) {
  98. throw '系统维护中~';
  99. }
  100. // 对sub_menu项目默认打开页进行配置
  101. const path = yield this.service.settingShow.getDefaultPath(this.session.sessionProject.id);
  102. path && (this.curListUrl = path);
  103. // 针对非wap重定向,去掉wap
  104. if (this.method === 'GET' && this.url.match(/\/wap\//) && !this.helper.isMobile(this.request.header['user-agent'])) {
  105. const returnUrl = this.url.replace(/\/wap/g, '');
  106. this.redirect(returnUrl);
  107. }
  108. } catch (error) {
  109. console.log(error);
  110. this.log(error);
  111. if (this.helper.isAjax(this.request)) {
  112. return this.body = {
  113. err: 2,
  114. msg: '登录信息异常,请重新登录',
  115. data: '',
  116. };
  117. } else if (this.session === null) {
  118. if (this.helper.isWap(this.request)) {
  119. this.session.wapTenderID = this.params.id ? this.params.id : null;
  120. return this.redirect('/wap/login?referer=' + this.url);
  121. }
  122. return this.redirect('/login?referer=' + this.url);
  123. }
  124. if (this.helper.isWap(this.request)) {
  125. this.session.wapTenderID = this.params.id ? this.params.id : null;
  126. return this.redirect('/wap/login?referer=' + this.url);
  127. }
  128. this.session.message = {
  129. type: messageType.ERROR,
  130. icon: 'exclamation-circle',
  131. message: '登录信息异常,请重新登录',
  132. };
  133. return this.redirect('/login?referer=' + this.url);
  134. }
  135. yield next;
  136. };
  137. };