优化模板,待测试

This commit is contained in:
2025-05-22 14:39:36 +08:00
parent bdd33043b3
commit fcc0390c80
7 changed files with 99 additions and 294 deletions

View File

@@ -77,7 +77,7 @@ public class TemplateUtils {
resolver.setTemplateMode("HTML");
// 设置模板引擎使用这个自定义的模板解析器
templateEngine.setTemplateResolver(resolver);
}else {
} else {
SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
resolver.setPrefix("classpath:/templates/");
resolver.setSuffix(".html");
@@ -111,9 +111,10 @@ public class TemplateUtils {
/**
* 生成图片
*
* @param html html内容
*/
private BufferedImage generate(String html, int width, int height){
private BufferedImage generate(String html, int width, int height) {
try {
// 转换为xhtml
String xhtml = this.htmlToXhtml(html);
@@ -132,13 +133,14 @@ public class TemplateUtils {
/**
* 通过JsonObject进行context内容填充
*
* @param data jsonObject
* @return context
*/
private Context getContext(JSONObject data) {
// 创建Thymeleaf上下文
Context context = new Context();
if ( data != null) {
if (data != null) {
Set<String> keys = data.keySet();
for (String key : keys) {
// 判单是否有图片生成统一后面采用的是_2base64Type
@@ -168,8 +170,9 @@ public class TemplateUtils {
Map<EncodeHintType, Object> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); // 设置字符编码为 UTF-8
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); // 设置纠错级别
// 设置外边距为 0 以去除白边
hints.put(EncodeHintType.MARGIN, 0);
// 设置外边距为1
hints.put(EncodeHintType.MARGIN, 1);
if (type == 1) {
QRCodeWriter qrCodeWriter = new QRCodeWriter();
@@ -182,7 +185,7 @@ public class TemplateUtils {
qrImage = MatrixToImageWriter.toBufferedImage(bitMatrix);
}
if ( qrImage == null ){
if (qrImage == null) {
throw new RRException("图片渲染失败");
}
@@ -212,6 +215,7 @@ public class TemplateUtils {
/**
* xhtml 转换为 Document
*
* @param xhtml xhtml
* @return document
* @throws Exception e
@@ -232,6 +236,7 @@ public class TemplateUtils {
/**
* 转换将html转换为xhtml
*
* @param html html内容
* @return xhtml
*/
@@ -259,9 +264,10 @@ public class TemplateUtils {
/**
* 通过document转换为图片
*
* @param document doc
* @param width 图片的宽度
* @param height 图片的高度
* @param width 图片的宽度
* @param height 图片的高度
* @return bufferedImage
*/
private BufferedImage createImageToDocument(Document document, int width, int height) {
@@ -309,15 +315,16 @@ public class TemplateUtils {
/**
* 校验这个模板内容是不是html字符串而非模板名称
*
* @param template template
* @return 是否是html字符串
*/
private boolean checkIsHtml(String template){
private boolean checkIsHtml(String template) {
try {
String pattern = "<(\"[^\"]*\"|'[^']*'|[^'\">])*>";
Pattern r = Pattern.compile(pattern);
return r.matcher(template).find();
}catch (Exception e) {
} catch (Exception e) {
return false;
}
}
@@ -325,6 +332,7 @@ public class TemplateUtils {
/**
* 将document对象转换为字符串
*
* @param doc document
* @return document转换为的字符串
*/