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

---
 UserFBTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/GDMapGeocodingUtil.java |   43 +++++++++++++++++++++++++++++++++++--------
 1 files changed, 35 insertions(+), 8 deletions(-)

diff --git a/UserFBTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/GDMapGeocodingUtil.java b/UserFBTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/GDMapGeocodingUtil.java
index 62bd4fb..8768d8f 100644
--- a/UserFBTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/GDMapGeocodingUtil.java
+++ b/UserFBTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/GDMapGeocodingUtil.java
@@ -20,13 +20,40 @@
 @Component
 public class GDMapGeocodingUtil {
 
-    private String key = "ca723c3c8a78962a7751129ae035dc9d";
-
     @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<>();
@@ -80,8 +107,8 @@
 
     public Map<String, Object> geocoding(String address){
         Map<String, Object> map = new HashMap<>();
-        String url = "https://restapi.amap.com/v3/geocode/geo?key=" + key + "&output=JSON&address=" + address;
-        String forObject = restTemplate.getForObject(url, String.class);
+        String url = "https://restapi.amap.com/v3/geocode/geo?output=JSON&address=" + address;
+        String forObject = doGetWithRetry(url);
         JSONObject jsonObject = JSON.parseObject(forObject);
         String status = jsonObject.getString("status");
         List<String> list = new ArrayList<>();
@@ -110,8 +137,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