package jiekou;
|
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
|
import java.io.*;
|
import java.net.HttpURLConnection;
|
import java.net.URL;
|
|
/**
|
* 还卡接口工具类
|
* 使用Slf4j日志输出,仅支持同步处理
|
*/
|
public class ReturnCardUtil {
|
|
private static final Logger logger = LoggerFactory.getLogger(ReturnCardUtil.class);
|
|
// 连接超时和读取超时时间
|
private static final int CONNECT_TIMEOUT = 5000;
|
private static final int READ_TIMEOUT = 10000;
|
|
/**
|
* 调用还卡接口
|
*
|
* @param apiUrl 接口的URL地址
|
* @param apiKey API密钥
|
* @param doorNo 柜门号(1到60的16进制数)
|
* @param deviceId 设备ID
|
* @param cardId 工作卡号
|
* @param faceId 人脸Id
|
* @param userId 用户ID(用户唯一识别码)
|
* @return 响应结果JSON字符串
|
*/
|
public static String callReturnCard(
|
String apiUrl,
|
String apiKey,
|
String doorNo,
|
String deviceId,
|
String cardId,
|
Integer faceId,
|
String userId
|
) {
|
|
HttpURLConnection connection = null;
|
BufferedReader reader = null;
|
|
try {
|
// 构建URL
|
URL url = new URL(apiUrl);
|
connection = (HttpURLConnection) url.openConnection();
|
|
// 设置连接参数
|
connection.setRequestMethod("POST");
|
connection.setRequestProperty("Content-Type", "application/json");
|
connection.setRequestProperty("apiKey", apiKey);
|
connection.setConnectTimeout(CONNECT_TIMEOUT);
|
connection.setReadTimeout(READ_TIMEOUT);
|
connection.setDoOutput(true);
|
|
// 构建请求体
|
String requestBody = buildRequestBody(doorNo, deviceId, cardId, faceId, userId);
|
logger.info("还卡接口请求URL: {}", apiUrl);
|
logger.info("还卡接口请求参数: {}", requestBody);
|
|
// 发送请求体
|
try (OutputStream os = connection.getOutputStream()) {
|
byte[] input = requestBody.getBytes("utf-8");
|
os.write(input, 0, input.length);
|
}
|
|
// 获取响应码
|
int responseCode = connection.getResponseCode();
|
logger.info("还卡接口响应码: {}", responseCode);
|
|
// 读取响应内容
|
StringBuilder response = new StringBuilder();
|
if (responseCode == HttpURLConnection.HTTP_OK) {
|
reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
|
} else {
|
reader = new BufferedReader(new InputStreamReader(connection.getErrorStream(), "utf-8"));
|
}
|
|
String line;
|
while ((line = reader.readLine()) != null) {
|
response.append(line);
|
}
|
|
String responseBody = response.toString();
|
logger.info("还卡接口响应: {}", responseBody);
|
|
return responseBody;
|
|
} catch (Exception e) {
|
logger.error("调用还卡接口发生异常", e);
|
return buildErrorResponse("接口调用异常: " + e.getMessage());
|
} finally {
|
// 关闭资源
|
if (reader != null) {
|
try {
|
reader.close();
|
} catch (Exception e) {
|
logger.error("关闭BufferedReader失败", e);
|
}
|
}
|
if (connection != null) {
|
connection.disconnect();
|
}
|
}
|
}
|
|
/**
|
* 构建请求体JSON
|
*
|
* @param doorNo 柜门号
|
* @param deviceId 设备ID
|
* @param cardId 工作卡号
|
* @param faceId 人脸Id(可为null)
|
* @param userId 用户ID
|
* @return JSON格式的请求体
|
*/
|
private static String buildRequestBody(String doorNo, String deviceId, String cardId, Integer faceId, String userId) {
|
StringBuilder sb = new StringBuilder();
|
sb.append("{");
|
sb.append("\"doorNo\":\"").append(doorNo).append("\",");
|
sb.append("\"deviceId\":\"").append(deviceId).append("\",");
|
sb.append("\"cardId\":\"").append(cardId).append("\",");
|
|
// faceId是非必填字段,如果为null则不包含在请求体中
|
if (faceId != null) {
|
sb.append("\"faceId\":").append(faceId).append(",");
|
}
|
|
sb.append("\"userId\":\"").append(userId).append("\"");
|
sb.append("}");
|
|
return sb.toString();
|
}
|
|
/**
|
* 构建错误响应
|
*
|
* @param errorMsg 错误信息
|
* @return JSON格式的错误响应
|
*/
|
private static String buildErrorResponse(String errorMsg) {
|
return "{\"code\":\"500\",\"status\":\"0\",\"msg\":\"" + errorMsg + "\"}";
|
}
|
|
/**
|
* 判断还卡是否成功
|
*
|
* @param responseJson 响应JSON字符串
|
* @return true=成功, false=失败
|
*/
|
public static boolean isReturnSuccess(String responseJson) {
|
try {
|
if (responseJson != null && responseJson.contains("\"code\"") && responseJson.contains("\"status\"")) {
|
// 检查code是否为200
|
int codeIndex = responseJson.indexOf("\"code\"");
|
if (codeIndex != -1) {
|
int codeColonIndex = responseJson.indexOf(":", codeIndex);
|
int codeCommaIndex = responseJson.indexOf(",", codeColonIndex);
|
if (codeCommaIndex == -1) {
|
codeCommaIndex = responseJson.indexOf("}", codeColonIndex);
|
}
|
|
if (codeColonIndex != -1 && codeCommaIndex != -1) {
|
String codeValue = responseJson.substring(codeColonIndex + 1, codeCommaIndex).trim();
|
codeValue = codeValue.replace("\"", "").replace("'", "");
|
|
// 如果code不是200,直接返回失败
|
if (!"200".equals(codeValue)) {
|
return false;
|
}
|
}
|
}
|
|
// 检查status是否为1
|
int statusIndex = responseJson.indexOf("\"status\"");
|
if (statusIndex != -1) {
|
int colonIndex = responseJson.indexOf(":", statusIndex);
|
int commaIndex = responseJson.indexOf(",", colonIndex);
|
int endIndex = responseJson.indexOf("}", colonIndex);
|
if (commaIndex == -1 || commaIndex > endIndex) {
|
commaIndex = endIndex;
|
}
|
|
if (colonIndex != -1 && commaIndex != -1) {
|
String statusValue = responseJson.substring(colonIndex + 1, commaIndex).trim();
|
statusValue = statusValue.replace("\"", "").replace("'", "");
|
return "1".equals(statusValue);
|
}
|
}
|
}
|
} catch (Exception e) {
|
logger.error("解析还卡接口响应状态失败", e);
|
}
|
return false;
|
}
|
|
/**
|
* 获取响应消息
|
*
|
* @param responseJson 响应JSON字符串
|
* @return 消息内容,如果解析失败返回空字符串
|
*/
|
public static String getResponseMessage(String responseJson) {
|
try {
|
if (responseJson != null && responseJson.contains("\"msg\"")) {
|
int msgIndex = responseJson.indexOf("\"msg\"");
|
if (msgIndex != -1) {
|
int colonIndex = responseJson.indexOf(":", msgIndex);
|
int commaIndex = responseJson.indexOf(",", colonIndex);
|
int endIndex = responseJson.indexOf("}", colonIndex);
|
if (commaIndex == -1 || commaIndex > endIndex) {
|
commaIndex = endIndex;
|
}
|
|
if (colonIndex != -1 && commaIndex != -1) {
|
String msgValue = responseJson.substring(colonIndex + 1, commaIndex).trim();
|
return msgValue.replace("\"", "").replace("'", "");
|
}
|
}
|
}
|
} catch (Exception e) {
|
logger.error("解析还卡接口响应消息失败", e);
|
}
|
return "";
|
}
|
}
|