From c3d98a259ea29c449245ad5b9a47e542af0ac499 Mon Sep 17 00:00:00 2001 From: 潘志宝 <979469083@qq.com> Date: 星期三, 12 三月 2025 17:40:44 +0800 Subject: [PATCH] SDK 业务功能 --- iailab-plat-sdk/src/main/java/com/iailab/sdk/auth/client/IailabAuthClient.java | 165 ++++++++++++++++++++++++++++++++++++++++++++++-------- 1 files changed, 139 insertions(+), 26 deletions(-) diff --git a/iailab-plat-sdk/src/main/java/com/iailab/sdk/auth/client/IailabAuthClient.java b/iailab-plat-sdk/src/main/java/com/iailab/sdk/auth/client/IailabAuthClient.java index 17c088b..5635425 100644 --- a/iailab-plat-sdk/src/main/java/com/iailab/sdk/auth/client/IailabAuthClient.java +++ b/iailab-plat-sdk/src/main/java/com/iailab/sdk/auth/client/IailabAuthClient.java @@ -1,18 +1,24 @@ 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.AuthProperties; +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.stereotype.Component; -import org.springframework.stereotype.Service; 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; @@ -25,17 +31,28 @@ * @Description * @createTime 2025年02月18日 */ -@Component -@Service public class IailabAuthClient { private static final RestTemplate restTemplate = new RestTemplate(); - private static AuthProperties authProperties; - public static void setAuthProperties(AuthProperties properties) { - authProperties = properties; - } + /** + * 平台地址 + */ + 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 String PASSWORD = SdkAutoConfiguration.PASSWORD; private static final String GRAND_TYPE = "password"; @@ -43,12 +60,23 @@ private static final String CHARSET = "utf-8"; + private static final IailabAuthClient iailabAuthClient = new IailabAuthClient(); + + public static IailabAuthClient getInstance() { + return iailabAuthClient; + } + + // 鉴权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 @@ -60,12 +88,11 @@ addClientHeader(headers); headers.setContentType(MediaType.APPLICATION_JSON_UTF8); // 1.2 构建authenticate请求URL - // 1.2 构建authenticate请求URL - String authenticateUrl = authProperties.getBaseUrl() + "/system/oauth2/token?" + String authenticateUrl = BASE_URL + "/system/oauth2/token?" // 密码模式的参数 + "&grant_type=" + GRAND_TYPE - + "&username=" + authProperties.getUsername() - + "&password=" + authProperties.getPassword() + + "&username=" + USERNAME + + "&password=" + PASSWORD + "&scope=" + SCOPE; // 2. 执行请求 ResponseEntity<Map<String, Object>> exchange = restTemplate.exchange( @@ -77,17 +104,17 @@ 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)) { + if (SdkErrorCodeConstants.AUTH_BAD_CREDENTIALS.getCode().equals(code)) { throw exception(SdkErrorCodeConstants.AUTH_BAD_CREDENTIALS); - } else if(SdkErrorCodeConstants.AUTH_LOGIN_BAD_CREDENTIALS.getCode().equals(code)) { + } else if (SdkErrorCodeConstants.AUTH_LOGIN_BAD_CREDENTIALS.getCode().equals(code)) { throw exception(SdkErrorCodeConstants.AUTH_LOGIN_BAD_CREDENTIALS); - } else if(SdkErrorCodeConstants.OAUTH2_CLIENT_REDIRECT_URI_NOT_MATCH.getCode().equals(code)) { + } 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)) { + } 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)) { + } 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)) { + } else if (SdkErrorCodeConstants.OAUTH2_CLIENT_DISABLE.getCode().equals(code)) { throw exception(SdkErrorCodeConstants.OAUTH2_CLIENT_DISABLE); } accessToken = authMap.get("access_token").toString(); @@ -102,7 +129,7 @@ HttpHeaders headers = new HttpHeaders(); addClientHeader(headers); // 1.2 构建authenticate请求URL - String authenticateUrl = authProperties.getBaseUrl() + "/system/auth/client-refresh-token?refreshToken=" + refreshToken+ "&clientId=" + authProperties.getClientId(); + String authenticateUrl = BASE_URL + "/system/auth/client-refresh-token?refreshToken=" + refreshToken + "&clientId=" + CLIENT_ID; // 2. 执行请求 ResponseEntity<Map<String, Object>> exchange = restTemplate.exchange( authenticateUrl, @@ -129,6 +156,7 @@ /** * 平台http请求封装 + * * @param method * @param url * @param params @@ -138,9 +166,9 @@ 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)) { + if ("GET".equals(upperMethod)) { response = IailabHttpUtils.doGet(url, params, CHARSET); - } else if("POST".equals(upperMethod)) { + } else if ("POST".equals(upperMethod)) { ObjectMapper objectMapper = new ObjectMapper(); response = IailabHttpUtils.doPost(url, objectMapper.writeValueAsString(params), CHARSET); } @@ -149,23 +177,108 @@ private static void addClientHeader(HttpHeaders headers) { // client 拼接,需要 BASE64 编码 - String client = authProperties.getClientId() + ":" + authProperties.getClientSecret(); + 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", authProperties.getTenantId()); + 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(); + 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; } + /** + * 查询多个测点当前值 + * + * @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); + } } \ No newline at end of file -- Gitblit v1.9.3