Procházet zdrojové kódy

feat(geo): 添加地理位置查询接口

- 新增 RestTemplate 配置类用于HTTP请求
- 创建地理编码控制器处理经纬度查询
- 实现调用外部地理服务获取最近地点功能
- 添加异常处理机制确保服务稳定性
- 集成自定义响应结构统一返回格式
mcbaiyun před 2 měsíci
rodič
revize
021d4ec0e3

+ 14 - 0
src/main/java/work/baiyun/chronicdiseaseapp/config/RestTemplateConfig.java

@@ -0,0 +1,14 @@
+package work.baiyun.chronicdiseaseapp.config;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.client.RestTemplate;
+
+@Configuration
+public class RestTemplateConfig {
+
+    @Bean
+    public RestTemplate restTemplate() {
+        return new RestTemplate();
+    }
+}

+ 30 - 0
src/main/java/work/baiyun/chronicdiseaseapp/controller/GeoController.java

@@ -0,0 +1,30 @@
+package work.baiyun.chronicdiseaseapp.controller;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.client.RestTemplate;
+import work.baiyun.chronicdiseaseapp.common.R;
+
+@RestController
+@RequestMapping("/geo")
+public class GeoController {
+
+    @Autowired
+    private RestTemplate restTemplate;
+
+    @GetMapping("/nearest")
+    public R<String> getNearestGeo(@RequestParam double latitude, @RequestParam double longitude) throws work.baiyun.chronicdiseaseapp.exception.CustomException {
+        try {
+            String url = "http://45.207.222.6/geo/nearest.php?latitude=" + latitude + "&longitude=" + longitude;
+            ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
+            return R.success(200, "ok", response.getBody());
+        } catch (Exception e) {
+            // 异常由全局处理器处理,这里抛出 CustomException 或直接返回 fail
+            throw new work.baiyun.chronicdiseaseapp.exception.CustomException("地理编码请求失败: " + e.getMessage());
+        }
+    }
+}