|
|
|
@@ -2,9 +2,9 @@ package com.xueyi.system.interfaces.airport.controller; |
|
|
|
|
|
|
|
import com.alibaba.fastjson2.JSONObject; |
|
|
|
import com.xueyi.common.redis.utils.RedisUtil; |
|
|
|
import com.xueyi.common.web.constant.ResponseCode; |
|
|
|
import com.xueyi.common.web.controller.BaseApiController; |
|
|
|
import com.xueyi.common.web.response.MyResponse; |
|
|
|
import com.xueyi.system.interfaces.airport.bean.FlightsQueryReq; |
|
|
|
import com.xueyi.system.interfaces.airport.bean.PlaneQueryReq; |
|
|
|
import okhttp3.MediaType; |
|
|
|
import okhttp3.OkHttpClient; |
|
|
|
@@ -13,7 +13,6 @@ import okhttp3.Response; |
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
import org.springframework.util.DigestUtils; |
|
|
|
import org.springframework.web.bind.annotation.RequestBody; |
|
|
|
import org.springframework.web.bind.annotation.RequestMapping; |
|
|
|
import org.springframework.web.bind.annotation.RestController; |
|
|
|
@@ -32,42 +31,25 @@ import java.io.IOException; |
|
|
|
public class PlaneController extends BaseApiController { |
|
|
|
private final Logger logger = LoggerFactory.getLogger(PlaneController.class); |
|
|
|
|
|
|
|
@Value("${notification.airport.flight.key}") |
|
|
|
@Value("${notification.airport.flight.juhe.key}") |
|
|
|
private String key; |
|
|
|
|
|
|
|
@Value("${notification.airport.flight.juhe.url}") |
|
|
|
private String juHeUrl; |
|
|
|
|
|
|
|
@Value("${notification.airport.flight.icredit.app-code}") |
|
|
|
private String icreditCode; |
|
|
|
|
|
|
|
@Value("${notification.airport.flight.icredit.url}") |
|
|
|
private String icreditUrl; |
|
|
|
|
|
|
|
@RequestMapping("/query-flight") |
|
|
|
public JSONObject query (@Valid @RequestBody PlaneQueryReq req, HttpServletRequest httpServletRequest) { |
|
|
|
String sign = httpServletRequest.getHeader("sign"); |
|
|
|
String ts = httpServletRequest.getHeader("ts"); |
|
|
|
System.err.println("----------sign check start----------"); |
|
|
|
if (ts == null || ts.isEmpty()) { |
|
|
|
return output(400, "ts is null").toJSON(); |
|
|
|
} |
|
|
|
if (sign == null || sign.isEmpty()) { |
|
|
|
return new MyResponse(400, "sign is null").toJSON(); |
|
|
|
} |
|
|
|
ts.replace(".", ""); |
|
|
|
Long tsVal = Long.parseLong(ts); |
|
|
|
if (ts.length() == 10) { |
|
|
|
// 将10位时间戳转为13位 |
|
|
|
tsVal *= 1000; |
|
|
|
} |
|
|
|
Long currentTs = System.currentTimeMillis(); |
|
|
|
System.err.println(currentTs); |
|
|
|
System.err.println(tsVal); |
|
|
|
|
|
|
|
if (currentTs - tsVal > 1000 * 60 || tsVal - currentTs > 1000 * 60){//时间误差正负1分钟内为合法 |
|
|
|
return new MyResponse(400, "请求时间戳ts非法").toJSON(); |
|
|
|
} |
|
|
|
String singStr = req.getDate() + req.getFNum() + ts; |
|
|
|
System.err.println(singStr); |
|
|
|
String signStr = DigestUtils.md5DigestAsHex(singStr.getBytes()); |
|
|
|
System.err.println(signStr); |
|
|
|
System.err.println(sign); |
|
|
|
System.err.println("----------sign check end----------"); |
|
|
|
|
|
|
|
if (!sign.equals(signStr)) { |
|
|
|
return output(ResponseCode.AUTH_NOT_PASS).toJSON(); |
|
|
|
MyResponse resp = super.checkSign(req, sign, ts); |
|
|
|
if (resp.getStatus() != 200) { |
|
|
|
return resp.toJSON(); |
|
|
|
} |
|
|
|
|
|
|
|
logger.info("查询航班信息:{}", req); |
|
|
|
@@ -83,7 +65,7 @@ public class PlaneController extends BaseApiController { |
|
|
|
logger.info("请求参数:{}", "key="+key+"&"+req.toQueryString()); |
|
|
|
okhttp3.RequestBody body = okhttp3.RequestBody.Companion.create("key="+key+"&"+req.toQueryString(), mediaType); |
|
|
|
Request request = new Request.Builder() |
|
|
|
.url("http://v.juhe.cn/flight_dynamic/query") |
|
|
|
.url(juHeUrl) |
|
|
|
.method("POST", body) |
|
|
|
.addHeader("Content-Type", "application/x-www-form-urlencoded") |
|
|
|
.build(); |
|
|
|
@@ -100,8 +82,48 @@ public class PlaneController extends BaseApiController { |
|
|
|
} catch (IOException e) { |
|
|
|
throw new RuntimeException(e); |
|
|
|
} |
|
|
|
return new JSONObject(); |
|
|
|
} |
|
|
|
|
|
|
|
@RequestMapping("/flight-list") |
|
|
|
public JSONObject flightList (@Valid @RequestBody FlightsQueryReq req, HttpServletRequest httpServletRequest) { |
|
|
|
String sign = httpServletRequest.getHeader("sign"); |
|
|
|
String ts = httpServletRequest.getHeader("ts"); |
|
|
|
MyResponse resp = super.checkSign(req, sign, ts); |
|
|
|
if (resp.getStatus() != 200) { |
|
|
|
return resp.toJSON(); |
|
|
|
} |
|
|
|
|
|
|
|
logger.info("查询航班列表信息:{}", req); |
|
|
|
String dateStr = req.getStartDate().replaceAll("-",""); |
|
|
|
String redisKey = "dgman:airport:flight:"+req.getEndCity()+":" + dateStr; |
|
|
|
if (RedisUtil.existed(redisKey)){ |
|
|
|
return JSONObject.parseObject(RedisUtil.getVal(redisKey).toString()); |
|
|
|
} |
|
|
|
|
|
|
|
req.setStartDate(req.getStartDate().replaceAll("-","")); |
|
|
|
|
|
|
|
OkHttpClient client = new OkHttpClient().newBuilder() |
|
|
|
.build(); |
|
|
|
Request request = new Request.Builder() |
|
|
|
.url(icreditUrl+"?"+req.toQueryString()) |
|
|
|
.get() |
|
|
|
.addHeader("Authorization", "APPCODE "+icreditCode) |
|
|
|
.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8") |
|
|
|
.build(); |
|
|
|
try { |
|
|
|
Response response = client.newCall(request).execute(); |
|
|
|
if (response.isSuccessful()) { |
|
|
|
String result = response.body().string(); |
|
|
|
logger.info("查询航班列表结果:{}", result); |
|
|
|
RedisUtil.setVal(redisKey,result,60*60*24); |
|
|
|
|
|
|
|
return JSONObject.parseObject(result); |
|
|
|
} |
|
|
|
|
|
|
|
} catch (IOException e) { |
|
|
|
throw new RuntimeException(e); |
|
|
|
} |
|
|
|
return new JSONObject(); |
|
|
|
} |
|
|
|
|
|
|
|
|