潘志宝
2025-03-18 7d9c6787a4bfe2cd50d3250e08a5d337f8fe933d
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package com.iailab.sdk.auth.client;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.iailab.sdk.auth.client.common.exception.enums.GlobalErrorCodeConstants;
import com.iailab.sdk.auth.client.common.pojo.CommonResult;
import com.iailab.sdk.auth.client.common.pojo.PageResult;
import com.iailab.sdk.auth.client.dto.ApiPointsValueQueryDTO;
import com.iailab.sdk.auth.client.dto.StAlarmAndSuggestPageReqDTO;
import com.iailab.sdk.auth.client.dto.StAlarmAndSuggestRespDTO;
import com.iailab.sdk.auth.client.dto.TokenDTO;
import com.iailab.sdk.auth.config.SdkAutoConfiguration;
import com.iailab.sdk.auth.constants.SdkErrorCodeConstants;
import com.iailab.sdk.util.http.IailabHttpUtils;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.*;
import org.springframework.util.Assert;
import org.springframework.util.Base64Utils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;
 
import java.nio.charset.StandardCharsets;
import java.util.*;
 
import static com.iailab.framework.common.exception.util.ServiceExceptionUtil.exception;
 
/**
 * @author Houzhongjian
 * @Description
 * @createTime 2025年02月18日
 */
public class IailabClient {
 
    private static final RestTemplate restTemplate = new RestTemplate();
 
 
    /**
     * 平台地址
     */
    public static String BASE_URL = SdkAutoConfiguration.BASE_URL;
 
    /**
     * 租户编号
     */
    public static Long TENANT_ID = SdkAutoConfiguration.TENANT_ID;
 
    /**
     * 客户端信息
     */
    private static String CLIENT_ID = SdkAutoConfiguration.CLIENT_ID;
    private static String CLIENT_SECRET = SdkAutoConfiguration.CLIENT_SECRET;
    private static String USERNAME = SdkAutoConfiguration.USERNAME;
 
    private static final String GRAND_TYPE = "client_credentials";
 
    private static final String SCOPE = "user.read user.write";
 
    private static final String CHARSET = "utf-8";
 
    private static final IailabClient iailabClient = new IailabClient();
 
    public static IailabClient getInstance() {
        return iailabClient;
    }
 
 
    // 鉴权token
    public static String accessToken;
    // 刷新token
    public static String refreshToken;
    // 鉴权token过期时间
    public static Long expireTime;
 
    private static final String RESP_CODE = "code";
    private static final String RESP_MSG = "msg";
    private static final String RESP_DATA = "data";
 
    /**
     * 用户名密码方式获取平台token
     */
    public static synchronized TokenDTO authenticate() {
        System.out.println("登录获取平台token");
        // 1.1 构建请求头
        HttpHeaders headers = new HttpHeaders();
        addClientHeader(headers);
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        // 1.2 构建authenticate请求URL
        String authenticateUrl = BASE_URL + "/system/oauth2/token?"
                // 客户端模式的参数
                + "&grant_type=" + GRAND_TYPE
                + "&username=" + USERNAME
                + "&scope=" + SCOPE;
        // 2. 执行请求
        ResponseEntity<Map<String, Object>> exchange = restTemplate.exchange(
                authenticateUrl,
                HttpMethod.POST,
                new HttpEntity<>(headers),
                new ParameterizedTypeReference<Map<String, Object>>() {
                });
        Assert.isTrue(exchange.getStatusCode().is2xxSuccessful(), "响应必须是 200 成功");
        Map<String, Object> authMap = exchange.getBody();
        Object code = authMap.get("code");
        if (SdkErrorCodeConstants.AUTH_BAD_CREDENTIALS.getCode().equals(code)) {
            throw exception(SdkErrorCodeConstants.AUTH_BAD_CREDENTIALS);
        } else if (SdkErrorCodeConstants.OAUTH2_CLIENT_REDIRECT_URI_NOT_MATCH.getCode().equals(code)) {
            throw exception(SdkErrorCodeConstants.OAUTH2_CLIENT_REDIRECT_URI_NOT_MATCH);
        } else if (SdkErrorCodeConstants.OAUTH2_CLIENT_CLIENT_SECRET_ERROR.getCode().equals(code)) {
            throw exception(SdkErrorCodeConstants.OAUTH2_CLIENT_CLIENT_SECRET_ERROR);
        } else if (SdkErrorCodeConstants.OAUTH2_CLIENT_NOT_EXISTS.getCode().equals(code)) {
            throw exception(SdkErrorCodeConstants.OAUTH2_CLIENT_NOT_EXISTS);
        } else if (SdkErrorCodeConstants.OAUTH2_CLIENT_DISABLE.getCode().equals(code)) {
            throw exception(SdkErrorCodeConstants.OAUTH2_CLIENT_DISABLE);
        }
        accessToken = authMap.get("access_token").toString();
        refreshToken = authMap.get("refresh_token").toString();
        expireTime = Long.valueOf(authMap.get("expires_time").toString());
        return handleResponse(exchange);
    }
 
    public static synchronized TokenDTO refreshToken() throws Exception {
        System.out.println("刷新token");
        // 1.1 构建请求头
        HttpHeaders headers = new HttpHeaders();
        addClientHeader(headers);
        // 1.2 构建authenticate请求URL
        String authenticateUrl = BASE_URL + "/system/auth/client-refresh-token?refreshToken=" + refreshToken + "&clientId=" + CLIENT_ID;
        // 2. 执行请求
        ResponseEntity<Map<String, Object>> exchange = restTemplate.exchange(
                authenticateUrl,
                HttpMethod.POST,
                new HttpEntity<>(headers),
                new ParameterizedTypeReference<Map<String, Object>>() {
                });
        Assert.isTrue(exchange.getStatusCode().is2xxSuccessful(), "响应必须是 200 成功");
        Map<String, Object> authMap = exchange.getBody();
        //刷新token过期,重新获取token
        if (!ObjectUtils.isEmpty(authMap.get("code"))) {
            Integer code = Integer.valueOf(authMap.get("code").toString());
            if (code == 401) {
                authenticate();
            } else {
                throw exception(SdkErrorCodeConstants.AUTH_REFRESH_TOKEN_ERROR);
            }
        } else {
            accessToken = authMap.get("access_token").toString();
            expireTime = Long.valueOf(authMap.get("expires_time").toString());
        }
        return handleResponse(exchange);
    }
 
    /**
     * 平台http请求封装
     *
     * @param method
     * @param url
     * @param params
     * @return
     * @throws Exception
     */
    public static String doHttp(String method, String url, Map<String, Object> params) throws Exception {
        String response = null;
        String upperMethod = method.toUpperCase();
        if ("GET".equals(upperMethod)) {
            response = IailabHttpUtils.doGet(url, params, CHARSET);
        } else if ("POST".equals(upperMethod)) {
            ObjectMapper objectMapper = new ObjectMapper();
            response = IailabHttpUtils.doPost(url, objectMapper.writeValueAsString(params), CHARSET);
        }
        return response;
    }
 
    private static void addClientHeader(HttpHeaders headers) {
        // client 拼接,需要 BASE64 编码
        String client = CLIENT_ID + ":" + CLIENT_SECRET;
        client = Base64Utils.encodeToString(client.getBytes(StandardCharsets.UTF_8));
        headers.add("Authorization", "Basic " + client);
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        headers.set("tenant-id", TENANT_ID.toString());
    }
 
    /**
     * 统一处理响应
     *
     * @param response
     * @param <T>
     * @return
     */
    private static <T> TokenDTO handleResponse(ResponseEntity<T> response) {
        Assert.isTrue(response.getStatusCode().is2xxSuccessful(), "响应必须是 200 成功");
        System.out.println(response);
        TokenDTO authTokenDTO = new TokenDTO();
        Map<String, Object> authMap = (Map<String, Object>) response.getBody();
        authTokenDTO.setAccessToken(authMap.get("access_token").toString());
        authTokenDTO.setRefreshToken(authMap.get("refresh_token").toString());
        authTokenDTO.setExpiresTime(Long.valueOf(authMap.get("expires_time").toString()));
        return authTokenDTO;
    }
 
    public CommonResult<JSONObject> handleHttp(String method, String url, Map<String, Object> params) {
        JSONObject data = new JSONObject();
        String resp = null;
        try {
            String upperMethod = method.toUpperCase();
            if ("GET".equals(upperMethod)) {
                resp = IailabHttpUtils.doGet(url, params, CHARSET);
            } else if ("POST".equals(upperMethod)) {
                ObjectMapper objectMapper = new ObjectMapper();
                resp = IailabHttpUtils.doPost(url, objectMapper.writeValueAsString(params), CHARSET);
            }
            if (StringUtils.isEmpty(resp)) {
                return CommonResult.error(GlobalErrorCodeConstants.EMPTY_RESP);
            }
            JSONObject jsonObject = JSON.parseObject(resp);
            Integer respCode = jsonObject.getInteger(RESP_CODE);
            if (!GlobalErrorCodeConstants.SUCCESS.getCode().equals(respCode)) {
                CommonResult.error(respCode, jsonObject.getString(RESP_MSG));
            }
            data = jsonObject.getJSONObject(RESP_DATA);
        } catch (Exception ex) {
            ex.printStackTrace();
            return CommonResult.error(GlobalErrorCodeConstants.UNKNOWN.getCode(), ex.getMessage());
        }
        return CommonResult.success(data);
    }
 
    /**
     * 查询多个测点当前值
     *
     * @param pointNos
     * @return
     */
    public CommonResult<Map<String, Object>> queryPointsRealValue(List<String> pointNos) {
        Map<String, Object> data = new HashMap<>();
        try {
            String url = BASE_URL + "/data/api/query-points/real-value";
            String resp = IailabHttpUtils.doPost(url, JSON.toJSONString(pointNos), "UTF-8");
            if (StringUtils.isEmpty(resp)) {
                return CommonResult.error(GlobalErrorCodeConstants.EMPTY_RESP);
            }
            JSONObject jsonObject = JSON.parseObject(resp);
            Integer respCode = jsonObject.getInteger(RESP_CODE);
            if (!GlobalErrorCodeConstants.SUCCESS.getCode().equals(respCode)) {
                CommonResult.error(respCode, jsonObject.getString(RESP_MSG));
            }
            data = jsonObject.getJSONObject(RESP_DATA).toJavaObject(Map.class);
        } catch (Exception ex) {
            return CommonResult.error(GlobalErrorCodeConstants.UNKNOWN.getCode(), ex.getMessage());
        }
        return CommonResult.success(data);
    }
 
    /**
     * 查询多个测点历史值
     *
     * @param queryDto
     * @return
     */
    public CommonResult<Map<String, List<Map<String, Object>>>> queryPointsHistoryValue(ApiPointsValueQueryDTO queryDto) {
        Map<String, List<Map<String, Object>>> data = new HashMap<>();
        try {
            String url = BASE_URL + "/data/api/query-points/history-value";
            String resp = IailabHttpUtils.doPost(url, JSON.toJSONString(queryDto), "UTF-8");
            if (StringUtils.isEmpty(resp)) {
                return CommonResult.error(GlobalErrorCodeConstants.EMPTY_RESP);
            }
            JSONObject jsonObject = JSON.parseObject(resp);
            Integer respCode = jsonObject.getInteger(RESP_CODE);
            if (!GlobalErrorCodeConstants.SUCCESS.getCode().equals(respCode)) {
                CommonResult.error(respCode, jsonObject.getString(RESP_MSG));
            }
            data = jsonObject.getJSONObject(RESP_DATA).toJavaObject(Map.class);
        } catch (Exception ex) {
            return CommonResult.error(GlobalErrorCodeConstants.UNKNOWN.getCode(), ex.getMessage());
        }
        return CommonResult.success(data);
    }
 
    /**
     * 获取预警信息和调度建议分页列表
     *
     * @param reqVO
     * @return
     */
    public CommonResult<PageResult<StAlarmAndSuggestRespDTO>> getAlarmAndSuggestPage(StAlarmAndSuggestPageReqDTO reqVO) {
        PageResult<StAlarmAndSuggestRespDTO> data = new PageResult<>();
        try {
            String url = BASE_URL + "/model/api/mcs/alarm-suggest/page";
            String resp = IailabHttpUtils.doPost(url, JSON.toJSONString(reqVO), "UTF-8");
            if (StringUtils.isEmpty(resp)) {
                return CommonResult.error(GlobalErrorCodeConstants.EMPTY_RESP);
            }
            JSONObject jsonObject = JSON.parseObject(resp);
            Integer respCode = jsonObject.getInteger(RESP_CODE);
            if (!GlobalErrorCodeConstants.SUCCESS.getCode().equals(respCode)) {
                CommonResult.error(respCode, jsonObject.getString(RESP_MSG));
            }
            Long total = Long.parseLong(jsonObject.getJSONObject(RESP_DATA).get("total").toString());
            List<StAlarmAndSuggestRespDTO> list = jsonObject.getJSONObject(RESP_DATA).getJSONArray("list").toJavaList(StAlarmAndSuggestRespDTO.class);
            data = new PageResult<>(list, total);
        } catch (Exception ex) {
            return CommonResult.error(GlobalErrorCodeConstants.UNKNOWN.getCode(), ex.getMessage());
        }
        return CommonResult.success(data);
    }
}