tender_check.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. tender.userAssistsId = auditAssists.filter(x => {return x.user_id === tender.data.user_id;})
  65. .map(x => { return x.ass_user_id; });
  66. const settleAuditors = yield this.service.settleAudit.getAllAuditors(tender.id);
  67. const settleAuditorsId = this.helper._.map(settleAuditors, 'audit_id');
  68. const phasePayAuditors = yield this.service.phasePayAudit.getAllAuditors(tender.id);
  69. const phasePayAuditorsId = this.helper._.map(phasePayAuditors, 'audit_id');
  70. const changeAuditors = yield this.service.changeAudit.getAllAuditors(tender.id);
  71. const changeAuditorsId = this.helper._.map(changeAuditors, 'uid');
  72. const reviseAuditors = yield this.service.reviseAudit.getAllAuditors(tender.id);
  73. const reviseAuditorsId = this.helper._.map(reviseAuditors, 'audit_id');
  74. const materialAuditors = yield this.service.materialAudit.getAllAuditors(tender.id);
  75. const materialAuditorsId = this.helper._.map(materialAuditors, 'aid');
  76. const changeProjectAuditors = this.subProject.page_show.openChangeProject ? yield this.service.changeProjectAudit.getAllAuditors(tender.id) : [];
  77. const changeProjectAuditorsId = this.helper._.map(changeProjectAuditors, 'aid');
  78. const changeProjectXsAuditors = this.subProject.page_show.openChangeProject ? yield this.service.changeProjectXsAudit.getAllAuditors(tender.id) : [];
  79. const changeProjectXsAuditorsId = this.helper._.map(changeProjectXsAuditors, 'aid');
  80. const changeApplyAuditors = this.subProject.page_show.openChangeApply ? yield this.service.changeApplyAudit.getAllAuditors(tender.id) : [];
  81. const changeApplyAuditorsId = this.helper._.map(changeApplyAuditors, 'aid');
  82. const changePlanAuditors = this.subProject.page_show.openChangePlan ? yield this.service.changePlanAudit.getAllAuditors(tender.id) : [];
  83. const changePlanAuditorsId = this.helper._.map(changePlanAuditors, 'aid');
  84. const tenderPermission = this.session.sessionUser.permission ? this.session.sessionUser.permission.tender : null;
  85. const isTenderTourist = yield this.service.tenderTourist.getDataByCondition({ tid: tender.id, user_id: accountId });
  86. // 判断访问人是否具有游客身份
  87. tender.isTourist = isTenderTourist !== null;
  88. // 游客权限
  89. tender.touristPermission = yield this.service.tenderTourist.getTouristPermission(isTenderTourist);
  90. if (auditorsId.indexOf(accountId) === -1 && tender.data.user_id !== accountId &&
  91. (tenderPermission === null || tenderPermission === undefined || tenderPermission.indexOf('2') === -1) &&
  92. stageAuditorsId.indexOf(accountId) === -1 && auditAssistsId.indexOf(accountId) === -1 &&
  93. settleAuditorsId.indexOf(accountId) === -1 && phasePayAuditorsId.indexOf(accountId) === -1 && changeAuditorsId.indexOf(accountId) === -1 &&
  94. reviseAuditorsId.indexOf(accountId) === -1 && materialAuditorsId.indexOf(accountId) === -1 &&
  95. changeProjectAuditorsId.indexOf(accountId) === -1 && changeProjectXsAuditorsId.indexOf(accountId) === -1 &&
  96. changeApplyAuditorsId.indexOf(accountId) === -1 && changePlanAuditorsId.indexOf(accountId) === -1 &&
  97. advanceAuditorsId.indexOf(accountId) === -1 && !this.session.sessionUser.is_admin && !isTenderTourist) {
  98. throw '您无权查看该项目';
  99. }
  100. tender.ledgerReadOnly = this.session.sessionUser.accountId !== tender.data.user_id ||
  101. tender.data.ledger_status === auditConst.status.checking || tender.data.ledger_status === auditConst.status.checked;
  102. tender.advanceAuditorsId = advanceAuditorsId;
  103. tender.ledgerUsers = tender.ledger_status === auditConst.status.uncheck ? [tender.data.user_id] : [tender.data.user_id, ...auditorsId];
  104. this.tender = tender;
  105. // this.session.sessionProject.page_show = yield this.service.project.getPageshow(this.session.sessionProject.id);
  106. // 投资进度权限获取
  107. let schedule_permission = scPermission.no;
  108. if (this.session.sessionUser.accountId === tender.data.user_id) {
  109. schedule_permission = scPermission.edit;
  110. } else {
  111. const scheduleUser = yield this.service.scheduleAudit.getDataByCondition({ tid: tender.id, audit_id: this.session.sessionUser.accountId });
  112. if (scheduleUser) {
  113. if (tender.isTourist && scheduleUser.permission === scPermission.no) {
  114. schedule_permission = scPermission.show;
  115. } else {
  116. schedule_permission = scheduleUser.permission;
  117. }
  118. } else if (tender.isTourist) {
  119. schedule_permission = scPermission.show;
  120. }
  121. }
  122. tender.schedule_permission = schedule_permission;
  123. yield next;
  124. } catch (err) {
  125. console.log(err);
  126. // 输出错误到日志
  127. if (err.stack) {
  128. this.logger.error(err);
  129. } else {
  130. this.session.message = {
  131. type: messageType.ERROR,
  132. icon: 'exclamation-circle',
  133. message: err,
  134. };
  135. this.getLogger('fail').info(JSON.stringify({
  136. error: err,
  137. project: this.session.sessionProject,
  138. user: this.session.sessionUser,
  139. body: this.session.body,
  140. }));
  141. }
  142. if (this.helper.isAjax(this.request)) {
  143. if (err.stack) {
  144. this.body = {err: 4, msg: '标段数据未知错误', data: null};
  145. } else {
  146. this.body = {err: 3, msg: err.toString(), data: null};
  147. }
  148. } else {
  149. if (this.helper.isWap(this.request)) {
  150. this.redirect('/wap/list');
  151. } else {
  152. err === '您无权查看该内容' ? this.redirect(this.request.headers.referer) : this.redirect('/list');
  153. }
  154. }
  155. }
  156. };
  157. };