change_project_check.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Ellisran
  6. * @date 2020/10/15
  7. * @version
  8. */
  9. const status = require('../const/audit').changeProject.status;
  10. const _ = require('lodash');
  11. module.exports = options => {
  12. /**
  13. * 标段校验 中间件
  14. * 1. 读取标段数据(包括属性)
  15. * 2. 检验用户是否可见标段(不校验具体权限)
  16. *
  17. * @param {function} next - 中间件继续执行的方法
  18. * @return {void}
  19. */
  20. return function* changeProjectCheck(next) {
  21. try {
  22. // 获取revise
  23. if (!this.session.sessionProject.page_show.openChangeProject) {
  24. throw '该功能已关闭';
  25. }
  26. const cpid = this.params.cpid || this.request.body.cpid;
  27. if (!cpid) {
  28. throw '您访问的变更立项不存在';
  29. }
  30. const change = yield this.service.changeProject.getDataById(cpid);
  31. // 读取原报、审核人数据
  32. change.auditors = yield this.service.changeProjectAudit.getAuditors(change.id, change.times);
  33. change.curAuditor = yield this.service.changeProjectAudit.getCurAuditor(change.id, change.times);
  34. change.xsAuditors = yield this.service.changeProjectXsAudit.getAuditList(change.id);
  35. if (!change) throw '变更令数据有误';
  36. // 权限相关
  37. // todo 校验权限 (标段参与人、分享)
  38. const accountId = this.session.sessionUser.accountId,
  39. auditorIds = _.map(change.auditors, 'aid'),
  40. xsAuditorIds = _.map(change.xsAuditors, 'aid'),
  41. shareIds = [];
  42. if (accountId === change.uid) { // 原报
  43. change.curTimes = change.times;
  44. change.filePermission = true;
  45. } else if (auditorIds.indexOf(accountId) !== -1 || xsAuditorIds.indexOf(accountId) !== -1) { // 审批人或者协审人
  46. if (change.status === status.uncheck) {
  47. throw '您无权查看该数据';
  48. }
  49. change.curTimes = change.status === status.back || change.status === status.revise ? change.times - 1 : change.times;
  50. change.filePermission = true;
  51. } else if ((change.status === status.back || change.status === status.revise) && change.uid !== accountId) {
  52. const preAuditors = yield this.service.changeProjectAudit.getAuditors(change.id, change.times - 1);
  53. const preAuditorIds = _.map(preAuditors, 'aid');
  54. if (preAuditorIds.indexOf(accountId) === -1) {
  55. throw '您无权查看该数据';
  56. }
  57. change.filePermission = true;
  58. } else if (shareIds.indexOf(accountId) !== -1) { // 分享人
  59. if (change.status === status.uncheck) {
  60. throw '您无权查看该数据';
  61. }
  62. change.curTimes = change.status === status.back || change.status === status.revise ? change.times - 1 : change.times;
  63. change.filePermission = false;
  64. } else if (this.tender.isTourist) {
  65. change.curTimes = change.times;
  66. change.filePermission = this.tender.touristPermission.file || auditorIds.indexOf(accountId) !== -1;
  67. } else { // 其他不可见
  68. throw '您无权查看该数据';
  69. }
  70. // 调差的readOnly 指表格和页面只能看不能改,和审批无关
  71. change.readOnly = !((change.status === status.uncheck || change.status === status.back || change.status === status.revise) && accountId === change.uid);
  72. this.change = change;
  73. yield this.service.changeProject.doCheckChangeCanCancel(this.change);
  74. yield next;
  75. } catch (err) {
  76. console.log(err);
  77. // 输出错误到日志
  78. if (err.stack) {
  79. this.logger.error(err);
  80. } else {
  81. this.getLogger('fail').info(JSON.stringify({
  82. error: err,
  83. project: this.session.sessionProject,
  84. user: this.session.sessionUser,
  85. body: this.session.body,
  86. }));
  87. }
  88. // 重定向值标段管理
  89. this.redirect(this.request.headers.referer);
  90. }
  91. };
  92. };