|
@@ -0,0 +1,269 @@
|
|
|
+package com.zswl.cloud.springBtach.server.core.api.ypp;
|
|
|
+
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import cn.hutool.http.HttpRequest;
|
|
|
+import cn.hutool.http.HttpResponse;
|
|
|
+import cn.hutool.http.HttpUtil;
|
|
|
+import cn.hutool.json.JSONConfig;
|
|
|
+import cn.hutool.json.JSONObject;
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
+import com.zswl.cloud.springBatch.client.model.ypp.kfc.request.OrderListRequest;
|
|
|
+import com.zswl.cloud.springBatch.client.model.ypp.kfc.request.CreateOrdereRquest;
|
|
|
+import com.zswl.cloud.springBatch.client.model.ypp.kfc.response.*;
|
|
|
+import com.zswl.cloud.springBtach.server.core.dao.ApiLogDao;
|
|
|
+import com.zswl.cloud.springBtach.server.core.domain.ApiLog;
|
|
|
+import com.zswl.cloud.springBtach.server.core.helper.RedisHelper;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.util.Assert;
|
|
|
+import org.springframework.web.util.UriComponentsBuilder;
|
|
|
+
|
|
|
+import java.util.HashMap;
|
|
|
+
|
|
|
+//接口地址 https://apifox.com/apidoc/shared-48c64918-c4ce-49f4-b846-d23855299d7a/doc-2590155
|
|
|
+@Component
|
|
|
+public class KfcApi {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ RedisHelper redisHelper;
|
|
|
+
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ApiLogDao apiLogDao;
|
|
|
+
|
|
|
+ @Value("${ypp.entId}")
|
|
|
+ private String entId;
|
|
|
+
|
|
|
+ @Value("${ypp.appSecret}")
|
|
|
+ private String appSecret;
|
|
|
+
|
|
|
+ @Value("${ypp.appId}")
|
|
|
+ private String appId;
|
|
|
+ @Value("${ypp.test}")
|
|
|
+ private Boolean test;
|
|
|
+
|
|
|
+
|
|
|
+ private final String tokenName = "ypp-token";
|
|
|
+ private final String tokenValue = "Bearer ";
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 订单上传文件列表
|
|
|
+ *
|
|
|
+ * @param orderId
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public OrderFileResponse order_files(String orderId) {
|
|
|
+ String entries = httpGet("https://channel.youpiaopiao.cn/api/kfc/order_files?id=" + orderId);
|
|
|
+
|
|
|
+ OrderFileResponse bean = JSONUtil.toBean(entries, OrderFileResponse.class);
|
|
|
+ return bean;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 订单详情
|
|
|
+ *
|
|
|
+ * @param orderId
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+
|
|
|
+ public OrderDetailResponse order_detail(String orderId) {
|
|
|
+ String entries = httpGet("https://channel.youpiaopiao.cn/api/kfc/order_detail?id=" + orderId);
|
|
|
+
|
|
|
+ OrderDetailResponse bean = JSONUtil.toBean(entries, OrderDetailResponse.class);
|
|
|
+ return bean;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 城市列表
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public CityListResponse cityList() {
|
|
|
+ String entries = httpGet("https://channel.youpiaopiao.cn/api/city/list");
|
|
|
+
|
|
|
+ CityListResponse bean = JSONUtil.toBean(entries, CityListResponse.class);
|
|
|
+ return bean;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 支付订单(支付成功后调用)
|
|
|
+ *
|
|
|
+ * @param orderId
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public JSONObject pay_order(String orderId) {
|
|
|
+ String entries = httpGet("https://channel.youpiaopiao.cn/api/kfc/pay_order?id=" + orderId + "&test=" + test);
|
|
|
+ JSONConfig jsonConfig = JSONConfig.create();
|
|
|
+ jsonConfig.setIgnoreError(true);
|
|
|
+ return JSONUtil.parseObj(entries, jsonConfig);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 下单(自己做产品展示,才需要下单接口)
|
|
|
+ *
|
|
|
+ * @param createOrdereRquest
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public CreateOrdereResponse create_order(CreateOrdereRquest createOrdereRquest) {
|
|
|
+ createOrdereRquest.setAppId(appId);
|
|
|
+ JSONObject entries = httpPost("https://channel.youpiaopiao.cn/api/kfc/create_order", createOrdereRquest);
|
|
|
+ CreateOrdereResponse bean = JSONUtil.toBean(entries, CreateOrdereResponse.class);
|
|
|
+ return bean;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 订单列表
|
|
|
+ *
|
|
|
+ * @param orderListRequest
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public JSONObject order_list(OrderListRequest orderListRequest) {
|
|
|
+ JSONObject entries = httpPost("https://channel.youpiaopiao.cn/api/kfc/order_list", orderListRequest);
|
|
|
+ return entries;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 平台店铺列表
|
|
|
+ *
|
|
|
+ * @param ot
|
|
|
+ * @param cityId
|
|
|
+ * @param lat
|
|
|
+ * @param lon
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public ShopsResponse shops(String ot, String cityId, String lat, String lon) {
|
|
|
+ String baseUrl = "https://channel.youpiaopiao.cn/api/kfc/shops";
|
|
|
+
|
|
|
+ UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(baseUrl);
|
|
|
+ builder.queryParam("ot", ot);
|
|
|
+ builder.queryParam("cityId", cityId);
|
|
|
+ builder.queryParam("lat", lat);
|
|
|
+ builder.queryParam("lon", lon);
|
|
|
+ String entries = httpGet(builder.build().toUriString());
|
|
|
+ return JSONUtil.toBean(entries, ShopsResponse.class);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 店铺详情,如果传了经纬度,返回距离
|
|
|
+ * @param id
|
|
|
+ * @param lat
|
|
|
+ * @param lon
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public ShopResponse shop(String id, String lat, String lon) {
|
|
|
+ String baseUrl = "https://channel.youpiaopiao.cn/api/kfc/shop";
|
|
|
+
|
|
|
+ UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(baseUrl);
|
|
|
+ builder.queryParam("id", id);
|
|
|
+ builder.queryParam("lat", lat);
|
|
|
+ builder.queryParam("lon", lon);
|
|
|
+ String entries = httpGet(builder.build().toUriString());
|
|
|
+ return JSONUtil.toBean(entries, ShopResponse.class);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 菜单
|
|
|
+ * @param shopId
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public JSONObject menus(String shopId) {
|
|
|
+ String baseUrl = "https://channel.youpiaopiao.cn/api/kfc/menus";
|
|
|
+
|
|
|
+ UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(baseUrl);
|
|
|
+ builder.queryParam("shopId", shopId);
|
|
|
+ String entries = httpGet(builder.build().toUriString());
|
|
|
+ JSONConfig jsonConfig = new JSONConfig();
|
|
|
+ jsonConfig.setIgnoreError(true);
|
|
|
+ return JSONUtil.parseObj(entries, jsonConfig);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 商品详情(不同平台返回的商品详情不一致)
|
|
|
+ * @param shopId
|
|
|
+ * @param productId
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public JSONObject goods_detail(String shopId, String productId) {
|
|
|
+ String baseUrl = "https://channel.youpiaopiao.cn/api/kfc/goods_detail";
|
|
|
+
|
|
|
+ UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(baseUrl);
|
|
|
+ builder.queryParam("shopId", shopId);
|
|
|
+ builder.queryParam("productId", productId);
|
|
|
+ String entries = httpGet(builder.build().toUriString());
|
|
|
+ JSONConfig jsonConfig = new JSONConfig();
|
|
|
+ jsonConfig.setIgnoreError(true);
|
|
|
+ return JSONUtil.parseObj(entries, jsonConfig);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private String httpGet(String url) {
|
|
|
+ ApiLog apiLog = new ApiLog();
|
|
|
+ apiLog.setRequest(url);
|
|
|
+ apiLog.setSystem("kfc");
|
|
|
+ String token = getoken();
|
|
|
+ HttpRequest get = HttpUtil.createGet(url);
|
|
|
+ HttpResponse execute = get.header("Authorization", token).execute();
|
|
|
+ String body = execute.body();
|
|
|
+ apiLog.setResponse(body);
|
|
|
+ apiLogDao.save(apiLog);
|
|
|
+ JSONObject entries = JSONUtil.parseObj(body);
|
|
|
+ Integer code = (Integer) entries.get("code");
|
|
|
+ Assert.isTrue(execute.isOk(), "飘飘响应错误");
|
|
|
+ Assert.isTrue(code.equals(0), String.valueOf(entries.get("message")));
|
|
|
+ return body;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private JSONObject httpPost(String url, Object body) {
|
|
|
+
|
|
|
+ HttpRequest post = HttpUtil.createPost(url);
|
|
|
+ if (!url.contains("api/oauth/token")) {
|
|
|
+ String token = getoken();
|
|
|
+ post.header("Authorization", token);
|
|
|
+ }
|
|
|
+
|
|
|
+ ApiLog apiLog = new ApiLog();
|
|
|
+ apiLog.setRequest(url);
|
|
|
+ apiLog.setSystem("kfc");
|
|
|
+ apiLog.setService(url);
|
|
|
+ HttpResponse execute = post.body(JSONUtil.toJsonStr(body)).execute();
|
|
|
+ String response = execute.body();
|
|
|
+ apiLog.setRequest(JSONUtil.toJsonStr(body));
|
|
|
+ apiLog.setResponse(response);
|
|
|
+ apiLogDao.save(apiLog);
|
|
|
+ JSONObject entries = JSONUtil.parseObj(response);
|
|
|
+ Integer code = (Integer) entries.get("code");
|
|
|
+ Assert.isTrue(code.equals(0), String.valueOf(entries.get("message")));
|
|
|
+ return entries;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getoken() {
|
|
|
+ Object value = redisHelper.getValue(tokenName);
|
|
|
+ if (!ObjectUtil.isEmpty(value)) {
|
|
|
+ return String.valueOf(value);
|
|
|
+ }
|
|
|
+ HashMap<String, String> para = new HashMap<>();
|
|
|
+ para.put("entId", entId);
|
|
|
+ para.put("appSecret", appSecret);
|
|
|
+ JSONObject http = httpPost("https://channel.youpiaopiao.cn/api/oauth/token", para);
|
|
|
+ String access_token = (String) http.getByPath("data.access_token");
|
|
|
+ String token = tokenValue + access_token;
|
|
|
+ redisHelper.cacheValue(tokenName, token, 82800);
|
|
|
+ return token;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|