From 8def7d50fdac0d2d464edce1d94f5bb29c519302 Mon Sep 17 00:00:00 2001
From: puzhibing <393733352@qq.com>
Date: 星期五, 09 十月 2026 10:45:33 +0800
Subject: [PATCH] 修改高德账号切换bug

---
 DriverFBTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/GDMapGeocodingUtil.java |   39 +++++++++++++++++++++++++++++++++------
 1 files changed, 33 insertions(+), 6 deletions(-)

diff --git a/DriverFBTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/GDMapGeocodingUtil.java b/DriverFBTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/GDMapGeocodingUtil.java
index f7fd529..f4e206f 100644
--- a/DriverFBTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/GDMapGeocodingUtil.java
+++ b/DriverFBTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/GDMapGeocodingUtil.java
@@ -20,13 +20,40 @@
 @Component
 public class GDMapGeocodingUtil {
 
-    private String key = "c473445a931ee8c42eef8b63fc3602c5";
-
     @Autowired
     private RestTemplate restTemplate;
 
     @Autowired
     private IGDInterfaceService gdInterfaceService;
+
+    @Autowired
+    private GDKeyManager gdKeyManager;
+
+    /**
+     * 执行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;
+    }
 
 
     /**
@@ -55,9 +82,9 @@
             return map;
         }
 
-        String url = "https://restapi.amap.com/v3/geocode/geo?key=" + key + "&output=JSON";
+        String url = "https://restapi.amap.com/v3/geocode/geo?output=JSON";
         url += "&address=" + province + (ToolUtil.isNotEmpty(city) ? city : "") + (ToolUtil.isNotEmpty(county) ? county : "") + (ToolUtil.isNotEmpty(address) ? address : "");
-        String forObject = restTemplate.getForObject(url, String.class);
+        String forObject = doGetWithRetry(url);
         JSONObject jsonObject = JSON.parseObject(forObject);
         String status = jsonObject.getString("status");
         List<String> list = new ArrayList<>();
@@ -85,8 +112,8 @@
      * @throws Exception
      */
     public Map<String, String> geocode(String lon, String lan) throws Exception{
-        String url = "https://restapi.amap.com/v3/geocode/regeo?key=" + key + "&location=" + lon + "," + lan;
-        String forObject = restTemplate.getForObject(url, String.class);
+        String url = "https://restapi.amap.com/v3/geocode/regeo?location=" + lon + "," + lan;
+        String forObject = doGetWithRetry(url);
         JSONObject jsonObject = JSON.parseObject(forObject);
         Map<String, String> map = new HashMap<>();
 

--
Gitblit v1.7.1