Browse Source

refactor(bszx): 重构报名和支付海报生成逻辑

- 提取共用的 ImageCombiner 初始化和处理逻辑
- 优化文件路径生成和质量压缩流程- 添加 @NotNull 注解提高代码可读性
- 修改时间格式化方式,使用 LocalDateTime
- 更新 Jackson 配置,支持 LocalDateTime序列化和反序列化- 添加文件服务器地址配置
pan
科技小王子 3 weeks ago
parent
commit
f821658e78
  1. 65
      src/main/java/com/gxwebsoft/bszx/service/impl/BszxBmServiceImpl.java
  2. 20
      src/main/java/com/gxwebsoft/bszx/service/impl/BszxPayServiceImpl.java
  3. 5
      src/main/java/com/gxwebsoft/cms/param/CmsWebsiteParam.java
  4. 6
      src/main/java/com/gxwebsoft/cms/service/impl/CmsWebsiteServiceImplHelper.java
  5. 29
      src/main/java/com/gxwebsoft/common/core/config/JacksonConfig.java
  6. 2
      src/main/resources/application-prod.yml
  7. 3
      src/main/resources/application.yml

65
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<BszxBmMapper, BszxBm> 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<BszxBmMapper, BszxBm> 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<BszxBm>().eq(BszxBm::getUserId, userId).last("limit 1"));

20
src/main/java/com/gxwebsoft/bszx/service/impl/BszxPayServiceImpl.java

@ -124,13 +124,14 @@ public class BszxPayServiceImpl extends ServiceImpl<BszxPayMapper, BszxPay> 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<BszxPayMapper, BszxPay> 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<BszxPayMapper, BszxPay> 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);

5
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)

6
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());

29
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;
}
}

2
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/

3
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:

Loading…
Cancel
Save