package jiekou;
|
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
|
import java.io.*;
|
import java.net.HttpURLConnection;
|
import java.net.URL;
|
|
/**
|
* 取卡成功通知工具类
|
* 使用Slf4j日志输出,仅支持同步处理
|
*/
|
public class TakeCardSuccessUtil {
|
|
private static final Logger logger = LoggerFactory.getLogger(TakeCardSuccessUtil.class);
|
|
// 连接超时和读取超时时间
|
private static final int CONNECT_TIMEOUT = 5000;
|
private static final int READ_TIMEOUT = 10000;
|
|
/**
|
* 调用取卡成功通知接口
|
*
|
* @param apiUrl 接口的URL地址
|
* @param doorNo 门号(十六进制)
|
* @param deviceId 设备ID
|
* @param cardId 卡号(15字节工作卡卡号)
|
* @param faceId 人脸ID
|
* @param userId 用户ID
|
* @param apiKey API密钥
|
* @return 响应结果JSON字符串
|
*/
|
@SuppressWarnings("deprecation")
|
public static String callTakeSuccess(
|
String apiUrl,
|
String apiKey,
|
String doorNo,
|
String deviceId,
|
String cardId,
|
String 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
|
* @param userId 用户ID
|
* @return JSON格式的请求体
|
*/
|
private static String buildRequestBody(String doorNo, String deviceId, String cardId, String faceId, String userId) {
|
return "{\"doorNo\":\"" + doorNo +
|
"\",\"deviceId\":\"" + deviceId +
|
"\",\"cardId\":\"" + cardId +
|
"\",\"faceId\":\"" + faceId +
|
"\",\"userId\":\"" + userId + "\"}";
|
}
|
|
/**
|
* 构建错误响应
|
*
|
* @param errorMsg 错误信息
|
* @return JSON格式的错误响应
|
*/
|
private static String buildErrorResponse(String errorMsg) {
|
return "{\"status\":\"0\",\"error\":\"" + errorMsg + "\"}";
|
}
|
|
/**
|
* 判断响应是否成功
|
*
|
* @param responseJson 响应JSON字符串
|
* @return true=成功, false=失败
|
*/
|
public static boolean isTakeSuccess(String responseJson) {
|
try {
|
if (responseJson != null && responseJson.contains("\"status\"")) {
|
int statusIndex = responseJson.indexOf("\"status\"");
|
if (statusIndex != -1) {
|
// 查找status值
|
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;
|
}
|
}
|