tender_check.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. if (tender.data.ledger_status === auditConst.status.uncheck) {
  47. if (tender.data.user_id !== accountId) {
  48. throw '您无权查看该项目';
  49. }
  50. } else {
  51. const times = tender.data.ledger_status === auditConst.status.checkNo ? tender.data.ledger_times - 1 : tender.data.ledger_times;
  52. const auditors = yield this.service.ledgerAudit.getAuditors(tender.id, times);
  53. const auditorsId = this.helper._.map(auditors, 'audit_id');
  54. const stageAuditors = yield this.service.stageAudit.getAllAuditors(tender.id);
  55. const stageAuditorsId = this.helper._.map(stageAuditors, 'aid');
  56. const changeAuditors = yield this.service.changeAudit.getAllAuditors(tender.id);
  57. const changeAuditorsId = this.helper._.map(changeAuditors, 'uid');
  58. const reviseAuditors = yield this.service.reviseAudit.getAllAuditors(tender.id);
  59. const reviseAuditorsId = this.helper._.map(reviseAuditors, 'audit_id');
  60. const materialAuditors = yield this.service.materialAudit.getAllAuditors(tender.id);
  61. const materialAuditorsId = this.helper._.map(materialAuditors, 'aid');
  62. const tenderPermission = this.session.sessionUser.permission ? this.session.sessionUser.permission.tender : null;
  63. if (auditorsId.indexOf(accountId) === -1 && tender.data.user_id !== accountId &&
  64. (tenderPermission === null || tenderPermission === undefined || tenderPermission.indexOf('2') === -1) &&
  65. stageAuditorsId.indexOf(accountId) === -1 && changeAuditorsId.indexOf(accountId) === -1 &&
  66. reviseAuditorsId.indexOf(accountId) === -1 && materialAuditorsId.indexOf(accountId) === -1) {
  67. throw '您无权查看该项目';
  68. }
  69. }
  70. }
  71. tender.ledgerReadOnly = this.session.sessionUser.accountId !== tender.data.user_id ||
  72. tender.data.ledger_status === auditConst.status.checking || tender.data.ledger_status === auditConst.status.checked;
  73. this.tender = tender;
  74. yield next;
  75. } catch (err) {
  76. // 输出错误到日志
  77. if (err.stack) {
  78. this.logger.error(err);
  79. } else {
  80. this.session.message = {
  81. type: messageType.ERROR,
  82. icon: 'exclamation-circle',
  83. message: err,
  84. };
  85. this.getLogger('fail').info(JSON.stringify({
  86. error: err,
  87. project: this.session.sessionProject,
  88. user: this.session.sessionUser,
  89. body: this.session.body,
  90. }));
  91. }
  92. // 重定向值标段管理
  93. this.redirect('/list');
  94. }
  95. };
  96. };