Browse Source

资料归集,编辑名称

MaiXinRong 2 years ago
parent
commit
f87cc0ef29
4 changed files with 68 additions and 7 deletions
  1. 11 0
      app/controller/file_controller.js
  2. 44 6
      app/public/js/file_detail.js
  3. 1 0
      app/router.js
  4. 12 1
      app/service/file.js

+ 11 - 0
app/controller/file_controller.js

@@ -205,6 +205,17 @@ module.exports = app => {
                 ctx.ajaxErrorBody(error, '删除附件失败');
             }
         }
+        async saveFile(ctx) {
+            try {
+                const data = JSON.parse(ctx.request.body.data);
+                if (!data.id) throw '缺少参数';
+                const result = await ctx.service.file.saveFile(data.id, data.filename);
+                ctx.body = { err: 0, msg: '', data: result };
+            } catch (error) {
+                this.log(error);
+                ctx.ajaxErrorBody(error, '编辑附件失败');
+            }
+        }
 
         async loadValidRelaTender(ctx) {
             try {

+ 44 - 6
app/public/js/file_detail.js

@@ -47,13 +47,13 @@ $(document).ready(function() {
         }
         _getFileHtml(file) {
             const html = [];
-            html.push('<tr>');
-            html.push(`<tr><td><input type="checkbox" name="bd-check" fid="${file.id}"></td>`);
-            const editHtml = file.canEdit ? '<a href="javascript: void(0);" class="mr-1"><i class="fa fa-pencil fa-fw"></i></a>' : '';
+            html.push(`<tr fid="${file.id}">`);
+            html.push(`<td><input type="checkbox" name="bd-check" fid="${file.id}"></td>`);
+            const editHtml = file.canEdit ? `<a href="javascript: void(0);" class="mr-1" name="edit-file" fid="${file.id}"><i class="fa fa-pencil fa-fw"></i></a>` : '';
             const viewHtml = file.viewpath ? `<a href="${file.viewpath}" class="mr-1" target="_blank"><i class="fa fa-eye fa-fw"></i></a>` : '';
             const downHtml = `<a href="javascript: void(0);" onclick="AliOss.downloadFile('${file.filepath}', '${file.filename + file.fileext}')" class="mr-1"><i class="fa fa-download fa-fw"></i></a>`;
-            const delHtml = file.canEdit ? '<a href="javascript: void(0);" class="mr-1"><i class="fa fa-trash-o fa-fw"></i></a>' : '';
-            html.push(`<td><div class="d-flex justify-content-between align-items-center table-file"><div>${file.filename}${file.fileext}</div><div class="btn-group-table" style="display: none;">${editHtml}${viewHtml}${downHtml}${delHtml}</div></div></td>`);
+            const delHtml = file.canEdit ? `<a href="javascript: void(0);" class="mr-1" name="del-file" fid="${file.id}"><i class="fa fa-trash-o fa-fw"></i></a>` : '';
+            html.push(`<td><div class="d-flex justify-content-between align-items-center table-file"><div name="filename" fid="${file.id}">${file.filename}${file.fileext}</div><div class="btn-group-table" style="display: none;">${editHtml}${viewHtml}${downHtml}${delHtml}</div></div></td>`);
             html.push(`<td>${file.user_name}</td>`);
             html.push(`<td>${moment(file.create_time).format('YYYY-MM-DD HH:mm:ss')}</td>`);
             html.push(`<td>${file.fileext_str}</td>`);
@@ -63,7 +63,10 @@ $(document).ready(function() {
         refreshFilesTable() {
             const html = [];
             const files = this.curFiling.source_node.files;
-            if (!files || files.length === 0) return;
+            if (!files || files.length === 0) {
+                $('#file-list').html('');
+                return;
+            }
 
             const startIndex = (this.curPage - 1)*this.pageCount;
             const endIndex = this.curPage*this.pageCount;
@@ -193,6 +196,24 @@ $(document).ready(function() {
                 if (callback) callback();
             });
         }
+        renameFile(fileId, filename) {
+            const file = filingObj.curFiling.source_node.files.find(x => { return x.id === fileId });
+            if (!file) return;
+
+            const filenameDiv = $(`[name=filename][fid=${fileId}]`);
+            if (filename === file.filename + file.fileext) {
+                filenameDiv.html(file.filename + file.fileext);
+                return;
+            }
+
+            postData('file/save', { id: fileId, filename }, function(data) {
+                file.filename = data.filename;
+                file.fileext = data.fileext;
+                filenameDiv.html(file.filename + file.fileext);
+            }, function() {
+                filenameDiv.html(file.filename + file.fileext);
+            });
+        }
         relaFiles(files, callback) {
             postData('file/rela', { filing_id: this.curFiling.id, files: files }, async function(data) {
                 filingObj.curFiling.source_node.files.unshift(...data.files);
@@ -378,6 +399,23 @@ $(document).ready(function() {
     $('body').on('mouseleave', ".table-file", function(){
         $(this).children(".btn-group-table").css("display","none");
     });
+    $('body').on('click', "a[name=del-file]", function() {
+        const del = [this.getAttribute('fid')];
+        filingObj.delFiles(del);
+    });
+    $('body').on('click', "a[name=edit-file]", function() {
+        const id = this.getAttribute('fid');
+        const filename = $(`[name=filename][fid=${id}]`);
+        filename.html(`<input type="text" class="form-control form-control-sm" maxlength="100" value="${filename[0].innerHTML}" fid="${id}">`);
+    });
+    $('body').on('blur', "input[fid]", function() {
+        filingObj.renameFile(this.getAttribute('fid'), this.value);
+    });
+    $('body').on('keypress', "input[fid]", function(e) {
+        if (e.keyCode == 13) {
+            filingObj.renameFile(this.getAttribute('fid'), this.value);
+        }
+    });
     $('.page-select').click(function() {
         const content = this.getAttribute('content');
         switch(content) {

+ 1 - 0
app/router.js

@@ -728,6 +728,7 @@ module.exports = app => {
     app.post('/sp/:id/file/load', sessionAuth, subProjectCheck, 'fileController.loadFile');
     app.post('/sp/:id/file/upload', sessionAuth, subProjectCheck, 'fileController.uploadFile');
     app.post('/sp/:id/file/del', sessionAuth, subProjectCheck, 'fileController.delFile');
+    app.post('/sp/:id/file/save', sessionAuth, subProjectCheck, 'fileController.saveFile');
     app.post('/sp/:id/file/rela', sessionAuth, subProjectCheck, 'fileController.relaFile');
     app.post('/sp/:id/file/rela/tender', sessionAuth, subProjectCheck, 'fileController.loadValidRelaTender');
     app.post('/sp/:id/file/rela/files', sessionAuth, subProjectCheck, 'fileController.loadRelaFiles');

+ 12 - 1
app/service/file.js

@@ -8,7 +8,7 @@
  * @version
  */
 
-
+const path = require('path');
 
 module.exports = app => {
     class Filing extends app.BaseService {
@@ -117,6 +117,17 @@ module.exports = app => {
             result.files = await this.getFiles({ where: result.files });
             return result;
         }
+
+        async saveFile(id, filename){
+            const file = await this.getDataById(id);
+            if (!file) throw '文件不存在';
+            if (file.user_id !== this.ctx.session.sessionUser.accountId) throw '您无权编辑该文件';
+
+            const info = path.parse(filename);
+            const updateData = { id, filename: info.name, fileext: info.ext};
+            await this.defaultUpdate(updateData);
+            return updateData;
+        }
     }
 
     return Filing;