contract_check.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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.session.sessionProject.page_show.openContract) {
  24. throw '该功能已关闭或无法查看';
  25. }
  26. const stid = this.params.stid;
  27. const type = this.params.type ? contractConst.type[this.params.type] : contractConst.type.expenses;
  28. if (!stid) throw '参数错误';
  29. let spid = null;
  30. let tid = null;
  31. // 判断stid字符串是不是只有数字
  32. if (!/^\d+$/.test(stid)) {
  33. spid = stid;
  34. } else {
  35. tid = stid;
  36. }
  37. if (!spid && !tid) {
  38. throw '参数数据错误';
  39. }
  40. const info = spid ? yield this.service.subProject.getDataById(spid) : yield this.service.tender.getDataById(tid);
  41. if (!info) throw '项目或标段不存在';
  42. const options = spid ? { spid } : { tid };
  43. if (this.request.originalUrl && this.request.originalUrl.indexOf('detail') > -1) yield this.service.contractTree.insertTree(options, info);
  44. // 权限控制
  45. const cloneOptions = _.cloneDeep(options);
  46. cloneOptions.uid = this.session.sessionUser.accountId;
  47. const result = yield this.service.contractAudit.getDataByCondition(cloneOptions);
  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. yield next;
  57. } catch (err) {
  58. // 输出错误到日志
  59. if (err.stack) {
  60. this.logger.error(err);
  61. } else {
  62. this.session.message = {
  63. type: messageType.ERROR,
  64. icon: 'exclamation-circle',
  65. message: err,
  66. };
  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. if (this.helper.isAjax(this.request)) {
  75. if (err.stack) {
  76. this.body = { err: 4, msg: '标段数据未知错误', data: null };
  77. } else {
  78. this.body = { err: 3, msg: err.toString(), data: null };
  79. }
  80. } else {
  81. if (this.helper.isWap(this.request)) {
  82. this.redirect('/wap/list');
  83. } else {
  84. this.postError(err, '未知错误');
  85. err === '该功能已关闭或无法查看' ? this.redirect('/dashboard') : this.request.headers.referer ? this.redirect(this.request.headers.referer) : this.redirect('/contract');
  86. }
  87. }
  88. }
  89. };
  90. };