feat:增加利盟服务接口调用工具类

This commit is contained in:
石崇礼 2025-02-08 14:45:56 +08:00
parent 0ebf6d4258
commit 117f293e83
5 changed files with 273 additions and 17 deletions

View File

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

View File

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

View File

@ -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));
}
}
}

View 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("利盟服务请求失败");
}
}
}

View File

@ -25,3 +25,12 @@ server:
enabled: true
min-response-size: 1024
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