stage_check.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. 'use strict';
  2. /**
  3. *
  4. * @author Mai
  5. * @date
  6. * @version
  7. */
  8. const status = require('../const/audit').stage.status;
  9. const _ = require('lodash');
  10. module.exports = options => {
  11. /**
  12. * 期校验 中间件
  13. * 1. 读取期数据
  14. * 2. 检验用户是否参与期(不校验具体权限)
  15. *
  16. * 写入ctx.stage数据
  17. * 其中:
  18. * stage.auditors: 审批人列表(退回原报时,加载上一流程)
  19. * stage.curAuditor: 当前审批人(未上报为空,审批通过 or 退回原报时,为空)
  20. * stage.readonly: 登录人,是否可操作
  21. * stage.curTimes: 当前登录人,操作、查阅数据times
  22. * stage.curOrder: 当前登录人,操作、查阅数据order
  23. *
  24. * 该方法为通用方法,如需stage其他数据,请在controller中查询
  25. *
  26. * @param {function} next - 中间件继续执行的方法
  27. * @return {void}
  28. */
  29. return function* stageCheck(next) {
  30. try {
  31. // 读取标段数据
  32. const stageOrder = parseInt(this.params.order);
  33. if (stageOrder <= 0) {
  34. throw '您访问的期不存在';
  35. }
  36. const stage = yield this.service.stage.getDataByCondition({
  37. tid: this.tender.id,
  38. order: stageOrder,
  39. });
  40. if (!stage) {
  41. throw '期数据错误';
  42. }
  43. // 读取原报、审核人数据
  44. stage.auditors = yield this.service.stageAudit.getAuditors(stage.id, stage.times);
  45. stage.curAuditor = yield this.service.stageAudit.getCurAuditor(stage.id, stage.times);
  46. // 权限相关
  47. // todo 校验权限 (标段参与人、分享)
  48. const accountId = this.session.sessionUser.accountId, auditorIds = _.map(stage.auditors, 'aid'), shareIds = [];
  49. if (accountId === stage.user_id) { // 原报
  50. stage.readOnly = stage.status !== status.uncheck && stage.status !== status.checkNo;
  51. stage.curTimes = stage.times;
  52. if (stage.status === status.uncheck || stage.status === status.checkNo) {
  53. stage.curOrder = 0;
  54. } else if (stage.status === status.checked) {
  55. stage.curOrder = _.max(_.map(stage.auditors, 'order'));
  56. } else {
  57. stage.curOrder = stage.curAuditor.order - 1;
  58. }
  59. } else if (auditorIds.indexOf(accountId) !== -1) { // 审批人
  60. if (stage.status === status.uncheck) {
  61. throw '您无权查看该数据';
  62. }
  63. stage.readOnly = (stage.status !== status.checking && stage.status !== status.checkNoPre) || accountId !== stage.curAuditor.aid;
  64. stage.curTimes = stage.status === status.checkNo ? stage.times - 1 : stage.times;
  65. if (stage.status === status.checked) {
  66. stage.curOrder = _.max(_.map(stage.auditors, 'order'));
  67. } else if (stage.status === status.checkNo) {
  68. const audit = this.service.stageAudit.getDataByCondition({
  69. sid: stage.id, times: stage.times, status: status.checkNo
  70. });
  71. stage.curOrder = audit.order;
  72. } else {
  73. stage.curOrder = accountId === stage.curAuditor.aid ? stage.curAuditor.order : stage.curAuditor.order - 1;
  74. }
  75. } else if (shareIds.indexOf(accountId) !== -1) { // 分享人
  76. if (stage.status === status.uncheck) {
  77. throw '您无权查看该数据';
  78. }
  79. stage.readOnly = true;
  80. stage.curTimes = stage.status === status.checkNo ? stage.times - 1 : stage.times;
  81. stage.curOrder = stage.status === status.checked ? _.max(_.map(stage.auditors, 'order')) : stage.curAuditor.order - 1;
  82. } else { // 其他不可见
  83. throw '您无权查看该数据';
  84. }
  85. this.stage = stage;
  86. yield next;
  87. } catch (err) {
  88. console.log(err);
  89. // 输出错误到日志
  90. if (err.stack) {
  91. this.logger.error(err);
  92. } else {
  93. this.getLogger('fail').info(JSON.stringify({
  94. error: err,
  95. project: this.session.sessionProject,
  96. user: this.session.sessionUser,
  97. body: this.session.body,
  98. }));
  99. }
  100. // 重定向值标段管理
  101. this.redirect(this.request.headers.referer);
  102. }
  103. };
  104. };