Procházet zdrojové kódy

新增中交接口

donglang před 2 měsíci
rodič
revize
ae74a9d9ed

+ 50 - 18
sckw-common/sckw-common-core/src/main/java/com/sckw/core/service/VehicleCollectService.java

@@ -1,9 +1,13 @@
 package com.sckw.core.service;
 
 
+import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.sckw.core.reponse.CollectZjxlResponse;
+import com.sckw.core.utils.StringUtils;
 import jakarta.annotation.Resource;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Service;
 
 import javax.crypto.Mac;
 import javax.crypto.spec.SecretKeySpec;
@@ -26,13 +30,17 @@ import java.util.*;
  * Version: 1.0
  */
 
+@Service
+@Slf4j
 public class VehicleCollectService {
 
     @Resource
     private ObjectMapper objectMapper;
-
+    private static final String BASE_URL = "https://openapi-test.sinoiov.cn/save/apis/";
+    private static final String TRANS_TIME_MANAGE_URL = BASE_URL + "transTimeManageV3";
     public static final String HMAC_SHA1 = "HmacSHA1";
     public static final char[] DIGITAL = "0123456789ABCDEF".toCharArray();
+    private static final String DEFAULT_TIME_NEARBY = "24";
 
 
     /**
@@ -41,11 +49,14 @@ public class VehicleCollectService {
      * 1、其他 API,cid + 私钥生成的签名 + 业务参数
      * </pre>
      */
-    public CollectZjxlResponse transTimeManage(String carNo) throws Exception {
-        // URL 前缀
-        String baseUrl = "https://openapi-test.sinoiov.cn/save/apis/";
-        // 检索最新节点
-        String vLastLocationV3Url = baseUrl + "transTimeManageV3";
+    public CollectZjxlResponse transTimeManage(String carNo) {
+        // 1. 参数校验:前置检查入参合法性,避免无效调用
+        if (StringUtils.isBlank(carNo)) {
+            log.error("查询失败:车牌号不能为空");
+            throw new IllegalArgumentException("车牌号不能为空");
+        }
+
+        //2、构建参数
         Map<String, String> vLastLocationParam = new HashMap<>();
         // 客户端 ID
         vLastLocationParam.put("cid", "7598ca30-000e-4930-953d-39e6756b6ee5");
@@ -53,12 +64,33 @@ public class VehicleCollectService {
         vLastLocationParam.put("srt", "2b15bfd6-3546-4e25-bc9b-7cf88d4d2c3c");
         // 业务参数
         vLastLocationParam.put("vclN", carNo);
-        vLastLocationParam.put("timeNearby", "24");
-        String vLastLocationV3Result = httpsCall(vLastLocationV3Url, vLastLocationParam, false);
+        vLastLocationParam.put("timeNearby", DEFAULT_TIME_NEARBY);
+
+        //3、调用第三方
+        String responseJson = null;
+        try {
+            log.info("开始调用中交接口,车牌号:{},URL:{}", carNo, TRANS_TIME_MANAGE_URL);
+            responseJson = httpsCall(TRANS_TIME_MANAGE_URL, vLastLocationParam, false);
+            log.info("中交接口返回原始数据:{}", responseJson);
+
+            // 4. 反序列化
+            if (responseJson == null) {
+                log.error("查询失败:接口返回空数据,车牌号:{}", carNo);
+                throw new RuntimeException("中交接口返回空数据");
+            }
+
+            CollectZjxlResponse response = objectMapper.readValue(responseJson, CollectZjxlResponse.class);
+            log.info("查询成功,车牌号:{},返回状态码:{}", carNo, response.getStatus());
+            return response;
+
+        } catch (JsonProcessingException e) {
+            log.error("JSON解析失败,原始数据:{},错误:{}", responseJson, e.getMessage());
+            throw new RuntimeException("第三方接口返回格式异常", e);
+        } catch (Exception e) {
+            log.error("中交接口调用失败,车牌号:{},错误:{}", carNo, e.getMessage());
+            throw new RuntimeException("中交接口调用失败", e);
+        }
 
-        //反序列化
-        CollectZjxlResponse response = objectMapper.readValue(vLastLocationV3Result, CollectZjxlResponse.class);
-        return response;
     }
 
 
@@ -69,7 +101,7 @@ public class VehicleCollectService {
      * @param param  请求参数
      * @param strict 严格模式 true 校验证书等;false 不校验
      */
-    public static String httpsCall(String url, Map<String, String> param, boolean strict) throws Exception {
+    public String httpsCall(String url, Map<String, String> param, boolean strict) throws Exception {
         // 1、处理参数
         processParam(param);
         // 2、将 map 参数拼接成 string 格式
@@ -115,7 +147,7 @@ public class VehicleCollectService {
      *
      * @param param 原始参数
      */
-    private static void processParam(Map<String, String> param) throws Exception {
+    private void processParam(Map<String, String> param) throws Exception {
         if (!param.containsKey("srt")) {
             throw new NullPointerException();
         }
@@ -138,7 +170,7 @@ public class VehicleCollectService {
      *
      * @param param 请求参数
      */
-    private static String convertMapToString(Map<String, String> param) {
+    private String convertMapToString(Map<String, String> param) {
         StringBuilder sb = new StringBuilder();
         if (param != null && !param.isEmpty()) {
             for (Map.Entry<String, String> entry : param.entrySet()) {
@@ -153,7 +185,7 @@ public class VehicleCollectService {
     /**
      * 信任所有证书
      */
-    private static void trustAllCerts() throws Exception {
+    private void trustAllCerts() throws Exception {
         TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
             public X509Certificate[] getAcceptedIssuers() {
                 return null;
@@ -173,7 +205,7 @@ public class VehicleCollectService {
     /**
      * 信任所有域名
      */
-    private static void trustAllHosts() {
+    private void trustAllHosts() {
         HostnameVerifier trustAllHosts = (hostname, session) -> true;
         HttpsURLConnection.setDefaultHostnameVerifier(trustAllHosts);
     }
@@ -186,7 +218,7 @@ public class VehicleCollectService {
      * @param key     key
      * @return 签名
      */
-    private static byte[] hmacSha1(String[] strings, byte[] key) throws Exception {
+    private byte[] hmacSha1(String[] strings, byte[] key) throws Exception {
         SecretKeySpec signingKey = new SecretKeySpec(key, HMAC_SHA1);
         Mac mac = Mac.getInstance(HMAC_SHA1);
         mac.init(signingKey);
@@ -204,7 +236,7 @@ public class VehicleCollectService {
      * @param bytes 字节数组
      * @return Hex 字符串
      */
-    private static String encodeHexStr(final byte[] bytes) {
+    private String encodeHexStr(final byte[] bytes) {
         if (bytes == null) {
             return null;
         }