wenhongquan 5 miesięcy temu
rodzic
commit
841d2f0ca8

+ 6 - 0
ruoyi-modules/ruoyi-dzbc/src/main/java/org/dromara/system/controller/WeatherController.java

@@ -21,4 +21,10 @@ public class WeatherController extends BaseController {
         WeatherVo weather = weatherService.getFuzhouWeather();
         return R.ok(weather);
     }
+
+    @GetMapping("/fuan")
+    public R<WeatherVo> getFuAnWeather() {
+        WeatherVo weather = weatherService.getFuAnWeather();
+        return R.ok(weather);
+    }
 }

+ 2 - 0
ruoyi-modules/ruoyi-dzbc/src/main/java/org/dromara/system/service/IWeatherService.java

@@ -5,4 +5,6 @@ import org.dromara.system.domain.vo.WeatherVo;
 public interface IWeatherService {
 
     WeatherVo getFuzhouWeather();
+
+    WeatherVo getFuAnWeather();
 }

+ 30 - 5
ruoyi-modules/ruoyi-dzbc/src/main/java/org/dromara/system/service/impl/WeatherServiceImpl.java

@@ -25,9 +25,11 @@ import java.util.List;
 public class WeatherServiceImpl implements IWeatherService {
 
     private static final String FUZHOU_LOCATION = "101210101";
+    private static final String FUAN_LOCATION = "101210901";
     private static final String WEATHER_API_URL = "https://p26hexq5xk.yun.qweatherapi.com/v7/weather/7d";
-    private static final String REDIS_KEY_PREFIX = "weather:fuzhou:";
+    private static final String REDIS_KEY_PREFIX = "weather:";
     private static final String REDIS_KEY = REDIS_KEY_PREFIX + FUZHOU_LOCATION;
+    private static final String REDIS_KEY_FUAN = REDIS_KEY_PREFIX + FUAN_LOCATION;
 
     @Value("${qweather.apiKey}")
     private String apiKey;
@@ -42,7 +44,7 @@ public class WeatherServiceImpl implements IWeatherService {
             }
 
             log.info("调用和风天气 API 获取福州天气数据");
-            WeatherVo weather = fetchWeatherFromApi();
+            WeatherVo weather = fetchWeatherFromApi(FUZHOU_LOCATION, "福州", REDIS_KEY);
             if (weather != null) {
                 long expireTime = calculateExpireTime();
                 RedisUtils.setCacheObject(REDIS_KEY, weather, Duration.ofMillis(expireTime));
@@ -55,9 +57,32 @@ public class WeatherServiceImpl implements IWeatherService {
         }
     }
 
-    private WeatherVo fetchWeatherFromApi() {
+    @Override
+    public WeatherVo getFuAnWeather() {
+        try {
+            WeatherVo cachedWeather = RedisUtils.getCacheObject(REDIS_KEY_FUAN);
+            if (cachedWeather != null) {
+                log.info("从 Redis 获取福安天气数据");
+                return cachedWeather;
+            }
+
+            log.info("调用和风天气 API 获取福安天气数据");
+            WeatherVo weather = fetchWeatherFromApi(FUAN_LOCATION, "福安", REDIS_KEY_FUAN);
+            if (weather != null) {
+                long expireTime = calculateExpireTime();
+                RedisUtils.setCacheObject(REDIS_KEY_FUAN, weather, Duration.ofMillis(expireTime));
+                log.info("福安天气数据已缓存,过期时间:{} 毫秒", expireTime);
+            }
+            return weather;
+        } catch (Exception e) {
+            log.error("获取福安天气失败", e);
+            return null;
+        }
+    }
+
+    private WeatherVo fetchWeatherFromApi(String location, String cityName, String redisKey) {
         try {
-            String url = WEATHER_API_URL + "?location=" + FUZHOU_LOCATION + "&key=" + apiKey;
+            String url = WEATHER_API_URL + "?location=" + location + "&key=" + apiKey;
             String response = HttpUtil.get(url, 30000);
             JSONObject jsonResponse = JSONUtil.parseObj(response);
 
@@ -67,7 +92,7 @@ public class WeatherServiceImpl implements IWeatherService {
             }
 
             WeatherVo weather = new WeatherVo();
-            weather.setLocation("福州");
+            weather.setLocation(cityName);
             weather.setFxLink(jsonResponse.getStr("fxLink"));
 
             List<JSONObject> dailyJsonList = jsonResponse.getBeanList("daily", JSONObject.class);