tender_check.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. const scPermission = require('../const/schedule').permission;
  12. const buildStatus = require('../const/tender').buildStatus;
  13. module.exports = options => {
  14. /**
  15. * 标段校验 中间件
  16. * 1. 读取标段数据(包括属性)
  17. * 2. 检验用户是否可见标段(不校验具体权限)
  18. *
  19. * @param {function} next - 中间件继续执行的方法
  20. * @return {void}
  21. */
  22. return function* tenderCheck(next) {
  23. try {
  24. // 读取标段数据
  25. const tender = { id: parseInt(this.params.id) };
  26. if (!tender.id) {
  27. throw '当前未打开标段';
  28. }
  29. tender.data = yield this.service.tender.getTender(tender.id);
  30. if (!tender.data) {
  31. throw '标段数据错误';
  32. }
  33. if (!tender.data.measure_type) {
  34. throw '请先选择计量模式';
  35. }
  36. tender.readOnly = tender.data.build_status === buildStatus.status.finish;
  37. tender.info = yield this.service.tenderInfo.getTenderInfo(tender.id);
  38. if (!tender.data.ledger_status) {
  39. tender.data.ledger_status = auditConst.status.uncheck;
  40. }
  41. tender.auditLedgerConst = auditConst;
  42. if (!tender.data.ledger_times) {
  43. tender.data.ledger_times = 1;
  44. }
  45. if (tender.data.project_id !== this.session.sessionProject.id) {
  46. throw '您无权查看该项目';
  47. }
  48. tender.his = tender.data.ledger_status === auditConst.status.checkNo
  49. ? yield this.service.ledgerHistory.getDataById(tender.data.his_id)
  50. : null;
  51. const accountId = this.session.sessionUser.accountId;
  52. const advanceAuditors = yield this.service.advanceAudit.getAllAuditors(tender.id);
  53. const advanceAuditorsId = this.helper._.map(advanceAuditors, 'audit_id');
  54. const times = tender.data.ledger_status === auditConst.status.checkNo ? tender.data.ledger_times - 1 : tender.data.ledger_times;
  55. const auditors = yield this.service.ledgerAudit.getAuditors(tender.id, times);
  56. const auditorsId = this.helper._.map(auditors, 'audit_id');
  57. const stageAuditors = yield this.service.stageAudit.getAllAuditors(tender.id);
  58. const stageAuditorsId = this.helper._.map(stageAuditors, 'aid');
  59. let auditAssists = yield this.service.auditAss.getData(tender.id);
  60. auditAssists = auditAssists.filter(x => {
  61. return x.user_id === tender.data.user_id || stageAuditorsId.indexOf(x.user_id) >= 0;
  62. });
  63. const auditAssistsId = this.helper._.map(auditAssists, 'ass_user_id');
  64. const settleAuditors = yield this.service.settleAudit.getAllAuditors(tender.id);
  65. const settleAuditorsId = this.helper._.map(settleAuditors, 'audit_id');
  66. const changeAuditors = yield this.service.changeAudit.getAllAuditors(tender.id);
  67. const changeAuditorsId = this.helper._.map(changeAuditors, 'uid');
  68. const reviseAuditors = yield this.service.reviseAudit.getAllAuditors(tender.id);
  69. const reviseAuditorsId = this.helper._.map(reviseAuditors, 'audit_id');
  70. const materialAuditors = yield this.service.materialAudit.getAllAuditors(tender.id);
  71. const materialAuditorsId = this.helper._.map(materialAuditors, 'aid');
  72. const changeProjectAuditors = this.session.sessionProject.page_show.openChangeProject ? yield this.service.changeProjectAudit.getAllAuditors(tender.id) : [];
  73. const changeProjectAuditorsId = this.helper._.map(changeProjectAuditors, 'aid');
  74. const changeProjectXsAuditors = this.session.sessionProject.page_show.openChangeProject ? yield this.service.changeProjectXsAudit.getAllAuditors(tender.id) : [];
  75. const changeProjectXsAuditorsId = this.helper._.map(changeProjectXsAuditors, 'aid');
  76. const changeApplyAuditors = this.session.sessionProject.page_show.openChangeApply ? yield this.service.changeApplyAudit.getAllAuditors(tender.id) : [];
  77. const changeApplyAuditorsId = this.helper._.map(changeApplyAuditors, 'aid');
  78. const changePlanAuditors = this.session.sessionProject.page_show.openChangePlan ? yield this.service.changePlanAudit.getAllAuditors(tender.id) : [];
  79. const changePlanAuditorsId = this.helper._.map(changePlanAuditors, 'aid');
  80. const tenderPermission = this.session.sessionUser.permission ? this.session.sessionUser.permission.tender : null;
  81. const isTenderTourist = yield this.service.tenderTourist.getDataByCondition({ tid: tender.id, user_id: accountId });
  82. // 判断访问人是否具有游客身份
  83. tender.isTourist = isTenderTourist !== null;
  84. // 游客权限
  85. tender.touristPermission = yield this.service.tenderTourist.getTouristPermission(isTenderTourist);
  86. if (auditorsId.indexOf(accountId) === -1 && tender.data.user_id !== accountId &&
  87. (tenderPermission === null || tenderPermission === undefined || tenderPermission.indexOf('2') === -1) &&
  88. stageAuditorsId.indexOf(accountId) === -1 && auditAssistsId.indexOf(accountId) === -1 &&
  89. settleAuditorsId.indexOf(accountId) === -1 && changeAuditorsId.indexOf(accountId) === -1 &&
  90. reviseAuditorsId.indexOf(accountId) === -1 && materialAuditorsId.indexOf(accountId) === -1 &&
  91. changeProjectAuditorsId.indexOf(accountId) === -1 && changeProjectXsAuditorsId.indexOf(accountId) === -1 &&
  92. changeApplyAuditorsId.indexOf(accountId) === -1 && changePlanAuditorsId.indexOf(accountId) === -1 &&
  93. advanceAuditorsId.indexOf(accountId) === -1 && !this.session.sessionUser.is_admin && !isTenderTourist) {
  94. throw '您无权查看该项目';
  95. }
  96. tender.ledgerReadOnly = this.session.sessionUser.accountId !== tender.data.user_id ||
  97. tender.data.ledger_status === auditConst.status.checking || tender.data.ledger_status === auditConst.status.checked;
  98. tender.advanceAuditorsId = advanceAuditorsId;
  99. tender.ledgerUsers = tender.ledger_status === auditConst.status.uncheck ? [tender.data.user_id] : [tender.data.user_id, ...auditorsId];
  100. this.tender = tender;
  101. // this.session.sessionProject.page_show = yield this.service.project.getPageshow(this.session.sessionProject.id);
  102. // 投资进度权限获取
  103. let schedule_permission = scPermission.no;
  104. if (this.session.sessionUser.accountId === tender.data.user_id) {
  105. schedule_permission = scPermission.edit;
  106. } else {
  107. const scheduleUser = yield this.service.scheduleAudit.getDataByCondition({ tid: tender.id, audit_id: this.session.sessionUser.accountId });
  108. if (scheduleUser) {
  109. if (tender.isTourist && scheduleUser.permission === scPermission.no) {
  110. schedule_permission = scPermission.show;
  111. } else {
  112. schedule_permission = scheduleUser.permission;
  113. }
  114. } else if (tender.isTourist) {
  115. schedule_permission = scPermission.show;
  116. }
  117. }
  118. tender.schedule_permission = schedule_permission;
  119. yield next;
  120. } catch (err) {
  121. console.log(err);
  122. // 输出错误到日志
  123. if (err.stack) {
  124. this.logger.error(err);
  125. } else {
  126. this.session.message = {
  127. type: messageType.ERROR,
  128. icon: 'exclamation-circle',
  129. message: err,
  130. };
  131. this.getLogger('fail').info(JSON.stringify({
  132. error: err,
  133. project: this.session.sessionProject,
  134. user: this.session.sessionUser,
  135. body: this.session.body,
  136. }));
  137. }
  138. if (this.helper.isAjax(this.request)) {
  139. if (err.stack) {
  140. this.body = {err: 4, msg: '标段数据未知错误', data: null};
  141. } else {
  142. this.body = {err: 3, msg: err.toString(), data: null};
  143. }
  144. } else {
  145. if (this.helper.isWap(this.request)) {
  146. this.redirect('/wap/list');
  147. } else {
  148. err === '您无权查看该内容' ? this.redirect(this.request.headers.referer) : this.redirect('/list');
  149. }
  150. }
  151. }
  152. };
  153. };