|
@@ -8,6 +8,10 @@
|
|
|
* @version
|
|
|
*/
|
|
|
const auditConst = require('../const/audit').stage;
|
|
|
+const sendToWormhole = require('stream-wormhole');
|
|
|
+const path = require('path');
|
|
|
+const moment = require('moment');
|
|
|
+const fs = require('fs');
|
|
|
|
|
|
module.exports = app => {
|
|
|
|
|
@@ -88,7 +92,7 @@ module.exports = app => {
|
|
|
try {
|
|
|
const renderData = {
|
|
|
jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.stageExtra.bonus)
|
|
|
- }
|
|
|
+ };
|
|
|
await this.layout('stage_extra/bonus.ejs', renderData, 'stage_extra/bonus_modal.ejs');
|
|
|
} catch (err) {
|
|
|
ctx.helper.log(err);
|
|
@@ -103,9 +107,13 @@ module.exports = app => {
|
|
|
async loadBonus (ctx) {
|
|
|
try {
|
|
|
const data = await ctx.service.stageBonus.getStageData(ctx.stage.id);
|
|
|
- console.log(data);
|
|
|
+ for (const d of data) {
|
|
|
+ for (const pf of d.proof_file) {
|
|
|
+ delete pf.filepath;
|
|
|
+ pf.username = (await ctx.service.projectAccount.getAccountInfoById(pf.uid)).name;
|
|
|
+ }
|
|
|
+ }
|
|
|
const preData = await ctx.service.stageBonus.getPreStageData(ctx.stage.order);
|
|
|
- console.log(preData);
|
|
|
ctx.body = {err: 0, msg: '', data: data.concat(preData)};
|
|
|
} catch (error) {
|
|
|
ctx.helper.log(error);
|
|
@@ -182,6 +190,120 @@ module.exports = app => {
|
|
|
ctx.body = this.ajaxErrorBody(error, '提交数据失败,请重试');
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ async uploadFile(ctx) {
|
|
|
+ let stream;
|
|
|
+ try {
|
|
|
+ const parts = ctx.multipart({ autoFields: true });
|
|
|
+ let index = 0;
|
|
|
+ const create_time = Date.parse(new Date()) / 1000;
|
|
|
+ let bonus;
|
|
|
+ while ((stream = await parts()) !== undefined) {
|
|
|
+ if (!stream.filename) {
|
|
|
+ throw '未发现上传文件!';
|
|
|
+ }
|
|
|
+ if (!bonus) bonus = await ctx.service.stageBonus.getStageDataById(parts.field.bonus_id);
|
|
|
+ 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'),
|
|
|
+ });
|
|
|
+ ++index;
|
|
|
+ }
|
|
|
+ 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 (error) {
|
|
|
+ ctx.helper.log(error);
|
|
|
+ // 失败需要消耗掉stream 以防卡死
|
|
|
+ if (stream) {
|
|
|
+ await sendToWormhole(stream);
|
|
|
+ }
|
|
|
+ ctx.body = this.ajaxErrorBody(error, '上传附件失败,请重试');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async downloadFile(ctx) {
|
|
|
+ const b_id = ctx.query.b_id;
|
|
|
+ const index = ctx.query.index;
|
|
|
+ if (b_id && index) {
|
|
|
+ try {
|
|
|
+ const bonus = await ctx.service.stageBonus.getStageDataById(b_id);
|
|
|
+
|
|
|
+ if (!bonus || !bonus.proof_file || !bonus.proof_file[index]) throw '下载的文件不存在';
|
|
|
+
|
|
|
+ const fileInfo = bonus.proof_file[index];
|
|
|
+ const fileName = path.join(this.app.baseDir, fileInfo.filepath);
|
|
|
+ // 解决中文无法下载问题
|
|
|
+ const userAgent = (ctx.request.header['user-agent'] || '').toLowerCase();
|
|
|
+ let disposition = '';
|
|
|
+ if (userAgent.indexOf('msie') >= 0 || userAgent.indexOf('chrome') >= 0) {
|
|
|
+ disposition = 'attachment; filename=' + encodeURIComponent(fileInfo.filename + fileInfo.fileext);
|
|
|
+ } else if (userAgent.indexOf('firefox') >= 0) {
|
|
|
+ disposition = 'attachment; filename*="utf8\'\'' + encodeURIComponent(fileInfo.filename + fileInfo.fileext) + '"';
|
|
|
+ } else {
|
|
|
+ /* safari等其他非主流浏览器只能自求多福了 */
|
|
|
+ disposition = 'attachment; filename=' + new Buffer(fileInfo.filename + fileInfo.fileext).toString('binary');
|
|
|
+ }
|
|
|
+ ctx.response.set({
|
|
|
+ 'Content-Type': 'application/octet-stream',
|
|
|
+ 'Content-Disposition': disposition,
|
|
|
+ 'Content-Length': fileInfo.filesize,
|
|
|
+ });
|
|
|
+ ctx.body = await fs.createReadStream(fileName);
|
|
|
+ } catch (err) {
|
|
|
+ this.log(err);
|
|
|
+ this.postError(err, '下载文件失败');
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ 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];
|
|
|
+ // 先删除文件
|
|
|
+ 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.postError(err, '删除文件失败');
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
return StageExtraController;
|