Bladeren bron

优化日志

gongfuzhu 11 maanden geleden
bovenliggende
commit
703714084a

+ 138 - 68
SpringBatchServiceServer/src/main/java/com/zswl/cloud/springBtach/server/core/api/qiuxiang/QiuXiangApi.java

@@ -1,11 +1,21 @@
 package com.zswl.cloud.springBtach.server.core.api.qiuxiang;
 
+import cn.hutool.core.util.ObjectUtil;
 import cn.hutool.http.HttpRequest;
+import cn.hutool.json.JSONArray;
 import cn.hutool.json.JSONObject;
+import cn.hutool.json.JSONUtil;
+import com.github.microservice.auth.security.helper.AuthHelper;
 import com.zswl.cloud.springBatch.client.model.qiuxiang.OrderRequest;
+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 java.util.Map;
+
 @Component
 public class QiuXiangApi {
 
@@ -18,20 +28,48 @@ public class QiuXiangApi {
     @Value("${qxdj.secret}")
     private String SECRET;
 
+    @Autowired
+    private ApiLogDao apiLogDao;
+
+    @Autowired
+    AuthHelper authHelper;
+
+    private final String qiuxiang_token = "qiuxiang_token";
+    private final String qxUserToken = "qxUser_token";
+    private final String system = "qiuxiang";
+
+    @Autowired
+    RedisHelper redisHelper;
+
     /**
      * 获取接口调用凭证
      *
      * @return token
      */
     public String getPlatformToken() {
-        String url = BASE_URL + "/token";
-        String result = HttpRequest.get(url)
-                .form("appid", APPID)
-                .form("secret", SECRET)
-                .execute().body();
+        Object token = redisHelper.getValue(qiuxiang_token);
+
+        if (ObjectUtil.isEmpty(token)) {
+            ApiLog apiLog = new ApiLog();
+            String url = BASE_URL + "/token";
+            HttpRequest httpRequest = HttpRequest.get(url);
+            Map<String, String> appid = Map.of("appid", APPID, "secret", SECRET);
+            String result = httpRequest
+                    .form("appid", APPID)
+                    .formStr(appid)
+                    .execute().body();
+            apiLog.setService("/token");
+            apiLog.setSystem(system);
+            apiLog.setRequest(appid.toString());
+            apiLog.setResponse(result);
+            apiLogDao.save(apiLog);
+            JSONObject json = new JSONObject(result);
+            String newToken = json.getStr("token");
+            redisHelper.cacheValue(qiuxiang_token, newToken, 7000);
+            return newToken;
+        }
 
-        JSONObject json = new JSONObject(result);
-        return json.getStr("token");
+        return String.valueOf(token);
     }
 
     /**
@@ -40,10 +78,10 @@ public class QiuXiangApi {
      * @param token 接口调用凭证
      * @return permissions
      */
-    public String getPermissions(String token) {
+    public String getPermissions() {
         String url = BASE_URL + "/permissions";
         String result = HttpRequest.get(url)
-                .header("Authorization", "Bearer " + token)
+                .header("Authorization", "Bearer " + getPlatformToken())
                 .execute().body();
 
         JSONObject json = new JSONObject(result);
@@ -56,15 +94,31 @@ public class QiuXiangApi {
      * @param phone 用户手机号码
      * @return 登录信息,包含uid和新的token
      */
-    public String loginUser(String token, String phone) {
-        String url = BASE_URL + "/user/login";
-        String result = HttpRequest.get(url)
-                .header("Token", token)
-                .form("phone", phone)
-                .execute().body();
+    public String loginUser(String phone) {
+
+        Object userToken = redisHelper.getValue(qxUserToken + phone);
+        if (ObjectUtil.isEmpty(userToken)) {
+            ApiLog apiLog = new ApiLog();
+            String url = BASE_URL + "/user/login";
+            Map<String, String> parm = Map.of("phone", phone);
+            String result = HttpRequest.get(url)
+                    .header("Token", getPlatformToken())
+                    .formStr(parm)
+                    .execute().body();
+
+            apiLog.setSystem(system);
+            apiLog.setService("/user/login");
+            apiLog.setRequest(phone.toString());
+            apiLog.setResponse(result);
+            apiLogDao.save(apiLog);
+
+            JSONObject json = new JSONObject(result);
+            String token = json.getStr("token");
+            redisHelper.cacheValue(qxUserToken + phone, token, 2591000);
+            return token;
+        }
 
-        JSONObject json = new JSONObject(result);
-        return json.toString();
+        return String.valueOf(userToken);
     }
 
     /**
@@ -91,11 +145,7 @@ public class QiuXiangApi {
             body.put("destinationLatitude", orderRequest.getDestinationLatitude());
         }
 
-        String result = HttpRequest.post(url)
-                .header("Token", token)
-                .body(body.toString())
-                .execute().body();
-
+        String result = postRequest(url, body.toString());
         JSONObject json = new JSONObject(result);
         return json.toString();
     }
@@ -108,20 +158,13 @@ public class QiuXiangApi {
      * @param count
      * @return
      */
-    public JSONObject orderList(String userToken, String lastOrderId, int count) {
-        String url = BASE_URL + "/user/order/list";
-        JSONObject body = new JSONObject();
-        body.put("lastOrderId", lastOrderId);
-        body.put("count", count);
+    public JSONArray orderList(String lastOrderId, int count) {
+        String url = "/user/order/list";
+        Map<String, String> orderId1 = Map.of("lastOrderId", lastOrderId, "count", String.valueOf(count));
+        String request = getRequest(url, orderId1);
 
-        String result = HttpRequest.get(url)
-                .header("Token", userToken)
-                .form("lastOrderId", lastOrderId)
-                .form("count", count)
-                .execute().body();
 
-        JSONObject json = new JSONObject(result);
-        return json;
+        return JSONUtil.parseArray(request);
     }
 
     /**
@@ -131,12 +174,10 @@ public class QiuXiangApi {
      * @param orderId
      * @return
      */
-    public JSONObject orderDelit(String userToken, String orderId) {
-        String url = BASE_URL + "/user/order";
-        String result = HttpRequest.get(url)
-                .header("Token", userToken)
-                .form("orderId", orderId)
-                .execute().body();
+    public JSONObject orderDelit(String orderId) {
+        String url = "/user/order";
+        Map<String, String> orderId1 = Map.of("orderId", orderId);
+        String result = getRequest(url, orderId1);
 
         JSONObject json = new JSONObject(result);
         return json;
@@ -151,16 +192,10 @@ public class QiuXiangApi {
      * @param reason
      * @return
      */
-    public JSONObject cancelOrder(String token, String orderId, String reason) {
-        String url = BASE_URL + "/user/order";
-        JSONObject body = new JSONObject();
-        body.put("orderId", orderId);
-        body.put("reason", reason);
-
-        String result = HttpRequest.delete(url)
-                .header("Token", token)
-                .body(body.toString())
-                .execute().body();
+    public JSONObject cancelOrder(String orderId, String reason) {
+        String url = "/user/order";
+        Map<String, String> params = Map.of("orderId", orderId, "reason", reason);
+        String result = deleteRequest(url, JSONUtil.toJsonStr(params));
 
         JSONObject json = new JSONObject(result);
         return json;
@@ -174,17 +209,10 @@ public class QiuXiangApi {
      * @return
      */
     public JSONObject driverLocation(String token, String orderId) {
-        String url = BASE_URL + "/user/order/driver/location";
-        JSONObject body = new JSONObject();
-        body.put("orderId", orderId);
+        String url = "/user/order/driver/location";
+        String request = getRequest(url, Map.of("orderId", orderId));
 
-        String result = HttpRequest.get(url)
-                .header("Token", token)
-                .body(body.toString())
-                .execute().body();
-
-        JSONObject json = new JSONObject(result);
-        return json;
+        return new JSONObject(request);
     }
 
     /**
@@ -194,18 +222,60 @@ public class QiuXiangApi {
      * @param orderId
      * @return
      */
-    public JSONObject driverLocationLog(String token, String orderId) {
-        String url = BASE_URL + "/user/order/driver/location/log";
-        JSONObject body = new JSONObject();
-        body.put("orderId", orderId);
+    public JSONArray driverLocationLog(String orderId) {
+        String url = "/user/order/driver/location/log";
+        String request = getRequest(url, Map.of("orderId", orderId));
+        return JSONUtil.parseArray(request);
+    }
 
-        String result = HttpRequest.get(url)
+    private String getRequest(String url, Map<String, String> params) {
+//        String phone = authHelper.getCurrentUser().getPhone();
+        String token = loginUser("13368172379");
+        String result = HttpRequest.get(BASE_URL + url)
                 .header("Token", token)
-                .form("orderId", orderId)
+                .formStr(params)
                 .execute().body();
+        ApiLog apiLog = new ApiLog();
+        apiLog.setSystem(system);
+        apiLog.setService(url);
+        apiLog.setRequest(JSONUtil.toJsonStr(params));
+        apiLog.setResponse(result);
+        apiLogDao.save(apiLog);
+        return result;
+    }
 
-        JSONObject json = new JSONObject(result);
-        return json;
+    private String deleteRequest(String url, String jsonString) {
+//        String phone = authHelper.getCurrentUser().getPhone();
+        String token = loginUser("13368172379");
+        String result = HttpRequest.delete(BASE_URL + url)
+                .header("Token", token)
+                .body(jsonString)
+                .execute().body();
+        ApiLog apiLog = new ApiLog();
+        apiLog.setSystem(system);
+        apiLog.setService(url);
+        apiLog.setRequest(jsonString);
+        apiLog.setResponse(result);
+        apiLogDao.save(apiLog);
+
+        return result;
     }
 
+    private String postRequest(String url, String jsonString) {
+        String phone = authHelper.getCurrentUser().getPhone();
+        String token = loginUser("13368172379");
+        String result = HttpRequest.post(url)
+                .header("Token", token)
+                .body(jsonString)
+                .execute().body();
+        ApiLog apiLog = new ApiLog();
+        apiLog.setSystem(system);
+        apiLog.setService(url);
+        apiLog.setRequest(jsonString);
+        apiLog.setResponse(result);
+        apiLogDao.save(apiLog);
+
+        return result;
+
+    }
 }

+ 5 - 0
SpringBatchServiceServer/src/main/resources/application-dev.yml

@@ -66,6 +66,11 @@ yqd:
   createUrl: http://open.yiqida.cn/api/UserOrder/CreateOrder?timestamp=%s&userName=%s&sign=%s
   callbackUrl: https://api.dev.zonelife.cn/springbatchservice/sync/yqd/orderState
 
+qxdj:
+  url: http://test.qxtech.top/api/customer
+  appid: C24031111
+  secret: a7b3961a1e5f4b5cbb611f830e299857
+
 
 
 #logging: