Selaa lähdekoodia

feat: 新增预付款附件预览|下载功能

lanjianrong 4 vuotta sitten
vanhempi
commit
aaf0c53954

+ 41 - 1
app/controller/advance_controller.js

@@ -336,7 +336,7 @@ module.exports = app => {
                     const fileInfo = path.parse(stream.filename);
                     const filepath = `public/upload/${this.ctx.tender.id.toString()}/yfk/fujian_${create_time + idx.toString() + fileInfo.ext}`;
                     await ctx.helper.saveStreamFile(stream, path.resolve(this.app.baseDir, 'app', filepath));
-                    files.push({ filepath, name: `fujian_${create_time + idx.toString() + fileInfo.ext}` });
+                    files.push({ filepath, name: stream.filename, ext: fileInfo.ext });
                     ++idx;
                     stream && (await sendToWormhole(stream));
                 }
@@ -355,6 +355,7 @@ module.exports = app => {
                         filepath: file.filepath,
                         filesize: ctx.helper.bytesToSize(idx === 'isString' ? parts.field.size : parts.field.size[idx]),
                         filename: file.name,
+                        fileext: file.ext,
                     };
                     return newFile;
                 });
@@ -393,6 +394,45 @@ module.exports = app => {
                 ctx.body = { err: 1, msg: err.toString(), data: null };
             }
         }
+
+        /**
+         * 下载附件
+         * @param {Object} ctx - egg全局变量
+         * @return {void}
+         */
+        async downloadFile(ctx) {
+            const id = ctx.params.fid;
+            if (id) {
+                try {
+                    const fileInfo = await ctx.service.advanceFile.getDataById(id);
+                    if (fileInfo !== undefined && fileInfo !== '') {
+                        const fileName = path.join(this.app.baseDir, 'app', 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);
+                        } else if (userAgent.indexOf('firefox') >= 0) {
+                            disposition = 'attachment; filename*="utf8\'\'' + encodeURIComponent(fileInfo.filename) + '"';
+                        } else {
+                            /* safari等其他非主流浏览器只能自求多福了 */
+                            disposition = 'attachment; filename=' + new Buffer(fileInfo.filename).toString('binary');
+                        }
+                        ctx.response.set({
+                            'Content-Type': 'application/octet-stream',
+                            'Content-Disposition': disposition,
+                            'Content-Length': fileInfo.filesize,
+                        });
+                        ctx.body = await fs.createReadStream(fileName);
+                    } else {
+                        throw '不存在该文件';
+                    }
+                } catch (err) {
+                    this.log(err);
+                    this.setMessage(err.toString(), this.messageType.ERROR);
+                }
+            }
+        }
     }
     return advanceController;
 };

+ 10 - 0
app/extend/helper.js

@@ -1198,4 +1198,14 @@ module.exports = {
             }
         }
     },
+
+    /**
+     * 匹配图片、pdf(用于预览)
+     * @param {String} ext 后缀名
+     * @return {Boolean} 匹配结果
+     */
+    canPreview(ext) {
+        const reg = /(.png)|(.gif)|(.txt)|(.jpg)|(.jpeg)|(.pdf)/;
+        return reg.test(ext);
+    },
 };

+ 1 - 0
app/router.js

@@ -119,6 +119,7 @@ module.exports = app => {
     app.post('/tender/:id/advance/:order/audit/check', sessionAuth, tenderCheck, advanceCheck, 'advanceController.checkAudit');
     app.post('/tender/:id/advance/:order/update', sessionAuth, tenderCheck, advanceCheck, 'advanceController.update');
     app.post('/tender/:id/advance/:order/file/upload', sessionAuth, tenderCheck, advanceCheck, 'advanceController.upload');
+    app.get('/tender/:id/advance/:order/file/:fid/download', sessionAuth, tenderCheck, 'advanceController.downloadFile');
     app.post('/tender/:id/advance/:order/file/del', sessionAuth, tenderCheck, advanceCheck, 'advanceController.deleteFile');
 
     // 标段协作办公

+ 1 - 0
app/service/advance.js

@@ -31,6 +31,7 @@ module.exports = app => {
                 if (item.status === auditConst.status.checkNoPre) {
                     item.curAuditor2 = await this.ctx.service.advanceAudit.getAuditorByStatus(item.id, auditConst.status.checking);
                 }
+                item.fileList = await this.ctx.service.advanceFile.getAdvanceFiles({ vid: item.id });
             }
             return advance;
         }

+ 9 - 1
app/service/advance_file.js

@@ -24,7 +24,15 @@ module.exports = app => {
          * @param {*} payload 载荷|查询条件
          */
         async getAdvanceFiles(payload) {
-            return this.db.select(this.tableName, payload);
+            const { ctx } = this;
+            const result = await this.db.select(this.tableName, { where: payload });
+            const list = result.map(item => {
+                if (!ctx.helper.canPreview(item.fileext)) {
+                    item.filepath = `tender/${ctx.tender.id}/advance/${item.vid}/file/${item.id}/download`;
+                }
+                return item;
+            });
+            return list;
         }
         /**
          * 存储上传的文件信息至数据库