session_auth.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. 'use strict';
  2. // 加密类
  3. const crypto = require('crypto');
  4. const messageType = require('../const/message_type');
  5. const accountPermission = require('../const/account_permission');
  6. module.exports = options => {
  7. /**
  8. * session判断中间件
  9. *
  10. * @param {function} next - 中间件继续执行的方法
  11. * @return {void}
  12. */
  13. return function* sessionAuth(next) {
  14. try {
  15. const share = this.query.share;
  16. if (share && this.request.url.indexOf('/tender') === 0) {
  17. const tender = yield this.service.tender.getDataByCondition({ id: this.params.id });
  18. if (tender.share_token !== share) {
  19. throw '分享码不正确';
  20. }
  21. if (tender.share_expiration < new Date()) {
  22. throw '分享码已过期';
  23. }
  24. const currentTime = new Date().getTime() / 1000;
  25. this.session.sessionUser = {
  26. account: '游客',
  27. name: '游客',
  28. accountId: 0,
  29. loginTime: currentTime,
  30. is_admin: false,
  31. loginType: 4,
  32. loginStatus: 0,
  33. dskAccountData: null,
  34. permission: null,
  35. };
  36. this.session.sessionUser.sessionToken = crypto.createHmac('sha1', currentTime + '')
  37. .update(this.session.sessionUser.account).digest('hex').toString('base64');
  38. const projectData = yield this.service.project.getDataByCondition({ id: tender.project_id });
  39. this.session.sessionProject = {
  40. id: projectData.id,
  41. code: projectData.code,
  42. name: projectData.name,
  43. userAccount: projectData.user_account,
  44. custom: projectData.custom,
  45. };
  46. }
  47. // 判断session
  48. const sessionUser = this.session.sessionUser;
  49. if (sessionUser === undefined) {
  50. throw '不存在session';
  51. }
  52. // 校验session
  53. if (sessionUser.account === undefined || sessionUser.loginTime === undefined) {
  54. throw '用户数据不完整';
  55. }
  56. // 校验session
  57. const sessionToken = crypto.createHmac('sha1', sessionUser.loginTime + '')
  58. .update(sessionUser.account).digest('hex').toString('base64');
  59. if (sessionToken !== sessionUser.sessionToken) {
  60. throw 'session数据错误';
  61. }
  62. // 获取用户新建标段权利
  63. if (sessionUser.loginType !== 4) {
  64. const accountInfo = yield this.service.projectAccount.getDataById(this.session.sessionUser.accountId);
  65. this.session.sessionUser.permission = accountInfo !== undefined && accountInfo.permission !== '' ? JSON.parse(accountInfo.permission) : null;
  66. }
  67. const projectData = yield this.service.project.getDataById(this.session.sessionProject.id);
  68. this.session.sessionProject.page_show = yield this.service.projectAccount.getPageShow(projectData.page_show);
  69. this.session.sessionProject.custom = projectData.custom;
  70. this.session.sessionProject.customType = projectData.customType;
  71. this.session.sessionProject.funSet = projectData.fun_set ? JSON.parse(projectData.fun_set) : null;
  72. this.showProjectScreen = false;
  73. if (this.session.sessionProject.page_show.openProjectScreen) {
  74. const hasProjectScreen = yield this.service.projectScreen.hasScreen(projectData.id);
  75. const canViewProjectScreen = this.session.sessionUser.is_admin ||
  76. accountPermission.PermissionCheck.projectScreen(this.session.sessionUser.permission);
  77. this.showProjectScreen = hasProjectScreen && canViewProjectScreen;
  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. };