change_check.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. console.log(change.curAuditor);
  33. if (!change) throw '变更令数据有误';
  34. // 权限相关
  35. // todo 校验权限 (变更参与人)
  36. const accountId = this.session.sessionUser.accountId,
  37. auditorIds = _.map(change.auditors, 'uid'),
  38. shareIds = [];
  39. const permission = this.session.sessionUser.permission;
  40. if (accountId === change.uid) { // 原报
  41. if (change.curAuditor) {
  42. change.readOnly = change.curAuditor.uid !== accountId;
  43. } else {
  44. change.readOnly = change.status !== status.uncheck && change.status !== status.back;
  45. }
  46. } else if (auditorIds.indexOf(accountId) !== -1) { // 审批人
  47. if (change.status === status.uncheck) {
  48. throw '您无权查看该数据';
  49. }
  50. change.readOnly = true;
  51. } else if (shareIds.indexOf(accountId) !== -1 || (permission !== null && permission.tender !== undefined && permission.tender.indexOf('2') !== -1)) { // 分享人
  52. if (change.status === status.uncheck) {
  53. throw '您无权查看该数据';
  54. }
  55. change.readOnly = true;
  56. } else { // 其他不可见
  57. throw '您无权查看该数据';
  58. }
  59. this.change = change;
  60. yield next;
  61. } catch (err) {
  62. console.log(err);
  63. // 输出错误到日志
  64. if (err.stack) {
  65. this.logger.error(err);
  66. } else {
  67. this.getLogger('fail').info(JSON.stringify({
  68. error: err,
  69. project: this.session.sessionProject,
  70. user: this.session.sessionUser,
  71. body: this.session.body,
  72. }));
  73. }
  74. // 重定向值标段管理
  75. this.redirect(this.request.headers.referer);
  76. }
  77. };
  78. };