1、后端获取预报天气接口实现
This commit is contained in:
parent
9bbeb47e53
commit
b0ede0818c
|
|
@ -119,9 +119,13 @@ public class ApiConst {
|
|||
*/
|
||||
public static final String INFOCODE_VALUE = "10000";
|
||||
/**
|
||||
* 高德返回值lives名称(需要的天气参数)
|
||||
* 高德返回值lives名称(需要的天气参数) (实时天气数据)
|
||||
*/
|
||||
public static final String LIVES= "lives";
|
||||
/**
|
||||
* 高德返回值forecasts名称(预报天气数据)
|
||||
*/
|
||||
public static final String FORECASTS= "forecasts";
|
||||
|
||||
|
||||
//---------------------自定义相关请求响应常量----------------------------
|
||||
|
|
|
|||
|
|
@ -12,32 +12,37 @@ public class RedisConst {
|
|||
/**
|
||||
* 翻译字典常量key
|
||||
*/
|
||||
public static final String TRAN_DICT = "tranDict";
|
||||
public static final String TRAN_DICT = "tianxing:tran_dict";
|
||||
|
||||
/**
|
||||
* 英语一言常量key
|
||||
*/
|
||||
public static final String ONE_ENGLISH = "oneEnglish";
|
||||
public static final String ONE_ENGLISH = "tianxing:one_english";
|
||||
|
||||
/**
|
||||
* 热搜常量key
|
||||
*/
|
||||
public static final String HOT = "hot";
|
||||
public static final String HOT = "tianxing:hot";
|
||||
|
||||
/**
|
||||
* websocket常量key
|
||||
*/
|
||||
public static final String WEBSOCKET = "WEBSOCKET";
|
||||
public static final String WEBSOCKET = "websocket";
|
||||
|
||||
/**
|
||||
* ip信息常量key
|
||||
*/
|
||||
public static final String IP_INFO = "IPInfo";
|
||||
public static final String IP_INFO = "ip_info";
|
||||
|
||||
/**
|
||||
* 天气常量信息key
|
||||
* 实时天气常量信息key
|
||||
*/
|
||||
public static final String NOW_WEATHER = "nowWeather";
|
||||
public static final String NOW_WEATHER = "weather:now";
|
||||
|
||||
/**
|
||||
* 预报天气常量信息key
|
||||
*/
|
||||
public static final String FORECAST_WEATHER = "weather:forecast";
|
||||
|
||||
|
||||
//-------------------有效时间-----------------------
|
||||
|
|
@ -51,5 +56,7 @@ public class RedisConst {
|
|||
|
||||
public static final Long NOW_WHEATHER_EXPIRE = 10L; //分钟
|
||||
|
||||
public static final Long FORECAST_WHEATHER_EXPIRE = 10L; //分钟
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -66,7 +66,7 @@ public class IPUtils {
|
|||
// 方法1
|
||||
private static String getNowIP1() throws IOException {
|
||||
String ip = null;
|
||||
String chinaz = "http://ip.chinaz.com";
|
||||
String chinaz = "https://ip.chinaz.com";
|
||||
StringBuilder inputLine = new StringBuilder();
|
||||
String read = "";
|
||||
URL url = null;
|
||||
|
|
@ -174,4 +174,16 @@ public class IPUtils {
|
|||
}
|
||||
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
String nowIP1 = IPUtils.getNowIP1();
|
||||
//String nowIP2 = IPUtils.getNowIP2();
|
||||
//String nowIP3 = IPUtils.getNowIP3();
|
||||
//String nowIP4 = IPUtils.getNowIP4();
|
||||
System.out.println(nowIP1);
|
||||
//System.out.println(nowIP2);
|
||||
//System.out.println(nowIP3);
|
||||
//System.out.println(nowIP4);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,14 +32,24 @@ public class WeatherController {
|
|||
private WeatherService weatherService;
|
||||
|
||||
|
||||
@GetMapping
|
||||
@ApiOperation("获取天气信息")
|
||||
@Log(title = "获取天气")
|
||||
@GetMapping("now")
|
||||
@ApiOperation("获取实时天气信息")
|
||||
@Log(title = "获取实时天气")
|
||||
@RequiresLogin
|
||||
public AjaxResult getWeatherApiData() {
|
||||
public AjaxResult getNowWeatherApiData() {
|
||||
return AjaxResult.success(weatherService.saveNowWeather());
|
||||
}
|
||||
|
||||
@GetMapping("forecast")
|
||||
@ApiOperation("获取预报天气信息")
|
||||
@Log(title = "获取预报天气")
|
||||
@RequiresLogin
|
||||
public AjaxResult getForecastWeatherApiData() {
|
||||
return AjaxResult.success(weatherService.cacheForecastWeather());
|
||||
}
|
||||
|
||||
|
||||
|
||||
@GetMapping("getWeatherForRPC")
|
||||
@ApiOperation("远程调用获取天气信息ForRPC")
|
||||
public R getWeatherForRPC() {
|
||||
|
|
|
|||
|
|
@ -39,6 +39,6 @@ public class ForecastWeather implements Serializable {
|
|||
/**
|
||||
* 预报数据list结构,元素cast,按顺序为当天、第二天、第三天的预报数据
|
||||
*/
|
||||
private List<Casts> CastsList;
|
||||
private List<Casts> casts;
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,51 @@
|
|||
package com.xjs.weather.factory.impl;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.xjs.common.client.api.gaode.GaodeWeatherFeignClient;
|
||||
import com.xjs.config.GaodeProperties;
|
||||
import com.xjs.weather.domain.ForecastWeather;
|
||||
import com.xjs.weather.domain.IPInfoVo;
|
||||
import com.xjs.weather.domain.RequestBody;
|
||||
import com.xjs.weather.factory.WeatherFactory;
|
||||
import com.xjs.weather.service.IPService;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import static com.xjs.consts.ApiConst.*;
|
||||
|
||||
/**
|
||||
* 高德预报天气工厂实现
|
||||
* @author xiejs
|
||||
* @since 2022-01-17
|
||||
*/
|
||||
@Component
|
||||
@Log4j2
|
||||
public class GaodeForecastWeatherFactory implements WeatherFactory<ForecastWeather> {
|
||||
|
||||
@Autowired
|
||||
private GaodeProperties gaodeProperties;
|
||||
@Autowired
|
||||
private GaodeWeatherFeignClient gaodeWeatherFeignClient;
|
||||
@Autowired
|
||||
private IPService ipService;
|
||||
|
||||
|
||||
@Override
|
||||
public ForecastWeather weatherApi() {
|
||||
RequestBody requestBody = new RequestBody();
|
||||
//获取城市编码
|
||||
IPInfoVo ipApiData = ipService.getIPApiData();
|
||||
requestBody.setKey(gaodeProperties.getKey())
|
||||
.setCity(ipApiData.getCityId())
|
||||
.setExtensions(GAODE_EXTENSIONS_ALL);
|
||||
JSONObject jsonObject = gaodeWeatherFeignClient.WeatherApi(requestBody);
|
||||
if (!jsonObject.containsKey(DEMOTE_ERROR)) {
|
||||
if (INFOCODE_VALUE.equals(jsonObject.getString(INFOCODE))) {
|
||||
JSONObject forecasts = jsonObject.getJSONArray(FORECASTS).getJSONObject(0);
|
||||
return forecasts.toJavaObject(ForecastWeather.class);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
package com.xjs.weather.service;
|
||||
|
||||
import com.xjs.weather.domain.ForecastWeather;
|
||||
import com.xjs.weather.domain.NowWeather;
|
||||
|
||||
/**
|
||||
|
|
@ -21,5 +22,11 @@ public interface WeatherService {
|
|||
*/
|
||||
NowWeather save();
|
||||
|
||||
/**
|
||||
* 预报天气放入缓存
|
||||
* @return ForecastWeather
|
||||
*/
|
||||
ForecastWeather cacheForecastWeather();
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,10 @@
|
|||
package com.xjs.weather.service.impl;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.ruoyi.common.redis.service.RedisService;
|
||||
import com.xjs.exception.BusinessException;
|
||||
import com.xjs.weather.domain.ForecastWeather;
|
||||
import com.xjs.weather.domain.NowWeather;
|
||||
import com.xjs.weather.factory.WeatherFactory;
|
||||
import com.xjs.weather.mapper.NowWeatherMapper;
|
||||
|
|
@ -11,12 +14,12 @@ import org.springframework.stereotype.Service;
|
|||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Date;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static com.xjs.consts.RedisConst.NOW_WEATHER;
|
||||
import static com.xjs.consts.RedisConst.NOW_WHEATHER_EXPIRE;
|
||||
import static com.xjs.consts.RedisConst.*;
|
||||
|
||||
/**
|
||||
* 天气服务实现
|
||||
|
|
@ -29,6 +32,8 @@ public class WeatherServiceImpl implements WeatherService {
|
|||
|
||||
@Autowired
|
||||
private WeatherFactory<NowWeather> gaodeNowWeatherFactory;
|
||||
@Autowired
|
||||
private WeatherFactory<ForecastWeather> gaodeForecastWeatherFactory;
|
||||
@Resource
|
||||
private NowWeatherMapper nowWeatherMapper;
|
||||
@Autowired
|
||||
|
|
@ -41,11 +46,11 @@ public class WeatherServiceImpl implements WeatherService {
|
|||
if (!redisService.hasKey(NOW_WEATHER)) {
|
||||
NowWeather nowWeather = gaodeNowWeatherFactory.weatherApi();
|
||||
if (Objects.nonNull(nowWeather)) {
|
||||
nowWeatherMapper.insert(nowWeather);
|
||||
this.checkExistSave(nowWeather);
|
||||
redisService.setCacheObject(NOW_WEATHER, nowWeather, NOW_WHEATHER_EXPIRE, TimeUnit.MINUTES);
|
||||
return nowWeather;
|
||||
} else {
|
||||
throw new BusinessException("获取天气数据为空");
|
||||
throw new BusinessException("获取实时天气数据为空");
|
||||
}
|
||||
} else {
|
||||
return (NowWeather) redisService.getCacheObject(NOW_WEATHER);
|
||||
|
|
@ -55,9 +60,38 @@ public class WeatherServiceImpl implements WeatherService {
|
|||
@Override
|
||||
public NowWeather save() {
|
||||
NowWeather nowWeather = Optional.ofNullable(gaodeNowWeatherFactory.weatherApi()).orElseGet(NowWeather::new);
|
||||
nowWeatherMapper.insert(nowWeather);
|
||||
this.checkExistSave(nowWeather);
|
||||
return nowWeather;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ForecastWeather cacheForecastWeather() {
|
||||
if (redisService.hasKey(FORECAST_WEATHER)) {
|
||||
return (ForecastWeather) redisService.getCacheObject(FORECAST_WEATHER);
|
||||
}
|
||||
ForecastWeather forecastWeather = gaodeForecastWeatherFactory.weatherApi();
|
||||
if (Objects.nonNull(forecastWeather)) {
|
||||
redisService.setCacheObject(FORECAST_WEATHER, forecastWeather, FORECAST_WHEATHER_EXPIRE, TimeUnit.MINUTES);
|
||||
return forecastWeather;
|
||||
}else {
|
||||
throw new BusinessException("获取预报天气数据为空");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 校验当前天气数据数据库是否存在
|
||||
*
|
||||
* @param nowWeather 天气数据
|
||||
*/
|
||||
private void checkExistSave(NowWeather nowWeather) {
|
||||
Date reporttime = nowWeather.getReporttime();
|
||||
String dateTime = DateUtil.formatDateTime(reporttime);
|
||||
NowWeather selectOne = nowWeatherMapper.selectOne(new QueryWrapper<NowWeather>().eq("reporttime", dateTime));
|
||||
if (Objects.isNull(selectOne)) {
|
||||
nowWeatherMapper.insert(nowWeather);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue