change_project_check.js 4.5 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. if (!change) throw '变更立项数据有误';
  32. // 读取原报、审核人数据
  33. yield this.service.changeProject.loadChangeUser(change);
  34. // change.auditors = yield this.service.changeProjectAudit.getAuditors(change.id, change.times);
  35. // change.curAuditor = yield this.service.changeProjectAudit.getCurAuditor(change.id, change.times);
  36. change.xsAuditors = yield this.service.changeProjectXsAudit.getAuditList(change.id);
  37. // 权限相关
  38. // todo 校验权限 (标段参与人、分享)
  39. const accountId = this.session.sessionUser.accountId,
  40. xsAuditorIds = _.map(change.xsAuditors, 'aid'),
  41. shareIds = [];
  42. const permission = this.session.sessionUser.permission;
  43. if (accountId === change.uid) { // 原报
  44. change.curTimes = change.times;
  45. change.filePermission = true;
  46. } else if (change.auditorIds.indexOf(accountId) !== -1 || xsAuditorIds.indexOf(accountId) !== -1) { // 审批人或者协审人
  47. if (change.status === status.uncheck) {
  48. throw '您无权查看该数据';
  49. }
  50. change.curTimes = change.status === status.back || change.status === status.revise ? change.times - 1 : change.times;
  51. change.filePermission = true;
  52. } else if ((change.status === status.back || change.status === status.revise) && change.uid !== accountId) {
  53. const preAuditors = yield this.service.changeProjectAudit.getAuditors(change.id, change.times - 1);
  54. const preAuditorIds = _.map(preAuditors, 'aid');
  55. if (preAuditorIds.indexOf(accountId) === -1) {
  56. throw '您无权查看该数据';
  57. }
  58. change.filePermission = true;
  59. } else if (this.tender.isTourist || this.session.sessionUser.is_admin) {
  60. change.curTimes = change.times;
  61. change.filePermission = this.tender.touristPermission.file || change.auditorIds.indexOf(accountId) !== -1;
  62. } else if (shareIds.indexOf(accountId) !== -1 || (permission !== null && permission.tender !== undefined && permission.tender.indexOf('2') !== -1)) { // 分享人
  63. if (change.status === status.uncheck) {
  64. throw '您无权查看该数据';
  65. }
  66. change.curTimes = change.status === status.back || change.status === status.revise ? change.times - 1 : change.times;
  67. change.filePermission = false;
  68. } else { // 其他不可见
  69. throw '您无权查看该数据';
  70. }
  71. // 调差的readOnly 指表格和页面只能看不能改,和审批无关
  72. change.readOnly = !((change.status === status.uncheck || change.status === status.back || change.status === status.revise) && accountId === change.uid);
  73. this.change = change;
  74. yield this.service.changeProject.doCheckChangeCanCancel(this.change);
  75. yield next;
  76. } catch (err) {
  77. console.log(err);
  78. // 输出错误到日志
  79. if (err.stack) {
  80. this.logger.error(err);
  81. } else {
  82. this.getLogger('fail').info(JSON.stringify({
  83. error: err,
  84. project: this.session.sessionProject,
  85. user: this.session.sessionUser,
  86. body: this.session.body,
  87. }));
  88. }
  89. // 重定向值标段管理
  90. this.redirect(this.request.headers.referer);
  91. }
  92. };
  93. };