payment_tender_check.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const messageType = require('../const/message_type');
  10. const paymentConst = require('../const/payment');
  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* paymentTenderCheck(next) {
  22. try {
  23. if (!this.session.sessionProject.showPayment) {
  24. throw '该功能已关闭或无法查看';
  25. }
  26. if (!this.params.id) {
  27. throw '当前未打开标段';
  28. }
  29. const tender = yield this.service.paymentTender.getDataById(this.params.id);
  30. const projectInfo = yield this.service.project.getDataById(this.session.sessionProject.id);
  31. const modes = projectInfo.payment_setting ? JSON.parse(projectInfo.payment_setting) : _.cloneDeep(paymentConst.setting_modes);
  32. for (const m in paymentConst.setting_modes) {
  33. if (!modes[m]) modes[m] = _.cloneDeep(paymentConst.setting_modes[m]);
  34. }
  35. const auditPermission = yield this.service.paymentPermissionAudit.getOnePermission(this.session.sessionUser.is_admin, this.session.sessionUser.accountId);
  36. if (!auditPermission) {
  37. throw '权限不足';
  38. }
  39. if (!tender) {
  40. throw '标段不存在';
  41. }
  42. const payment = {
  43. auditPermission,
  44. project_setting: modes,
  45. }
  46. this.paymentTender = tender;
  47. this.payment = payment;
  48. yield next;
  49. } catch (err) {
  50. // 输出错误到日志
  51. if (err.stack) {
  52. this.logger.error(err);
  53. } else {
  54. this.session.message = {
  55. type: messageType.ERROR,
  56. icon: 'exclamation-circle',
  57. message: err,
  58. };
  59. this.getLogger('fail').info(JSON.stringify({
  60. error: err,
  61. project: this.session.sessionProject,
  62. user: this.session.sessionUser,
  63. body: this.session.body,
  64. }));
  65. }
  66. if (this.helper.isAjax(this.request)) {
  67. if (err.stack) {
  68. this.body = {err: 4, msg: '标段数据未知错误', data: null};
  69. } else {
  70. this.body = {err: 3, msg: err.toString(), data: null};
  71. }
  72. } else {
  73. if (this.helper.isWap(this.request)) {
  74. this.redirect('/wap/list');
  75. } else {
  76. this.postError(err, '未知错误');
  77. err === '该功能已关闭或无法查看' ? this.redirect('/dashboard') : (err === '您无权查看该内容' ? this.redirect(this.request.headers.referer) : this.redirect('/payment'));
  78. }
  79. }
  80. }
  81. };
  82. };