pay_controller.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. 'use strict';
  2. /**
  3. * 合同支付
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const audit = require('../const/audit');
  10. module.exports = app => {
  11. class PayController extends app.BaseController {
  12. /**
  13. * 构造函数
  14. *
  15. * @param {Object} ctx - egg全局变量
  16. * @return {void}
  17. */
  18. constructor(ctx) {
  19. super(ctx);
  20. }
  21. async index(ctx) {
  22. try {
  23. const phasePays = await this.ctx.service.phasePay.getAllPhasePay(ctx.tender.id, 'DESC');
  24. this.ctx.service.phasePay.calculatePhasePay(phasePays);
  25. const renderData = {
  26. phasePays,
  27. auditConst: audit.common,
  28. };
  29. await this.layout('phase_pay/index.ejs', renderData);
  30. } catch (err) {
  31. ctx.helper.log(err);
  32. }
  33. }
  34. async pay(ctx) {
  35. try {
  36. const pays = await this.ctx.service.phasePay.getAllPhasePay(ctx.tender.id, 'DESC');
  37. const renderData = {
  38. pays,
  39. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.pay.list)
  40. };
  41. await this.layout('pay/pay.ejs', renderData);
  42. } catch (err) {
  43. ctx.helper.log(err);
  44. ctx.postError(err, '读取合同支付数据错误');
  45. ctx.redirect(this.request.headers.referer);
  46. }
  47. }
  48. async uploadFile(ctx) {
  49. let stream;
  50. try {
  51. const parts = ctx.multipart({ autoFields: true });
  52. let index = 0;
  53. const create_time = Date.parse(new Date()) / 1000;
  54. let stream = await parts();
  55. const bonus = await ctx.service.stageBonus.getStageDataById(parts.field.bonus_id);
  56. //if (!bonus || bonus.sid !== ctx.stage.id) throw '该奖罚金,当前不允许上传附件';
  57. while (stream !== undefined) {
  58. if (!stream.filename) {
  59. throw '未发现上传文件!';
  60. }
  61. const fileInfo = path.parse(stream.filename);
  62. const dirName = 'app/public/upload/extra/' + moment().format('YYYYMMDD');
  63. const fileName = create_time + '_' + index + fileInfo.ext;
  64. // 保存文件
  65. await ctx.helper.saveStreamFile(stream, path.join(this.app.baseDir, dirName, fileName));
  66. await sendToWormhole(stream);
  67. // 插入到stage_pay对应的附件列表中
  68. bonus.proof_file.push({
  69. filename: fileInfo.name,
  70. fileext: fileInfo.ext,
  71. filesize: Array.isArray(parts.field.size) ? parts.field.size[index] : parts.field.size,
  72. filepath: path.join(dirName, fileName),
  73. uid: ctx.session.sessionUser.accountId,
  74. in_time: moment(create_time * 1000).format('YYYY-MM-DD'),
  75. renew: bonus.sid === ctx.stage.id ? ctx.stage.status === auditConst.status.checked : true,
  76. });
  77. ++index;
  78. if (Array.isArray(parts.field.size) && index < parts.field.size.length) {
  79. stream = await parts();
  80. } else {
  81. stream = undefined;
  82. }
  83. }
  84. const result = await ctx.service.stageBonus.updateDatas({
  85. update: [
  86. { id: bonus.id, proof_file: bonus.proof_file },
  87. ]
  88. });
  89. for (const pf of bonus.proof_file) {
  90. pf.username = (await ctx.service.projectAccount.getAccountInfoById(pf.uid)).name;
  91. if (ctx.helper.canPreview(pf.fileext)){
  92. pf.viewpath = pf.filepath.substr(3, pf.filepath.length - 3);
  93. }
  94. delete pf.filepath;
  95. }
  96. ctx.body = {err: 0, msg: '', data: bonus.proof_file};
  97. } catch (error) {
  98. ctx.helper.log(error);
  99. // 失败需要消耗掉stream 以防卡死
  100. if (stream) {
  101. await sendToWormhole(stream);
  102. }
  103. ctx.body = this.ajaxErrorBody(error, '上传附件失败,请重试');
  104. }
  105. }
  106. async deleteFile(ctx) {
  107. try {
  108. const data = JSON.parse(ctx.request.body.data);
  109. const bonus = await ctx.service.stageBonus.getStageDataById(data.b_id);
  110. if (!bonus || !bonus.proof_file || !bonus.proof_file[data.index]) throw '删除的文件不存在';
  111. const fileInfo = bonus.proof_file[data.index];
  112. if (fileInfo.uid !== ctx.session.sessionUser.accountId) throw '您无权删除该文件';
  113. const deleteFilePermission = PermissionCheck.delFile(this.ctx.session.sessionUser.permission);
  114. if (ctx.stage.status === auditConst.status.checked && !fileInfo.renew && !deleteFilePermission) throw '不可删除该文件';
  115. // 先删除文件
  116. await fs.unlinkSync(path.join(this.app.baseDir, fileInfo.filepath));
  117. // 再删除数据库
  118. bonus.proof_file.splice(data.index, 1);
  119. const result = await ctx.service.stageBonus.updateDatas({
  120. update: [
  121. { id: bonus.id, proof_file: bonus.proof_file },
  122. ]
  123. });
  124. for (const pf of bonus.proof_file) {
  125. delete pf.filepath;
  126. pf.username = (await ctx.service.projectAccount.getAccountInfoById(pf.uid)).name;
  127. }
  128. ctx.body = {err: 0, msg: '', data: bonus.proof_file};
  129. } catch (err) {
  130. this.log(err);
  131. this.ctx.ajaxErrorBody(err, '删除文件失败');
  132. }
  133. }
  134. }
  135. return PayController;
  136. };