From 1f9454af3356f628c25c3e2e492869163731da56 Mon Sep 17 00:00:00 2001
From: puzhibing <393733352@qq.com>
Date: 星期五, 09 十月 2026 10:47:24 +0800
Subject: [PATCH] 修改bug

---
 DriverFBTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/GDMapElectricFenceUtil.java |  132 ++++++++++++++++++++++++++++++++++---------
 1 files changed, 104 insertions(+), 28 deletions(-)

diff --git a/DriverFBTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/GDMapElectricFenceUtil.java b/DriverFBTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/GDMapElectricFenceUtil.java
index 8bf3b02..785eaa1 100644
--- a/DriverFBTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/GDMapElectricFenceUtil.java
+++ b/DriverFBTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/GDMapElectricFenceUtil.java
@@ -33,9 +33,98 @@
     @Autowired
     private IGDInterfaceService gdInterfaceService;
 
-    private String key = "c473445a931ee8c42eef8b63fc3602c5";
+    @Autowired
+    private GDKeyManager gdKeyManager;
 
     private JSONArray jsonArray = new JSONArray();
+
+    /**
+     * 执行GET请求:key次数用完时自动切换可用key重试
+     */
+    private String doGetWithRetry(String baseUrl) {
+        String lastResp = null;
+        int maxAttempts = 5;
+        for (int i = 0; i < maxAttempts; i++) {
+            String key = gdKeyManager.getKey();
+            String url = baseUrl + (baseUrl.contains("?") ? "&" : "?") + "key=" + key;
+            String resp = restTemplate.getForObject(url, String.class);
+            lastResp = resp;
+            JSONObject jsonObject;
+            try {
+                jsonObject = JSON.parseObject(resp);
+            } catch (Exception e) {
+                return resp;
+            }
+            if (GDKeyManager.isWebKeyExhausted(jsonObject)) {
+                gdKeyManager.reportExhausted(key);
+                continue;
+            }
+            return resp;
+        }
+        return lastResp;
+    }
+
+    /**
+     * 执行POST请求(JSON体):key次数用完时自动切换可用key重试
+     */
+    private String doPostJsonWithRetry(String baseUrl, String jsonBody) {
+        String lastResp = null;
+        int maxAttempts = 5;
+        HttpHeaders headers = new HttpHeaders();
+        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
+        headers.setContentType(type);
+        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
+        for (int i = 0; i < maxAttempts; i++) {
+            String key = gdKeyManager.getKey();
+            String url = baseUrl + (baseUrl.contains("?") ? "&" : "?") + "key=" + key;
+            HttpEntity<String> formEntity = new HttpEntity<>(jsonBody, headers);
+            String resp = restTemplate.postForObject(url, formEntity, String.class);
+            lastResp = resp;
+            JSONObject jsonObject;
+            try {
+                jsonObject = JSON.parseObject(resp);
+            } catch (Exception e) {
+                return resp;
+            }
+            if (GDKeyManager.isWebKeyExhausted(jsonObject)) {
+                gdKeyManager.reportExhausted(key);
+                continue;
+            }
+            return resp;
+        }
+        return lastResp;
+    }
+
+    /**
+     * 执行PATCH请求(JSON体):key次数用完时自动切换可用key重试
+     */
+    private String doPatchJsonWithRetry(String baseUrl, String jsonBody) {
+        String lastResp = null;
+        int maxAttempts = 5;
+        HttpHeaders headers = new HttpHeaders();
+        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
+        headers.setContentType(type);
+        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
+        for (int i = 0; i < maxAttempts; i++) {
+            String key = gdKeyManager.getKey();
+            String url = baseUrl + (baseUrl.contains("?") ? "&" : "?") + "key=" + key;
+            HttpEntity<String> formEntity = new HttpEntity<>(jsonBody, headers);
+            String resp = restTemplate.patchForObject(url, formEntity, String.class);
+            lastResp = resp;
+            JSONObject jsonObject;
+            try {
+                jsonObject = JSON.parseObject(resp);
+            } catch (Exception e) {
+                return resp;
+            }
+            if (GDKeyManager.isWebKeyExhausted(jsonObject)) {
+                gdKeyManager.reportExhausted(key);
+                continue;
+            }
+            return resp;
+        }
+        return lastResp;
+    }
 
 
     @Bean
@@ -81,14 +170,7 @@
             json.put("alert_condition", "enter;leave");
             json.put("desc", id);
 
-            HttpHeaders headers = new HttpHeaders();
-            MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
-            headers.setContentType(type);
-            headers.add("Accept", MediaType.APPLICATION_JSON.toString());
-            HttpEntity<String> formEntity = new HttpEntity<>(json.toString(), headers);
-
-            String url = "https://restapi.amap.com/v4/geofence/meta?key=" + key;
-            String js = restTemplate.postForObject(url, formEntity, String.class);
+            String js = doPostJsonWithRetry("https://restapi.amap.com/v4/geofence/meta", json.toString());
             JSONObject jsonObject = JSON.parseObject(js);
             JSONObject data = jsonObject.getJSONObject("data");
             String status = data.getString("status");
@@ -148,8 +230,8 @@
      * 查询以创建的所有电子围栏
      */
     public String findElectricFenc(String center, String radius, String points){
-        String url = "https://restapi.amap.com/v4/geofence/meta?key=" + key;
-        String forObject = restTemplate.getForObject(url, String.class);
+        String url = "https://restapi.amap.com/v4/geofence/meta";
+        String forObject = doGetWithRetry(url);
         JSONObject jsonObject = JSON.parseObject(forObject);
         JSONObject data = jsonObject.getJSONObject("data");
 
@@ -185,8 +267,8 @@
         if(page == 1){
             jsonArray = new JSONArray();
         }
-        String url = "https://restapi.amap.com/v4/geofence/meta?key=" + key + "&page_no=" + page + "&page_size=" + 30;
-        String forObject = restTemplate.getForObject(url, String.class);
+        String url = "https://restapi.amap.com/v4/geofence/meta?page_no=" + page + "&page_size=" + 30;
+        String forObject = doGetWithRetry(url);
         JSONObject jsonObject = JSON.parseObject(forObject);
         JSONObject data = jsonObject.getJSONObject("data");
         JSONArray rs_list = data.getJSONArray("rs_list");
@@ -275,14 +357,8 @@
         json.put("alert_condition", "enter;leave");
         json.put("desc", id + "围栏更新描述");
 
-        HttpHeaders headers = new HttpHeaders();
-        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
-        headers.setContentType(type);
-        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
-        HttpEntity<String> formEntity = new HttpEntity<>(json.toString(), headers);
-
-        String url = "https://restapi.amap.com/v4/geofence/meta?key=" + key + "&gid=" + gid;
-        String s = restTemplate.patchForObject(url, formEntity, String.class);
+        String url = "https://restapi.amap.com/v4/geofence/meta?gid=" + gid;
+        String s = doPatchJsonWithRetry(url, json.toString());
         System.out.println(s);
 
 //        gdInterfaceService.saveData("https://restapi.amap.com/v4/geofence/meta", "更新电子围栏");
@@ -304,8 +380,8 @@
             diu = "B78F99DD-2496-4A43-843B-172CDA2D9798";
         }
         locations = locations + "," + (new Date().getTime() / 1000);
-        String url = "https://restapi.amap.com/v4/geofence/status?key=" + key + "&diu=" + diu + "&locations=" + locations;
-        String forObject = restTemplate.getForObject(url, String.class);
+        String url = "https://restapi.amap.com/v4/geofence/status?diu=" + diu + "&locations=" + locations;
+        String forObject = doGetWithRetry(url);
         JSONObject jsonObject = JSON.parseObject(forObject);
         List<String> ids = new ArrayList<>();
         JSONObject data = jsonObject.getJSONObject("data");
@@ -334,7 +410,7 @@
      * @return
      */
     public boolean deleteElectricFenc(String gid){
-        restTemplate.delete("https://restapi.amap.com/v4/geofence/meta?key={1}&gid={2}", key, gid);
+        restTemplate.delete("https://restapi.amap.com/v4/geofence/meta?key={1}&gid={2}", gdKeyManager.getKey(), gid);
         System.out.println("删除:" );
 
 //        gdInterfaceService.saveData("https://restapi.amap.com/v4/geofence/meta", "删除电子围栏");
@@ -351,9 +427,9 @@
      * @return
      */
     public Map<String, String> getDistance(String origins, String destination, Integer type){
-        String url = "https://restapi.amap.com/v3/distance?key=" + key + "&origins=" + origins + "&destination=" + destination +
+        String url = "https://restapi.amap.com/v3/distance?origins=" + origins + "&destination=" + destination +
                 "&type=" + type;
-        String forObject = restTemplate.getForObject(url, String.class);
+        String forObject = doGetWithRetry(url);
         JSONObject jsonObject = JSON.parseObject(forObject);
         String status = jsonObject.getString("status");
 
@@ -381,9 +457,9 @@
      * @return
      */
     public Map<String, String> getDriving(String origins, String destination, String waypoints){
-        String url = "https://restapi.amap.com/v3/direction/driving?key=" + key + "&origin=" + origins + "&destination=" + destination +
+        String url = "https://restapi.amap.com/v3/direction/driving?origin=" + origins + "&destination=" + destination +
                 "&waypoints=" + waypoints + "&extensions=base&strategy=0";
-        String forObject = restTemplate.getForObject(url, String.class);
+        String forObject = doGetWithRetry(url);
         JSONObject jsonObject = JSON.parseObject(forObject);
         String status = jsonObject.getString("status");
 

--
Gitblit v1.7.1