payment_tender_check.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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.params.id) {
  24. throw '当前未打开标段';
  25. }
  26. const tender = yield this.service.paymentTender.getDataById(this.params.id);
  27. const projectInfo = yield this.service.project.getDataById(this.session.sessionProject.id);
  28. const modes = projectInfo.payment_setting ? JSON.parse(projectInfo.payment_setting) : _.cloneDeep(paymentConst.setting_modes);
  29. for (const m in paymentConst.setting_modes) {
  30. if (!modes[m]) modes[m] = _.cloneDeep(paymentConst.setting_modes[m]);
  31. }
  32. if (!tender) {
  33. throw '标段不存在';
  34. }
  35. this.tender = tender;
  36. this.project_setting = modes;
  37. yield next;
  38. } catch (err) {
  39. // 输出错误到日志
  40. if (err.stack) {
  41. this.logger.error(err);
  42. } else {
  43. this.session.message = {
  44. type: messageType.ERROR,
  45. icon: 'exclamation-circle',
  46. message: err,
  47. };
  48. this.getLogger('fail').info(JSON.stringify({
  49. error: err,
  50. project: this.session.sessionProject,
  51. user: this.session.sessionUser,
  52. body: this.session.body,
  53. }));
  54. }
  55. if (this.helper.isAjax(this.request)) {
  56. if (err.stack) {
  57. this.body = {err: 4, msg: '标段数据未知错误', data: null};
  58. } else {
  59. this.body = {err: 3, msg: err.toString(), data: null};
  60. }
  61. } else {
  62. if (this.helper.isWap(this.request)) {
  63. this.redirect('/wap/list');
  64. } else {
  65. err === '您无权查看该内容' ? this.redirect(this.request.headers.referer) : this.redirect('/payment');
  66. }
  67. }
  68. }
  69. };
  70. };