financial_check.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 financialConst = require('../const/financial');
  12. module.exports = options => {
  13. /**
  14. * 标段校验 中间件
  15. * 1. 读取标段数据(包括属性)
  16. * 2. 检验用户是否可见标段(不校验具体权限)
  17. *
  18. * @param {function} next - 中间件继续执行的方法
  19. * @return {void}
  20. */
  21. return function* financialCheck(next) {
  22. try {
  23. if (!this.subProject) {
  24. throw '项目不存在';
  25. }
  26. if (!this.subProject.page_show.openFinancial) {
  27. throw '该功能已关闭或无法查看';
  28. }
  29. // const spid = this.params.spid;
  30. // if (!spid) {
  31. // throw '参数数据错误';
  32. // }
  33. // this.subProject = yield this.service.subProject.getDataById(spid);
  34. if (this.subProject.project_id !== this.session.sessionProject.id) throw '您无权查看该项目资金监管';
  35. const fAudit = yield this.service.financialAudit.getDataByCondition({ spid: this.subProject.id, uid: this.session.sessionUser.accountId });
  36. if (!fAudit && !this.session.sessionUser.is_admin) throw '您无权查看该项目资金监管,请联系管理员添加';
  37. // if (!this.subProject) throw '项目不存在';
  38. yield next;
  39. } catch (err) {
  40. // 输出错误到日志
  41. if (err.stack) {
  42. this.logger.error(err);
  43. } else {
  44. this.session.message = {
  45. type: messageType.ERROR,
  46. icon: 'exclamation-circle',
  47. message: err,
  48. };
  49. this.getLogger('fail').info(JSON.stringify({
  50. error: err,
  51. project: this.session.sessionProject,
  52. user: this.session.sessionUser,
  53. body: this.session.body,
  54. }));
  55. }
  56. if (this.helper.isAjax(this.request)) {
  57. if (err.stack) {
  58. this.body = { err: 4, msg: '标段数据未知错误', data: null };
  59. } else {
  60. this.body = { err: 3, msg: err.toString(), data: null };
  61. }
  62. } else {
  63. if (this.helper.isWap(this.request)) {
  64. this.redirect('/wap/list');
  65. } else {
  66. this.postError(err, '未知错误');
  67. err === '该功能已关闭或无法查看' ? this.redirect('/dashboard') : this.request.headers.referer ? this.redirect(this.request.headers.referer) : this.redirect(`/sp/${this.subProject.id}/financial`);
  68. }
  69. }
  70. }
  71. };
  72. };