change_plan_check.js 4.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Ellisran
  6. * @date 2020/10/15
  7. * @version
  8. */
  9. const status = require('../const/audit').changePlan.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* changePlanCheck(next) {
  21. try {
  22. // 获取revise
  23. if (!this.subProject.page_show.openChangePlan) {
  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.changePlan.getDataById(cpid);
  31. if (!change) throw '变更方案数据有误';
  32. // 读取原报、审核人数据
  33. yield this.service.changePlan.loadChangeUser(change);
  34. // decimal小数位设置
  35. change.decimal = change.decimal ? JSON.parse(change.decimal) : { tp: this.tender.info.decimal.tp, up: this.tender.info.decimal.up, precision: this.tender.info.precision };
  36. // 权限相关
  37. // todo 校验权限 (标段参与人、分享)
  38. const accountId = this.session.sessionUser.accountId,
  39. shareIds = [];
  40. const permission = this.session.sessionUser.permission;
  41. if (accountId === change.uid) { // 原报
  42. change.curTimes = change.times;
  43. change.filePermission = true;
  44. } else if (change.auditorIds.indexOf(accountId) !== -1) { // 审批人
  45. if (change.status === status.uncheck) {
  46. throw '您无权查看该数据';
  47. }
  48. // change.readOnly = change.status !== status.checking || accountId !== change.curAuditor.aid;
  49. change.curTimes = change.status === status.checkNo || change.status === status.revise ? change.times - 1 : change.times;
  50. change.filePermission = true;
  51. } else if ((change.status === status.checkNo || change.status === status.revise) && change.uid !== accountId) {
  52. const preAuditors = yield this.service.changePlanAudit.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 (this.tender.isTourist || this.session.sessionUser.is_admin) {
  59. change.curTimes = change.times;
  60. change.filePermission = this.tender.touristPermission.file || change.auditorIds.indexOf(accountId) !== -1;
  61. } else if (shareIds.indexOf(accountId) !== -1 || (permission !== null && permission.tender !== undefined && permission.tender.indexOf('2') !== -1)) { // 分享人
  62. if (change.status === status.uncheck) {
  63. throw '您无权查看该数据';
  64. }
  65. // change.readOnly = true;
  66. change.curTimes = change.status === status.checkNo || 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.checkNo || change.status === status.revise) && accountId === change.uid);
  73. change.shenpiPower = change.status === status.checking && change.curAuditorIds.indexOf(accountId) !== -1;
  74. this.change = change;
  75. yield this.service.changePlan.doCheckChangeCanCancel(this.change);
  76. yield next;
  77. } catch (err) {
  78. console.log(err);
  79. // 输出错误到日志
  80. if (err.stack) {
  81. this.logger.error(err);
  82. } else {
  83. this.getLogger('fail').info(JSON.stringify({
  84. error: err,
  85. project: this.session.sessionProject,
  86. user: this.session.sessionUser,
  87. body: this.session.body,
  88. }));
  89. }
  90. // 重定向值标段管理
  91. this.redirect(this.request.headers.referer);
  92. }
  93. };
  94. };