pay_controller.js 5.4 KB

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