From e03505135d9816c8b6ea67a91a7e1f493e662b6f Mon Sep 17 00:00:00 2001 From: houzhongjian <houzhongyi@126.com> Date: 星期三, 12 三月 2025 14:53:08 +0800 Subject: [PATCH] 修改Client封装方式 --- iailab-plat-sdk/src/main/java/com/iailab/sdk/auth/client/IailabAuthClient.java | 90 +++++++++++++++++++++++++++++++++++++-------- 1 files changed, 74 insertions(+), 16 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..0a25789 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,19 @@ 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.dto.ApiDataQueryDTO; 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 +26,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 +55,22 @@ 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_DATA = "data"; /** * 用户名密码方式获取平台token @@ -60,12 +82,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( @@ -102,7 +123,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, @@ -149,11 +170,11 @@ 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()); } // 统一处理响应 @@ -168,4 +189,41 @@ return authTokenDTO; } + /** + * 查询计划chart (测试用) + * @param apiDataQueryDTO + * @return + * @throws Exception + */ + public Map<String, Object> queryPlanChart(ApiDataQueryDTO apiDataQueryDTO) throws Exception { + Map<String, Object> data = new HashMap<>(); + ObjectMapper objectMapper = new ObjectMapper(); + String jsonString = objectMapper.writeValueAsString(apiDataQueryDTO); + String resp = IailabHttpUtils.doPost(BASE_URL + "/data/api/query-plans/chart", jsonString, "utf-8"); + if (StringUtils.isEmpty(resp)) { + return data; + } + JSONObject jsonObject = JSON.parseObject(resp); + data = jsonObject.getJSONObject(RESP_DATA).toJavaObject(Map.class); + return data; + } + + /** + * 查询多个测点当前值 + * + * @param pointNos + * @return + */ + public Map<String, Object> queryPointsRealValue(List<String> pointNos) throws Exception { + Map<String, Object> data = new HashMap<>(); + 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 data; + } + JSONObject jsonObject = JSON.parseObject(resp); + data = jsonObject.getJSONObject(RESP_DATA).toJavaObject(Map.class); + return data; + } + } \ No newline at end of file -- Gitblit v1.9.3