Browse Source

识别地址信息

zxf 17 hours ago
parent
commit
750f462179

+ 14 - 0
jd-logistics-modules/jd-logistics-system/src/main/java/com/ruoyi/front/controller/Kd100Controller.java

@@ -67,6 +67,20 @@ public class Kd100Controller {
 
 
 
+    @PostMapping("/queryIcrExpres")
+    public Map queryIcrExpres(@RequestBody Map param)  {
+        try {
+                Map   result = kuaidi100Service.queryIcrExpres(param);
+                return  result;
+        } catch (Exception e) {
+            return AjaxResult.error("智能识别文字出错!");
+        }
+
+
+    }
+
+
+
 
 
 

+ 50 - 0
jd-logistics-modules/jd-logistics-system/src/main/java/com/ruoyi/front/service/impl/Kuaidi100Service.java

@@ -2,6 +2,7 @@ package com.ruoyi.front.service.impl;
 
 
 import com.alibaba.fastjson2.JSONObject;
+import com.fasterxml.jackson.databind.ObjectMapper;
 import com.ruoyi.logistics.config.KD100ExpressConfig;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.jdbc.core.JdbcTemplate;
@@ -15,6 +16,7 @@ import java.net.URL;
 import java.net.URLEncoder;
 import java.nio.charset.StandardCharsets;
 import java.security.MessageDigest;
+import java.time.Instant;
 import java.util.List;
 import java.util.Map;
 
@@ -145,4 +147,52 @@ public class Kuaidi100Service {
         return response.toString();
     }
 
+
+    /**
+     * 该接口提供智能识别姓名、电话、地址的功能,并解析地址返回对应信息
+     */
+
+    public Map queryIcrExpres(Map params) throws Exception {
+        String param = "{\"content\":\"" + params.get("param")+ "\" }";
+        String time= String.valueOf(Instant.now().toEpochMilli());
+        // 2. 生成签名
+        String sign = md5(param +time+ kd100ExpressConfig.getAppKey() + kd100ExpressConfig.getAppSecret()).toUpperCase();
+        // 3. 构建请求体
+        String postData = "key=" + URLEncoder.encode(kd100ExpressConfig.getAppKey(), "UTF-8")
+                + "&sign=" + URLEncoder.encode(sign, "UTF-8")
+                + "&t=" + time
+                + "&param=" + URLEncoder.encode(param, "UTF-8");
+        // 4. 发送HTTP POST请求
+        URL url = new URL(kd100ExpressConfig.getQueryIcrUrl());
+        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
+        conn.setRequestMethod("POST");
+        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
+        conn.setDoOutput(true);
+        conn.setDoInput(true);
+        conn.setConnectTimeout(5000);
+        conn.setReadTimeout(10000);
+        // 5. 写入请求数据
+        try (OutputStream os = conn.getOutputStream()) {
+            os.write(postData.getBytes(StandardCharsets.UTF_8));
+            os.flush();
+        }
+        // 6. 读取响应结果
+        int responseCode = conn.getResponseCode();
+        StringBuilder response = new StringBuilder();
+        if (responseCode == HttpURLConnection.HTTP_OK) {
+            BufferedReader in = new BufferedReader(
+                    new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
+            String inputLine;
+            while ((inputLine = in.readLine()) != null) {
+                response.append(inputLine);
+            }
+            in.close();
+        } else {
+            throw new RuntimeException("HTTP请求失败,状态码: " + responseCode);
+        }
+        conn.disconnect();
+        ObjectMapper objectMapper = new ObjectMapper();
+        Map<String, Object> map = objectMapper.readValue(response.toString(), Map.class);
+        return map;
+    }
 }

+ 2 - 0
jd-logistics-modules/jd-logistics-system/src/main/java/com/ruoyi/logistics/config/KD100ExpressConfig.java

@@ -31,4 +31,6 @@ public class KD100ExpressConfig {
 
     private String queryPUrl;
 
+    private String queryIcrUrl;
+
 }