张世豪
昨天 8f8eed75beb5bb9b66f2a87de856f2dbf11e6ffe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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;
    }
}