快递wms工具类
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.kuaidi100.sdk.api.QueryTrack;
import com.kuaidi100.sdk.api.Subscribe;
import com.kuaidi100.sdk.contant.ApiInfoConstant;
import com.kuaidi100.sdk.core.IBaseClient;
import com.kuaidi100.sdk.pojo.HttpResult;
import com.kuaidi100.sdk.request.QueryTrackParam;
import com.kuaidi100.sdk.request.QueryTrackReq;
import com.kuaidi100.sdk.request.SubscribeParam;
import com.kuaidi100.sdk.request.SubscribeParameters;
import com.kuaidi100.sdk.request.SubscribeReq;
import com.kuaidi100.sdk.response.QueryTrackResp;
import com.kuaidi100.sdk.response.SubscribeResp;
import com.kuaidi100.sdk.utils.SignUtils;
import jakarta.annotation.Nullable;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.util.Objects;
@Component
@Slf4j
public class WmsUtil {
@Value("${wms.kuaidi-100.key}")
private String key;
@Value("${wms.kuaidi-100.customer}")
private String customer;
@Value("${wms.kuaidi-100.callbackUrl}")
private String callbackUrl;
public void subscribe(String companyEncoded, String wmsOrderNo, String phoneNum) throws Exception {
SubscribeParameters subscribeParameters = new SubscribeParameters();
subscribeParameters.setCallbackurl(callbackUrl);
subscribeParameters.setPhone(phoneNum);
SubscribeParam subscribeParam = new SubscribeParam();
subscribeParam.setParameters(subscribeParameters);
subscribeParam.setCompany(companyEncoded);
subscribeParam.setNumber(wmsOrderNo);
subscribeParam.setKey(key);
SubscribeReq subscribeReq = new SubscribeReq();
subscribeReq.setSchema(ApiInfoConstant.SUBSCRIBE_SCHEMA);
subscribeReq.setParam(new Gson().toJson(subscribeParam));
IBaseClient subscribe = new Subscribe();
HttpResult httpResult = subscribe.execute(subscribeReq);
SubscribeResp subscribeResp = handleWmsResult(httpResult, SubscribeResp.class);
log.info("订阅物流返回信息:{}", JSON.toJSONString(subscribeResp));
}
public QueryTrackResp queryTrack(String companyEncoded, String wmsOrderNo, String phoneNum) throws Exception {
QueryTrackReq queryTrackReq = new QueryTrackReq();
QueryTrackParam queryTrackParam = new QueryTrackParam();
queryTrackParam.setCom(companyEncoded);
queryTrackParam.setNum(wmsOrderNo);
queryTrackParam.setPhone(phoneNum);
String param = new Gson().toJson(queryTrackParam);
queryTrackReq.setParam(param);
queryTrackReq.setCustomer(customer);
queryTrackReq.setSign(SignUtils.querySign(param, key, customer));
IBaseClient baseClient = new QueryTrack();
HttpResult httpResult = baseClient.execute(queryTrackReq);
return handleWmsResult(httpResult, QueryTrackResp.class);
}
@Nullable
@SuppressWarnings("all")
private <T> T handleWmsResult(@NotNull HttpResult httpResult, Class<T> clazz) {
if (httpResult.getStatus() == HttpStatus.SC_OK && StringUtils.isNotBlank(httpResult.getBody())) {
String body = httpResult.getBody();
JSONObject jsonObject = JSON.parseObject(httpResult.getBody());
if (Objects.nonNull(jsonObject.get("data"))) {
return JSONObject.toJavaObject(jsonObject, clazz);
} else if (Objects.nonNull(jsonObject.get("message"))) {
QueryTrackResp queryTrackResp = JSON.parseObject(httpResult.getBody(), QueryTrackResp.class);
return JSONObject.toJavaObject(jsonObject, clazz);
}
return JSONObject.toJavaObject(jsonObject, clazz);
}
return null;
}
}