Browse Source

feat: 材料调差附件下载功能改版

lanjianrong 4 years ago
parent
commit
69d08d1b72
4 changed files with 54 additions and 2 deletions
  1. 43 1
      app/controller/material_controller.js
  2. 1 0
      app/router.js
  3. 8 1
      app/service/material_file.js
  4. 2 0
      sql/update.sql

+ 43 - 1
app/controller/material_controller.js

@@ -739,7 +739,7 @@ module.exports = app => {
                     // const filepath = path.join('public/upload', this.ctx.tender.id.toString(), 'tc', 'fujian_' + create_time + fileInfo.ext);
                     const filepath = `public/upload/${this.ctx.tender.id.toString()}/tc/fujian_${create_time + idx.toString() + fileInfo.ext}`;
                     await ctx.helper.saveStreamFile(stream, path.resolve(this.app.baseDir, 'app', filepath));
-                    files.push({ filepath, name: stream.filename });
+                    files.push({ filepath, name: stream.filename, ext: fileInfo.ext });
                     ++idx;
                 }
                 const upload_time = this.ctx.helper.dateTran(new Date());
@@ -759,6 +759,7 @@ module.exports = app => {
                         filepath: file.filepath,
                         file_size: ctx.helper.bytesToSize(idx === 'isString' ? parts.field.size : parts.field.size[idx]),
                         file_name: file.name,
+                        fileext: file.ext,
                     };
                     return newFile;
                 });
@@ -773,6 +774,47 @@ 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.materialFile.getDataById(id);
+                    if (fileInfo !== undefined && fileInfo !== '') {
+                        const fileName = path.join(__dirname, '../', 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.file_name);
+                        } else if (userAgent.indexOf('firefox') >= 0) {
+                            disposition = 'attachment; filename*="utf8\'\'' + encodeURIComponent(fileInfo.file_name) + '"';
+                        } else {
+                            /* safari等其他非主流浏览器只能自求多福了 */
+                            disposition = 'attachment; filename=' + new Buffer(fileInfo.file_name).toString('binary');
+                        }
+                        ctx.response.set({
+                            'Content-Type': 'application/octet-stream',
+                            'Content-Disposition': disposition,
+                            'Content-Length': fileInfo.file_size,
+                        });
+                        ctx.body = await fs.createReadStream(fileName);
+                    } else {
+                        throw '不存在该文件';
+                    }
+                } catch (err) {
+                    this.log(err);
+                    this.setMessage(err.toString(), this.messageType.ERROR);
+                }
+            }
+        }
+
+
         /**
          * 查看当前标段以往所有期的附件
          * @param {Object} ctx 上下文

+ 1 - 0
app/router.js

@@ -336,6 +336,7 @@ module.exports = app => {
     // 附件
     app.get('/tender/:id/measure/material/:order/file', sessionAuth, tenderCheck, materialCheck, 'materialController.file');
     app.post('/tender/:id/measure/material/:order/file/upload', sessionAuth, tenderCheck, materialCheck, 'materialController.upload');
+    app.get('/tender/:id/measure/material/:order/file/:fid/download', sessionAuth, tenderCheck, 'materialController.downloadFile');
     app.post('/tender/:id/measure/material/:order/file/find', sessionAuth, tenderCheck, materialCheck, 'materialController.getCurMatericalFiles');
     app.post('/tender/measure/material/file/delete', sessionAuth, 'materialController.deleteFile');
 

+ 8 - 1
app/service/material_file.js

@@ -27,12 +27,19 @@ module.exports = app => {
          * @return {Promise<void>} 数据库查询实例
          */
         async getAllMaterialFiles(tid, mid) {
+            const { ctx } = this;
             const where = { tid };
             if (mid) where.mid = mid;
-            return await this.db.select(this.tableName, {
+            const result = await this.db.select(this.tableName, {
                 where,
                 orders: [['upload_time', 'desc']],
             });
+            return result.map(item => {
+                if (!ctx.helper.canPreview(item.fileext)) {
+                    item.filepath = `tender/${ctx.tender.id}/measure/material/${item.s_order}/file/${item.id}/download`;
+                }
+                return item;
+            });
         }
 
 

+ 2 - 0
sql/update.sql

@@ -112,3 +112,5 @@ CREATE TABLE `zh_material_month` (
 ALTER TABLE `zh_material` ADD `months` VARCHAR(1000) NULL DEFAULT NULL COMMENT '月信息价-年月份列表' AFTER `s_order`;
 
 
+ALTER TABLE `calculation`.`zh_material_file`
+ADD COLUMN `fileext` VARCHAR(45) NULL COMMENT '文件类型' AFTER `s_order`;