From af6a08398e2bc22f3c8a3a615cbd9e23190be906 Mon Sep 17 00:00:00 2001
From: 疯狂的狮子li <15040126243@163.com>
Date: 星期四, 12 五月 2022 10:55:44 +0800
Subject: [PATCH] update 优化 文件与图片上传组件 使用id存储回显

---
 ruoyi-ui/src/api/system/oss.js                                                  |    8 ++++
 ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysOssServiceImpl.java |    5 ++
 ruoyi-ui/src/components/FileUpload/index.vue                                    |   36 ++++++++++++-----
 ruoyi-ui/src/components/ImageUpload/index.vue                                   |   35 +++++++++++------
 ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysOssController.java |   14 +++++++
 ruoyi-system/src/main/java/com/ruoyi/system/service/ISysOssService.java         |    3 +
 6 files changed, 78 insertions(+), 23 deletions(-)

diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysOssController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysOssController.java
index a128a15..d4e52ac 100644
--- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysOssController.java
+++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysOssController.java
@@ -32,6 +32,7 @@
 import java.io.IOException;
 import java.util.Arrays;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 /**
@@ -59,6 +60,19 @@
     }
 
     /**
+     * 鏌ヨOSS瀵硅薄鍩轰簬id涓�
+     */
+    @ApiOperation("鏌ヨOSS瀵硅薄鍩轰簬ID")
+    @SaCheckPermission("system:oss:list")
+    @GetMapping("/listByIds/{ossIds}")
+    public R<List<SysOssVo>> listByIds(@ApiParam("OSS瀵硅薄ID涓�")
+                                     @NotEmpty(message = "涓婚敭涓嶈兘涓虹┖")
+                                     @PathVariable Long[] ossIds) {
+        List<SysOssVo> list = iSysOssService.listByIds(Arrays.asList(ossIds));
+        return R.ok(list);
+    }
+
+    /**
      * 涓婁紶OSS瀵硅薄瀛樺偍
      */
     @ApiOperation("涓婁紶OSS瀵硅薄瀛樺偍")
diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysOssService.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysOssService.java
index b444e6d..c55e5bc 100644
--- a/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysOssService.java
+++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysOssService.java
@@ -8,6 +8,7 @@
 import org.springframework.web.multipart.MultipartFile;
 
 import java.util.Collection;
+import java.util.List;
 
 /**
  * 鏂囦欢涓婁紶 鏈嶅姟灞�
@@ -18,6 +19,8 @@
 
     TableDataInfo<SysOssVo> queryPageList(SysOssBo sysOss, PageQuery pageQuery);
 
+    List<SysOssVo> listByIds(Collection<Long> ossIds);
+
     SysOss getById(Long ossId);
 
     SysOss upload(MultipartFile file);
diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysOssServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysOssServiceImpl.java
index 25dfd5c..28bee3d 100644
--- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysOssServiceImpl.java
+++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysOssServiceImpl.java
@@ -42,6 +42,11 @@
         return TableDataInfo.build(result);
     }
 
+    @Override
+    public List<SysOssVo> listByIds(Collection<Long> ossIds) {
+        return baseMapper.selectVoById(ossIds);
+    }
+
     private LambdaQueryWrapper<SysOss> buildQueryWrapper(SysOssBo bo) {
         Map<String, Object> params = bo.getParams();
         LambdaQueryWrapper<SysOss> lqw = Wrappers.lambdaQuery();
diff --git a/ruoyi-ui/src/api/system/oss.js b/ruoyi-ui/src/api/system/oss.js
index 83adca5..7d80026 100644
--- a/ruoyi-ui/src/api/system/oss.js
+++ b/ruoyi-ui/src/api/system/oss.js
@@ -9,6 +9,14 @@
   })
 }
 
+// 鏌ヨOSS瀵硅薄鍩轰簬id涓�
+export function listByIds(ossId) {
+  return request({
+    url: '/system/oss/listByIds/' + ossId,
+    method: 'get'
+  })
+}
+
 // 鍒犻櫎OSS瀵硅薄瀛樺偍
 export function delOss(ossId) {
   return request({
diff --git a/ruoyi-ui/src/components/FileUpload/index.vue b/ruoyi-ui/src/components/FileUpload/index.vue
index f41a712..e999f14 100644
--- a/ruoyi-ui/src/components/FileUpload/index.vue
+++ b/ruoyi-ui/src/components/FileUpload/index.vue
@@ -41,7 +41,7 @@
 
 <script>
 import { getToken } from "@/utils/auth";
-import { delOss } from "@/api/system/oss";
+import { listByIds, delOss } from "@/api/system/oss";
 
 export default {
   name: "FileUpload",
@@ -83,19 +83,24 @@
   },
   watch: {
     value: {
-      handler(val) {
+      async handler(val) {
         if (val) {
           let temp = 1;
           // 棣栧厛灏嗗�艰浆涓烘暟缁�
-          const list = Array.isArray(val) ? val : this.value.split(',');
+          let list;
+          if (Array.isArray(val)) {
+            list = val;
+          } else {
+            await listByIds(val).then(res => {
+              list = res.data.map(oss => {
+                oss = { name: oss.originalName, url: oss.url, ossId: oss.ossId };
+                return oss;
+              });
+            })
+          }
           // 鐒跺悗灏嗘暟缁勮浆涓哄璞℃暟缁�
           this.fileList = list.map(item => {
-            // 瀛楃涓插洖鏄惧鐞� 濡傛灉姝ゅ瀛樼殑鏄痷rl鍙洿鎺ュ洖鏄� 濡傛灉瀛樼殑鏄痠d闇�瑕佽皟鐢ㄦ帴鍙f煡鍑烘潵
-            if (typeof item === "string") {
-              item = { name: item, url: item };
-            } else {
-              item = { name: item.name, url: item.url, ossId: item.ossId };
-            }
+            item = { name: item.name, url: item.url, ossId: item.ossId };
             item.uid = item.uid || new Date().getTime() + temp++;
             return item;
           });
@@ -162,7 +167,7 @@
           this.fileList = this.fileList.concat(this.uploadList);
           this.uploadList = [];
           this.number = 0;
-          this.$emit("input", this.fileList);
+          this.$emit("input", this.listToString(this.fileList));
           this.$modal.closeLoading();
         }
       } else {
@@ -175,7 +180,7 @@
       let ossId = this.fileList[index].ossId;
       delOss(ossId);
       this.fileList.splice(index, 1);
-      this.$emit("input", this.fileList);
+      this.$emit("input", this.listToString(this.fileList));
     },
     // 鑾峰彇鏂囦欢鍚嶇О
     getFileName(name) {
@@ -186,6 +191,15 @@
         return name;
       }
     },
+    // 瀵硅薄杞垚鎸囧畾瀛楃涓插垎闅�
+    listToString(list, separator) {
+      let strs = "";
+      separator = separator || ",";
+      for (let i in list) {
+        strs += list[i].ossId + separator;
+      }
+      return strs != "" ? strs.substr(0, strs.length - 1) : "";
+    },
   },
 };
 </script>
diff --git a/ruoyi-ui/src/components/ImageUpload/index.vue b/ruoyi-ui/src/components/ImageUpload/index.vue
index 2d1ed57..6219979 100644
--- a/ruoyi-ui/src/components/ImageUpload/index.vue
+++ b/ruoyi-ui/src/components/ImageUpload/index.vue
@@ -44,7 +44,7 @@
 
 <script>
 import { getToken } from "@/utils/auth";
-import { delOss } from "@/api/system/oss";
+import { listByIds, delOss } from "@/api/system/oss";
 
 export default {
   props: {
@@ -87,19 +87,21 @@
   },
   watch: {
     value: {
-      handler(val) {
+      async handler(val) {
         if (val) {
           // 棣栧厛灏嗗�艰浆涓烘暟缁�
-          const list = Array.isArray(val) ? val : this.value.split(',');
+          let list;
+          if (Array.isArray(val)) {
+            list = val;
+          } else {
+            await listByIds(val).then(res => {
+              list = res.data;
+            })
+          }
           // 鐒跺悗灏嗘暟缁勮浆涓哄璞℃暟缁�
           this.fileList = list.map(item => {
-            // 瀛楃涓插洖鏄惧鐞� 濡傛灉姝ゅ瀛樼殑鏄痷rl鍙洿鎺ュ洖鏄� 濡傛灉瀛樼殑鏄痠d闇�瑕佽皟鐢ㄦ帴鍙f煡鍑烘潵
-            if (typeof item === "string") {
-              item = { name: item, url: item };
-            } else {
-              // 姝ゅname浣跨敤ossId 闃叉鍒犻櫎鍑虹幇閲嶅悕
-              item = { name: item.ossId, url: item.url, ossId: item.ossId };
-            }
+            // 姝ゅname浣跨敤ossId 闃叉鍒犻櫎鍑虹幇閲嶅悕
+            item = { name: item.ossId, url: item.url, ossId: item.ossId };
             return item;
           });
         } else {
@@ -125,7 +127,7 @@
         let ossId = this.fileList[findex].ossId;
         delOss(ossId);
         this.fileList.splice(findex, 1);
-        this.$emit("input", this.fileList);
+        this.$emit("input", this.listToString(this.fileList));
       }
     },
     // 涓婁紶鎴愬姛鍥炶皟
@@ -136,7 +138,7 @@
           this.fileList = this.fileList.concat(this.uploadList);
           this.uploadList = [];
           this.number = 0;
-          this.$emit("input", this.fileList);
+          this.$emit("input", this.listToString(this.fileList));
           this.$modal.closeLoading();
         }
       } else {
@@ -189,6 +191,15 @@
       this.dialogImageUrl = file.url;
       this.dialogVisible = true;
     },
+    // 瀵硅薄杞垚鎸囧畾瀛楃涓插垎闅�
+    listToString(list, separator) {
+      let strs = "";
+      separator = separator || ",";
+      for (let i in list) {
+        strs += list[i].ossId + separator;
+      }
+      return strs != "" ? strs.substr(0, strs.length - 1) : "";
+    },
   }
 };
 </script>

--
Gitblit v1.9.3