|
@@ -21,6 +21,7 @@ const measureType = tenderConst.measureType;
|
|
|
const path = require('path');
|
|
|
const PayCalculator = require('../lib/pay_calc');
|
|
|
const accountGroup = require('../const/account_group').group;
|
|
|
+const sendToWormhole = require('stream-wormhole');
|
|
|
const billsPosConvert = require('../lib/bills_pos_convert');
|
|
|
const fs = require('fs');
|
|
|
const stdConst = require('../const/standard');
|
|
@@ -668,16 +669,12 @@ module.exports = app => {
|
|
|
try {
|
|
|
this._checkStageCanModify(ctx);
|
|
|
|
|
|
- const file = ctx.request.files[0];
|
|
|
- try {
|
|
|
- const create_time = Date.parse(new Date()) / 1000;
|
|
|
- const fileInfo = path.parse(file.filename);
|
|
|
- const fileName = path.join('public/upload', this.ctx.tender.id.toString(), 'im', 'calcImg_' + create_time + fileInfo.ext);
|
|
|
- await ctx.helper.copyFileSync(file.filepath, path.join(this.app.baseDir, 'app', fileName));
|
|
|
- ctx.body = { err: 0, msg: '', data: fileName };
|
|
|
- } finally {
|
|
|
- await fs.unlinkSync(file.filepath);
|
|
|
- }
|
|
|
+ const stream = await ctx.getFileStream();
|
|
|
+ const create_time = Date.parse(new Date()) / 1000;
|
|
|
+ const fileInfo = path.parse(stream.filename);
|
|
|
+ const fileName = path.join('public/upload', this.ctx.tender.id.toString(), 'im', 'calcImg_' + create_time + fileInfo.ext);
|
|
|
+ await ctx.helper.saveStreamFile(stream, path.join(this.app.baseDir, 'app', fileName));
|
|
|
+ ctx.body = { err: 0, msg: '', data: fileName };
|
|
|
} catch (err) {
|
|
|
this.log(err);
|
|
|
ctx.body = { err: 1, msg: err.toString(), data: null };
|
|
@@ -1319,46 +1316,68 @@ module.exports = app => {
|
|
|
msg: '',
|
|
|
data: [],
|
|
|
};
|
|
|
+ let stream;
|
|
|
try {
|
|
|
// this._checkStageCanModifyRe(ctx);
|
|
|
|
|
|
- let index = 0;
|
|
|
+ const parts = ctx.multipart({ autoFields: true });
|
|
|
const files = [];
|
|
|
+ let index = 0;
|
|
|
const extra_upload = ctx.stage.status === auditConst.status.checked;
|
|
|
- for (const file of ctx.request.files) {
|
|
|
- if (!file.filename) throw '请选择上传的文件!';
|
|
|
- try {
|
|
|
- const fileInfo = path.parse(file.filename);
|
|
|
- const create_time = Date.parse(new Date()) / 1000;
|
|
|
- const filepath = `app/public/upload/${this.ctx.tender.id}/stage/fujian_${create_time + index.toString() + fileInfo.ext}`;
|
|
|
- await this.ctx.helper.recursiveMkdirSync(path.dirname(path.join(this.app.baseDir, filepath)));
|
|
|
- await fs.copyFileSync(file.filepath, path.resolve(this.app.baseDir, filepath));
|
|
|
-
|
|
|
- const fileData = {
|
|
|
- tid: ctx.params.id,
|
|
|
- sid: ctx.params.order,
|
|
|
- in_time: create_time,
|
|
|
- filename: fileInfo.name,
|
|
|
- fileext: fileInfo.ext,
|
|
|
- filesize: Array.isArray(ctx.request.body.size) ? ctx.request.body.size[index] : ctx.request.body.size,
|
|
|
- filepath,
|
|
|
- extra_upload,
|
|
|
- };
|
|
|
- const result = await ctx.service.stageAtt.save(ctx.request.body, fileData, ctx.session.sessionUser.accountId);
|
|
|
- if (!result) {
|
|
|
- throw '导入数据库保存失败';
|
|
|
- }
|
|
|
- const attData = await ctx.service.stageAtt.getDataByFid(result.insertId);
|
|
|
- attData.in_time = moment(create_time * 1000).format('YYYY-MM-DD');
|
|
|
- files.length !== 0 ? files.unshift(attData) : files.push(attData);
|
|
|
- ++index;
|
|
|
- } finally {
|
|
|
- await fs.unlinkSync(file.filepath);
|
|
|
+ while ((stream = await parts()) !== undefined) {
|
|
|
+ // 判断用户是否选择上传文件
|
|
|
+ if (!stream.filename) {
|
|
|
+ throw '请选择上传的文件!';
|
|
|
+ }
|
|
|
+ // const dirName = 'app/public/upload/stage/' + moment().format('YYYYMMDD');
|
|
|
+ // 判断文件夹是否存在,不存在则直接创建文件夹
|
|
|
+ // if (!fs.existsSync(path.join(this.app.baseDir, dirName))) {
|
|
|
+ // await fs.mkdirSync(path.join(this.app.baseDir, dirName));
|
|
|
+ // }
|
|
|
+ const fileInfo = path.parse(stream.filename);
|
|
|
+ const create_time = Date.parse(new Date()) / 1000;
|
|
|
+ const filepath = `app/public/upload/${this.ctx.tender.id}/stage/fujian_${create_time + index.toString() + fileInfo.ext}`;
|
|
|
+ await ctx.helper.saveStreamFile(stream, path.resolve(this.app.baseDir, filepath));
|
|
|
+ // console.log(await fs.existsSync(path.resolve(this.app.baseDir, 'app', filepath)));
|
|
|
+ // const fileInfo = path.parse(stream.filename);
|
|
|
+ // const fileName = 'stage' + create_time + '_' + index + fileInfo.ext;
|
|
|
+ // 保存文件
|
|
|
+ // await ctx.helper.saveStreamFile(stream, path.resolve(this.app.baseDir, dirName, fileName));
|
|
|
+
|
|
|
+ if (stream) {
|
|
|
+ await sendToWormhole(stream);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 保存数据到att表
|
|
|
+ const fileData = {
|
|
|
+ tid: ctx.params.id,
|
|
|
+ sid: ctx.params.order,
|
|
|
+ in_time: create_time,
|
|
|
+ filename: fileInfo.name,
|
|
|
+ fileext: fileInfo.ext,
|
|
|
+ filesize: Array.isArray(parts.field.size) ? parts.field.size[index] : parts.field.size,
|
|
|
+ filepath,
|
|
|
+ extra_upload,
|
|
|
+ };
|
|
|
+ // if (ctx.reUploadPermission) {
|
|
|
+ // fileData.re_upload = 1;
|
|
|
+ // }
|
|
|
+ const result = await ctx.service.stageAtt.save(parts.field, fileData, ctx.session.sessionUser.accountId);
|
|
|
+ if (!result) {
|
|
|
+ throw '导入数据库保存失败';
|
|
|
}
|
|
|
+ const attData = await ctx.service.stageAtt.getDataByFid(result.insertId);
|
|
|
+ attData.in_time = moment(create_time * 1000).format('YYYY-MM-DD');
|
|
|
+ files.length !== 0 ? files.unshift(attData) : files.push(attData);
|
|
|
+ ++index;
|
|
|
}
|
|
|
responseData.data = files;
|
|
|
} catch (err) {
|
|
|
this.log(err);
|
|
|
+ // 失败需要消耗掉stream 以防卡死
|
|
|
+ if (stream) {
|
|
|
+ await sendToWormhole(stream);
|
|
|
+ }
|
|
|
this.setMessage(err.toString(), this.messageType.ERROR);
|
|
|
responseData.err = 1;
|
|
|
responseData.msg = err.toString();
|
|
@@ -1492,34 +1511,42 @@ module.exports = app => {
|
|
|
msg: '',
|
|
|
data: [],
|
|
|
};
|
|
|
+ let stream;
|
|
|
try {
|
|
|
- //this._checkStageCanModifyRe(ctx);
|
|
|
-
|
|
|
- const file = ctx.request.files[0];
|
|
|
+ this._checkStageCanModifyRe(ctx);
|
|
|
+ stream = await ctx.getFileStream({ requireFile: false });
|
|
|
let fileData = {};
|
|
|
- if (file && file.filename !== undefined) {
|
|
|
+ if (stream.filename !== undefined) {
|
|
|
const create_time = Date.parse(new Date()) / 1000;
|
|
|
- const fileInfo = path.parse(file.filename);
|
|
|
+ const fileInfo = path.parse(stream.filename);
|
|
|
const dirName = 'app/public/upload/stage/' + moment().format('YYYYMMDD');
|
|
|
const fileName = 'stage' + create_time + fileInfo.ext;
|
|
|
|
|
|
+ // 判断文件夹是否存在,不存在则直接创建文件夹
|
|
|
+ if (!fs.existsSync(path.join(this.app.baseDir, dirName))) {
|
|
|
+ await fs.mkdirSync(path.join(this.app.baseDir, dirName));
|
|
|
+ }
|
|
|
// 保存文件
|
|
|
- await ctx.helper.copyFileSync(file.filepath, path.join(this.app.baseDir, dirName, fileName));
|
|
|
+ await ctx.helper.saveStreamFile(stream, path.join(this.app.baseDir, dirName, fileName));
|
|
|
// 保存数据到att表
|
|
|
fileData = {
|
|
|
- filesize: ctx.request.body.size,
|
|
|
+ filesize: stream.fields.size,
|
|
|
filepath: path.join(dirName, fileName),
|
|
|
};
|
|
|
}
|
|
|
- const result = await ctx.service.stageAtt.updateByID(ctx.request.body, fileData);
|
|
|
+ const result = await ctx.service.stageAtt.updateByID(stream.fields, fileData);
|
|
|
if (!result) {
|
|
|
throw '导入数据库保存失败';
|
|
|
}
|
|
|
- const attData = await ctx.service.stageAtt.getDataByFid(ctx.request.body.id);
|
|
|
+ const attData = await ctx.service.stageAtt.getDataByFid(stream.fields.id);
|
|
|
attData.in_time = moment(attData.in_time * 1000).format('YYYY-MM-DD');
|
|
|
responseData.data = attData;
|
|
|
} catch (err) {
|
|
|
this.log(err);
|
|
|
+ // 失败需要消耗掉stream 以防卡死
|
|
|
+ if (stream) {
|
|
|
+ await sendToWormhole(stream);
|
|
|
+ }
|
|
|
this.setMessage(err.toString(), this.messageType.ERROR);
|
|
|
responseData.err = 1;
|
|
|
responseData.msg = err.toString();
|
|
@@ -1578,45 +1605,55 @@ module.exports = app => {
|
|
|
msg: '',
|
|
|
data: [],
|
|
|
};
|
|
|
+ let stream;
|
|
|
try {
|
|
|
this._checkStageCanModify(ctx);
|
|
|
|
|
|
- let index = 0;
|
|
|
+ const parts = ctx.multipart({ autoFields: true });
|
|
|
const files = [];
|
|
|
+ let index = 0;
|
|
|
const create_time = Date.parse(new Date()) / 1000;
|
|
|
- for (const file of ctx.request.files) {
|
|
|
- if (!file.filename) throw '请选择上传的文件!';
|
|
|
- try {
|
|
|
- const fileInfo = path.parse(file.filename);
|
|
|
- const dirName = 'app/public/upload/pay/' + moment().format('YYYYMMDD');
|
|
|
- const fileName = 'pay' + create_time + '_' + index + fileInfo.ext;
|
|
|
-
|
|
|
- await this.ctx.helper.recursiveMkdirSync(path.join(this.app.baseDir, dirName));
|
|
|
- await fs.copyFileSync(file.filepath, path.resolve(this.app.baseDir, dirName, fileName));
|
|
|
-
|
|
|
- const attData = {
|
|
|
- filename: fileInfo.name,
|
|
|
- fileext: fileInfo.ext,
|
|
|
- filesize: Array.isArray(ctx.request.body.size) ? ctx.request.body.size[index] : ctx.request.body.size,
|
|
|
- filepath: path.join(dirName, fileName),
|
|
|
- uid: ctx.session.sessionUser.accountId,
|
|
|
- in_time: moment(create_time * 1000).format('YYYY-MM-DD'),
|
|
|
- };
|
|
|
- const result = await ctx.service.stagePay.saveAtt(ctx.request.body.pay_id, attData);
|
|
|
- if (!result) {
|
|
|
- throw '导入数据库保存失败';
|
|
|
- }
|
|
|
- delete attData.filepath;
|
|
|
- attData.username = ctx.session.sessionUser.name;
|
|
|
- files.length !== 0 ? files.unshift(attData) : files.push(attData);
|
|
|
- ++index;
|
|
|
- } finally {
|
|
|
- await fs.unlinkSync(file.filepath);
|
|
|
+ while ((stream = await parts()) !== undefined) {
|
|
|
+ // 判断用户是否选择上传文件
|
|
|
+ if (!stream.filename) {
|
|
|
+ throw '请选择上传的文件!';
|
|
|
}
|
|
|
+ const fileInfo = path.parse(stream.filename);
|
|
|
+ const dirName = 'app/public/upload/pay/' + moment().format('YYYYMMDD');
|
|
|
+ const fileName = 'pay' + create_time + '_' + index + fileInfo.ext;
|
|
|
+
|
|
|
+ // 判断文件夹是否存在,不存在则直接创建文件夹
|
|
|
+ if (!fs.existsSync(path.join(this.app.baseDir, dirName))) {
|
|
|
+ await fs.mkdirSync(path.join(this.app.baseDir, dirName));
|
|
|
+ }
|
|
|
+ // 保存文件
|
|
|
+ await ctx.helper.saveStreamFile(stream, path.join(this.app.baseDir, dirName, fileName));
|
|
|
+ await sendToWormhole(stream);
|
|
|
+ // 插入到stage_pay对应的附件列表中
|
|
|
+ const attData = {
|
|
|
+ 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'),
|
|
|
+ };
|
|
|
+ const result = await ctx.service.stagePay.saveAtt(parts.field.pay_id, attData);
|
|
|
+ if (!result) {
|
|
|
+ throw '导入数据库保存失败';
|
|
|
+ }
|
|
|
+ delete attData.filepath;
|
|
|
+ attData.username = ctx.session.sessionUser.name;
|
|
|
+ files.length !== 0 ? files.unshift(attData) : files.push(attData);
|
|
|
+ ++index;
|
|
|
}
|
|
|
responseData.data = files;
|
|
|
} catch (err) {
|
|
|
this.log(err);
|
|
|
+ // 失败需要消耗掉stream 以防卡死
|
|
|
+ if (stream) {
|
|
|
+ await sendToWormhole(stream);
|
|
|
+ }
|
|
|
this.setMessage(err.toString(), this.messageType.ERROR);
|
|
|
responseData.err = 1;
|
|
|
responseData.msg = err.toString();
|