|
@@ -0,0 +1,205 @@
|
|
|
+package com.fs.express.http;
|
|
|
+
|
|
|
+import cn.hutool.http.HttpRequest;
|
|
|
+import cn.hutool.http.HttpResponse;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.fs.common.config.FSSysConfig;
|
|
|
+import com.fs.express.dto.ExpressPushRespDTO;
|
|
|
+import com.fs.express.http.dto.LogisticsInfoDTO;
|
|
|
+import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.security.MessageDigest;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * @技术QQ群: 可登录官网https://www.kdniao.com/右侧查看技术群号
|
|
|
+ * @see: https://kdniao.com/api-follow
|
|
|
+ * @copyright: 深圳市快金数据技术服务有限公司
|
|
|
+ * ID和Key请到官网申请:https://kdniao.com/reg
|
|
|
+ * <p>
|
|
|
+ * <p>
|
|
|
+ * <p>
|
|
|
+ * 轨迹订阅接口
|
|
|
+ * 此接口用于向快递鸟订阅物流轨迹信息。由轨迹订阅接口(RequestType1008)和轨迹推送接口(RequestType101)组成,接入时需要对接二个接口。该功能支持情况需查看技术文档。
|
|
|
+ * 正式地址:https://api.kdniao.com/api/dist
|
|
|
+ * <p>
|
|
|
+ * <p>
|
|
|
+ * 系统级参数
|
|
|
+ * RequestData String R 请求内容为JSON格式 详情可参考接口技术文档:https://www.kdniao.com/documents
|
|
|
+ * EBusinessID String R 用户ID
|
|
|
+ * RequestType String R 请求接口指令
|
|
|
+ * DataSign String R 数据内容签名,加密方法为:把(请求内容(未编码)+ApiKey)进行MD5加密--32位小写,然后Base64编码,最后进行URL(utf-8)编码
|
|
|
+ * DataType String R DataType=2,请求、返回数据类型均为JSON格式
|
|
|
+ * <p>
|
|
|
+ * <p>
|
|
|
+ * 应用级参数
|
|
|
+ * R-必填(Required),O-可选(Optional),C-报文中该参数在一定条件下可选(Conditional)
|
|
|
+ * Callback String(50) O 用户自定义回传字段
|
|
|
+ * ShipperCode String(10) R 快递公司编码 详细编码参考《快递鸟接口支持快递公司编码.xlsx》
|
|
|
+ * LogisticCode String(30) R 快递单号
|
|
|
+ * CustomerName String(50) C ShipperCode为SF时必填,对应寄件人/收件人手机号后四位;ShipperCode为其他快递时,可不填或保留字段,不可传值
|
|
|
+ */
|
|
|
+@Component
|
|
|
+@Slf4j
|
|
|
+public class KdniaoSubscribeHttp {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 用户ID,快递鸟提供,注意保管,不要泄漏
|
|
|
+ * 即用户ID,登录快递鸟官网会员中心获取 https://www.kdniao.com/UserCenter/v4/UserHome.aspx
|
|
|
+ */
|
|
|
+
|
|
|
+ private String EBusinessID;
|
|
|
+ /**
|
|
|
+ * API key,快递鸟提供,注意保管,不要泄漏
|
|
|
+ * 即API key,登录快递鸟官网会员中心获取 https://www.kdniao.com/UserCenter/v4/UserHome.aspx
|
|
|
+ */
|
|
|
+ private String ApiKey;
|
|
|
+ /**
|
|
|
+ * 请求url, 正式环境地址
|
|
|
+ */
|
|
|
+ private String ReqURL;
|
|
|
+
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ public void setSysConfig(FSSysConfig sysConfig) {
|
|
|
+ this.EBusinessID = sysConfig.getKdnId();
|
|
|
+ this.ApiKey = sysConfig.getKdnKeyId();
|
|
|
+ this.ReqURL = sysConfig.getKdnUrl();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+// public static void main(String[] args) {
|
|
|
+// try {
|
|
|
+// KdniaoSubscribeHttp api = new KdniaoSubscribeHttp();
|
|
|
+// LogisticsInfoDTO payload = new LogisticsInfoDTO();
|
|
|
+// payload.setCallback("#");
|
|
|
+// payload.setCustomerName("3432");
|
|
|
+// payload.setLogisticCode("SF1395044177288");
|
|
|
+// payload.setShipperCode("SF");
|
|
|
+// System.out.println(api.subscribeLogistic(payload));
|
|
|
+// } catch (Exception e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
+// }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 轨迹订阅接口
|
|
|
+ * @param payload
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public ExpressPushRespDTO subscribeLogistic(LogisticsInfoDTO payload) throws Exception {
|
|
|
+ // 组装应用级参数
|
|
|
+// String RequestData = "{" +
|
|
|
+// "'Callback':'#'," +
|
|
|
+// "'ShipperCode':'SF'," +
|
|
|
+// "'LogisticCode':'SF1395044177288'," +
|
|
|
+// "'CustomerName':'3432'," +
|
|
|
+// "}";
|
|
|
+ String RequestData = JSON.toJSONString(payload);
|
|
|
+ // 组装系统级参数
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
+ params.put("RequestData", urlEncoder(RequestData, "UTF-8"));
|
|
|
+ params.put("EBusinessID", EBusinessID);
|
|
|
+ //免费订阅接口指令1008/在途监控订阅接口指令8008/地图版订阅接口指令8005
|
|
|
+ params.put("RequestType", "8008");
|
|
|
+ String dataSign = encrypt(RequestData, ApiKey, "UTF-8");
|
|
|
+ params.put("DataSign", urlEncoder(dataSign, "UTF-8"));
|
|
|
+ params.put("DataType", "2");
|
|
|
+ // 以form表单形式提交post请求,post请求体中包含了应用级参数和系统级参数
|
|
|
+ String result = sendPost(ReqURL, params);
|
|
|
+ return JSON.parseObject(result, ExpressPushRespDTO.class);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * MD5加密
|
|
|
+ * str 内容
|
|
|
+ * charset 编码方式
|
|
|
+ *
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ @SuppressWarnings("unused")
|
|
|
+ private String MD5(String str, String charset) throws Exception {
|
|
|
+ MessageDigest md = MessageDigest.getInstance("MD5");
|
|
|
+ md.update(str.getBytes(charset));
|
|
|
+ byte[] result = md.digest();
|
|
|
+ StringBuffer sb = new StringBuffer(32);
|
|
|
+ for (int i = 0; i < result.length; i++) {
|
|
|
+ int val = result[i] & 0xff;
|
|
|
+ if (val <= 0xf) {
|
|
|
+ sb.append("0");
|
|
|
+ }
|
|
|
+ sb.append(Integer.toHexString(val));
|
|
|
+ }
|
|
|
+ return sb.toString().toLowerCase();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * base64编码
|
|
|
+ * str 内容
|
|
|
+ * charset 编码方式
|
|
|
+ *
|
|
|
+ * @throws UnsupportedEncodingException
|
|
|
+ */
|
|
|
+ private String base64(String str, String charset) throws UnsupportedEncodingException {
|
|
|
+ String encoded = Base64.encode(str.getBytes(charset));
|
|
|
+ return encoded;
|
|
|
+ }
|
|
|
+
|
|
|
+ @SuppressWarnings("unused")
|
|
|
+ private String urlEncoder(String str, String charset) throws UnsupportedEncodingException {
|
|
|
+ String result = URLEncoder.encode(str, charset);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 电商Sign签名生成
|
|
|
+ * content 内容
|
|
|
+ * keyValue ApiKey
|
|
|
+ * charset 编码方式
|
|
|
+ *
|
|
|
+ * @return DataSign签名
|
|
|
+ * @throws UnsupportedEncodingException ,Exception
|
|
|
+ */
|
|
|
+ @SuppressWarnings("unused")
|
|
|
+ private String encrypt(String content, String keyValue, String charset) throws UnsupportedEncodingException, Exception {
|
|
|
+ if (keyValue != null) {
|
|
|
+ return base64(MD5(content + keyValue, charset), charset);
|
|
|
+ }
|
|
|
+ return base64(MD5(content, charset), charset);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 使用 Hutool 的 HttpRequest 发送 POST 请求
|
|
|
+ *
|
|
|
+ * @param url 请求的 URL
|
|
|
+ * @param params 请求参数
|
|
|
+ * @return 响应结果
|
|
|
+ */
|
|
|
+ public static String sendPost(String url, Map<String, Object> params) {
|
|
|
+ log.info("开始发送POST请求,URL: {},请求参数: {}", url, params);
|
|
|
+
|
|
|
+ HttpRequest request = HttpRequest.post(url)
|
|
|
+ .header("Content-Type", "application/x-www-form-urlencoded")
|
|
|
+ .header("accept", "*/*")
|
|
|
+ .header("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
|
|
+
|
|
|
+ log.info("请求头信息 - ContentType: {},Accept: {},UserAgent: {}",
|
|
|
+ "application/x-www-form-urlencoded", "*/*", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
|
|
+
|
|
|
+ HttpResponse response = request.form(params).execute();
|
|
|
+ String responseBody = response.body();
|
|
|
+ log.info("请求完成,URL: {},响应状态码: {},响应内容: {}", url, response.getStatus(), responseBody);
|
|
|
+
|
|
|
+ return responseBody;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|