From 39b7bd04dc871134c332ab7396a2a6e7d7805a3a Mon Sep 17 00:00:00 2001
From: 马宇豪 <978517621@qq.com>
Date: 星期四, 24 八月 2023 13:56:16 +0800
Subject: [PATCH] 图片多传和查看

---
 src/main/resources/templates/tr/hiddenDangerCheck/dangerReport/addDangerReport.html |  190 ++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 155 insertions(+), 35 deletions(-)

diff --git a/src/main/resources/templates/tr/hiddenDangerCheck/dangerReport/addDangerReport.html b/src/main/resources/templates/tr/hiddenDangerCheck/dangerReport/addDangerReport.html
index ac12593..c08536d 100644
--- a/src/main/resources/templates/tr/hiddenDangerCheck/dangerReport/addDangerReport.html
+++ b/src/main/resources/templates/tr/hiddenDangerCheck/dangerReport/addDangerReport.html
@@ -93,11 +93,14 @@
             <label class="col-sm-3 control-label">隐患整改前照片:</label>
             <div class="col-sm-8">
                 <input name="preRectifyPhoto" id="preRectifyPhoto" type="hidden">
-                <button type="button" class="layui-btn" id="preRectifyPhotoUpload">上传</button>
-                <div class="layui-upload-list">
-                    <img class="layui-upload-img" style="width: 92px; height: 92px;" id="ThumbImg">
-                    <p id="msgText"></p>
-                </div>
+<!--                <button type="button" class="layui-btn" id="preRectifyPhotoUpload">上传</button>-->
+<!--                <div class="layui-upload-list">-->
+<!--                    <img class="layui-upload-img" style="width: 92px; height: 92px;" id="ThumbImg">-->
+<!--                    <p id="msgText"></p>-->
+<!--                </div>-->
+                <button type="button" class="layui-btn layui-btn-normal" id="preChooseList">选择文件(不超过3张)</button>
+                <button type="button" class="layui-btn" id="preUpAction">确认上传(上传后不可修改)</button>
+                <div id="preFileList"></div>
             </div>
         </div>
 
@@ -223,40 +226,157 @@
 
 
     <!--layui图片文件上传-->
-    layui.use('upload', function () {
-        var $ = layui.jquery
-            , upload = layui.upload;
+    // layui.use('upload', function () {
+    //     var $ = layui.jquery
+    //         , upload = layui.upload;
+    //
+    //     //普通图片上传
+    //     var uploadInst = upload.render({
+    //         elem: '#preRectifyPhotoUpload'
+    //         , url: ctx + 'tr/upload'
+    //         , before: function (obj) {
+    //             //预读本地文件示例,不支持ie8
+    //             obj.preview(function (index, file, result) {
+    //                 $('#ThumbImg').attr('src', result); //图片链接(base64)
+    //             });
+    //         }
+    //         , done: function (res) {
+    //             //如果上传失败
+    //             if (res.code > 0) {
+    //                 return layer.msg('上传失败');
+    //             }
+    //             //上传成功
+    //             $('#preRectifyPhoto').val(res.imageUrl);
+    //         }
+    //         , error: function () {
+    //             //演示失败状态,并实现重传
+    //             var msgText = $('#msgText');
+    //             msgText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
+    //             msgText.find('.demo-reload').on('click', function () {
+    //                 uploadInst.upload();
+    //             });
+    //         }
+    //     });
+    // });
 
-        //普通图片上传
-        var uploadInst = upload.render({
-            elem: '#preRectifyPhotoUpload'
-            , url: ctx + 'tr/upload'
-            , before: function (obj) {
-                //预读本地文件示例,不支持ie8
-                obj.preview(function (index, file, result) {
-                    $('#ThumbImg').attr('src', result); //图片链接(base64)
-                });
-            }
-            , done: function (res) {
-                //如果上传失败
-                if (res.code > 0) {
-                    return layer.msg('上传失败');
-                }
-                //上传成功
-                $('#preRectifyPhoto').val(res.imageUrl);
-            }
-            , error: function () {
-                //演示失败状态,并实现重传
-                var msgText = $('#msgText');
-                msgText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
-                msgText.find('.demo-reload').on('click', function () {
-                    uploadInst.upload();
-                });
+    layui.use('upload', function() {
+        var $ = layui.jquery;
+        upload = layui.upload;
+        var uploadList = {};
+        var urls = []
+        //多文件列表示例
+        var preFileListDom = $('#preFileList')
+        var uploadListIns = upload.render({
+            elem: '#preChooseList',
+            url: ctx + 'tr/upload' ,
+            accept: 'file',
+            multiple: true,
+            number: 3,
+            auto: false,
+            bindAction: '#preUpAction',
+            choose: function(obj) {
+                // 展示上传按钮
+                $('#preUpAction').show();
+                // 将每次选择的文件追加到文件队列
+                uploadList = obj.pushFile();
+                const len = preFileListDom.find('.file-list-item1').length;
+                // 还可以上传的文件数量
+                const limit = 3 - len;
+                var number = 0;
+                // 预读本地文件,如果是多文件,则会遍历。(不支持ie8/9)
+                obj.preview(function(index, file, result){
+                    if (number < limit){
+                        number++;
+                        appendFile(index, file.name, result);
+                    }else{
+                        deleteTempFile(index)
+                    }
+                })
+            },
+            // before: function(obj){
+            //     layer.load(1);
+            // },
+            //上传完毕回调
+            //当文件全部被提交后,才触发
+            allDone: function (obj) {
+                layer.msg("已上传 "+obj.successful+" 个文件");
+                layer.closeAll('loading');
+                deleteTempFile(0)
+            },
+            done: function(res, index, upload){
+                preFileListDom.find('.file-temp1').each(function () {
+                    if (!res || res.code > 0){
+                        // 若上传失败,则直接移除
+                        return layer.msg('上传失败');
+                        $(this).remove();
+                    }else{
+                        //上传成功
+                        // 添加上传返回的数据到对应的文件中
+                        // var hidden = '<input type="hidden" name="path[]" value="'+res.imageUrl+'"/>';
+                        // $(this).append(hidden);
+                        // 修改预览文件样式
+                        $(this).removeClass('file-temp1');
+                        // $(this).find('.file-cancel').removeClass('layui-btn-primary');
+                        // $(this).find('.file-cancel').addClass('layui-btn-danger');
+                        // $(this).find('.file-cancel').val('删除');
+                        $(this).find('.file-cancel1').remove();
+                        $(this).find('.layui-badge1').remove();
+                        urls.push(res.imageUrl)
+                    }
+                    $('#preRectifyPhoto').val(urls.join(','));
+                })
+            },
+            error: function(index, upload) {
+                var tr = preFileListDom.find('tr#upload-' + index),
+                    tds = tr.children();
+                tds.eq(1).html('<span style="color: #FF5722;">上传失败</span>');
+                tds.eq(2).find('.demo-reload').removeClass('layui-hide'); //显示重传
             }
         });
+
+        // 预览选择的文件
+        function appendFile(index, name, result) {
+            const dom = '<div class="file-list-item1 file-temp1" data-index="'+index+'"><div><img src="'+result+'" alt="'+name+'"><span class="layui-badge1 layui-bg-blue">待上传</span></div>' +
+                '<div><input type="button" class="file-cancel1" value="删除"></div></div>';
+            preFileListDom.append(dom);
+            fileCancelEvent();
+        }
+        // 为待上传文件【取消】按钮添加点击事件
+        function fileCancelEvent() {
+            preFileListDom.find('.file-cancel1').each(function () {
+                const event = $._data($(this)[0],"events");
+                // 仅对未绑定事件的按钮进行绑定
+                if (!event || !event["click"]){
+                    $(this).click(function () {
+                        var fileDom = $(this).parent().parent();
+                        var index = fileDom.data('index');
+                        if (index){
+                            deleteTempFile(index);
+                        }
+                        fileDom.remove();
+                    })
+                }
+            })
+        }
+        // 删除待上传的文件(从文件队列中删除文件)
+        function deleteTempFile(index){
+            if (index === 0){
+                // 清空文件队列中所有数据
+                for (const key in uploadList) {
+                    delete uploadList[key];
+                }
+                // 隐藏上传按钮
+                $('#preUpAction').hide();
+            }else{
+                delete uploadList[index];
+                var number = Object.keys(uploadList).length;
+                // 若上传文件队列中无数据则隐藏上传按钮
+                if (number <= 0){
+                    $('#preUpAction').hide();
+                }
+            }
+        }
     });
-
-
 </script>
 </body>
 </html>
\ No newline at end of file

--
Gitblit v1.9.2