瀏覽代碼

修复SysFileList总数Total的BUG; 新增 target_label, username 字段

tsurumure 3 月之前
父節點
當前提交
23729d7999

+ 4 - 0
src/main/java/com/backendsys/modules/system/service/SysCommonService.java

@@ -20,7 +20,11 @@ public interface SysCommonService {
 
     // 获取系统配置值 (按标识)
     Object getCommonByTag(String tag);
+    // 获取系统配置可选值组 (按标识)
     JSONArray getCommonOptionByTag(String tag);
+    // 通过可选值组,获取对应的文本翻译
+    Object getLabelByValue(JSONArray common_options, Object value);
+
     // 获取系统配置值 (按分类)
     List<SysCommon> getCommonByCategory(String category);
 

+ 16 - 0
src/main/java/com/backendsys/modules/system/service/impl/SysCommonServiceImpl.java

@@ -1,7 +1,9 @@
 package com.backendsys.modules.system.service.impl;
 
+import cn.hutool.core.convert.Convert;
 import cn.hutool.core.util.StrUtil;
 import cn.hutool.json.JSONArray;
+import cn.hutool.json.JSONObject;
 import cn.hutool.json.JSONUtil;
 import com.backendsys.exception.CustException;
 import com.backendsys.modules.system.dao.SysCommonDao;
@@ -76,6 +78,20 @@ public class SysCommonServiceImpl implements SysCommonService {
         return (sysCommon != null) ? JSONUtil.parseArray(sysCommon.getValue_option()) : null;
     }
 
+    // 通过可选值组,获取对应的文本翻译
+    @Override
+    public Object getLabelByValue(JSONArray common_options, Object value) {
+        Object label ="";
+        for (Object option : common_options) {
+            JSONObject option_obj = JSONUtil.parseObj(option);
+            Object opt_obj_value = option_obj.get("value");
+            if (opt_obj_value == value) {
+                label = option_obj.get("label");
+            }
+        }
+        return label;
+    }
+
     // 获取系统配置值 (按分类)
     @Override
     @Cacheable(value = "catch::common", key = "'category::' + #category", unless = "#result == null")

+ 2 - 0
src/main/java/com/backendsys/modules/upload/entity/SysFile.java

@@ -32,6 +32,8 @@ public class SysFile {
     private Integer upload_chunk_index;
 
     private Long user_id;
+    @TableField(exist = false)
+    private String username;
 
     @NotEmpty(message = "文件名不能为空", groups = { Update.class })
     @Size(max = 100, message = "资讯分类名称长度不超过 {max} 字符", groups = { Update.class })

+ 1 - 1
src/main/java/com/backendsys/modules/upload/enums/StyleEnums.java

@@ -5,7 +5,7 @@ package com.backendsys.modules.upload.enums;
  */
 public enum StyleEnums {
 
-    THUMB_BACKGROUND("#f8f8f8")
+    THUMB_BACKGROUND("f8f8f8")
     ;
 
     private String value;

+ 18 - 13
src/main/java/com/backendsys/modules/upload/service/impl/SysFileMultipartServiceImpl.java

@@ -62,18 +62,23 @@ public class SysFileMultipartServiceImpl implements SysFileMultipartService {
     //   2:阿里云:
     //   3:抖音云: https://www.volcengine.com/docs/6349/153626
     private SysFile setThumbUrl(SysFile sysFile, Integer width, Integer height, String backgroundColor) {
-        // 本地上传
-        if (sysFile.getTarget() == -1) {
-            sysFile.setUrl_thumb(sysFile.getUrl() + "?w=" + width + "&h=" + height);
-        }
-        // 腾讯云 (color值通过base64加密, #f8f8f8)
-        if (sysFile.getTarget() == 1) {
-            System.out.println("base64 encode: " + Base64.encode(backgroundColor));
-            sysFile.setUrl_thumb(sysFile.getUrl() + "?imageMogr2/thumbnail/" + width + "x" + height + "/pad/1/color/" + Base64.encode(backgroundColor));
-        }
-        // 抖音云
-        if (sysFile.getTarget() == 3) {
-            sysFile.setUrl_thumb(sysFile.getUrl() + "?x-tos-process=image/resize,w_" + width + ",h_" + height + ",m_pad,color_" + backgroundColor);
+        if (sysFile.getContent_type() != null) {
+            if (sysFile.getContent_type().toLowerCase().contains("image")) {
+                // 本地上传
+                if (sysFile.getTarget() == -1) {
+                    sysFile.setUrl_thumb(sysFile.getUrl() + "?w=" + width + "&h=" + height);
+                }
+                // 腾讯云 (color值通过base64加密, #f8f8f8)
+                if (sysFile.getTarget() == 1) {
+                    backgroundColor = "#" + backgroundColor;
+                    System.out.println("base64 encode: " + Base64.encode(backgroundColor));
+                    sysFile.setUrl_thumb(sysFile.getUrl() + "?imageMogr2/thumbnail/" + width + "x" + height + "/pad/1/color/" + Base64.encode(backgroundColor));
+                }
+                // 抖音云
+                if (sysFile.getTarget() == 3) {
+                    sysFile.setUrl_thumb(sysFile.getUrl() + "?x-tos-process=image/resize,w_" + width + ",h_" + height + ",m_pad,color_" + backgroundColor);
+                }
+            }
         }
         return sysFile;
     }
@@ -261,8 +266,8 @@ public class SysFileMultipartServiceImpl implements SysFileMultipartService {
 
         }
 
-        // 如果是图片类型,就设置封面
         /*
+            如果是图片类型,就设置封面:
             图片Icon (png / jpg / jpeg / gif / bmp / webp)
             视频 Icon (mp4 / avi / mov / flv / 3gp / mpeg / wmv)
             Word Icon (doc / docx)

+ 50 - 45
src/main/java/com/backendsys/modules/upload/service/impl/SysFileServiceImpl.java

@@ -33,10 +33,7 @@ import org.springframework.util.StringUtils;
 import org.springframework.web.multipart.MultipartFile;
 
 import java.io.IOException;
-import java.util.Arrays;
-import java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.atomic.AtomicReference;
 import java.util.stream.Collectors;
@@ -66,19 +63,24 @@ public class SysFileServiceImpl extends ServiceImpl<SysFileDao, SysFile> impleme
     //   2:阿里云:
     //   3:抖音云: https://www.volcengine.com/docs/6349/153626
     private SysFile setThumbUrl(SysFile sysFile, Integer width, Integer height, String backgroundColor) {
-        if (StrUtil.isEmpty(sysFile.getUpload_id())) {
-            // 本地上传
-            if (sysFile.getTarget() == -1) {
-                sysFile.setUrl_thumb(sysFile.getUrl() + "?w=" + width + "&h=" + height);
-            }
-            // 腾讯云 (color值通过base64加密, #f8f8f8)
-            if (sysFile.getTarget() == 1) {
-                System.out.println("base64 encode: " + Base64.encode(backgroundColor));
-                sysFile.setUrl_thumb(sysFile.getUrl() + "?imageMogr2/thumbnail/" + width + "x" + height + "/pad/1/color/" + Base64.encode(backgroundColor));
-            }
-            // 抖音云
-            if (sysFile.getTarget() == 3) {
-                sysFile.setUrl_thumb(sysFile.getUrl() + "?x-tos-process=image/resize,w_" + width + ",h_" + height + ",m_pad,color_" + backgroundColor);
+        if (sysFile.getContent_type() != null) {
+            if (sysFile.getContent_type().toLowerCase().contains("image")) {
+                if (StrUtil.isEmpty(sysFile.getUpload_id())) {
+                    // 本地上传
+                    if (sysFile.getTarget() == -1) {
+                        sysFile.setUrl_thumb(sysFile.getUrl() + "?w=" + width + "&h=" + height);
+                    }
+                    // 腾讯云 (color值通过base64加密, #f8f8f8)
+                    if (sysFile.getTarget() == 1) {
+                        backgroundColor = "#" + backgroundColor;
+                        System.out.println("base64 encode: " + Base64.encode(backgroundColor));
+                        sysFile.setUrl_thumb(sysFile.getUrl() + "?imageMogr2/thumbnail/" + width + "x" + height + "/pad/1/color/" + Base64.encode(backgroundColor));
+                    }
+                    // 抖音云
+                    if (sysFile.getTarget() == 3) {
+                        sysFile.setUrl_thumb(sysFile.getUrl() + "?x-tos-process=image/resize,w_" + width + ",h_" + height + ",m_pad,color_" + backgroundColor);
+                    }
+                }
             }
         }
         return sysFile;
@@ -92,39 +94,25 @@ public class SysFileServiceImpl extends ServiceImpl<SysFileDao, SysFile> impleme
         PageUtils.startPage();  // 分页
         List<SysFile> sysFileList = sysFileDao.selectUploadFileList(sysFile);
 
-        Integer UPLOAD_THUMB_SIZE = Convert.toInt(sysCommonService.getCommonByTag("UPLOAD_THUMB_SIZE"));
-
-
-        // 要写成方便调用的封装方法?
-
-        // 根据 Tag 获得 Options
-        JSONArray options = sysCommonService.getCommonOptionByTag("UPLOAD_TARGET");
-        System.out.println(options);
-
-
-
+        // 完成分页渲染
+        PageEntity pageEntity = new PageInfoResult(sysFileList).toEntity();
 
+        // -- 完成分页渲染之后,再做列表格式化 -----------------------------------
+        // [Common] 根据 Tag 获得 Options
+        JSONArray COMMON_OPTIONS = sysCommonService.getCommonOptionByTag("UPLOAD_TARGET");
+        // 遍历列表,赋值公共值翻译
         sysFileList = sysFileList.stream().map(item -> {
-
-
-
-            // 根据 Tag Options 查询出 Label
-            for (Object opt : options) {
-                JSONObject opt_obj = JSONUtil.parseObj(opt);
-                Integer value = Convert.toInt(opt_obj.get("value"));
-                if (item.getTarget() == value) {
-                    item.setTarget_label(Convert.toStr(opt_obj.get("label")));
-                }
-            }
-
-
-            // 不是图片不应该有 url_thumb?
-
-            setThumbUrl(item, UPLOAD_THUMB_SIZE, UPLOAD_THUMB_SIZE, StyleEnums.THUMB_BACKGROUND.getValue());
+            // 查询出 上传存储介质(target) 的翻译
+            String target_label = Convert.toStr(sysCommonService.getLabelByValue(COMMON_OPTIONS, item.getTarget()));
+            item.setTarget_label(target_label);
             return item;
         }).collect(Collectors.toList());
 
-        return new PageInfoResult(sysFileList).toEntity();
+        List<Object> objectList = sysFileList.stream().map(file -> (Object) file).collect(Collectors.toList());
+        pageEntity.setList(objectList);
+        // -----------------------------------------------------------------
+
+        return pageEntity;
     }
 
     @Override
@@ -200,6 +188,23 @@ public class SysFileServiceImpl extends ServiceImpl<SysFileDao, SysFile> impleme
                 sysFileEntity.setMd5(uploadResult.getE_tag().replace("\"", ""));
             }
             sysFileEntity.setTarget(target);
+
+
+            // 获得公共配置
+            List<SysCommon> sysCommonList = sysCommonService.getCommonByCategory("UPLOAD");
+            AtomicReference<Integer> UPLOAD_TARGET = new AtomicReference<>();
+            AtomicReference<Integer> UPLOAD_THUMB_SIZE = new AtomicReference<>();
+            sysCommonList.stream().forEach(sysCommon -> {
+                if (sysCommon.getTag().equals("UPLOAD_TARGET")) UPLOAD_TARGET.set(Convert.toInt(sysCommon.getValue()));
+                if (sysCommon.getTag().equals("UPLOAD_THUMB_SIZE")) UPLOAD_THUMB_SIZE.set(Convert.toInt(sysCommon.getValue()));
+            });
+            System.out.println("[系统配置] 上传目标(-1:本地, 1:腾讯云, 2:阿里云, 3.抖音云): " + UPLOAD_TARGET);
+            System.out.println("[系统配置] 图片的缩略图宽高尺寸 (px): " + UPLOAD_THUMB_SIZE);
+
+            // 设置缩略图
+            sysFileEntity = setThumbUrl(sysFileEntity, UPLOAD_THUMB_SIZE.get(), UPLOAD_THUMB_SIZE.get(), StyleEnums.THUMB_BACKGROUND.getValue());
+
+
             sysFileEntity.setCreate_time(DateUtil.now());
             sysFileEntity.setUpdate_time(DateUtil.now());
             sysFileDao.insert(sysFileEntity);

+ 3 - 0
src/main/resources/mapper/upload/SysFileDao.xml

@@ -11,6 +11,7 @@
         COALESCE(f.upload_chunk_count, '') upload_chunk_count,
         COALESCE(f.upload_chunk_index, '') upload_chunk_index,
         f.user_id,
+        fu.username,
         f.name,
         f.content_type,
         f.url,
@@ -33,6 +34,7 @@
         <result property="upload_chunk_count" column="upload_chunk_count" javaType="java.lang.Integer" />
         <result property="upload_chunk_index" column="upload_chunk_index" javaType="java.lang.Integer" />
         <result property="user_id" column="user_id" javaType="java.lang.Long" />
+        <result property="username" column="username" />
         <result property="name" column="name" />
         <result property="content_type" column="content_type" />
         <result property="url" column="url" />
@@ -49,6 +51,7 @@
         SELECT <include refid="includeFile" />
         FROM sys_file f
         LEFT JOIN sys_file_category fc ON f.category_id = fc.id
+        LEFT JOIN sys_user fu ON f.user_id = fu.id
         <where>
             <if test="name != null and name != ''">
                 AND f.name LIKE CONCAT('%', #{name}, '%')