feat:增加模板相关接口

This commit is contained in:
石崇礼 2025-01-19 16:00:40 +08:00
parent 1474746864
commit 9073fd9180
9 changed files with 11473 additions and 0 deletions

33
pom.xml
View File

@ -71,12 +71,45 @@
<version>${pdfbox.version}</version>
</dependency>
<!-- json处理-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<!-- thymeleaf-->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
</dependency>
<!-- Flying Saucer for HTML to Image 把html转换为图片 -->
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-core</artifactId>
<version>9.1.22</version>
</dependency>
<!-- ZXing for QR Code 二维码生成-->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.4.1</version>
</dependency>
<!-- JSoup HTML Parser -->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.15.4</version>
</dependency>
<dependency>
<groupId>ognl</groupId>
<artifactId>ognl</artifactId>
<version>3.1.28</version> <!-- 可以根据实际情况调整版本 -->
</dependency>
</dependencies>

View File

@ -0,0 +1,37 @@
package com.dpkj.common.config;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
/**
* mvcConfig配置类
*
* @author <a href="https://gitee.com/shi-chongli">石头人</a>
* @since 2023-05-23 15:24:43
*/
@RequiredArgsConstructor
@Configuration
public class MvcConfig extends WebMvcConfigurationSupport {
/**
* 添加资源过滤处理器
* @param registry 资源处理器
*/
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**")
.addResourceLocations("classpath:/static/");
registry.addResourceHandler("/swagger-ui.html/**")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
registry.addResourceHandler("/swagger-ui/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/")
.resourceChain(false);
super.addResourceHandlers(registry);
}
}

View File

@ -0,0 +1,48 @@
package com.dpkj.modules.autoReplyPrint.controller;
import com.alibaba.fastjson.JSONObject;
import com.dpkj.common.vo.Result;
import com.dpkj.modules.autoReplyPrint.service.impl.TemplateService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
/**
* 打印模板控制层
*
* @author <a href="https://gitee.com/shi-chongli">石头人</a>
* @version 1.0
* @since 2025-01-17 15:25:03
*/
@RestController
@RequestMapping("/autoReplyPrint")
public class TemplateController {
@Resource
private TemplateService templateService;
@GetMapping("/template/{templateName}")
public Result<String> testTemplate(@RequestParam Integer width, @RequestParam Integer height, String jsonData, @PathVariable String templateName) {
this.templateService.generateReceiptImage(JSONObject.parseObject(jsonData), templateName, width, height);
return Result.ok("模板生成成功");
}
@GetMapping("/template")
public Result<String> testTemplate(String jsonData, @RequestParam Integer width, @RequestParam Integer height) {
String html = "<!DOCTYPE html>\n" +
"<html>\n" +
"<head><title>Test</title></head>\n" +
"<body>\n" +
"<h1>你好,我是一个好的模板</h1>\n" +
"<h2><span th:text='${name}'> </span></h2>\n" +
"</body>\n" +
"</html>";
this.templateService.generateReceiptImage(JSONObject.parseObject(jsonData), html, width, height);
return Result.ok("模板生成成功");
}
}

View File

@ -0,0 +1,326 @@
package com.dpkj.modules.autoReplyPrint.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.dpkj.common.exception.RRException;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebApplicationContext;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.StringTemplateResolver;
import org.w3c.dom.Document;
import org.xhtmlrenderer.layout.SharedContext;
import org.xhtmlrenderer.simple.Graphics2DRenderer;
import javax.imageio.ImageIO;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import org.thymeleaf.context.Context;
/**
* 模板服务类
*
* @author <a href="https://gitee.com/shi-chongli">石头人</a>
* @version 1.0
* @since 2025-01-18 15:33:51
*/
@Service
@Slf4j
public class TemplateService {
/**
* 生成小票图片
* @param data json数据用来填充模板
* @param template 模板html字符串或者模板名称
* @param width 图片宽度
* @param height 图片高度
* @return 图片字节数组
*/
public byte[] generateReceiptImage(JSONObject data, String template, int width, int height) {
try {
// 获取模板上下文
Context context = this.getContext(data);
TemplateEngine templateEngine = new TemplateEngine();
if (checkIsHtml(template)) {
StringTemplateResolver resolver = new StringTemplateResolver();
resolver.setTemplateMode("HTML");
// 设置模板引擎使用这个自定义的模板解析器
templateEngine.setTemplateResolver(resolver);
}else {
SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
resolver.setPrefix("classpath:/templates/");
resolver.setSuffix(".html");
resolver.setTemplateMode(TemplateMode.HTML);
resolver.setCacheable(true);
resolver.setApplicationContext(new AnnotationConfigReactiveWebApplicationContext());
templateEngine.setTemplateResolver(resolver);
}
// 渲染模板
String html = templateEngine.process(template, context);
return this.generate(html, width, height);
} catch (Exception e) {
e.printStackTrace();
throw new RRException("图片打印失败");
}
}
/**
* 生成图片
* @param html html内容
*/
private byte[] generate(String html, int width, int height){
try {
// 转换为xhtml
String xhtml = this.htmlToXhtml(html);
// 转换为document
Document document = this.xhtmlToDocument(xhtml);
// 生成图片
BufferedImage image = createImageToDocument(document, width, height);
// 保存图片
String outputPath = "E:\\images\\test_" + System.currentTimeMillis() + ".png";
ImageIO.write(image, "PNG", new File(outputPath));
ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
ImageIO.write(image, "PNG", byteOutputStream);
return byteOutputStream.toByteArray();
} catch (Exception e) {
e.printStackTrace();
throw new RRException("图片打印失败");
}
}
/**
* 通过JsonObject进行context内容填充
* @param data jsonObject
* @return context
*/
private Context getContext(JSONObject data) {
// 创建Thymeleaf上下文
Context context = new Context();
Set<String> keys = data.keySet();
for (String key : keys) {
// 判单是否有图片生成统一后面采用的是_2base64Type
String[] split = key.split("_");
if (split.length > 1 && split[1].equals("2base64Type")) {
int width = split.length > 2 ? Integer.parseInt(split[2]) : 100;
int height = split.length > 3 ? Integer.parseInt(split[3]) : 100;
// 如果是图片类型,需要进行base64转换
String base64 = this.generateQRCode(String.valueOf(data.get(key)), width, height);
context.setVariable(split[0], "data:image/jpeg;base64," + base64);
} else {
// 普通字段直接设置
context.setVariable(key, data.get(key));
}
}
return context;
}
/**
* 根据内容生成二维码
* @param content 转换内容
*/
private String generateQRCode(String content, int width, int height) {
try {
Map<EncodeHintType, Object> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); // 设置字符编码为 UTF-8
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); // 设置纠错级别
QRCodeWriter qrCodeWriter = new QRCodeWriter();
BitMatrix bitMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
BufferedImage qrImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
qrImage.createGraphics();
Graphics2D graphics = (Graphics2D) qrImage.getGraphics();
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, width, height);
graphics.setColor(Color.BLACK);
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
if (bitMatrix.get(i, j)) {
graphics.fillRect(i, j, 1, 1);
}
}
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(qrImage, "png", baos);
return Base64.getEncoder().encodeToString(baos.toByteArray());
}catch (Exception e){
log.error("二维码生成失败");
throw new RRException("二维码生成失败");
}
}
/**
* xhtml 转换为 Document
* @param xhtml xhtml
* @return document
* @throws Exception e
*/
private Document xhtmlToDocument(String xhtml) throws Exception {
// 创建DocumentBuilder
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(false);
factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
DocumentBuilder builder = factory.newDocumentBuilder();
// 解析XHTML字符串为Document
return builder.parse(new ByteArrayInputStream(xhtml.getBytes(StandardCharsets.UTF_8)));
}
/**
* 转换将html转换为xhtml
* @param html html内容
* @return xhtml
*/
private String htmlToXhtml(String html) {
org.jsoup.nodes.Document doc = Jsoup.parse(html);
doc.outputSettings()
.syntax(org.jsoup.nodes.Document.OutputSettings.Syntax.xml)
.charset(StandardCharsets.UTF_8);
// 不使用外部DTD
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<html xmlns=\"http://www.w3.org/1999/xhtml\">\n" +
"<head>\n" +
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>\n" +
"<style type=\"text/css\">\n" +
"body { font-family: SimSun, serif; }\n" +
"</style>\n" +
"</head>\n" +
"<body>\n" +
doc.body().html() +
"\n</body>\n" +
"</html>";
}
/**
* 通过document转换为图片
* @param document doc
* @param width 图片的宽度
* @param height 图片的高度
* @return bufferedImage
*/
private BufferedImage createImageToDocument(Document document, int width, int height) {
try {
// 创建Dimension对象
Dimension dimension = new Dimension(width, height);
// 创建图片
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics = (Graphics2D) image.getGraphics();
// 设置白色背景
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, width, height);
// 设置渲染提示
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
// 创建渲染器
Graphics2DRenderer renderer = new Graphics2DRenderer();
renderer.setDocument(document, null);
// 设置渲染参数
SharedContext sharedContext = renderer.getSharedContext();
sharedContext.setInteractive(false);
sharedContext.setDPI(256f);
// 使用Dimension对象进行布局
renderer.layout(graphics, dimension);
renderer.render(graphics);
graphics.dispose();
return image;
} catch (Exception e) {
throw new RRException("渲染图片失败", e);
}
}
/**
* 校验这个模板内容是不是html字符串而非模板名称
* @param template template
* @return 是否是html字符串
*/
private boolean checkIsHtml(String template){
try {
String pattern = "<(\"[^\"]*\"|'[^']*'|[^'\">])*>";
Pattern r = Pattern.compile(pattern);
return r.matcher(template).find();
}catch (Exception e) {
return false;
}
}
/**
* 将document对象转换为字符串
* @param doc document
* @return document转换为的字符串
*/
private String documentToString(Document doc) {
try {
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
StringWriter writer = new StringWriter();
transformer.transform(new DOMSource(doc), new StreamResult(writer));
return writer.getBuffer().toString();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}

View File

View File

@ -0,0 +1,28 @@
/**
* 获取路径参数
* @returns {{}}
*/
function getQueryString() {
let qs = location.search.substring(1), // 获取url中"?"符后的字串
args = {}, // 保存参数数据的对象
items = qs.length ? qs.split("&") : [], // 取得每一个参数项,
item = null,
len = items.length;
for(let i = 0; i < len; i++) {
item = items[i].split("=");
let name = decodeURIComponent(item[0]),
value = decodeURIComponent(item[1]);
if(name) {
args[name] = value;
}
}
return args;
}
/**
* 前往首页
*/
function loginToIndexFn(loginToIndex=getQueryString()["loginToIndex"]){
window.location.assign(loginToIndex);
}

View File

@ -0,0 +1,88 @@
/**
* 获取路径参数
*/
let myLoginToIndex = "";
let registerBind = "";
let bindExisting = "";
try {
let queryString = getQueryString();
myLoginToIndex = queryString["loginToIndex"]
registerBind = queryString["registerBind"]
bindExisting = queryString['bindExisting']
} catch (ignore){}
/**
* 注册并绑定
*/
function registerAndBind(){
let email = $("#email").val();
$.ajax({
url: registerBind,
type: 'POST',
data: {
email: email
},
success: function (result){
if ( result.code === 200 ){
window.location.assign(myLoginToIndex);
}else {
alert(result.message);
}
}
})
}
/**
* 点击展示或者隐藏绑定输入框
*/
function show(){
let bindChange = document.getElementById("bind");
let computedStyle = window.getComputedStyle(bindChange);
let displayValue = computedStyle.getPropertyValue("display");
let beginValue = displayValue;
displayValue = displayValue === "none" ? "block" : "none";
$("#bind").css("display", displayValue);
$("#register-email").css("display", "none");
}
/**
* 点击展示输入注册邮箱的输入框
*/
function registerAndBindShow() {
let bindChange = document.getElementById("register-email");
let computedStyle = window.getComputedStyle(bindChange);
let displayValue = computedStyle.getPropertyValue("display");
let beginValue = displayValue;
displayValue = displayValue === "none" ? "block" : "none";
// computedStyle.setProperty("display", displayValue)
$("#bind").css("display", "none");
$("#register-email").css("display", displayValue);
}
/**
* 绑定已经注册了的账号
*/
function bindExistingAccount(){
let username = document.getElementById("username").value;
let password = document.getElementById("password").value;
if ( username === "" || password === ""){
alert("用户名或者密码未进行输入");
return;
}
$.ajax({
url: bindExisting,
type: 'POST',
data: {
account: username,
password: password
},
success: function (result){
if ( result.code === 200 ){
window.location.assign(myLoginToIndex);
}else {
alert(result.message);
}
}
})
}

10872
src/main/resources/static/js/jquery351.js vendored Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,41 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>挂号单</title>
</head>
<body>
<div class="receipt">
<div style="margin-top: 30px;">
<div style="text-align: center; font-size: 20px; font-weight: 700;"><span th:text="${hospitalName}"></span></div>
<div style="text-align: center">****<span th:text="${registeTerminalName}"></span></span>****</div>
<div style="margin-bottom: 5px; text-align: center;">---------------------------------------------------------------------</div>
<div style="text-align: center; font-size: 20px; font-weight: 700;"><span th:text="${registeType}"></span></div>
<div style="text-align: center;">---------------------------------------------------------------------</div>
</div>
<div >
<div style="margin-left: 21px;">&emsp;&emsp;名:<span th:text="${name}"></span></div>
<div style="margin-left: 21px;">&emsp;&emsp;别:<span th:text="${gender}"></span></div>
<div style="margin-left: 21px;">&emsp;&emsp;龄:<span th:text="${age}"></span></div>
<div style="margin-left: 21px;">出生日期:<span th:text="${birthDate}"></span></div>
<div style="margin-left: 21px;">&emsp;&emsp;号:<span th:text="${cardNumber}"></span></div>
<div style="margin-left: 21px;">&ensp;&ensp;号:<span th:text="${outpatientNumber}"></span></div>
<div style="margin-left: 21px;">就诊科室:<span th:text="${department}"></span></div>
<div style="margin-left: 21px;">出诊级别:<span th:text="${visitLevel}"></span></div>
<div style="margin-left: 21px;">就诊医生:<span th:text="${doctor}"></span></div>
<div style="margin-left: 21px;">&emsp;&emsp;序:<span th:text="${sequence}"></span></div>
<div style="margin-left: 21px;">挂号日期:<span th:text="${registerDate}"></span></div>
<div style="margin-left: 21px;">&ensp;&ensp;用:<span th:text="${totalFee}"></span></div>
<div style="margin-left: 21px;">支付方式:<span th:text="${paymentMethod}"></span></div>
<div style="margin-left: 21px;">&ensp;&ensp;号:<span th:text="${orderNumber}"></span></div>
<div style="margin-left: 21px;">交易流水:<span th:text="${transactionNumber}"></span></div>
<img th:src="${qrCodeBase64}" alt="QR Code"/>
</div>
<div>
<div style="margin-top: -20px; text-align: center;">---------------------------------------------------------------------</div>
<div style="margin-left: 21px; font-weight: 700;">备注(REFERENCE):凭此条退费,请妥善保管!</div>
<div style="margin-left: 21px;">&ensp;&ensp;号:<span th:text="${terminalNumber}"></span></div>
<div style="margin-left: 21px;">打印时间:<span th:text="${printTime}"></span></div>
</div>
</div>
</body>
</html>