session_auth.js 5.8 KB

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