feat:增加利盟服务接口调用工具类
This commit is contained in:
parent
0ebf6d4258
commit
117f293e83
|
@ -0,0 +1,63 @@
|
||||||
|
package com.dpkj.common.dto;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.ToString;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 利盟请求参数DTO
|
||||||
|
*
|
||||||
|
* @author <a href="https://gitee.com/shi-chongli">石头人</a>
|
||||||
|
* @version 1.0
|
||||||
|
* @since 2025-02-08 10:46:44
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@ToString
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class LexMarkDTO implements Serializable {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 默认异步
|
||||||
|
* 0:代表同步,1:代表异步,3:代表属性
|
||||||
|
*/
|
||||||
|
private Integer methodType = 1;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 默认:com.gwi.device
|
||||||
|
* 插件名称:如果是设备调用,则固定填写com.gwi.device;如果是插件调用,此处填
|
||||||
|
* 写插件动态库文件名(去掉文件名前面的lib和文件后缀名,如:GWI_Plugin)
|
||||||
|
*/
|
||||||
|
private String pluginName = "com.gwi.device";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 固定填写execute
|
||||||
|
*/
|
||||||
|
private String pluginMethod = "execute";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设备名。如果是设备调用,则填写设备名称,需要和SP服务配置中的 DevList.ini 配置的设备名称一致;
|
||||||
|
* 如果是插件调用,可忽略此参数
|
||||||
|
*/
|
||||||
|
private String devName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 调用id,应用生成,用于区分不同的调用
|
||||||
|
*/
|
||||||
|
private Integer callID;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 调用的函数名称,设备执行的动作名称,简称动作名
|
||||||
|
*/
|
||||||
|
private String actionName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设备执行的输入参数,格式为json字符串(注意,此处为字符串,不是json对象)
|
||||||
|
*/
|
||||||
|
private String param;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,101 @@
|
||||||
|
package com.dpkj.common.dto;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.ToString;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 利盟接口返回值DTO
|
||||||
|
*
|
||||||
|
* @author <a href="https://gitee.com/shi-chongli">石头人</a>
|
||||||
|
* @version 1.0
|
||||||
|
* @since 2025-02-08 11:03:06
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@ToString
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class LexMarkResultDTO implements Serializable {
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 对应发送请求中的devName
|
||||||
|
*/
|
||||||
|
private String devName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 事件名,由发送请求中的actionName+Over组成。
|
||||||
|
*/
|
||||||
|
private String msgName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 对应发送请求的callID。
|
||||||
|
*/
|
||||||
|
private int callID;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 错误码:0表示成功;其他表示失败,比如-4表示取消,-48表示超时,-14表示硬件故障
|
||||||
|
*/
|
||||||
|
private int result;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送请求中的actionName
|
||||||
|
*/
|
||||||
|
private String cmdName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回参数
|
||||||
|
*/
|
||||||
|
private Param param;
|
||||||
|
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public static class Param { // 不同的actionName导致返回值不同,需要的参数可以加上
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 请求ID
|
||||||
|
*/
|
||||||
|
private int RequestID;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 命令编码
|
||||||
|
*/
|
||||||
|
private int dwCommandCode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 事件名,由发送请求中的actionName+Over组成。
|
||||||
|
*/
|
||||||
|
private String eventName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 事件类型编码
|
||||||
|
*/
|
||||||
|
private int eventType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 服务
|
||||||
|
*/
|
||||||
|
private int hService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 对应发送请求中的devName
|
||||||
|
*/
|
||||||
|
private String cmdName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 错误码:0表示成功;其他表示失败,比如-4表示取消,-48表示超时,-14表示硬件故障
|
||||||
|
*/
|
||||||
|
private int result;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 详情描述
|
||||||
|
*/
|
||||||
|
private String desc;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -63,7 +63,7 @@ public class TemplateUtils {
|
||||||
* @param saveDir 图片的保存路径,如果为空,那么不进行图片的保存
|
* @param saveDir 图片的保存路径,如果为空,那么不进行图片的保存
|
||||||
* @return 图片字节数组
|
* @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 {
|
try {
|
||||||
// 获取模板上下文
|
// 获取模板上下文
|
||||||
Context context = this.getContext(data);
|
Context context = this.getContext(data);
|
||||||
|
@ -90,11 +90,12 @@ public class TemplateUtils {
|
||||||
BufferedImage image = this.generate(html, width, height);
|
BufferedImage image = this.generate(html, width, height);
|
||||||
|
|
||||||
// 保存图片
|
// 保存图片
|
||||||
if (saveDir != null && !"".equals(saveDir)) {
|
if (saveDir != null && !"".equals(saveDir.toString())) {
|
||||||
String outputPath = saveDir + "\\genera_image_" + System.currentTimeMillis() + ".png";
|
String outputPath = saveDir.toString() + "\\genera_image_" + System.currentTimeMillis() + ".png";
|
||||||
ImageIO.write(image, "PNG", new File(outputPath));
|
ImageIO.write(image, "PNG", new File(outputPath));
|
||||||
|
saveDir.reverse();
|
||||||
|
saveDir.append(outputPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
|
ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
|
||||||
ImageIO.write(image, "PNG", byteOutputStream);
|
ImageIO.write(image, "PNG", byteOutputStream);
|
||||||
return byteOutputStream.toByteArray();
|
return byteOutputStream.toByteArray();
|
||||||
|
@ -134,19 +135,21 @@ public class TemplateUtils {
|
||||||
private Context getContext(JSONObject data) {
|
private Context getContext(JSONObject data) {
|
||||||
// 创建Thymeleaf上下文
|
// 创建Thymeleaf上下文
|
||||||
Context context = new Context();
|
Context context = new Context();
|
||||||
Set<String> keys = data.keySet();
|
if ( data != null) {
|
||||||
for (String key : keys) {
|
Set<String> keys = data.keySet();
|
||||||
// 判单是否有图片生成,统一后面采用的是_2base64Type
|
for (String key : keys) {
|
||||||
String[] split = key.split("_");
|
// 判单是否有图片生成,统一后面采用的是_2base64Type
|
||||||
if (split.length > 1 && split[1].equals("2base64Type")) {
|
String[] split = key.split("_");
|
||||||
int width = split.length > 2 ? Integer.parseInt(split[2]) : 100;
|
if (split.length > 1 && split[1].equals("2base64Type")) {
|
||||||
int height = split.length > 3 ? Integer.parseInt(split[3]) : 100;
|
int width = split.length > 2 ? Integer.parseInt(split[2]) : 100;
|
||||||
// 如果是图片类型,需要进行base64转换
|
int height = split.length > 3 ? Integer.parseInt(split[3]) : 100;
|
||||||
String base64 = this.generateQRCode(String.valueOf(data.get(key)), width, height);
|
// 如果是图片类型,需要进行base64转换
|
||||||
context.setVariable(split[0], "data:image/jpeg;base64," + base64);
|
String base64 = this.generateQRCode(String.valueOf(data.get(key)), width, height);
|
||||||
} else {
|
context.setVariable(split[0], "data:image/jpeg;base64," + base64);
|
||||||
// 普通字段直接设置
|
} else {
|
||||||
context.setVariable(key, data.get(key));
|
// 普通字段直接设置
|
||||||
|
context.setVariable(key, data.get(key));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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("利盟服务请求失败");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -25,3 +25,12 @@ server:
|
||||||
enabled: true
|
enabled: true
|
||||||
min-response-size: 1024
|
min-response-size: 1024
|
||||||
mime-types: application/javascript,application/json,application/xml,text/html,text/xml,text/plain,text/css,image/*
|
mime-types: application/javascript,application/json,application/xml,text/html,text/xml,text/plain,text/css,image/*
|
||||||
|
|
||||||
|
|
||||||
|
# 自定义app参数
|
||||||
|
app:
|
||||||
|
custom:
|
||||||
|
lexMarkServiceIp: http://127.0.0.1
|
||||||
|
lexMarkServicePort: 12346
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue