contract_check.js 3.3 KB

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