tender_check.js 9.4 KB

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