change_check.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Ellisran
  6. * @date 2020/10/15
  7. * @version
  8. */
  9. const status = require('../const/audit').flow.status;
  10. const shenpiConst = require('../const/shenpi');
  11. const _ = require('lodash');
  12. module.exports = options => {
  13. /**
  14. * 标段校验 中间件
  15. * 1. 读取标段数据(包括属性)
  16. * 2. 检验用户是否可见标段(不校验具体权限)
  17. *
  18. * @param {function} next - 中间件继续执行的方法
  19. * @return {void}
  20. */
  21. return function* changeCheck(next) {
  22. try {
  23. // 获取revise
  24. const cid = this.params.cid || this.request.body.cid;
  25. if (!cid) {
  26. throw '您访问的变更令不存在';
  27. }
  28. const change = yield this.service.change.getDataByCondition({ cid });
  29. // 读取原报、审核人数据
  30. change.auditors = yield this.service.changeAudit.getListGroupByTimes(change.cid, change.times);
  31. change.curAuditor = yield this.service.changeAudit.getCurAuditor(change.cid, change.times);
  32. if (!change) throw '变更令数据有误';
  33. // 权限相关
  34. // todo 校验权限 (变更参与人)
  35. const accountId = this.session.sessionUser.accountId,
  36. auditorIds = _.map(change.auditors, 'uid'),
  37. shareIds = [];
  38. const permission = this.session.sessionUser.permission;
  39. if (accountId === change.uid) { // 原报
  40. if (change.curAuditor) {
  41. change.readOnly = change.curAuditor.uid !== accountId;
  42. } else {
  43. change.readOnly = change.status !== status.uncheck && change.status !== status.back;
  44. }
  45. } else if (auditorIds.indexOf(accountId) !== -1) { // 审批人
  46. if (change.status === status.uncheck) {
  47. throw '您无权查看该数据';
  48. }
  49. change.readOnly = (change.status !== status.checking && change.status !== status.back) || accountId !== change.curAuditor.uid;
  50. } else if (shareIds.indexOf(accountId) !== -1 || (permission !== null && permission.tender !== undefined && permission.tender.indexOf('2') !== -1)) { // 分享人
  51. if (change.status === status.uncheck) {
  52. throw '您无权查看该数据';
  53. }
  54. change.readOnly = true;
  55. } else { // 其他不可见
  56. throw '您无权查看该数据';
  57. }
  58. this.change = change;
  59. yield next;
  60. } catch (err) {
  61. console.log(err);
  62. // 输出错误到日志
  63. if (err.stack) {
  64. this.logger.error(err);
  65. } else {
  66. this.getLogger('fail').info(JSON.stringify({
  67. error: err,
  68. project: this.session.sessionProject,
  69. user: this.session.sessionUser,
  70. body: this.session.body,
  71. }));
  72. }
  73. // 重定向值标段管理
  74. this.redirect(this.request.headers.referer);
  75. }
  76. };
  77. };