tender_check.js 5.1 KB

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