contract_check.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const messageType = require('../const/message_type');
  10. const _ = require('lodash');
  11. const contractConst = require('../const/contract');
  12. module.exports = options => {
  13. /**
  14. * 标段校验 中间件
  15. * 1. 读取标段数据(包括属性)
  16. * 2. 检验用户是否可见标段(不校验具体权限)
  17. *
  18. * @param {function} next - 中间件继续执行的方法
  19. * @return {void}
  20. */
  21. return function* contractCheck(next) {
  22. try {
  23. if (!this.subProject.page_show.openContract) {
  24. throw '该功能已关闭或无法查看';
  25. }
  26. const tid = this.params.tid || null;
  27. const type = this.params.type ? contractConst.type[this.params.type] : contractConst.type.expenses;
  28. const info = tid ? yield this.service.tender.getDataById(tid) : this.subProject;
  29. if (!info) throw '项目或标段不存在';
  30. const options = tid ? { tid } : { spid: this.subProject.id };
  31. if (this.request.originalUrl && this.request.originalUrl.indexOf('detail') > -1) yield this.service.contractTree.insertTree(options, info);
  32. // 权限控制
  33. const cloneOptions = _.cloneDeep(options);
  34. cloneOptions.uid = this.session.sessionUser.accountId;
  35. let result = false;
  36. if (tid) {
  37. result = yield this.service.contractAudit.getDataByCondition(cloneOptions);
  38. } else {
  39. const contractPermission = this.subProject.permission.contract_permission;
  40. if (contractPermission.length > 0 && _.intersection([3, 4, 5], contractPermission).length > 0) {
  41. result = yield this.service.subProjPermission.getContractPermission(contractPermission);
  42. }
  43. }
  44. // const result = yield this.service.contractAudit.checkPermission(options, this.session.sessionUser.accountId);
  45. if (!result && !this.session.sessionUser.is_admin) {
  46. throw '当前账号权限不足,请联系管理员添加权限';
  47. }
  48. this.contract = info;
  49. this.contractOptions = options;
  50. this.contract_audit_permission = result;
  51. this.contract_type = type;
  52. this.contract_tender = !!tid;
  53. yield next;
  54. } catch (err) {
  55. // 输出错误到日志
  56. if (err.stack) {
  57. this.logger.error(err);
  58. } else {
  59. this.session.message = {
  60. type: messageType.ERROR,
  61. icon: 'exclamation-circle',
  62. message: err,
  63. };
  64. this.getLogger('fail').info(JSON.stringify({
  65. error: err,
  66. project: this.session.sessionProject,
  67. user: this.session.sessionUser,
  68. body: this.session.body,
  69. }));
  70. }
  71. if (this.helper.isAjax(this.request)) {
  72. if (err.stack) {
  73. this.body = { err: 4, msg: '标段数据未知错误', data: null };
  74. } else {
  75. this.body = { err: 3, msg: err.toString(), data: null };
  76. }
  77. } else {
  78. if (this.helper.isWap(this.request)) {
  79. this.redirect('/wap/subproj');
  80. } else {
  81. this.postError(err, '未知错误');
  82. err === '该功能已关闭或无法查看' ? this.redirect('/dashboard') : this.request.headers.referer ? this.redirect(this.request.headers.referer) : this.redirect(`/sp/${this.subProject.id}/dashboard`);
  83. }
  84. }
  85. }
  86. };
  87. };