stage_check.js 6.1 KB

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