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);
|
}
|
|
|
}
|