浏览代码

资料归集,选择文件后,检查文件是否存在同名

MaiXinRong 1 年之前
父节点
当前提交
aeeb8e010f
共有 5 个文件被更改,包括 70 次插入0 次删除
  1. 12 0
      app/controller/file_controller.js
  2. 47 0
      app/public/js/file_detail.js
  3. 1 0
      app/router.js
  4. 6 0
      app/service/file.js
  5. 4 0
      app/view/file/file_modal.ejs

+ 12 - 0
app/controller/file_controller.js

@@ -158,6 +158,18 @@ module.exports = app => {
             if (child) throw '该分类下存在子分类,请在子分类下上传、导入文件';
             if (child) throw '该分类下存在子分类,请在子分类下上传、导入文件';
         }
         }
 
 
+        async checkFiles(ctx) {
+            try{
+                const data = JSON.parse(ctx.request.body.data);
+                if (!data.filing_id || !data.files) throw '缺少参数';
+                const result = await ctx.service.file.checkFiles(data.filing_id, data.files);
+                ctx.body = { err: 0, msg: '', data: result };
+            } catch(error) {
+                this.log(error);
+                ctx.ajaxErrorBody(error, '检查附件错误');
+            }
+        }
+
         async uploadFile(ctx){
         async uploadFile(ctx){
             let stream;
             let stream;
             try {
             try {

+ 47 - 0
app/public/js/file_detail.js

@@ -220,6 +220,16 @@ $(document).ready(function() {
                 parent = parent.getParentNode();
                 parent = parent.getParentNode();
             }
             }
         }
         }
+        checkFilesExist(files, callback) {
+            const data = [];
+            for (const file of files) {
+                if (file === undefined) return [];
+                data.push(file.name);
+            }
+            postData('file/check', { filing_id: filingObj.curFiling.id, files: data }, function(result) {
+                callback(result);
+            });
+        }
         uploadFiles(files, callback) {
         uploadFiles(files, callback) {
             const formData = new FormData();
             const formData = new FormData();
             formData.append('filing_id', filingObj.curFiling.id);
             formData.append('filing_id', filingObj.curFiling.id);
@@ -563,6 +573,25 @@ $(document).ready(function() {
             $('#del-filing').modal('hide');
             $('#del-filing').modal('hide');
         });
         });
     });
     });
+    $('#add-file').on('show.bs.modal', function() {
+        $('#upload-file-hint').hide();
+        $('#upload-file')[0].value = '';
+        if ($('#add-file-ok').hasClass('btn-warning')) $('#add-file-ok').removeClass('btn-warning').addClass('btn-primary');
+    });
+    $('#upload-file').change(() => {
+        $('#add-file-ok').attr('disabled', true);
+        const input = $('#upload-file');
+        filingObj.checkFilesExist(input[0].files, function(result) {
+            if (result.length === 0) {
+                $('#upload-file-hint').hide();
+                $('#add-file-ok').removeClass('btn-warning').addClass('btn-primary').attr('disabled', false);
+            } else {
+                const msg = result[0] + (result.length > 1 ? `(等${result.length}个文件)`: '') + '</br>存在同名文件,请确认是否上传重复';
+                $('#upload-file-hint').html(msg).show();
+                $('#add-file-ok').removeClass('btn-primary').addClass('btn-warning').attr('disabled', false);
+            }
+        });
+    });
     $('#add-file-ok').click(() => {
     $('#add-file-ok').click(() => {
         const input = $('#upload-file');
         const input = $('#upload-file');
         filingObj.uploadFiles(input[0].files, function() {
         filingObj.uploadFiles(input[0].files, function() {
@@ -570,6 +599,24 @@ $(document).ready(function() {
             $('#add-file').modal('hide');
             $('#add-file').modal('hide');
         });
         });
     });
     });
+    $('#add-big-file').on('show.bs.modal', function() {
+        $('#upload-big-file-hint').hide();
+        $('#upload-big-file')[0].value = '';
+        if ($('#add-big-file-ok').hasClass('btn-warning')) $('#add-big-file-ok').removeClass('btn-warning').addClass('btn-primary');
+    });
+    $('#upload-big-file').change(() => {
+        $('#add-big-file-ok').attr('disabled', true);
+        const input = $('#upload-big-file');
+        filingObj.checkFilesExist(input[0].files, function(result) {
+            if (result.length === 0) {
+                $('#upload-big-file-hint').hide();
+                $('#add-big-file-ok').removeClass('btn-warning').addClass('btn-primary').attr('disabled', false);
+            } else {
+                $('#upload-big-file-hint').html('存在同名文件,请确认是否上传重复').show();
+                $('#add-big-file-ok').removeClass('btn-primary').addClass('btn-warning').attr('disabled', false);
+            }
+        });
+    });
     $('#add-big-file-ok').click(() => {
     $('#add-big-file-ok').click(() => {
         const input = $('#upload-big-file');
         const input = $('#upload-big-file');
         filingObj.uploadBigFile(input[0].files[0], function () {
         filingObj.uploadBigFile(input[0].files[0], function () {

+ 1 - 0
app/router.js

@@ -839,6 +839,7 @@ module.exports = app => {
     app.post('/sp/:id/filing/del', sessionAuth, subProjectCheck, 'fileController.delFiling');
     app.post('/sp/:id/filing/del', sessionAuth, subProjectCheck, 'fileController.delFiling');
     app.post('/sp/:id/filing/move', sessionAuth, subProjectCheck, 'fileController.moveFiling');
     app.post('/sp/:id/filing/move', sessionAuth, subProjectCheck, 'fileController.moveFiling');
     app.post('/sp/:id/file/load', sessionAuth, subProjectCheck, 'fileController.loadFile');
     app.post('/sp/:id/file/load', sessionAuth, subProjectCheck, 'fileController.loadFile');
+    app.post('/sp/:id/file/check', sessionAuth, subProjectCheck, 'fileController.checkFiles');
     app.post('/sp/:id/file/upload', sessionAuth, subProjectCheck, 'fileController.uploadFile');
     app.post('/sp/:id/file/upload', sessionAuth, subProjectCheck, 'fileController.uploadFile');
     app.post('/sp/:id/file/upload/big', sessionAuth, subProjectCheck, 'fileController.uploadBigFile');
     app.post('/sp/:id/file/upload/big', sessionAuth, subProjectCheck, 'fileController.uploadBigFile');
     app.post('/sp/:id/file/del', sessionAuth, subProjectCheck, 'fileController.delFile');
     app.post('/sp/:id/file/del', sessionAuth, subProjectCheck, 'fileController.delFile');

+ 6 - 0
app/service/file.js

@@ -44,6 +44,12 @@ module.exports = app => {
             return result;
             return result;
         }
         }
 
 
+        async checkFiles(filing_id, files) {
+            const existFiles = await this.getAllDataByCondition({ columns: ['filename', 'fileext'], where: { filing_id } });
+            const existFilesName = existFiles.map(x => { return x.filename + x.fileext });
+            return files.filter(x => { return existFilesName.indexOf(x) >= 0; });
+        }
+
         async addFiles(filing, fileInfo, user) {
         async addFiles(filing, fileInfo, user) {
             const conn = await this.db.beginTransaction();
             const conn = await this.db.beginTransaction();
             const result = {};
             const result = {};

+ 4 - 0
app/view/file/file_modal.ejs

@@ -70,6 +70,8 @@
                 <h5 class="modal-title">上传附件</h5>
                 <h5 class="modal-title">上传附件</h5>
             </div>
             </div>
             <div class="modal-body">
             <div class="modal-body">
+                <div class="form-group text-danger" style="display: none" id="upload-file-hint">
+                </div>
                 <div class="form-group">
                 <div class="form-group">
                     <label for="formGroupExampleInput">单个文件大小限制:50MB,支持<span data-toggle="tooltip" data-placement="bottom" title="" data-original-title="doc,docx,xls,xlsx,ppt,pptx,pdf">office等文档格式</span>、<span data-toggle="tooltip" data-placement="bottom" title="" data-original-title="jpg,png,bmp">图片格式</span>、<span data-toggle="tooltip" data-placement="bottom" title="" data-original-title="rar,zip">压缩包格式</span></label>
                     <label for="formGroupExampleInput">单个文件大小限制:50MB,支持<span data-toggle="tooltip" data-placement="bottom" title="" data-original-title="doc,docx,xls,xlsx,ppt,pptx,pdf">office等文档格式</span>、<span data-toggle="tooltip" data-placement="bottom" title="" data-original-title="jpg,png,bmp">图片格式</span>、<span data-toggle="tooltip" data-placement="bottom" title="" data-original-title="rar,zip">压缩包格式</span></label>
                     <input type="file" class="" id="upload-file" multiple>
                     <input type="file" class="" id="upload-file" multiple>
@@ -89,6 +91,8 @@
                 <h5 class="modal-title">上传附件</h5>
                 <h5 class="modal-title">上传附件</h5>
             </div>
             </div>
             <div class="modal-body">
             <div class="modal-body">
+                <div class="form-group text-danger" style="display: none" id="upload-big-file-hint">
+                </div>
                 <div class="form-group">
                 <div class="form-group">
                     <label for="formGroupExampleInput">文件大小限制:500MB,支持<span data-toggle="tooltip" data-placement="bottom" title="" data-original-title="doc,docx,xls,xlsx,ppt,pptx,pdf">office等文档格式</span>、<span data-toggle="tooltip" data-placement="bottom" title="" data-original-title="jpg,png,bmp">图片格式</span>、<span data-toggle="tooltip" data-placement="bottom" title="" data-original-title="rar,zip">压缩包格式</span></label>
                     <label for="formGroupExampleInput">文件大小限制:500MB,支持<span data-toggle="tooltip" data-placement="bottom" title="" data-original-title="doc,docx,xls,xlsx,ppt,pptx,pdf">office等文档格式</span>、<span data-toggle="tooltip" data-placement="bottom" title="" data-original-title="jpg,png,bmp">图片格式</span>、<span data-toggle="tooltip" data-placement="bottom" title="" data-original-title="rar,zip">压缩包格式</span></label>
                     <input type="file" class="" id="upload-big-file">
                     <input type="file" class="" id="upload-big-file">