瀏覽代碼

feat: 附件预览、下载逻辑改版完成

lanjianrong 4 年之前
父節點
當前提交
301ec33804

+ 16 - 12
app/controller/change_controller.js

@@ -266,7 +266,7 @@ module.exports = app => {
                 const auditStatus = await ctx.service.changeAudit.getStatusByChange(change);
 
                 // 获取附件列表
-                const attList = await ctx.service.changeAtt.getAllDataByCondition({ where: { cid: ctx.params.cid } });
+                const attList = await ctx.service.changeAtt.getAllChangeFiles(ctx.params.cid);
 
                 // 根据auditStatus获取审批人列表
                 const auditList = await ctx.service.changeAudit.getListByStatus(change, auditStatus);
@@ -626,16 +626,20 @@ module.exports = app => {
                     if (!stream.filename) {
                         throw '请选择上传的文件!';
                     }
-                    const create_time = Date.parse(new Date()) / 1000;
+                    // 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 + '_' + 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));
                     const fileInfo = path.parse(stream.filename);
-                    const dirName = 'app/public/upload/changes/' + moment().format('YYYYMMDD');
-                    const fileName = 'changes' + 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));
+                    const create_time = Date.parse(new Date()) / 1000;
+                    const filepath = `public/upload/change/fujian_${create_time + index.toString() + fileInfo.ext}`;
+                    await ctx.helper.saveStreamFile(stream, path.resolve(this.app.baseDir, 'app', filepath));
                     await sendToWormhole(stream);
                     // 保存数据到att表
                     const fileData = {
@@ -643,7 +647,7 @@ module.exports = app => {
                         filename: fileInfo.name,
                         fileext: fileInfo.ext,
                         filesize: Array.isArray(parts.field.size) ? parts.field.size[index] : parts.field.size,
-                        filepath: path.join(dirName, fileName),
+                        filepath,
                     };
                     const result = await ctx.service.changeAtt.save(parts.field, fileData, ctx.session.sessionUser.accountId);
                     if (!result) {
@@ -679,7 +683,7 @@ module.exports = app => {
                 try {
                     const fileInfo = await ctx.service.changeAtt.getDataById(id);
                     if (fileInfo !== undefined && fileInfo !== '') {
-                        const fileName = path.join(this.app.baseDir, fileInfo.filepath);
+                        const fileName = path.join(this.app.baseDir, 'app', fileInfo.filepath);
                         // 解决中文无法下载问题
                         const userAgent = (ctx.request.header['user-agent'] || '').toLowerCase();
                         let disposition = '';

+ 20 - 11
app/controller/stage_controller.js

@@ -1297,17 +1297,20 @@ module.exports = app => {
                     if (!stream.filename) {
                         throw '请选择上传的文件!';
                     }
-                    const dirName = 'app/public/upload/stage/' + moment().format('YYYYMMDD');
+                    // 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 create_time = Date.parse(new Date()) / 1000;
+                    // 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 fileName = 'stage' + create_time + '_' + index + fileInfo.ext;
-
+                    const create_time = Date.parse(new Date()) / 1000;
+                    const filepath = `public/upload/${this.ctx.tender.id}/stage/fujian_${create_time + index.toString() + fileInfo.ext}`;
+                    await ctx.helper.saveStreamFile(stream, path.resolve(this.app.baseDir, 'app', 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.join(this.app.baseDir, dirName, fileName));
+                    // await ctx.helper.saveStreamFile(stream, path.resolve(this.app.baseDir, dirName, fileName));
 
                     if (stream) {
                         await sendToWormhole(stream);
@@ -1321,7 +1324,7 @@ module.exports = app => {
                         filename: fileInfo.name,
                         fileext: fileInfo.ext,
                         filesize: Array.isArray(parts.field.size) ? parts.field.size[index] : parts.field.size,
-                        filepath: path.join(dirName, fileName),
+                        filepath,
                     };
                     if (ctx.reUploadPermission) {
                         fileData.re_upload = 1;
@@ -1360,7 +1363,7 @@ module.exports = app => {
                 try {
                     const fileInfo = await ctx.service.stageAtt.getDataById(id);
                     if (fileInfo !== undefined && fileInfo !== '') {
-                        const fileName = path.join(this.app.baseDir, fileInfo.filepath);
+                        const fileName = path.join(this.app.baseDir, 'app', fileInfo.filepath);
                         // 解决中文无法下载问题
                         const userAgent = (ctx.request.header['user-agent'] || '').toLowerCase();
                         let disposition = '';
@@ -1400,7 +1403,13 @@ module.exports = app => {
                 try {
                     const fileInfo = await ctx.service.stageAtt.getDataById(id);
                     if (fileInfo && Object.keys(fileInfo).length) {
-                        fileInfo.filepath && (responseData.data = { filepath: fileInfo.filepath.slice(3) });
+                        let filepath = fileInfo.filepath;
+                        if (!ctx.helper.canPreview(fileInfo.fileext)) {
+                            filepath = `/tender/${ctx.tender.id}/measure/stage/${ctx.params.order}/download/file/${fileInfo.id}`;
+                        } else {
+                            filepath = '/' + filepath;
+                        }
+                        fileInfo.filepath && (responseData.data = { filepath });
                     }
                 } catch (error) {
                     this.log(error);

+ 1 - 0
app/extend/helper.js

@@ -548,6 +548,7 @@ module.exports = {
         // 检查文件夹是否存在,不存在则直接创建文件夹
         const pathName = path.dirname(fileName);
         if (!fs.existsSync(pathName)) {
+            console.log('11111');
             await this.recursiveMkdirSync(pathName);
         }
         await fs.writeFileSync(fileName, buffer);

+ 18 - 0
app/service/change_att.js

@@ -53,6 +53,24 @@ module.exports = app => {
             const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, cid];
             return await this.db.query(sql, sqlParam);
         }
+
+        /**
+         * 获取所有附件
+         * @param {String} cid 变更令id
+         */
+        async getAllChangeFiles(cid) {
+            const { ctx } = this;
+            const result = await this.db.select(this.tableName, { where: { cid } });
+            return result.map(item => {
+                if (!ctx.helper.canPreview(item.fileext)) {
+                    item.filepath = `/change/download/file/${item.id}`;
+                } else {
+                    item.filepath = '/' + item.filepath;
+                }
+                console.log('item', item);
+                return item;
+            });
+        }
     }
 
     return ChangeAtt;

+ 17 - 4
app/service/stage_att.js

@@ -62,12 +62,21 @@ module.exports = app => {
          * @return {void}
          */
         async getDataByTenderIdAndStageId(tid, sid) {
-            const sql = 'SELECT att.id, att.lid, att.uid, att.filename, att.fileext, att.filesize, att.re_upload, att.remark, att.in_time,' +
+            const { ctx } = this;
+            const sql = 'SELECT att.id, att.lid, att.uid, att.filepath, att.filename, att.fileext, att.filesize, att.re_upload, att.remark, att.in_time,' +
                 ' pa.name as `username`, leg.name as `lname`, leg.code as `code`, leg.ledger_id as `ledger_id`, leg.b_code as `b_code`' +
                 ' FROM ?? AS att,?? AS pa,?? AS leg' +
                 ' WHERE leg.id = att.lid AND pa.id = att.uid AND att.tid = ? AND att.sid = ? ORDER BY att.id DESC';
             const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, this.ctx.service.ledger.tableName, tid, sid];
-            return await this.db.query(sql, sqlParam);
+            const result = await this.db.query(sql, sqlParam);
+            return result.map(item => {
+                if (!ctx.helper.canPreview(item.fileext)) {
+                    item.filepath = `/tender/${ctx.tender.id}/measure/stage/${ctx.params.order}/download/file/${item.id}`;
+                } else {
+                    item.filepath = '/' + item.filepath;
+                }
+                return item;
+            });
         }
 
         /**
@@ -77,12 +86,16 @@ module.exports = app => {
          * @return {void}
          */
         async getDataByFid(id) {
-            const sql = 'SELECT att.id, att.lid, att.uid, att.filename, att.re_upload, att.fileext, att.filesize, att.remark, att.in_time,' +
+            const { ctx } = this;
+            const sql = 'SELECT att.id, att.lid, att.uid, att.filepath, att.filename, att.re_upload, att.fileext, att.filesize, att.remark, att.in_time,' +
                 ' pa.name as `username`, leg.name as `lname`, leg.code as `code`, leg.ledger_id as `ledger_id`,leg.b_code as `b_code`' +
                 ' FROM ?? AS att,?? AS pa,?? AS leg' +
                 ' WHERE leg.id = att.lid AND pa.id = att.uid AND att.id = ? ORDER BY att.in_time DESC';
             const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, this.ctx.service.ledger.tableName, id];
-            return await this.db.queryOne(sql, sqlParam);
+            const result = await this.db.queryOne(sql, sqlParam);
+            if (!ctx.helper.canPreview(result.fileext)) result.filepath = `/tender/${ctx.tender.id}/measure/stage/${ctx.params.order}/download/file/${result.id}`;
+            else result.filepath = '/' + result.filepath;
+            return result;
         }
     }
 

+ 1 - 1
app/view/change/info.ejs

@@ -717,7 +717,7 @@
                 <% for (const [index,att] of attList.entries()) { %>
                 <tr>
                     <td><%- index+1 %></td>
-                    <td><a href="/change/download/file/<%- att.id %>"><%- att.filename %><%- att.fileext %></a></td>
+                    <td><a href="<%- att.filepath %>" target="_black"><%- att.filename %><%- att.fileext %></a></td>
                     <td><%- ctx.helper.bytesToSize(att.filesize) %></td>
                     <td><%- moment(att.in_time * 1000).format('YYYY-MM-DD') %></td>
                     <td>

+ 0 - 1
app/view/measure/stage_modal.ejs

@@ -126,7 +126,6 @@
     $('#audit-list').on('click', 'a', function() {
     const type = $(this).data('target')
     const auditCard = $(this).parent().parent()
-    console.log('auditCard', auditCard)
     if (type === 'show') {
         $(this).data('target', 'hide')
         auditCard.find('.fold-card').slideDown('swing', () => {

+ 1 - 1
app/view/stage/audit_modal.ejs

@@ -795,7 +795,7 @@
 </script>
 <% if (ctx.session.sessionUser.accountId === ctx.stage.user_id && (ctx.stage.status === auditConst.status.uncheck || ctx.stage.status === auditConst.status.checkNo)) { %>
 <script>
-    const accountList = JSON.parse('<%- JSON.stringify(accountList ) %>');
+    // const accountList = JSON.parse('<%- JSON.stringify(accountList ) %>');
     const accountGroup = JSON.parse('<%- JSON.stringify(accountGroup  ) %>');
 </script>
 <% } %>