'use strict'; /** * * 附件 * @author Ellisran * @date 2019/1/11 * @version */ const archiver = require('archiver'); const path = require('path'); const fs = require('fs'); module.exports = app => { class ChangeAtt extends app.BaseService { /** * 构造函数 * * @param {Object} ctx - egg全局变量 * @return {void} */ constructor(ctx) { super(ctx); this.tableName = 'change_attachment'; } /** * 添加附件 * @param {Object} postData - 表单信息 * @param {Object} fileData - 文件信息 * @param {int} uid - 上传者id * @return {void} */ async save(postData, fileData, uid) { const data = { tid: postData.tid, cid: postData.cid, uid, }; Object.assign(data, fileData); const result = await this.db.insert(this.tableName, data); return result; } /** * 获取 变更令 所有附件 * @param {uuid} cid - 变更令id * @return {Promise} */ async getChangeAttachment(cid, sort = 'asc') { const sql = 'SELECT ca.*, pa.name As u_name, pa.role As u_role ' + ' FROM ?? As ca ' + ' Left Join ?? As pa ' + ' On ca.uid = pa.id ' + ' Where ca.cid = ? order by id ' + sort; const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, cid]; const result = await this.db.query(sql, sqlParam); return result.map(item => { item.orginpath = this.ctx.app.config.fujianOssPath + item.filepath; if (!this.ctx.helper.canPreview(item.fileext)) { item.filepath = `/change/download/file/${item.id}`; } else { item.filepath = this.ctx.app.config.fujianOssPath + item.filepath; } return item; }); } /** * 获取所有附件 * @param {String} cid 变更令id */ async getAllChangeFiles(cid) { const { ctx } = this; const result = await this.db.select(this.tableName, { where: { cid } }); return result.map(item => { item.orginpath = this.ctx.app.config.fujianOssPath + item.filepath; if (!ctx.helper.canPreview(item.fileext)) { item.filepath = `/change/download/file/${item.id}`; } else { // item.filepath = item.filepath.replace(/^app|\/app/, ''); item.filepath = this.ctx.config.ossPath + item.filepath; } return item; }); } /** * 将文件压缩成zip,并返回zip文件的路径 * @param {array} fileIds - 文件数组id * @param {string} zipPath - 压缩文件存储路径 * @return {string} 压缩后的zip文件路径 */ async compressedFile(fileIds, zipPath) { this.initSqlBuilder(); this.sqlBuilder.setAndWhere('id', { value: fileIds, operate: 'in', }); const [sql, sqlParam] = this.sqlBuilder.build(this.tableName); const files = await this.db.query(sql, sqlParam); return new Promise((resolve, reject) => { // 每次开一个新的archiver const ziparchiver = archiver('zip'); const outputPath = fs.createWriteStream(path.resolve(this.app.baseDir, zipPath)); outputPath.on('error', err => { return reject(err); }); ziparchiver.pipe(outputPath); files.forEach(item => { ziparchiver.file(path.resolve(this.app.baseDir, item.filepath), { name: item.filename + item.fileext }); }); // 存档警告 ziparchiver.on('warning', function(err) { // if (err.code === 'ENOENT') { // console.warn('stat故障和其他非阻塞错误'); // } return reject(err); }); // 存档出错 ziparchiver.on('error', function(err) { // console.log(err); return reject(err); }); ziparchiver.finalize(); outputPath.on('close', () => { return resolve(ziparchiver.pointer()); }); }); } /** * 返回所查询的变更令的名称 * @param {string} cid - 变更令id */ async getChangeName(cid) { const sql = 'SELECT name FROM ?? WHERE cid = ?'; const sqlParam = [this.ctx.service.change.tableName, cid]; return await this.db.queryOne(sql, sqlParam); } } return ChangeAtt; };