|  | @@ -13,6 +13,9 @@ const officeList = require('../const/cld_office').list;
 | 
	
		
			
				|  |  |  const maintainConst = require('../const/maintain');
 | 
	
		
			
				|  |  |  const typeColMap = require('../const/advance').typeColMap;
 | 
	
		
			
				|  |  |  const moment = require('moment');
 | 
	
		
			
				|  |  | +const fs = require('fs');
 | 
	
		
			
				|  |  | +const path = require('path');
 | 
	
		
			
				|  |  | +const sendToWormhole = require('stream-wormhole');
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  module.exports = app => {
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -206,12 +209,17 @@ module.exports = app => {
 | 
	
		
			
				|  |  |                  const rule = ctx.service.message.rule();
 | 
	
		
			
				|  |  |                  const jsValidator = await this.jsValidator.convert(rule).build();
 | 
	
		
			
				|  |  |                  const msgInfo = id === 0 ? {} : await ctx.service.message.getDataById(id);
 | 
	
		
			
				|  |  | +                const files = await ctx.service.messageAtt.getAtt(id);
 | 
	
		
			
				|  |  |                  const renderData = {
 | 
	
		
			
				|  |  |                      jsValidator,
 | 
	
		
			
				|  |  |                      msgInfo,
 | 
	
		
			
				|  |  | +                    files,
 | 
	
		
			
				|  |  | +                    whiteList: ctx.app.config.multipart.whitelist,
 | 
	
		
			
				|  |  | +                    moment,
 | 
	
		
			
				|  |  |                  };
 | 
	
		
			
				|  |  |                  await this.layout('dashboard/msg_add.ejs', renderData, 'dashboard/msg_modal.ejs');
 | 
	
		
			
				|  |  |              } catch (error) {
 | 
	
		
			
				|  |  | +                console.log(error);
 | 
	
		
			
				|  |  |                  // this.setMessage(error.toString(), this.messageType.ERROR);
 | 
	
		
			
				|  |  |                  ctx.redirect(ctx.request.header.referer);
 | 
	
		
			
				|  |  |              }
 | 
	
	
		
			
				|  | @@ -266,6 +274,7 @@ module.exports = app => {
 | 
	
		
			
				|  |  |                      ctx.redirect('/dashboard/msg');
 | 
	
		
			
				|  |  |                  }
 | 
	
		
			
				|  |  |              } catch (error) {
 | 
	
		
			
				|  |  | +                console.log(error);
 | 
	
		
			
				|  |  |                  ctx.redirect(ctx.request.header.referer);
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |          }
 | 
	
	
		
			
				|  | @@ -287,11 +296,12 @@ module.exports = app => {
 | 
	
		
			
				|  |  |                  if (!msgInfo || msgInfo.create_uid !== ctx.session.sessionUser.accountId) {
 | 
	
		
			
				|  |  |                      throw '通知不存在或无权限操作';
 | 
	
		
			
				|  |  |                  }
 | 
	
		
			
				|  |  | -                const result = await ctx.service.message.deleteById(msgInfo.id);
 | 
	
		
			
				|  |  | +                const result = await ctx.service.message.deleteMsg(msgInfo.id);
 | 
	
		
			
				|  |  |                  if (result) {
 | 
	
		
			
				|  |  |                      ctx.redirect('/dashboard/msg');
 | 
	
		
			
				|  |  |                  }
 | 
	
		
			
				|  |  |              } catch (error) {
 | 
	
		
			
				|  |  | +                console.log(error);
 | 
	
		
			
				|  |  |                  ctx.redirect(ctx.request.header.referer);
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |          }
 | 
	
	
		
			
				|  | @@ -310,6 +320,93 @@ module.exports = app => {
 | 
	
		
			
				|  |  |                  ctx.body = { err: 1, msg: err.toString(), data: null };
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        /**
 | 
	
		
			
				|  |  | +         * 上传附件
 | 
	
		
			
				|  |  | +         * @param {*} ctx 上下文
 | 
	
		
			
				|  |  | +         */
 | 
	
		
			
				|  |  | +        async msgUploadFile(ctx) {
 | 
	
		
			
				|  |  | +            let stream;
 | 
	
		
			
				|  |  | +            try {
 | 
	
		
			
				|  |  | +                const responseData = { err: 0, msg: '', data: {} };
 | 
	
		
			
				|  |  | +                const mid = ctx.params.id || 0;
 | 
	
		
			
				|  |  | +                if (!mid) throw '参数有误';
 | 
	
		
			
				|  |  | +                const parts = this.ctx.multipart({
 | 
	
		
			
				|  |  | +                    autoFields: true,
 | 
	
		
			
				|  |  | +                });
 | 
	
		
			
				|  |  | +                const files = [];
 | 
	
		
			
				|  |  | +                const create_time = Date.parse(new Date()) / 1000;
 | 
	
		
			
				|  |  | +                let idx = 0;
 | 
	
		
			
				|  |  | +                while ((stream = await parts()) !== undefined) {
 | 
	
		
			
				|  |  | +                    if (!stream.filename) {
 | 
	
		
			
				|  |  | +                        // 如果没有传入直接返回
 | 
	
		
			
				|  |  | +                        return;
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                    const fileInfo = path.parse(stream.filename);
 | 
	
		
			
				|  |  | +                    const filepath = `app/public/upload/message/fujian_${create_time + idx.toString() + fileInfo.ext}`;
 | 
	
		
			
				|  |  | +                    await ctx.app.fujianOss.put(ctx.app.config.fujianOssFolder + filepath, stream);
 | 
	
		
			
				|  |  | +                    files.push({ filepath, name: stream.filename, ext: fileInfo.ext });
 | 
	
		
			
				|  |  | +                    ++idx;
 | 
	
		
			
				|  |  | +                    stream && (await sendToWormhole(stream));
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +                const in_time = new Date();
 | 
	
		
			
				|  |  | +                const payload = files.map(file => {
 | 
	
		
			
				|  |  | +                    let idx;
 | 
	
		
			
				|  |  | +                    if (Array.isArray(parts.field.name)) {
 | 
	
		
			
				|  |  | +                        idx = parts.field.name.findIndex(name => name === file.name);
 | 
	
		
			
				|  |  | +                    } else {
 | 
	
		
			
				|  |  | +                        idx = 'isString';
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                    const newFile = {
 | 
	
		
			
				|  |  | +                        project_id: ctx.session.sessionProject.id,
 | 
	
		
			
				|  |  | +                        mid,
 | 
	
		
			
				|  |  | +                        uid: ctx.session.sessionUser.accountId,
 | 
	
		
			
				|  |  | +                        filename: file.name,
 | 
	
		
			
				|  |  | +                        fileext: file.ext,
 | 
	
		
			
				|  |  | +                        filesize: ctx.helper.bytesToSize(idx === 'isString' ? parts.field.size : parts.field.size[idx]),
 | 
	
		
			
				|  |  | +                        filepath: file.filepath,
 | 
	
		
			
				|  |  | +                        upload_time: in_time,
 | 
	
		
			
				|  |  | +                    };
 | 
	
		
			
				|  |  | +                    return newFile;
 | 
	
		
			
				|  |  | +                });
 | 
	
		
			
				|  |  | +                // 执行文件信息写入数据库
 | 
	
		
			
				|  |  | +                await ctx.service.messageAtt.saveFileMsgToDb(payload);
 | 
	
		
			
				|  |  | +                // 将最新的当前标段的所有文件信息返回
 | 
	
		
			
				|  |  | +                responseData.data = await ctx.service.messageAtt.getAtt(mid);
 | 
	
		
			
				|  |  | +                ctx.body = responseData;
 | 
	
		
			
				|  |  | +            } catch (err) {
 | 
	
		
			
				|  |  | +                stream && (await sendToWormhole(stream));
 | 
	
		
			
				|  |  | +                this.log(err);
 | 
	
		
			
				|  |  | +                ctx.body = { err: 1, msg: err.toString(), data: null };
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        /**
 | 
	
		
			
				|  |  | +         * 删除附件
 | 
	
		
			
				|  |  | +         * @param {Ojbect} ctx 上下文
 | 
	
		
			
				|  |  | +         */
 | 
	
		
			
				|  |  | +        async msgDeleteFile(ctx) {
 | 
	
		
			
				|  |  | +            try {
 | 
	
		
			
				|  |  | +                const mid = ctx.params.id || 0;
 | 
	
		
			
				|  |  | +                const responseData = { err: 0, msg: '', data: {} };
 | 
	
		
			
				|  |  | +                const data = JSON.parse(ctx.request.body.data);
 | 
	
		
			
				|  |  | +                const fileInfo = await ctx.service.messageAtt.getDataById(data.id);
 | 
	
		
			
				|  |  | +                if (fileInfo) {
 | 
	
		
			
				|  |  | +                    // 先删除文件
 | 
	
		
			
				|  |  | +                    // await fs.unlinkSync(path.resolve(this.app.baseDir, './app', fileInfo.filepath));
 | 
	
		
			
				|  |  | +                    await ctx.app.fujianOss.delete(ctx.app.config.fujianOssFolder + fileInfo.filepath);
 | 
	
		
			
				|  |  | +                    // 再删除数据库
 | 
	
		
			
				|  |  | +                    await ctx.service.messageAtt.delete(data.id);
 | 
	
		
			
				|  |  | +                } else {
 | 
	
		
			
				|  |  | +                    throw '不存在该文件';
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +                responseData.data = await ctx.service.messageAtt.getAtt(mid);
 | 
	
		
			
				|  |  | +                ctx.body = responseData;
 | 
	
		
			
				|  |  | +            } catch (err) {
 | 
	
		
			
				|  |  | +                this.log(err);
 | 
	
		
			
				|  |  | +                ctx.body = { err: 1, msg: err.toString(), data: null };
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      return DashboardController;
 |