contract_check.js 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 && !this.subProject.page_show.openTenderContract) {
  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. if (this.session.sessionUser.is_admin) {
  38. result = yield this.service.subProjPermission.getContractPermission(this.subProject.permission.contract_permission);
  39. } else {
  40. result = yield this.service.contractAudit.getDataByCondition(cloneOptions);
  41. }
  42. } else {
  43. const contractPermission = this.subProject.permission.contract_permission;
  44. if (contractPermission.length > 0 && _.intersection([3, 4, 5], contractPermission).length > 0) {
  45. result = yield this.service.subProjPermission.getContractPermission(contractPermission);
  46. }
  47. }
  48. // const result = yield this.service.contractAudit.checkPermission(options, this.session.sessionUser.accountId);
  49. if (!result && !this.session.sessionUser.is_admin) {
  50. throw '当前账号权限不足,请联系管理员添加权限';
  51. }
  52. this.contract = info;
  53. this.contractOptions = options;
  54. this.contract_audit_permission = result;
  55. this.contract_type = type;
  56. this.contract_tender = !!tid;
  57. yield next;
  58. } catch (err) {
  59. // 输出错误到日志
  60. if (err.stack) {
  61. this.logger.error(err);
  62. } else {
  63. this.session.message = {
  64. type: messageType.ERROR,
  65. icon: 'exclamation-circle',
  66. message: err,
  67. };
  68. this.getLogger('fail').info(JSON.stringify({
  69. error: err,
  70. project: this.session.sessionProject,
  71. user: this.session.sessionUser,
  72. body: this.session.body,
  73. }));
  74. }
  75. if (this.helper.isAjax(this.request)) {
  76. if (err.stack) {
  77. this.body = { err: 4, msg: '标段数据未知错误', data: null };
  78. } else {
  79. this.body = { err: 3, msg: err.toString(), data: null };
  80. }
  81. } else {
  82. if (this.helper.isWap(this.request)) {
  83. this.redirect('/wap/subproj');
  84. } else {
  85. this.postError(err, '未知错误');
  86. err === '该功能已关闭或无法查看' ? this.redirect('/dashboard') : this.request.headers.referer ? this.redirect(this.request.headers.referer) : this.redirect(`/sp/${this.subProject.id}/dashboard`);
  87. }
  88. }
  89. }
  90. };
  91. };