puzhibing
5 天以前 1f9454af3356f628c25c3e2e492869163731da56
ManagementFBTravel/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<>();