|
@@ -25,9 +25,11 @@ import java.util.List;
|
|
|
public class WeatherServiceImpl implements IWeatherService {
|
|
public class WeatherServiceImpl implements IWeatherService {
|
|
|
|
|
|
|
|
private static final String FUZHOU_LOCATION = "101210101";
|
|
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 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 = REDIS_KEY_PREFIX + FUZHOU_LOCATION;
|
|
|
|
|
+ private static final String REDIS_KEY_FUAN = REDIS_KEY_PREFIX + FUAN_LOCATION;
|
|
|
|
|
|
|
|
@Value("${qweather.apiKey}")
|
|
@Value("${qweather.apiKey}")
|
|
|
private String apiKey;
|
|
private String apiKey;
|
|
@@ -42,7 +44,7 @@ public class WeatherServiceImpl implements IWeatherService {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
log.info("调用和风天气 API 获取福州天气数据");
|
|
log.info("调用和风天气 API 获取福州天气数据");
|
|
|
- WeatherVo weather = fetchWeatherFromApi();
|
|
|
|
|
|
|
+ WeatherVo weather = fetchWeatherFromApi(FUZHOU_LOCATION, "福州", REDIS_KEY);
|
|
|
if (weather != null) {
|
|
if (weather != null) {
|
|
|
long expireTime = calculateExpireTime();
|
|
long expireTime = calculateExpireTime();
|
|
|
RedisUtils.setCacheObject(REDIS_KEY, weather, Duration.ofMillis(expireTime));
|
|
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 {
|
|
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);
|
|
String response = HttpUtil.get(url, 30000);
|
|
|
JSONObject jsonResponse = JSONUtil.parseObj(response);
|
|
JSONObject jsonResponse = JSONUtil.parseObj(response);
|
|
|
|
|
|
|
@@ -67,7 +92,7 @@ public class WeatherServiceImpl implements IWeatherService {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
WeatherVo weather = new WeatherVo();
|
|
WeatherVo weather = new WeatherVo();
|
|
|
- weather.setLocation("福州");
|
|
|
|
|
|
|
+ weather.setLocation(cityName);
|
|
|
weather.setFxLink(jsonResponse.getStr("fxLink"));
|
|
weather.setFxLink(jsonResponse.getStr("fxLink"));
|
|
|
|
|
|
|
|
List<JSONObject> dailyJsonList = jsonResponse.getBeanList("daily", JSONObject.class);
|
|
List<JSONObject> dailyJsonList = jsonResponse.getBeanList("daily", JSONObject.class);
|