1.快递物流信息是具有实效性,丰网有效期1个月 其它3个月,超过时效性无法查看,需要根据业务情况是否需要保存到数据库中。
2.查询快递物流信息需要填写手机号的快递公司有:顺丰速运(“shunfeng”)、顺丰快运(“shunfengkuaiyun”)和丰网速运(“fengwang”)
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.unionx.core.util.ClientTimeOut;
import com.unionx.core.util.HttpClientUtil;
import com.unionx.core.vo.CommonResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.util.Map;
/* *
* @Author 15701
* @Description 快递100工具类
* @Date 2023/3/31
**/
@Slf4j
@Component
public class ExpressUtil {
/**
* 查询url
*/
private static final String QUERY_URL = "https://poll.kuaidi100.com/poll/query.do";
/**
* 快递查询地图轨迹url
*/
private static final String QUERY_MAP_VIEW_URL = "https://poll.kuaidi100.com/poll/maptrack.do";
/**
* 智能单号识别url
*/
//public static final String AUTO_NUM_URL = "http://www.kuaidi100.com/autonumber/auto"; //真实环境,没有沙箱测试
private static final String AUTO_NUM_URL = "http://cloud.kuaidi100.com/api";
/**
* C端寄件下单接口请求地址
*/
public static final String C_ORDER_URL = "https://order.kuaidi100.com/order/corderapi.do";
/**
* C端寄件下单接口方法
*/
public static final String C_ORDER_METHOD = "cOrder";
/**
* C端寄件查价接口方法
*/
public static final String C_ORDER_PRICE_METHOD = "price";
/**
* 可用性请求地址
*/
public static final String REACHABLE_URL = "http://api.kuaidi100.com/reachable.do";
/**
* 可用性方法
*/
public static final String REACHABLE_METHOD = "reachable";
@Value("${express.key}")
private String KEY;
@Value("${express.customer}")
private String CUSTOMER;
@Value("${express.secretKey}")
private String SECRET_KEY;
@Value("${express.secret}")
private String SECRET;
@Value("${express.code}")
private String CODE;
/* *
* @Author 15701
* @Description 自动识别查询快递信息
* @param [expressNo]
* 快递单号
* @Date 2023/3/31
**/
public String queryAutoExpress(String expressNo) {
String sign = SecureUtil.md5(SECRET_KEY + SECRET).toUpperCase();
Map<String, Object> map = Maps.newHashMap();
map.put("secret_key", SECRET_KEY);
map.put("secret_code", CODE);
map.put("secret_sign", sign);
map.put("num", expressNo);
String result = HttpClientUtil.doPost(AUTO_NUM_URL, ClientTimeOut.EXPRESS_TIME_OUT, map);
return result;
}
/* *
* @Author 15701
* @Description 查询快递信息
* @param [expressNo, expressCompanyNo, mobile]
* 快递单号、快递公司编号、手机号
* @Date 2023/4/3
**/
public String queryExpress(String expressNo, String expressCompanyNo,String mobile) {
JSONObject param = new JSONObject();
param.put("com", expressCompanyNo);
param.put("num", expressNo);
//顺丰速运、顺丰快运和丰网速运必填,其他快递公司选填。如座机号码有分机号,分机号无需传入
if(expressCompanyNo.equals("shunfeng")||expressCompanyNo.equals("shunfengkuaiyun")||expressCompanyNo.equals("fengwang")){
param.put("phone", mobile);
}
String sign = SecureUtil.md5(param.toJSONString() + KEY + CUSTOMER).toUpperCase();
//String sign = SignUtil.querySign(param.toJSONString(), KEY, CUSTOMER);
Map<String, Object> map = Maps.newHashMap();
map.put("customer", CUSTOMER);
map.put("sign", sign);
map.put("param", param.toJSONString());
String result = HttpClientUtil.doPost(QUERY_URL, ClientTimeOut.EXPRESS_TIME_OUT, map);
JSONObject jsonObject = JSONObject.parseObject(result);
String status = jsonObject.getString("status");
if (StringUtils.isNotBlank(status) && CommonResponse.SUCCESS_STATUS.equals(status)) {
return jsonObject.getString("data");
}
//return jsonObject.getJSONArray("data");
return null;
}
}
HttpClientUtil工具类:
import cn.hutool.http.HttpRequest;
import java.util.Map;
/* *
* @Author 15701
* @Description 远程调用客户端工具类
* @Date 2023/3/31
**/
public class HttpClientUtil {
/* *
* @Author 15701
* @Description get请求
* @param [url, time, map]
* @Return java.lang.String
* @Date 2023/3/31
**/
public static String doGet(final String url, final int time, Map<String, Object> map) {
String result = HttpRequest.get(url)
.header("Content-Type", "application/x-www-form-urlencoded")
.timeout(time)
.form(map)
.execute()
.body();
return result;
}
/* *
* @Author 15701
* @Description post请求
* @param [url, time, map]
* @Return java.lang.String
* @Date 2023/3/31
**/
public static String doPost(final String url, final int time, Map<String, Object> map) {
String result = HttpRequest.post(url)
.header("Content-Type", "application/x-www-form-urlencoded")
.timeout(time)
.form(map)
.execute()
.body();
return result;
}
}