material_check.js 5.7 KB

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