|
|
@@ -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
|
|
|
+ + "¶m=" + 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;
|
|
|
+ }
|
|
|
}
|