潘志宝
2025-03-12 fcc5791b5d0d03e346896800fbbc51994a93a32a
iailab-plat-sdk/src/main/java/com/iailab/sdk/auth/client/IailabAuthClient.java
@@ -3,7 +3,12 @@
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.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;
@@ -20,6 +25,7 @@
import java.util.*;
import static com.iailab.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.iailab.framework.common.pojo.CommonResult.success;
/**
 * @author Houzhongjian
@@ -70,6 +76,7 @@
    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";
    /**
@@ -98,17 +105,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();
@@ -150,6 +157,7 @@
    /**
     * 平台http请求封装
     *
     * @param method
     * @param url
     * @param params
@@ -159,9 +167,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);
        }
@@ -177,35 +185,22 @@
        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;
    }
    /**
     * 查询计划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;
    }
    /**
@@ -214,16 +209,77 @@
     * @param pointNos
     * @return
     */
    public Map<String, Object> queryPointsRealValue(List<String> pointNos) throws Exception {
    public CommonResult<Map<String, Object>> queryPointsRealValue(List<String> pointNos) {
        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;
        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());
        }
        JSONObject jsonObject = JSON.parseObject(resp);
        data = jsonObject.getJSONObject(RESP_DATA).toJavaObject(Map.class);
        return data;
        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);
    }
}