Commit 19f5144e authored by Quxl's avatar Quxl

x

parent e9991572
package com.egolm.shop.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONArray;
import com.egolm.common.bean.Rjx;
import com.egolm.shop.util.Express;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
@RestController
@Api(tags={"快递接口"})
@RequestMapping("express")
public class ExpressController {
@Autowired
private Express express;
@ApiOperation("快递查询")
@ApiImplicitParams({
@ApiImplicitParam(paramType = "query", name = "sExpressNO", dataType = "String", required = true, value = "快递单号", defaultValue = ""),
@ApiImplicitParam(paramType = "query", name = "sExpressType", dataType = "String", required = false, value = "快递公司", defaultValue = ""),
@ApiImplicitParam(paramType = "query", name = "terminal", dataType = "String", required = true, value = "终端标识", defaultValue = "wechat"),
@ApiImplicitParam(paramType = "query", name = "langID", dataType = "String", required = false, value = "语言", defaultValue = "936"),
@ApiImplicitParam(paramType = "query", name = "sign", dataType = "String", required = true, value = "签名", defaultValue = ""),
@ApiImplicitParam(paramType = "query", name = "timestamp", dataType = "String", required = true, value = "时间戳", defaultValue = "")
})
@RequestMapping(value = "/createGuest",method=RequestMethod.GET)
public Object createGuest(String sExpressNO, String sExpressType) {
JSONArray data = express.get(sExpressNO, sExpressType);
return Rjx.jsonOk().setData(data);
}
}
package com.egolm.shop.util;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.egolm.common.HttpUtil;
import com.egolm.common.StringUtil;
import com.egolm.shop.config.XException;
@Component
public class Express {
@Value("opt.express.url")
private String url;
@Value("opt.express.secretId")
private String secretId;
@Value("opt.express.secretKey")
private String secretKey;
private String source = "market";
private Map<String, String> getHeaders() {
Map<String, String> headers = new HashMap<String, String>();
headers.put("X-Source", source);
Calendar cd = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US);
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
String datetime = sdf.format(cd.getTime());
headers.put("X-Date", datetime);
String signStr = "x-date: " + datetime + "\n" + "x-source: " + source;
String sig = StringUtil.toHMACSHA1String(signStr, secretKey);
String auth = "hmac id=\"" + secretId + "\", algorithm=\"hmac-sha1\", headers=\"x-date x-source\", signature=\"" + sig + "\"";
headers.put("Authorization", auth);
return headers;
}
public JSONArray get(String sExpressNO, String sExpressType) {
Map<String, String> headers = this.getHeaders();
Map<String, String> queryParams = new HashMap<String, String>();
queryParams.put("number", sExpressNO);
queryParams.put("type", sExpressType);
String result = HttpUtil.get(url, queryParams, headers);
JSONObject jsonObject = null;
try {
jsonObject = JSON.parseObject(result);
} catch (Exception e) {
throw new XException("快递查询失败");
}
if(jsonObject != null) {
String status = jsonObject.getString("status");
if(status != null) {
status = status.trim();
if("0".equals(status)) {
JSONObject resultObject = jsonObject.getJSONObject("result");
if(resultObject != null) {
return resultObject.getJSONArray("list");
}
} else {
if("201".equals(status)) {
throw new XException("快递单号为空");
} else if("202".equals(status)) {
throw new XException("快递公司为空");
} else if("203".equals(status)) {
throw new XException("快递公司不存在");
} else if("204".equals(status)) {
throw new XException("快递公司识别失败");
} else if("205".equals(status)) {
throw new XException("没有信息");
}
}
}
}
return new JSONArray();
}
public JSONArray get(String sExpressNO) {
return this.get(sExpressNO, null);
}
}
...@@ -54,6 +54,10 @@ log.datasource.testWhileIdle=true ...@@ -54,6 +54,10 @@ log.datasource.testWhileIdle=true
log.datasource.validationQueryTimeout=5 log.datasource.validationQueryTimeout=5
log.datasource.validationQuery=SELECT 1 FROM DUAL log.datasource.validationQuery=SELECT 1 FROM DUAL
opt.express.url=https://service-5ptj62bx-1257101137.ap-shanghai.apigateway.myqcloud.com/release/express/type
opt.express.secretId=AKID3dJ0MqWwN23oZ8xt4GFB0s8My1rEfUW2diLU
opt.express.secretKey=4mp4j4Vu4ZE8lCI9R9jqlo0okFz3bRh24II19d9y
spring.redis.database=0 spring.redis.database=0
spring.redis.host=127.0.0.1 spring.redis.host=127.0.0.1
spring.redis.port=16379 spring.redis.port=16379
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment