Browse Source

fix: 修复变更附件下载出现的路径匹配不一致的bug

lanjianrong 4 years ago
parent
commit
433d81d7ef
4 changed files with 44 additions and 5 deletions
  1. 31 1
      app/controller/change_controller.js
  2. 10 2
      app/public/js/change_detail.js
  3. 1 0
      app/router.js
  4. 2 2
      app/view/change/info.ejs

+ 31 - 1
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.getAllChangeFiles(ctx.params.cid);
+                const attList = await ctx.service.changeAtt.getChangeAttachment(ctx.params.cid);
 
                 // 根据auditStatus获取审批人列表
                 const auditList = await ctx.service.changeAudit.getListByStatus(change, auditStatus);
@@ -749,6 +749,36 @@ module.exports = app => {
         }
 
         /**
+         * 查看附件
+         * @param {Object} ctx - egg全局变量
+         * @return {void}
+         */
+        async checkFile(ctx) {
+            const responseData = { err: 0, msg: '' };
+            const id = parseInt(ctx.params.id);
+            if (id) {
+                try {
+                    const fileInfo = await ctx.service.changeAtt.getDataById(id);
+                    if (fileInfo && Object.keys(fileInfo).length) {
+                        let filepath = fileInfo.filepath;
+                        if (!ctx.helper.canPreview(fileInfo.fileext)) {
+                            filepath = `/change/download/file/${fileInfo.id}`;
+                        } else {
+                            filepath = filepath.replace(/^app|\/app/, '');
+                        }
+                        fileInfo.filepath && (responseData.data = { filepath });
+                    }
+                } catch (error) {
+                    this.log(error);
+                    this.setMessage(error.toString(), this.messageType.ERROR);
+                    responseData.err = 1;
+                    responseData.msg = error.toString();
+                }
+            }
+            ctx.body = responseData;
+        }
+
+        /**
          * 删除变更令
          * @param {Object} ctx - egg全局变量
          * @return {void}

+ 10 - 2
app/public/js/change_detail.js

@@ -81,7 +81,7 @@ $(document).ready(() => {
             for (const fileInfo of data) {
                 html += '<tr> ' +
                     '<td>' + index + '</td> ' +
-                    '<td><a href="/change/download/file/' + fileInfo.id + '">' + fileInfo.filename + fileInfo.fileext + '</a></td> ' +
+                    `<td><a href="javascript: void(0);" class="file-atn" f-id="${fileInfo.id}">${fileInfo.filename}${fileInfo.fileext}</a></td>`+
                     '<td>' + fileInfo.filesize + '</td> ' +
                     '<td>' + fileInfo.in_time + '</td> ' +
                     '<td> <a class="btn btn-light btn-sm delete-file" data-attid="' + fileInfo.id + '"  title="删除附件"><span class="fa fa-trash text-danger"></span></a> </td> ' +
@@ -112,7 +112,15 @@ $(document).ready(() => {
             });
         });
     });
-
+    // /change/download/file/
+    $('#attList').on('click', '.file-atn', function() {
+        const id = $(this).attr('f-id')
+        postData(`/change/download/file/${id}`, {}, (data) => {
+            const { filepath } = data
+            $('#file-upload').attr('href', filepath)
+            $('#file-upload')[0].click()
+        })
+    })
     //
     const cca = getLocalCache('change-checkbox-account-' + accountId);
     if (cca !== null && cca !== undefined) {

+ 1 - 0
app/router.js

@@ -300,6 +300,7 @@ module.exports = app => {
     app.get('/tender/:id/change/:cid/info', sessionAuth, tenderCheck, 'changeController.info');
     app.post('/change/upload/file', sessionAuth, 'changeController.uploadFile');
     app.get('/change/download/file/:id', sessionAuth, 'changeController.downloadFile');
+    app.post('/change/download/file/:id', sessionAuth, 'changeController.checkFile');
     app.post('/change/delete/file', sessionAuth, 'changeController.deleteFile');
     app.post('/tender/:id/change/delete', sessionAuth, tenderCheck, 'changeController.delete');
     app.post('/tender/:id/change/bills', sessionAuth, tenderCheck, 'changeController.bills');

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

@@ -699,7 +699,7 @@
             <% } %>
             </div>
         </div>
-
+        <a href="" id="file-upload" target="_blank" style="display: none;"></a>
         <div class="c-body tab-pane" role="tabpanel" id="files">
             <div class="sjs-height-0">
             <table class="table table-bordered">
@@ -717,7 +717,7 @@
                 <% for (const [index,att] of attList.entries()) { %>
                 <tr>
                     <td><%- index+1 %></td>
-                    <td><a href="<%- att.filepath %>" target="_black"><%- att.filename %><%- att.fileext %></a></td>
+                    <td><a href="javascript: void(0);" class="file-atn" f-id="<%- att.id %>"><%- 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>