xuekang
2024-05-11 bac0878349a1db23e7b420ea164e22fb9db73a99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package com.nuvole.util;
 
import java.net.URLEncoder;
import java.util.HashMap;
 
import com.nuvole.common.domain.result.ExpressResult;
 
import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
 
/**
 * 快递鸟物流接口
 *
 * @Author: lc
 * @Date: 2019/6/13 15:36
 */
@Slf4j
public class ExpressUtil {
 
    private static String fileName = "express.properties";
 
 
    //电商ID
    public static String EBusinessID = "";
    //电商加密私钥,快递鸟提供,注意保管,不要泄漏
    public static String AppKey = "";
    //请求url,
    public static String ReqURL = "";
    //快递类型
    public static String Type = "";
 
    static {
        EBusinessID = PropertyUtil.getProp(fileName, "EBusinessID");
        AppKey = PropertyUtil.getProp(fileName, "AppKey");
        ReqURL = PropertyUtil.getProp(fileName, "ReqURL");
        Type = PropertyUtil.getProp(fileName, "Type");
    }
 
 
    /**
     * 查询订单物流轨迹(Json方式 )[3000次/天,每单每天限查 5 次,并发不超过 10 次/S]
     *
     * @Author: lc
     * @Date: 2019/6/13 15:38
     */
    public static ExpressResult getOrderTracesByJson(String expressType, String expressCode) {
        try {
            //如果不传快递公司标识则取默认值
            if (StrUtil.isEmpty(expressType)) {
                expressType = Type;
            }
            String requestData = "{'OrderCode':'','ShipperCode':'" + expressType + "','LogisticCode':'" + expressCode + "'}";
 
            HashMap<String, Object> params = new HashMap<>();
            params.put("RequestData", URLEncoder.encode(requestData, "UTF-8"));
            params.put("EBusinessID", EBusinessID);
            params.put("RequestType", "1002");   //请求接口指令
            String dataSign = encrypt(requestData, AppKey, "UTF-8");
            params.put("DataSign", URLEncoder.encode(dataSign, "UTF-8"));
            params.put("DataType", "2");   //json方式
            String result = HttpUtil.post(ReqURL, params);
            System.out.println(result);
            return JSONUtil.toBean(result, ExpressResult.class);
        } catch (Exception e) {
            e.printStackTrace();
            return new ExpressResult();
        }
    }
 
 
    //加密 (进行MD5加密,然后Base64编码,最后 进行URL(utf-8)编码)
    private static String encrypt(String content, String keyValue, String charset) {
        if (keyValue != null) {
            return Base64.encode(SecureUtil.md5(content + keyValue).toLowerCase(), charset);
        }
        return Base64.encode(SecureUtil.md5(content).toLowerCase(), charset);
    }
 
 
}