feat:增加利盟服务接口调用工具类
This commit is contained in:
@@ -63,7 +63,7 @@ public class TemplateUtils {
|
||||
* @param saveDir 图片的保存路径,如果为空,那么不进行图片的保存
|
||||
* @return 图片字节数组
|
||||
*/
|
||||
public byte[] generateReceiptImage(JSONObject data, String template, int width, int height, String saveDir) {
|
||||
public byte[] generateReceiptImage(JSONObject data, String template, int width, int height, StringBuilder saveDir) {
|
||||
try {
|
||||
// 获取模板上下文
|
||||
Context context = this.getContext(data);
|
||||
@@ -90,11 +90,12 @@ public class TemplateUtils {
|
||||
BufferedImage image = this.generate(html, width, height);
|
||||
|
||||
// 保存图片
|
||||
if (saveDir != null && !"".equals(saveDir)) {
|
||||
String outputPath = saveDir + "\\genera_image_" + System.currentTimeMillis() + ".png";
|
||||
if (saveDir != null && !"".equals(saveDir.toString())) {
|
||||
String outputPath = saveDir.toString() + "\\genera_image_" + System.currentTimeMillis() + ".png";
|
||||
ImageIO.write(image, "PNG", new File(outputPath));
|
||||
saveDir.reverse();
|
||||
saveDir.append(outputPath);
|
||||
}
|
||||
|
||||
ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
|
||||
ImageIO.write(image, "PNG", byteOutputStream);
|
||||
return byteOutputStream.toByteArray();
|
||||
@@ -134,19 +135,21 @@ public class TemplateUtils {
|
||||
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));
|
||||
if ( data != null) {
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
80
src/main/java/com/dpkj/common/utils/ThirdService.java
Normal file
80
src/main/java/com/dpkj/common/utils/ThirdService.java
Normal file
@@ -0,0 +1,80 @@
|
||||
package com.dpkj.common.utils;
|
||||
|
||||
import com.dpkj.common.dto.LexMarkDTO;
|
||||
import com.dpkj.common.dto.LexMarkResultDTO;
|
||||
import com.dpkj.common.exception.RRException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
* 第三方服务,主要是调用打印机等
|
||||
*
|
||||
* @author <a href="https://gitee.com/shi-chongli">石头人</a>
|
||||
* @version 1.0
|
||||
* @since 2025-02-08 10:45:01
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class ThirdService {
|
||||
|
||||
@Value("${app.custom.lexMarkServiceIp}")
|
||||
private String lexMarkServiceIp;
|
||||
@Value("${app.custom.lexMarkServicePort}")
|
||||
private String lexMarkServicePort;
|
||||
|
||||
/**
|
||||
* 利盟台式机-立体机 接口请求
|
||||
* @param lexMarkDTO 请求DTO
|
||||
* @return result
|
||||
*/
|
||||
public LexMarkResultDTO callDevice(LexMarkDTO lexMarkDTO) {
|
||||
try {
|
||||
URL url = new URL(lexMarkServiceIp + ":" + lexMarkServicePort + "/CallDevice");
|
||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
connection.setRequestMethod("POST");
|
||||
connection.setDoOutput(true);
|
||||
connection.setRequestProperty("Content - Type", "application/json");
|
||||
|
||||
// 将LexMarkDTO对象转换为JSON字符串
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
String jsonInputString = objectMapper.writeValueAsString(lexMarkDTO);
|
||||
|
||||
try (DataOutputStream wr = new DataOutputStream(connection.getOutputStream())) {
|
||||
byte[] input = jsonInputString.getBytes(StandardCharsets.UTF_8);
|
||||
wr.write(input, 0, input.length);
|
||||
}
|
||||
|
||||
int responseCode = connection.getResponseCode();
|
||||
if (responseCode!= HttpURLConnection.HTTP_OK) {
|
||||
log.error("利盟服务请求失败,响应码:{},请求参数:{}", responseCode, lexMarkDTO);
|
||||
throw new RRException("利盟服务请求失败,响应码:" + responseCode);
|
||||
}
|
||||
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||
String inputLine;
|
||||
StringBuilder response = new StringBuilder();
|
||||
while ((inputLine = in.readLine())!= null) {
|
||||
response.append(inputLine);
|
||||
}
|
||||
in.close();
|
||||
|
||||
// 将响应JSON字符串转换为LexMarkResultDTO对象
|
||||
ObjectMapper resultMapper = new ObjectMapper();
|
||||
return resultMapper.readValue(response.toString(), LexMarkResultDTO.class);
|
||||
}catch (Exception e){
|
||||
log.error("利盟服务请求失败:{}", lexMarkDTO, e);
|
||||
throw new RRException("利盟服务请求失败");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user