session_auth.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. const share = this.query.share;
  15. if (share && this.request.url.indexOf('/tender') === 0) {
  16. const tender = yield this.service.tender.getDataByCondition({ id: this.params.id });
  17. if (tender.share_token !== share) {
  18. throw '分享码不正确';
  19. }
  20. if (tender.share_expiration < new Date()) {
  21. throw '分享码已过期';
  22. }
  23. const currentTime = new Date().getTime() / 1000;
  24. this.session.sessionUser = {
  25. account: '游客',
  26. name: '游客',
  27. accountId: 0,
  28. loginTime: currentTime,
  29. is_admin: false,
  30. loginType: 4,
  31. loginStatus: 0,
  32. dskAccountData: null,
  33. permission: null,
  34. };
  35. this.session.sessionUser.sessionToken = crypto.createHmac('sha1', currentTime + '')
  36. .update(this.session.sessionUser.account).digest('hex').toString('base64');
  37. const projectData = yield this.service.project.getDataByCondition({ id: tender.project_id });
  38. this.session.sessionProject = {
  39. id: projectData.id,
  40. code: projectData.code,
  41. name: projectData.name,
  42. userAccount: projectData.user_account,
  43. custom: projectData.custom,
  44. };
  45. }
  46. // 判断session
  47. const sessionUser = this.session.sessionUser;
  48. if (sessionUser === undefined) {
  49. throw '不存在session';
  50. }
  51. // 校验session
  52. if (sessionUser.account === undefined || sessionUser.loginTime === undefined) {
  53. throw '用户数据不完整';
  54. }
  55. // 校验session
  56. const sessionToken = crypto.createHmac('sha1', sessionUser.loginTime + '')
  57. .update(sessionUser.account).digest('hex').toString('base64');
  58. if (sessionToken !== sessionUser.sessionToken) {
  59. throw 'session数据错误';
  60. }
  61. // 获取用户新建标段权利
  62. if (sessionUser.loginType !== 4) {
  63. const accountInfo = yield this.service.projectAccount.getDataById(this.session.sessionUser.accountId);
  64. this.session.sessionUser.permission = accountInfo !== undefined && accountInfo.permission !== '' ? JSON.parse(accountInfo.permission) : null;
  65. }
  66. const projectData = yield this.service.project.getDataById(this.session.sessionProject.id);
  67. this.session.sessionProject.page_show = yield this.service.projectAccount.getPageShow(projectData.page_show);
  68. this.session.sessionProject.custom = projectData.custom;
  69. this.session.sessionProject.customType = projectData.customType;
  70. this.session.sessionProject.funSet = projectData.fun_set ? JSON.parse(projectData.fun_set) : null;
  71. // 同步消息
  72. yield this.service.notify.syncNotifyData();
  73. // 同步系统维护信息
  74. yield this.service.maintain.syncMaintainData();
  75. if (this.session === null) {
  76. throw '系统维护中~';
  77. }
  78. // 对sub_menu项目默认打开页进行配置
  79. const path = yield this.service.settingShow.getDefaultPath(this.session.sessionProject.id);
  80. path && (this.curListUrl = path);
  81. // 针对非wap重定向,去掉wap
  82. if (this.method === 'GET' && this.url.match(/\/wap\//) && !this.helper.isMobile(this.request.header['user-agent'])) {
  83. const returnUrl = this.url.replace(/\/wap/g, '');
  84. this.redirect(returnUrl);
  85. }
  86. } catch (error) {
  87. this.log(error);
  88. if (this.helper.isAjax(this.request)) {
  89. return this.body = {
  90. err: 2,
  91. msg: '登录信息异常,请重新登录',
  92. data: '',
  93. };
  94. } else if (this.session === null) {
  95. if (this.helper.isWap(this.request)) {
  96. this.session.wapTenderID = this.params.id ? this.params.id : null;
  97. return this.redirect('/wap/login?referer=' + this.url);
  98. }
  99. return this.redirect('/login?referer=' + this.url);
  100. }
  101. if (this.helper.isWap(this.request)) {
  102. this.session.wapTenderID = this.params.id ? this.params.id : null;
  103. return this.redirect('/wap/login?referer=' + this.url);
  104. }
  105. this.session.message = {
  106. type: messageType.ERROR,
  107. icon: 'exclamation-circle',
  108. message: '登录信息异常,请重新登录',
  109. };
  110. return this.redirect('/login?referer=' + this.url);
  111. }
  112. yield next;
  113. };
  114. };