session_auth.js 6.9 KB

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