|
@@ -9,10 +9,12 @@
|
|
|
*/
|
|
|
|
|
|
const moment = require('moment');
|
|
|
+const fs = require('fs');
|
|
|
+const path = require('path');
|
|
|
const audit = require('../const/audit');
|
|
|
const codeRuleConst = require('../const/code_rule');
|
|
|
const changeConst = require('../const/change');
|
|
|
-const tenderMenu = require('../../config/menu').tenderMenu;
|
|
|
+// const tenderMenu = require('../../config/menu').tenderMenu;
|
|
|
|
|
|
module.exports = app => {
|
|
|
class ChangeController extends app.BaseController {
|
|
@@ -109,6 +111,7 @@ module.exports = app => {
|
|
|
*/
|
|
|
async index(ctx) {
|
|
|
try {
|
|
|
+ console.log(ctx);
|
|
|
await this._filterChanges(ctx);
|
|
|
} catch (err) {
|
|
|
this.log(err);
|
|
@@ -213,6 +216,7 @@ module.exports = app => {
|
|
|
*/
|
|
|
async info(ctx) {
|
|
|
try {
|
|
|
+ const whiteList = this.ctx.app.config.multipart.whitelist;
|
|
|
const tid = ctx.session.sessionUser.tenderId !== undefined ? ctx.session.sessionUser.tenderId : ctx.params.id;
|
|
|
const tender = await this.service.tender.getDataById(tid);
|
|
|
const change = await ctx.service.change.getDataByCondition({cid: ctx.params.cid});
|
|
@@ -223,6 +227,9 @@ module.exports = app => {
|
|
|
// 获取公司列表
|
|
|
const companyList = await ctx.service.changeCompany.getAllDataByCondition({ tid: tid });
|
|
|
|
|
|
+ // 获取附件列表
|
|
|
+ const attList = await ctx.service.changeAtt.getAllDataByCondition({ cid: ctx.params.cid });
|
|
|
+
|
|
|
const renderData = {
|
|
|
uid: ctx.session.sessionUser.accountId,
|
|
|
tender,
|
|
@@ -231,6 +238,8 @@ module.exports = app => {
|
|
|
auditStatus,
|
|
|
auditConst: audit.flow,
|
|
|
companyList,
|
|
|
+ attList,
|
|
|
+ whiteList,
|
|
|
};
|
|
|
await this.layout('change/info.ejs', renderData, 'change/info_modal.ejs');
|
|
|
} catch (err) {
|
|
@@ -314,6 +323,124 @@ module.exports = app => {
|
|
|
|
|
|
ctx.body = responseData;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 上传附件
|
|
|
+ * @param ctx
|
|
|
+ * @return {Promise<void>}
|
|
|
+ */
|
|
|
+ async uploadFile(ctx) {
|
|
|
+ const responseData = {
|
|
|
+ err: 0,
|
|
|
+ msg: '',
|
|
|
+ data: [],
|
|
|
+ };
|
|
|
+ let stream;
|
|
|
+ try {
|
|
|
+ stream = await ctx.getFileStream();
|
|
|
+ const create_time = Date.parse(new Date()) / 1000;
|
|
|
+ const fileInfo = path.parse(stream.filename);
|
|
|
+ const dirName = 'app/public/upload/changes/' + moment().format('YYYYMMDD');
|
|
|
+ const fileName = 'changes' + 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.saveStreamFile(stream, path.join(this.app.baseDir, dirName, fileName));
|
|
|
+ // 保存数据到att表
|
|
|
+ let fileData = {
|
|
|
+ in_time : create_time,
|
|
|
+ filename: fileInfo.name,
|
|
|
+ fileext: fileInfo.ext,
|
|
|
+ filesize: stream.fields.size,
|
|
|
+ filepath: path.join(dirName, fileName),
|
|
|
+ };
|
|
|
+ const result = await ctx.service.changeAtt.save(stream.fields, fileData, ctx.session.sessionUser.accountId);
|
|
|
+ if (!(result.effectRows = 1)) {
|
|
|
+ throw '导入数据库保存失败';
|
|
|
+ }
|
|
|
+ fileData.in_time = moment(create_time * 1000).format('YYYY-MM-DD');
|
|
|
+ fileData.filesize = await ctx.helper.bytesToSize(stream.fields.size);
|
|
|
+ fileData.id = result.insertId;
|
|
|
+ responseData.data = fileData;
|
|
|
+ } catch (err) {
|
|
|
+ this.log(err);
|
|
|
+ // 失败需要消耗掉stream 以防卡死
|
|
|
+ if (stream) {
|
|
|
+ await sendToWormhole(stream);
|
|
|
+ }
|
|
|
+ this.setMessage(err.toString(), this.messageType.ERROR);
|
|
|
+ }
|
|
|
+ ctx.body = responseData;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 下载附件
|
|
|
+ * @param ctx
|
|
|
+ * @return {Promise<void>}
|
|
|
+ */
|
|
|
+ async downloadFile(ctx) {
|
|
|
+ const id = ctx.params.id;
|
|
|
+ if (id) {
|
|
|
+ try {
|
|
|
+ const fileInfo = await ctx.service.changeAtt.getDataById(id);
|
|
|
+ if (fileInfo !== undefined && fileInfo !== '') {
|
|
|
+ const fileName = path.join(this.app.baseDir, fileInfo.filepath);
|
|
|
+ ctx.response.set({
|
|
|
+ 'Content-Type': 'application/octet-stream',
|
|
|
+ 'Content-Disposition': `attachment; filename=` + fileInfo.filename + fileInfo.fileext,
|
|
|
+ 'Content-Length': fileInfo.filesize
|
|
|
+ });
|
|
|
+ ctx.body = await fs.createReadStream(fileName);
|
|
|
+ } else {
|
|
|
+ throw '不存在该文件';
|
|
|
+ }
|
|
|
+ } catch (err) {
|
|
|
+ this.log(err);
|
|
|
+ this.setMessage(err.toString(), this.messageType.ERROR);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除附件
|
|
|
+ * @param ctx
|
|
|
+ * @return {Promise<void>}
|
|
|
+ */
|
|
|
+ async deleteFile(ctx) {
|
|
|
+ const responseData = {
|
|
|
+ err: 0,
|
|
|
+ msg: '',
|
|
|
+ data: '',
|
|
|
+ };
|
|
|
+ try {
|
|
|
+ const data = JSON.parse(ctx.request.body.data);
|
|
|
+ console.log(data.id);
|
|
|
+ const fileInfo = await ctx.service.changeAtt.getDataById(data.id);
|
|
|
+ if (fileInfo !== undefined && fileInfo !== '') {
|
|
|
+ // 先删除文件
|
|
|
+ await fs.unlinkSync(path.join(this.app.baseDir, fileInfo.filepath));
|
|
|
+ // 再删除数据库
|
|
|
+ await ctx.service.changeAtt.deleteById(data.id);
|
|
|
+ responseData.data = '';
|
|
|
+ } else {
|
|
|
+ throw '不存在该文件';
|
|
|
+ }
|
|
|
+
|
|
|
+ // if (data.tid === undefined || data.uci === undefined || data.uc === undefined || data.ac === undefined) {
|
|
|
+ // throw '参数有误';
|
|
|
+ // }
|
|
|
+ // const [addCompany, selectCompany] = await ctx.service.changeCompany.setCompanyList(data);
|
|
|
+ // responseData.data = { add: addCompany, select: selectCompany };
|
|
|
+ } catch (err) {
|
|
|
+ responseData.err = 1;
|
|
|
+ responseData.msg = err;
|
|
|
+ }
|
|
|
+
|
|
|
+ ctx.body = responseData;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
return ChangeController;
|