Explorar el Código

上传附件改成多附件上传

laiguoran hace 6 años
padre
commit
0e6f2a3139

+ 38 - 27
app/controller/change_controller.js

@@ -546,34 +546,45 @@ module.exports = app => {
             };
             let stream;
             try {
-                stream = await ctx.getFileStream();
-                const create_time = Date.parse(new Date()) / 1000;
-                const fileInfo = path.parse(stream.filename);
-                const dirName = 'app/public/upload/changes/' + moment().format('YYYYMMDD');
-                const fileName = 'changes' + create_time + fileInfo.ext;
-
-                // 判断文件夹是否存在,不存在则直接创建文件夹
-                if (!fs.existsSync(path.join(this.app.baseDir, dirName))) {
-                    await fs.mkdirSync(path.join(this.app.baseDir, dirName));
-                }
-                // 保存文件
-                await ctx.helper.saveStreamFile(stream, path.join(this.app.baseDir, dirName, fileName));
-                // 保存数据到att表
-                const fileData = {
-                    in_time: create_time,
-                    filename: fileInfo.name,
-                    fileext: fileInfo.ext,
-                    filesize: stream.fields.size,
-                    filepath: path.join(dirName, fileName),
-                };
-                const result = await ctx.service.changeAtt.save(stream.fields, fileData, ctx.session.sessionUser.accountId);
-                if (!result) {
-                    throw '导入数据库保存失败';
+                const parts = ctx.multipart({ autoFields: true });
+                const files = [];
+                let index = 0;
+                while ((stream = await parts()) !== undefined) {
+                    // 判断用户是否选择上传文件
+                    if (!stream.filename) {
+                        throw '请选择上传的文件!';
+                    }
+                    const create_time = Date.parse(new Date()) / 1000;
+                    const fileInfo = path.parse(stream.filename);
+                    const dirName = 'app/public/upload/changes/' + moment().format('YYYYMMDD');
+                    const fileName = 'changes' + create_time + '_' + index + fileInfo.ext;
+                    // 判断文件夹是否存在,不存在则直接创建文件夹
+                    if (!fs.existsSync(path.join(this.app.baseDir, dirName))) {
+                        await fs.mkdirSync(path.join(this.app.baseDir, dirName));
+                    }
+                    // 保存文件
+                    await ctx.helper.saveStreamFile(stream, path.join(this.app.baseDir, dirName, fileName));
+                    await sendToWormhole(stream);
+                    // 保存数据到att表
+                    const fileData = {
+                        in_time: create_time,
+                        filename: fileInfo.name,
+                        fileext: fileInfo.ext,
+                        filesize: Array.isArray(parts.field.size) ? parts.field.size[index] : parts.field.size,
+                        filepath: path.join(dirName, fileName),
+                    };
+                    const result = await ctx.service.changeAtt.save(parts.field, fileData, ctx.session.sessionUser.accountId);
+                    if (!result) {
+                        throw '导入数据库保存失败';
+                    }
+                    fileData.in_time = moment(create_time * 1000).format('YYYY-MM-DD');
+                    fileData.filesize = await ctx.helper.bytesToSize(fileData.filesize);
+                    fileData.id = result.insertId;
+                    delete fileData.filepath;
+                    files.push(fileData);
+                    ++index;
                 }
-                fileData.in_time = moment(create_time * 1000).format('YYYY-MM-DD');
-                fileData.filesize = await ctx.helper.bytesToSize(stream.fields.size);
-                fileData.id = result.insertId;
-                responseData.data = fileData;
+                responseData.data = files;
             } catch (err) {
                 this.log(err);
                 // 失败需要消耗掉stream 以防卡死

+ 79 - 54
app/controller/stage_controller.js

@@ -544,8 +544,7 @@ module.exports = app => {
                 renderData.whiteList = this.ctx.app.config.multipart.whitelist;
 
                 // 用户有无权限上传和删除附件
-                renderData.uploadPermission = (ctx.stage.status === auditConst.status.uncheck && ctx.session.sessionUser.accountId === ctx.stage.user_id) ||
-                    (ctx.stage.status === auditConst.status.checking && ctx.session.sessionUser.accountId === ctx.stage.user_id) ||
+                renderData.uploadPermission = ((ctx.stage.status === auditConst.status.uncheck || ctx.stage.status === auditConst.status.checkNo) && ctx.session.sessionUser.accountId === ctx.stage.user_id) ||
                     (ctx.stage.status === auditConst.status.checkNoPre && ctx.session.sessionUser.accountId === ctx.stage.curAuditor.aid) ||
                     (ctx.stage.status === auditConst.status.checking && ctx.stage.curAuditor && ctx.stage.curAuditor.aid === ctx.session.sessionUser.accountId);
 
@@ -1004,35 +1003,50 @@ module.exports = app => {
             };
             let stream;
             try {
-                stream = await ctx.getFileStream();
-                const create_time = Date.parse(new Date()) / 1000;
-                const fileInfo = path.parse(stream.filename);
-                const dirName = 'app/public/upload/stage/' + moment().format('YYYYMMDD');
-                const fileName = 'stage' + create_time + fileInfo.ext;
+                const parts = ctx.multipart({ autoFields: true });
+                const files = [];
+                let index = 0;
+                while ((stream = await parts()) !== undefined) {
+                    // 判断用户是否选择上传文件
+                    if (!stream.filename) {
+                        throw '请选择上传的文件!';
+                    }
+                    const dirName = 'app/public/upload/stage/' + moment().format('YYYYMMDD');
+                    // 判断文件夹是否存在,不存在则直接创建文件夹
+                    if (!fs.existsSync(path.join(this.app.baseDir, dirName))) {
+                        await fs.mkdirSync(path.join(this.app.baseDir, dirName));
+                    }
+                    const create_time = Date.parse(new Date()) / 1000;
+                    const fileInfo = path.parse(stream.filename);
+                    const fileName = 'stage' + create_time + '_' + index + fileInfo.ext;
 
-                // 判断文件夹是否存在,不存在则直接创建文件夹
-                if (!fs.existsSync(path.join(this.app.baseDir, dirName))) {
-                    await fs.mkdirSync(path.join(this.app.baseDir, dirName));
-                }
-                // 保存文件
-                await ctx.helper.saveStreamFile(stream, path.join(this.app.baseDir, dirName, fileName));
-                // 保存数据到att表
-                const fileData = {
-                    tid: ctx.params.id,
-                    sid: ctx.params.order,
-                    in_time: create_time,
-                    filename: fileInfo.name,
-                    fileext: fileInfo.ext,
-                    filesize: stream.fields.size,
-                    filepath: path.join(dirName, fileName),
-                };
-                const result = await ctx.service.stageAtt.save(stream.fields, fileData, ctx.session.sessionUser.accountId);
-                if (!result) {
-                    throw '导入数据库保存失败';
+                    // 保存文件
+                    await ctx.helper.saveStreamFile(stream, path.join(this.app.baseDir, dirName, fileName));
+
+                    if (stream) {
+                        await sendToWormhole(stream);
+                    }
+
+                    // 保存数据到att表
+                    const fileData = {
+                        tid: ctx.params.id,
+                        sid: ctx.params.order,
+                        in_time: create_time,
+                        filename: fileInfo.name,
+                        fileext: fileInfo.ext,
+                        filesize: Array.isArray(parts.field.size) ? parts.field.size[index] : parts.field.size,
+                        filepath: path.join(dirName, fileName),
+                    };
+                    const result = await ctx.service.stageAtt.save(parts.field, fileData, ctx.session.sessionUser.accountId);
+                    if (!result) {
+                        throw '导入数据库保存失败';
+                    }
+                    const attData = await ctx.service.stageAtt.getDataByFid(result.insertId);
+                    attData.in_time = moment(create_time * 1000).format('YYYY-MM-DD');
+                    files.length !== 0 ? files.unshift(attData) : files.push(attData);
+                    ++index;
                 }
-                const attData = await ctx.service.stageAtt.getDataByFid(result.insertId);
-                attData.in_time = moment(create_time * 1000).format('YYYY-MM-DD');
-                responseData.data = attData;
+                responseData.data = files;
             } catch (err) {
                 this.log(err);
                 // 失败需要消耗掉stream 以防卡死
@@ -1188,34 +1202,45 @@ module.exports = app => {
             };
             let stream;
             try {
-                stream = await ctx.getFileStream();
+                const parts = ctx.multipart({ autoFields: true });
+                const files = [];
+                let index = 0;
                 const create_time = Date.parse(new Date()) / 1000;
-                const fileInfo = path.parse(stream.filename);
-                const dirName = 'app/public/upload/pay/' + moment().format('YYYYMMDD');
-                const fileName = 'pay' + create_time + fileInfo.ext;
+                while ((stream = await parts()) !== undefined) {
+                    // 判断用户是否选择上传文件
+                    if (!stream.filename) {
+                        throw '请选择上传的文件!';
+                    }
+                    const fileInfo = path.parse(stream.filename);
+                    const dirName = 'app/public/upload/pay/' + moment().format('YYYYMMDD');
+                    const fileName = 'pay' + create_time + '_' + index + fileInfo.ext;
 
-                // 判断文件夹是否存在,不存在则直接创建文件夹
-                if (!fs.existsSync(path.join(this.app.baseDir, dirName))) {
-                    await fs.mkdirSync(path.join(this.app.baseDir, dirName));
-                }
-                // 保存文件
-                await ctx.helper.saveStreamFile(stream, path.join(this.app.baseDir, dirName, fileName));
-                // 插入到stage_pay对应的附件列表中
-                const attData = {
-                    filename: fileInfo.name,
-                    fileext: fileInfo.ext,
-                    filesize: stream.fields.size,
-                    filepath: path.join(dirName, fileName),
-                    uid: ctx.session.sessionUser.accountId,
-                    in_time: moment(create_time * 1000).format('YYYY-MM-DD'),
-                };
-                const result = await ctx.service.stagePay.saveAtt(stream.fields.pay_id, attData);
-                if (!result) {
-                    throw '导入数据库保存失败';
+                    // 判断文件夹是否存在,不存在则直接创建文件夹
+                    if (!fs.existsSync(path.join(this.app.baseDir, dirName))) {
+                        await fs.mkdirSync(path.join(this.app.baseDir, dirName));
+                    }
+                    // 保存文件
+                    await ctx.helper.saveStreamFile(stream, path.join(this.app.baseDir, dirName, fileName));
+                    await sendToWormhole(stream);
+                    // 插入到stage_pay对应的附件列表中
+                    const attData = {
+                        filename: fileInfo.name,
+                        fileext: fileInfo.ext,
+                        filesize: Array.isArray(parts.field.size) ? parts.field.size[index] : parts.field.size,
+                        filepath: path.join(dirName, fileName),
+                        uid: ctx.session.sessionUser.accountId,
+                        in_time: moment(create_time * 1000).format('YYYY-MM-DD'),
+                    };
+                    const result = await ctx.service.stagePay.saveAtt(parts.field.pay_id, attData);
+                    if (!result) {
+                        throw '导入数据库保存失败';
+                    }
+                    delete attData.filepath;
+                    attData.username = ctx.session.sessionUser.name;
+                    files.length !== 0 ? files.unshift(attData) : files.push(attData);
+                    ++index;
                 }
-                delete attData.filepath
-                attData.username = ctx.session.sessionUser.name;
-                responseData.data = attData;
+                responseData.data = files;
             } catch (err) {
                 this.log(err);
                 // 失败需要消耗掉stream 以防卡死

+ 31 - 27
app/public/js/change_detail.js

@@ -45,38 +45,42 @@ $(document).ready(() => {
 
     // 上传附件
     $('#upload-file-btn').click(function () {
-        const file = $('#upload-file')[0];
-        if (file.files[0] === undefined) {
-            toastr.error('未选择上传文件!');
-            return false;
-        }
-        const filesize = file.files[0].size;
-        if (filesize > 30 * 1024 * 1024) {
-            toastr.error('文件大小过大!');
-            return false;
-        }
-        const fileext = '.' + file.files[0].name.toLowerCase().split('.').splice(-1)[0];
-        if (whiteList.indexOf(fileext) === -1) {
-            toastr.error('只能上传指定格式的附件!');
-            return false;
-        }
+        const files = $('#upload-file')[0].files;
         const formData = new FormData();
         formData.append('cid', $('#changeId').val());
         formData.append('tid', $('#tenderId').val());
-        formData.append('size', filesize);
-        formData.append('file', file.files[0]);
+        for (const file of files) {
+            if (file === undefined) {
+                toastr.error('未选择上传文件!');
+                return false;
+            }
+            const filesize = file.size;
+            if (filesize > 30 * 1024 * 1024) {
+                toastr.error('文件大小过大!');
+                return false;
+            }
+            const fileext = '.' + file.name.toLowerCase().split('.').splice(-1)[0];
+            if (whiteList.indexOf(fileext) === -1) {
+                toastr.error('只能上传指定格式的附件!');
+                return false;
+            }
+            formData.append('size', filesize);
+            formData.append('file[]', file);
+        }
         postDataWithFile('/change/upload/file', formData, function (data) {
             $('#addfujian').modal('hide');
-            console.log(data);
-            const fileInfo = data;
-            let index = $('#attList tr').length;
-            let html = '<tr> ' +
-                '<td>' + (index+1) + '</td> ' +
-                '<td><a href="/change/download/file/' + 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> ' +
-                '</tr>';
+            let html = '';
+            let index = $('#attList tr').length + 1;
+            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>' + 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> ' +
+                    '</tr>';
+                ++index;
+            }
             $('#attList').append(html);
         }, function () {
 

+ 21 - 19
app/public/js/stage.js

@@ -1253,31 +1253,33 @@ $(document).ready(() => {
     });
     // 上传附件
     $('#upload-file-btn').click(function () {
-        const file = $('#upload-file')[0];
-        if (file.files[0] === undefined) {
-            toast('未选择上传文件!', 'error');
-            return false;
-        }
-        const filesize = file.files[0].size;
-        if (filesize > 10 * 1024 * 1024) {
-            toast('文件大小过大!', 'error');
-            return false;
-        }
-        const fileext = '.' + file.files[0].name.toLowerCase().split('.').splice(-1)[0];
-        if (whiteList.indexOf(fileext) === -1) {
-            toast('只能上传指定格式的附件!', 'error');
-            return false;
-        }
+        const files = $('#upload-file')[0].files;
         const node = SpreadJsObj.getSelectObject(slSpread.getActiveSheet());
         const formData = new FormData();
         formData.append('lid', node.id);
-        formData.append('size', filesize);
-        formData.append('file', file.files[0]);
+        for (const file of files) {
+            if (file === undefined) {
+                toast('未选择上传文件!', 'error');
+                return false;
+            }
+            const filesize = file.size;
+            if (filesize > 10 * 1024 * 1024) {
+                toast('存在上传文件大小过大!', 'error');
+                return false;
+            }
+            const fileext = '.' + file.name.toLowerCase().split('.').splice(-1)[0];
+            if (whiteList.indexOf(fileext) === -1) {
+                toast('只能上传指定格式的附件!', 'error');
+                return false;
+            }
+            formData.append('size', filesize);
+            formData.append('file[]', file);
+        }
         postDataWithFile('/tender/' + tender.id + '/measure/stage/' + stage.order + '/upload/file', formData, function (data) {
             $('#upload').modal('hide');
             // 插入到attData中
-            attData.unshift(data);
-            // 重新生成LIst
+            attData = data.concat(attData);
+            // 重新生成List
             getAllList();
             getNodeList(node.id);
         }, function () {

+ 27 - 21
app/public/js/stage_pay.js

@@ -762,32 +762,37 @@ $(document).ready(() => {
     });
 
     // 上传附件
-    $('#upload-file-btn').click(function () {
-        const file = $('#upload-file')[0];
-        if (file.files[0] === undefined) {
-            toast('未选择上传文件!', 'error');
-            return false;
-        }
-        const filesize = file.files[0].size;
-        if (filesize > 10 * 1024 * 1024) {
-            toast('文件大小过大!', 'error');
-            return false;
-        }
-        const fileext = '.' + file.files[0].name.toLowerCase().split('.').splice(-1)[0];
-        if (whiteList.indexOf(fileext) === -1) {
-            toast('只能上传指定格式的附件!', 'error');
-            return false;
-        }
+    $('#upload-file').change(function () {
+        const files = this.files;
         const select = SpreadJsObj.getSelectObject(paySpread.getActiveSheet());
         const formData = new FormData();
+        // const sizes = [];
         formData.append('pay_id', select.id);
-        formData.append('size', filesize);
-        formData.append('file', file.files[0]);
+        for (const file of files) {
+            if (file === undefined) {
+                toast('未选择上传文件!', 'error');
+                return false;
+            }
+            const filesize = file.size;
+            if (filesize > 10 * 1024 * 1024) {
+                toast('存在上传文件大小过大!', 'error');
+                return false;
+            }
+            const fileext = '.' + file.name.toLowerCase().split('.').splice(-1)[0];
+            if (whiteList.indexOf(fileext) === -1) {
+                toast('只能上传指定格式的附件!', 'error');
+                return false;
+            }
+            // sizes.push(filesize);
+            formData.append('size', filesize);
+            formData.append('file[]', file);
+        }
+        // formData.append('size', sizes.join(','));
         postDataWithFile('/tender/' + tender.id + '/measure/stage/' + tender.ledger_times + '/pay/upload/file', formData, function (data) {
             if (select.attachment === null) {
-                select.attachment = new Array(data);
+                select.attachment = data;
             } else {
-                select.attachment.unshift(data);
+                select.attachment = data.concat(select.attachment);
             }
             makeAttTable(select.id, select.attachment);
             const index = dealPay.indexOf(select);
@@ -811,7 +816,8 @@ $(document).ready(() => {
         postData('/tender/' + tender.id + '/measure/stage/' + tender.ledger_times + '/pay/delete/file', data, function (result) {
             select.attachment.splice(index, 1);
             makeAttTable(id, select.attachment);
-            dealPay.splice(index, 1, select);
+            const pay_index = dealPay.indexOf(select);
+            dealPay.splice(pay_index, 1, select);
             SpreadJsObj.reLoadSheetData(paySpread.getActiveSheet());
         });
     })

+ 1 - 1
app/view/change/info_modal.ejs

@@ -576,7 +576,7 @@
             </div>
             <div class="modal-body">
                 <p>大小限制:30MB,支持office等文档格式、图片格式、压缩包格式</p>
-                <p><input class="btn btn-primary" value="选择文件" type="file" id="upload-file" /></p>
+                <p><input class="btn btn-primary" value="选择文件" type="file" id="upload-file" multiple /></p>
             </div>
             <div class="modal-footer">
                 <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>

+ 1 - 1
app/view/stage/index.ejs

@@ -248,7 +248,7 @@
     const curStageData = JSON.parse('<%- JSON.stringify(curStageData) %>');
     const preStageData = JSON.parse('<%- JSON.stringify(preStageData) %>');
     const whiteList = JSON.parse('<%- JSON.stringify(whiteList) %>');
-    const attData = JSON.parse('<%- JSON.stringify(attData) %>');
+    let attData = JSON.parse('<%- JSON.stringify(attData) %>');
     const userID = '<%- ctx.session.sessionUser.accountId %>';
 </script>
 <% if (ctx.stage.status === auditConst.status.uncheck && ctx.session.sessionUser.accountId === ctx.stage.user_id) {%>

+ 1 - 1
app/view/stage/modal.ejs

@@ -238,7 +238,7 @@
             <div class="modal-body">
                 <div class="form-group">
                     <label for="formGroupExampleInput">大小限制:10MB,支持<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="form-control" id="upload-file">
+                    <input type="file" class="form-control" id="upload-file" multiple>
                 </div>
             </div>
             <div class="modal-footer">

+ 2 - 2
app/view/stage/pay_modal.ejs

@@ -78,7 +78,7 @@
                 <% if (uploadPermission) { %>
                 <div class="form-group">
                     <label for="formGroupExampleInput">大小限制:10MB,支持<span data-toggle="tooltip" data-placement="bottom" title="doc,docx,xls,xlsx,ppt,pptx,pdf">office等文档格式</span>、<span data-toggle="tooltip" data-placement="bottom" title="jpg,png,bmp">图片格式</span>、<span data-toggle="tooltip" data-placement="bottom" title="rar,zip">压缩包格式</span></label>
-                    <input type="file" class="form-control" id="upload-file">
+                    <input type="file" class="form-control" id="upload-file" multiple>
                 </div>
                 <% } %>
                 <div class="modal-height-500" style="overflow:auto;">
@@ -99,7 +99,7 @@
             </div>
             <div class="modal-footer">
                 <button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
-                <button type="button" class="btn btn-primary" id="upload-file-btn">确定</button>
+                <!--<button type="button" class="btn btn-primary" id="upload-file-btn">确定</button>-->
             </div>
         </div>
     </div>