From f821658e78b1b7ec35626957c62e4cece534e913 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E5=BF=A0=E6=9E=97?= <170083662@qq.com> Date: Tue, 9 Sep 2025 14:21:18 +0800 Subject: [PATCH] =?UTF-8?q?refactor(bszx):=20=E9=87=8D=E6=9E=84=E6=8A=A5?= =?UTF-8?q?=E5=90=8D=E5=92=8C=E6=94=AF=E4=BB=98=E6=B5=B7=E6=8A=A5=E7=94=9F?= =?UTF-8?q?=E6=88=90=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 提取共用的 ImageCombiner 初始化和处理逻辑 - 优化文件路径生成和质量压缩流程- 添加 @NotNull 注解提高代码可读性 - 修改时间格式化方式,使用 LocalDateTime - 更新 Jackson 配置,支持 LocalDateTime序列化和反序列化- 添加文件服务器地址配置 --- .../bszx/service/impl/BszxBmServiceImpl.java | 65 ++++++++++--------- .../bszx/service/impl/BszxPayServiceImpl.java | 20 +++--- .../gxwebsoft/cms/param/CmsWebsiteParam.java | 5 +- .../impl/CmsWebsiteServiceImplHelper.java | 6 +- .../common/core/config/JacksonConfig.java | 29 ++++++++- src/main/resources/application-prod.yml | 2 + src/main/resources/application.yml | 3 + 7 files changed, 86 insertions(+), 44 deletions(-) diff --git a/src/main/java/com/gxwebsoft/bszx/service/impl/BszxBmServiceImpl.java b/src/main/java/com/gxwebsoft/bszx/service/impl/BszxBmServiceImpl.java index 3ca601b..8051d41 100644 --- a/src/main/java/com/gxwebsoft/bszx/service/impl/BszxBmServiceImpl.java +++ b/src/main/java/com/gxwebsoft/bszx/service/impl/BszxBmServiceImpl.java @@ -23,6 +23,7 @@ import com.gxwebsoft.common.core.utils.FileServerUtil; import com.gxwebsoft.common.core.utils.ImageUtil; import com.gxwebsoft.common.core.web.PageParam; import com.gxwebsoft.common.core.web.PageResult; +import org.jetbrains.annotations.NotNull; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; @@ -113,41 +114,18 @@ public class BszxBmServiceImpl extends ServiceImpl impleme return "https://oss.wsdns.cn/20250908/f8b88d734aac4006a89b9aeb5c3f4f75.png"; } - + // 百色中学 // Font font = new Font("阿里巴巴普惠体", Font.PLAIN, 40); //合成器(指定背景图和输出格式,整个图片的宽高和相关计算依赖于背景图,所以背景图的大小是个基准) - ImageCombiner combiner = new ImageCombiner(article.getAddress(), OutputFormat.JPG); - //加文本元素:姓名 -// if (item.getType().equals(0)) { -// combiner.addTextElement(item.getName().concat(" 校友"), 40, 220, 540); -// } else { -// combiner.addTextElement(item.getName(), 40, 220, 540); -// } - -// combiner.addTextElement(DateUtil.format(DateUtil.date(), "yyyy年MM月"), 28,650, 1566); - //加图片元素:盖章 -// combiner.addImageElement("https://oss.wsdns.cn/20250304/6936b109b09b4919a3498ac5027e728b.png", 600, 1420); - - - if (item.getType().equals(0)) { - combiner.addTextElement(item.getName().concat(" 校友"), 30, 160, 1008); - } else { - combiner.addTextElement(item.getName(), 30, 160, 1008); - } + final ImageCombiner combiner = getImageCombiner(item, article); -// combiner.addTextElement(DateUtil.format(DateUtil.date(), "yyyy年MM月"), 28,650, 1566); - //加图片元素:盖章 -// combiner.addImageElement("https://oss.wsdns.cn/20250304/6936b109b09b4919a3498ac5027e728b.png", 600, 1420); - //执行图片合并 - combiner.combine(); - - if (!FileUtil.exist(uploadPath + "/file/poster/" + item.getTenantId() + "/bm")) { - FileUtil.mkdir(uploadPath + "/file/poster/" + item.getTenantId() + "/bm"); + if (!FileUtil.exist(uploadPath + "/poster/" + item.getTenantId() + "/bm")) { + FileUtil.mkdir(uploadPath + "/poster/" + item.getTenantId() + "/bm"); } String basePath = "/poster/" + item.getTenantId() + "/bm/big-" + item.getId() + ".jpg"; String smallPath = "/poster/" + item.getTenantId() + "/bm/" + item.getId() + ".jpg"; - String filename = uploadPath + "/file" + basePath; - String smallFileName = uploadPath + "/file" + smallPath; + String filename = uploadPath + basePath; + String smallFileName = uploadPath + smallPath; combiner.save(filename); File input = new File(filename); @@ -161,6 +139,35 @@ public class BszxBmServiceImpl extends ServiceImpl impleme return null; } + @NotNull + private static ImageCombiner getImageCombiner(BszxBm item, CmsArticle article) throws Exception { + ImageCombiner combiner = new ImageCombiner(article.getAddress(), OutputFormat.JPG); + //加文本元素:姓名 +// if (item.getType().equals(0)) { +// combiner.addTextElement(item.getName().concat(" 校友"), 40, 220, 540); +// } else { +// combiner.addTextElement(item.getName(), 40, 220, 540); +// } + +// combiner.addTextElement(DateUtil.format(DateUtil.date(), "yyyy年MM月"), 28,650, 1566); + //加图片元素:盖章 +// combiner.addImageElement("https://oss.wsdns.cn/20250304/6936b109b09b4919a3498ac5027e728b.png", 600, 1420); + + + if (item.getType().equals(0)) { + combiner.addTextElement(item.getName().concat(" 校友"), 30, 160, 1008); + } else { + combiner.addTextElement(item.getName(), 30, 160, 1008); + } + +// combiner.addTextElement(DateUtil.format(DateUtil.date(), "yyyy年MM月"), 28,650, 1566); + //加图片元素:盖章 +// combiner.addImageElement("https://oss.wsdns.cn/20250304/6936b109b09b4919a3498ac5027e728b.png", 600, 1420); + //执行图片合并 + combiner.combine(); + return combiner; + } + @Override public BszxBm getByUserId(Integer userId) { return getOne(new LambdaQueryWrapper().eq(BszxBm::getUserId, userId).last("limit 1")); diff --git a/src/main/java/com/gxwebsoft/bszx/service/impl/BszxPayServiceImpl.java b/src/main/java/com/gxwebsoft/bszx/service/impl/BszxPayServiceImpl.java index 273068c..ea7de78 100644 --- a/src/main/java/com/gxwebsoft/bszx/service/impl/BszxPayServiceImpl.java +++ b/src/main/java/com/gxwebsoft/bszx/service/impl/BszxPayServiceImpl.java @@ -124,13 +124,14 @@ public class BszxPayServiceImpl extends ServiceImpl impl //执行图片合并 combiner.combine(); - if (!FileUtil.exist(uploadPath + "/file/poster/" + payCert.getTenantId() + "/pay")) { - FileUtil.mkdir(uploadPath + "/file/poster/" + payCert.getTenantId() + "/pay"); + if (!FileUtil.exist(uploadPath + "/poster/" + payCert.getTenantId() + "/pay")) { + FileUtil.mkdir(uploadPath + "/poster/" + payCert.getTenantId() + "/pay"); } String basePath = "/poster/" + payCert.getTenantId() + "/pay/big-" + id + ".jpg"; String smallPath = "/poster/" + payCert.getTenantId() + "/pay/" + id + ".jpg"; - String filename = uploadPath + "/file" + basePath; - String smallFileName = uploadPath + "/file" + smallPath; + String filename = uploadPath + basePath; + String smallFileName = uploadPath + smallPath; + System.out.println("smallFileName = " + smallFileName); combiner.save(filename); File input = new File(filename); @@ -139,8 +140,9 @@ public class BszxPayServiceImpl extends ServiceImpl impl if (input.exists()) { input.delete(); } - return fileServer + smallPath + "?r=" + RandomUtil.randomNumbers(4); + return fileServer + smallPath + "?v=" + RandomUtil.randomNumbers(4); } + // 百色中学 if (ObjectUtil.isNotEmpty(payCert)) { //合成器(指定背景图和输出格式,整个图片的宽高和相关计算依赖于背景图,所以背景图的大小是个基准) ImageCombiner combiner = new ImageCombiner("https://oss.wsdns.cn/20250420/811a380e8e124097aa0940a7c68a1f72.jpeg", OutputFormat.JPG); @@ -171,13 +173,13 @@ public class BszxPayServiceImpl extends ServiceImpl impl //执行图片合并 combiner.combine(); - if (!FileUtil.exist(uploadPath + "/file/poster/" + payCert.getTenantId() + "/pay")) { - FileUtil.mkdir(uploadPath + "/file/poster/" + payCert.getTenantId() + "/pay"); + if (!FileUtil.exist(uploadPath + "/poster/" + payCert.getTenantId() + "/pay")) { + FileUtil.mkdir(uploadPath + "/poster/" + payCert.getTenantId() + "/pay"); } String basePath = "/poster/" + payCert.getTenantId() + "/pay/big-" + id + ".jpg"; String smallPath = "/poster/" + payCert.getTenantId() + "/pay/" + id + ".jpg"; - String filename = uploadPath + "/file" + basePath; - String smallFileName = uploadPath + "/file" + smallPath; + String filename = uploadPath + basePath; + String smallFileName = uploadPath + smallPath; combiner.save(filename); File input = new File(filename); diff --git a/src/main/java/com/gxwebsoft/cms/param/CmsWebsiteParam.java b/src/main/java/com/gxwebsoft/cms/param/CmsWebsiteParam.java index 423a59c..2cf16b1 100644 --- a/src/main/java/com/gxwebsoft/cms/param/CmsWebsiteParam.java +++ b/src/main/java/com/gxwebsoft/cms/param/CmsWebsiteParam.java @@ -1,6 +1,7 @@ package com.gxwebsoft.cms.param; import com.baomidou.mybatisplus.annotation.TableField; +import com.fasterxml.jackson.annotation.JsonFormat; import com.gxwebsoft.common.core.annotation.QueryField; import com.gxwebsoft.common.core.annotation.QueryType; import com.gxwebsoft.common.core.web.BaseParam; @@ -10,6 +11,7 @@ import io.swagger.v3.oas.annotations.media.Schema; import lombok.Data; import lombok.EqualsAndHashCode; +import java.time.LocalDateTime; import java.util.Set; /** @@ -78,7 +80,8 @@ public class CmsWebsiteParam extends BaseParam { private Integer version; @Schema(description = "服务到期时间") - private String expirationTime; + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") + private LocalDateTime expirationTime; @Schema(description = "模版ID") @QueryField(type = QueryType.EQ) diff --git a/src/main/java/com/gxwebsoft/cms/service/impl/CmsWebsiteServiceImplHelper.java b/src/main/java/com/gxwebsoft/cms/service/impl/CmsWebsiteServiceImplHelper.java index 4973065..4a37118 100644 --- a/src/main/java/com/gxwebsoft/cms/service/impl/CmsWebsiteServiceImplHelper.java +++ b/src/main/java/com/gxwebsoft/cms/service/impl/CmsWebsiteServiceImplHelper.java @@ -67,9 +67,9 @@ public class CmsWebsiteServiceImplHelper { } // 时间字段 - 格式化为字符串 - if (website.getExpirationTime() != null) { - vo.setExpirationTime(website.getExpirationTime().format(formatter)); - } +// if (website.getExpirationTime() != null) { +// vo.setExpirationTime(website.getExpirationTime().format(formatter)); +// } // 过期相关信息 vo.setExpired(website.getExpired()); diff --git a/src/main/java/com/gxwebsoft/common/core/config/JacksonConfig.java b/src/main/java/com/gxwebsoft/common/core/config/JacksonConfig.java index f1a87d0..e8ec164 100644 --- a/src/main/java/com/gxwebsoft/common/core/config/JacksonConfig.java +++ b/src/main/java/com/gxwebsoft/common/core/config/JacksonConfig.java @@ -4,10 +4,15 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer; +import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + /** * Jackson配置类 - 强制配置版本 * 确保JavaTimeModule被正确注册并覆盖Spring Boot的自动配置 @@ -18,6 +23,11 @@ import org.springframework.context.annotation.Primary; @Configuration public class JacksonConfig { + /** + * 日期时间格式 + */ + private static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss"; + /** * 强制配置ObjectMapper,确保JavaTimeModule被正确注册 */ @@ -26,8 +36,16 @@ public class JacksonConfig { public ObjectMapper objectMapper() { ObjectMapper mapper = new ObjectMapper(); + // 创建并配置JavaTimeModule + JavaTimeModule javaTimeModule = new JavaTimeModule(); + + // 配置LocalDateTime的序列化和反序列化格式 + DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_TIME_FORMAT); + javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(formatter)); + javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(formatter)); + // 注册JavaTimeModule - 这是关键 - mapper.registerModule(new JavaTimeModule()); + mapper.registerModule(javaTimeModule); // 禁用将日期写为时间戳 mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); @@ -43,6 +61,13 @@ public class JacksonConfig { */ @Bean public JavaTimeModule javaTimeModule() { - return new JavaTimeModule(); + JavaTimeModule module = new JavaTimeModule(); + + // 配置LocalDateTime的序列化和反序列化格式 + DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_TIME_FORMAT); + module.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(formatter)); + module.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(formatter)); + + return module; } } diff --git a/src/main/resources/application-prod.yml b/src/main/resources/application-prod.yml index fbff897..557caab 100644 --- a/src/main/resources/application-prod.yml +++ b/src/main/resources/application-prod.yml @@ -45,6 +45,8 @@ mqtt: # 框架配置 config: + # 文件服务器 + file-server: https://file-s209.shoplnk.cn # 生产环境接口 server-url: https://server.websoft.top/api upload-path: /www/wwwroot/file.ws/ diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 7d9426e..b82967e 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -26,6 +26,9 @@ spring: write-dates-as-timestamps: false deserialization: fail-on-unknown-properties: false + # 启用JSR310模块支持Java 8时间类型 + modules: + - com.fasterxml.jackson.datatype.jsr310.JavaTimeModule # 连接池配置 datasource: