tender_check.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const auditConst = require('../const/audit').ledger;
  10. const messageType = require('../const/message_type');
  11. module.exports = options => {
  12. /**
  13. * 标段校验 中间件
  14. * 1. 读取标段数据(包括属性)
  15. * 2. 检验用户是否可见标段(不校验具体权限)
  16. *
  17. * @param {function} next - 中间件继续执行的方法
  18. * @return {void}
  19. */
  20. return function* tenderCheck(next) {
  21. try {
  22. // 读取标段数据
  23. const tender = { id: parseInt(this.params.id) };
  24. if (!tender.id) {
  25. throw '当前未打开标段';
  26. }
  27. tender.data = yield this.service.tender.getTender(tender.id);
  28. if (!tender.data) {
  29. throw '标段数据错误';
  30. }
  31. if (!tender.data.measure_type) {
  32. throw '请先选择计量模式';
  33. }
  34. tender.info = yield this.service.tenderInfo.getTenderInfo(tender.id);
  35. if (!tender.data.ledger_status) {
  36. tender.data.ledger_status = auditConst.status.uncheck;
  37. }
  38. tender.auditLedgerConst = auditConst;
  39. if (!tender.data.ledger_times) {
  40. tender.data.ledger_times = 1;
  41. }
  42. if (tender.data.project_id !== this.session.sessionProject.id) {
  43. throw '您无权查看该项目';
  44. } else {
  45. const accountId = this.session.sessionUser.accountId;
  46. const advanceAuditors = yield this.service.advanceAudit.getAllAuditors(tender.id);
  47. const advanceAuditorsId = this.helper._.map(advanceAuditors, 'audit_id');
  48. if (tender.data.ledger_status === auditConst.status.uncheck) {
  49. if (tender.data.user_id !== accountId && advanceAuditorsId.indexOf(accountId) === -1) {
  50. throw '您无权查看该项目';
  51. } else if (advanceAuditorsId.indexOf(accountId) !== -1) {
  52. if (!(this.url === '/tender/' + this.params.id || this.url.indexOf('/advance') !== -1)) {
  53. throw '您无权查看该内容';
  54. }
  55. }
  56. } else {
  57. const times = tender.data.ledger_status === auditConst.status.checkNo ? tender.data.ledger_times - 1 : tender.data.ledger_times;
  58. const auditors = yield this.service.ledgerAudit.getAuditors(tender.id, times);
  59. const auditorsId = this.helper._.map(auditors, 'audit_id');
  60. const stageAuditors = yield this.service.stageAudit.getAllAuditors(tender.id);
  61. const stageAuditorsId = this.helper._.map(stageAuditors, 'aid');
  62. const changeAuditors = yield this.service.changeAudit.getAllAuditors(tender.id);
  63. const changeAuditorsId = this.helper._.map(changeAuditors, 'uid');
  64. const reviseAuditors = yield this.service.reviseAudit.getAllAuditors(tender.id);
  65. const reviseAuditorsId = this.helper._.map(reviseAuditors, 'audit_id');
  66. const materialAuditors = yield this.service.materialAudit.getAllAuditors(tender.id);
  67. const materialAuditorsId = this.helper._.map(materialAuditors, 'aid');
  68. // const advanceAuditors = yield this.service.advanceAudit.getAllAuditors(tender.id);
  69. // const advanceAuditorsId = this.helper._.map(advanceAuditors, 'audit_id');
  70. const tenderPermission = this.session.sessionUser.permission ? this.session.sessionUser.permission.tender : null;
  71. if (auditorsId.indexOf(accountId) === -1 && tender.data.user_id !== accountId &&
  72. (tenderPermission === null || tenderPermission === undefined || tenderPermission.indexOf('2') === -1) &&
  73. stageAuditorsId.indexOf(accountId) === -1 && changeAuditorsId.indexOf(accountId) === -1 &&
  74. reviseAuditorsId.indexOf(accountId) === -1 && materialAuditorsId.indexOf(accountId) === -1 &&
  75. advanceAuditorsId.indexOf(accountId) === -1) {
  76. throw '您无权查看该项目';
  77. }
  78. }
  79. }
  80. tender.ledgerReadOnly = this.session.sessionUser.accountId !== tender.data.user_id ||
  81. tender.data.ledger_status === auditConst.status.checking || tender.data.ledger_status === auditConst.status.checked;
  82. this.tender = tender;
  83. this.session.sessionProject.page_show = yield this.service.project.getPageshow(this.session.sessionProject.id);
  84. yield next;
  85. } catch (err) {
  86. console.log(err);
  87. // 输出错误到日志
  88. if (err.stack) {
  89. this.logger.error(err);
  90. } else {
  91. this.session.message = {
  92. type: messageType.ERROR,
  93. icon: 'exclamation-circle',
  94. message: err,
  95. };
  96. this.getLogger('fail').info(JSON.stringify({
  97. error: err,
  98. project: this.session.sessionProject,
  99. user: this.session.sessionUser,
  100. body: this.session.body,
  101. }));
  102. }
  103. // 重定向值标段管理
  104. if (this.helper.isWap(this.request)) {
  105. this.redirect('/wap/list');
  106. } else {
  107. err === '您无权查看该内容' ? this.redirect(this.request.headers.referer) : this.redirect('/list');
  108. }
  109. }
  110. };
  111. };