123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- 'use strict';
- /**
- * 合同支付
- *
- * @author Mai
- * @date
- * @version
- */
- module.exports = app => {
- class PayController extends app.BaseController {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- }
- async index(ctx) {
- try {
- const pays = await this.ctx.service.pay.getAllDataByCondition({ where: { tid: ctx.tender.id } });
- const renderData = {
- jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.pay.list)
- };
- await this.layout('pay/index.ejs', renderData);
- } catch (err) {
- ctx.helper.log(err);
- }
- }
- async uploadFile(ctx) {
- let stream;
- try {
- const parts = ctx.multipart({ autoFields: true });
- let index = 0;
- const create_time = Date.parse(new Date()) / 1000;
- let stream = await parts();
- const bonus = await ctx.service.stageBonus.getStageDataById(parts.field.bonus_id);
- //if (!bonus || bonus.sid !== ctx.stage.id) throw '该奖罚金,当前不允许上传附件';
- while (stream !== undefined) {
- if (!stream.filename) {
- throw '未发现上传文件!';
- }
- const fileInfo = path.parse(stream.filename);
- const dirName = 'app/public/upload/extra/' + moment().format('YYYYMMDD');
- const fileName = create_time + '_' + index + fileInfo.ext;
- // 保存文件
- await ctx.helper.saveStreamFile(stream, path.join(this.app.baseDir, dirName, fileName));
- await sendToWormhole(stream);
- // 插入到stage_pay对应的附件列表中
- bonus.proof_file.push({
- filename: fileInfo.name,
- fileext: fileInfo.ext,
- filesize: Array.isArray(parts.field.size) ? parts.field.size[index] : parts.field.size,
- filepath: path.join(dirName, fileName),
- uid: ctx.session.sessionUser.accountId,
- in_time: moment(create_time * 1000).format('YYYY-MM-DD'),
- renew: bonus.sid === ctx.stage.id ? ctx.stage.status === auditConst.status.checked : true,
- });
- ++index;
- if (Array.isArray(parts.field.size) && index < parts.field.size.length) {
- stream = await parts();
- } else {
- stream = undefined;
- }
- }
- const result = await ctx.service.stageBonus.updateDatas({
- update: [
- { id: bonus.id, proof_file: bonus.proof_file },
- ]
- });
- for (const pf of bonus.proof_file) {
- pf.username = (await ctx.service.projectAccount.getAccountInfoById(pf.uid)).name;
- if (ctx.helper.canPreview(pf.fileext)){
- pf.viewpath = pf.filepath.substr(3, pf.filepath.length - 3);
- }
- delete pf.filepath;
- }
- ctx.body = {err: 0, msg: '', data: bonus.proof_file};
- } catch (error) {
- ctx.helper.log(error);
- // 失败需要消耗掉stream 以防卡死
- if (stream) {
- await sendToWormhole(stream);
- }
- ctx.body = this.ajaxErrorBody(error, '上传附件失败,请重试');
- }
- }
- async deleteFile(ctx) {
- try {
- const data = JSON.parse(ctx.request.body.data);
- const bonus = await ctx.service.stageBonus.getStageDataById(data.b_id);
- if (!bonus || !bonus.proof_file || !bonus.proof_file[data.index]) throw '删除的文件不存在';
- const fileInfo = bonus.proof_file[data.index];
- if (fileInfo.uid !== ctx.session.sessionUser.accountId) throw '您无权删除该文件';
- const deleteFilePermission = PermissionCheck.delFile(this.ctx.session.sessionUser.permission);
- if (ctx.stage.status === auditConst.status.checked && !fileInfo.renew && !deleteFilePermission) throw '不可删除该文件';
- // 先删除文件
- await fs.unlinkSync(path.join(this.app.baseDir, fileInfo.filepath));
- // 再删除数据库
- bonus.proof_file.splice(data.index, 1);
- const result = await ctx.service.stageBonus.updateDatas({
- update: [
- { id: bonus.id, proof_file: bonus.proof_file },
- ]
- });
- for (const pf of bonus.proof_file) {
- delete pf.filepath;
- pf.username = (await ctx.service.projectAccount.getAccountInfoById(pf.uid)).name;
- }
- ctx.body = {err: 0, msg: '', data: bonus.proof_file};
- } catch (err) {
- this.log(err);
- this.ctx.ajaxErrorBody(err, '删除文件失败');
- }
- }
- }
- return PayController;
- };
|