1、后端获取预报天气接口实现

pull/254/head
xjs 4 years ago
parent 9bbeb47e53
commit b0ede0818c

@ -119,9 +119,13 @@ public class ApiConst {
*/ */
public static final String INFOCODE_VALUE = "10000"; public static final String INFOCODE_VALUE = "10000";
/** /**
* lives() * lives()
*/ */
public static final String LIVES= "lives"; public static final String LIVES= "lives";
/**
* forecasts
*/
public static final String FORECASTS= "forecasts";
//---------------------自定义相关请求响应常量---------------------------- //---------------------自定义相关请求响应常量----------------------------

@ -12,32 +12,37 @@ public class RedisConst {
/** /**
* key * key
*/ */
public static final String TRAN_DICT = "tranDict"; public static final String TRAN_DICT = "tianxing:tran_dict";
/** /**
* key * key
*/ */
public static final String ONE_ENGLISH = "oneEnglish"; public static final String ONE_ENGLISH = "tianxing:one_english";
/** /**
* key * key
*/ */
public static final String HOT = "hot"; public static final String HOT = "tianxing:hot";
/** /**
* websocketkey * websocketkey
*/ */
public static final String WEBSOCKET = "WEBSOCKET"; public static final String WEBSOCKET = "websocket";
/** /**
* ipkey * ipkey
*/ */
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 NOW_WHEATHER_EXPIRE = 10L; //分钟
public static final Long FORECAST_WHEATHER_EXPIRE = 10L; //分钟
} }

@ -66,7 +66,7 @@ public class IPUtils {
// 方法1 // 方法1
private static String getNowIP1() throws IOException { private static String getNowIP1() throws IOException {
String ip = null; String ip = null;
String chinaz = "http://ip.chinaz.com"; String chinaz = "https://ip.chinaz.com";
StringBuilder inputLine = new StringBuilder(); StringBuilder inputLine = new StringBuilder();
String read = ""; String read = "";
URL url = null; 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; private WeatherService weatherService;
@GetMapping @GetMapping("now")
@ApiOperation("获取天气信息") @ApiOperation("获取实时天气信息")
@Log(title = "获取天气") @Log(title = "获取实时天气")
@RequiresLogin @RequiresLogin
public AjaxResult getWeatherApiData() { public AjaxResult getNowWeatherApiData() {
return AjaxResult.success(weatherService.saveNowWeather()); return AjaxResult.success(weatherService.saveNowWeather());
} }
@GetMapping("forecast")
@ApiOperation("获取预报天气信息")
@Log(title = "获取预报天气")
@RequiresLogin
public AjaxResult getForecastWeatherApiData() {
return AjaxResult.success(weatherService.cacheForecastWeather());
}
@GetMapping("getWeatherForRPC") @GetMapping("getWeatherForRPC")
@ApiOperation("远程调用获取天气信息ForRPC") @ApiOperation("远程调用获取天气信息ForRPC")
public R getWeatherForRPC() { public R getWeatherForRPC() {

@ -39,6 +39,6 @@ public class ForecastWeather implements Serializable {
/** /**
* listcast, * listcast,
*/ */
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; package com.xjs.weather.service;
import com.xjs.weather.domain.ForecastWeather;
import com.xjs.weather.domain.NowWeather; import com.xjs.weather.domain.NowWeather;
/** /**
@ -21,5 +22,11 @@ public interface WeatherService {
*/ */
NowWeather save(); NowWeather save();
/**
*
* @return ForecastWeather
*/
ForecastWeather cacheForecastWeather();
} }

@ -1,7 +1,10 @@
package com.xjs.weather.service.impl; 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.ruoyi.common.redis.service.RedisService;
import com.xjs.exception.BusinessException; import com.xjs.exception.BusinessException;
import com.xjs.weather.domain.ForecastWeather;
import com.xjs.weather.domain.NowWeather; import com.xjs.weather.domain.NowWeather;
import com.xjs.weather.factory.WeatherFactory; import com.xjs.weather.factory.WeatherFactory;
import com.xjs.weather.mapper.NowWeatherMapper; import com.xjs.weather.mapper.NowWeatherMapper;
@ -11,12 +14,12 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects; import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import static com.xjs.consts.RedisConst.NOW_WEATHER; import static com.xjs.consts.RedisConst.*;
import static com.xjs.consts.RedisConst.NOW_WHEATHER_EXPIRE;
/** /**
* *
@ -29,6 +32,8 @@ public class WeatherServiceImpl implements WeatherService {
@Autowired @Autowired
private WeatherFactory<NowWeather> gaodeNowWeatherFactory; private WeatherFactory<NowWeather> gaodeNowWeatherFactory;
@Autowired
private WeatherFactory<ForecastWeather> gaodeForecastWeatherFactory;
@Resource @Resource
private NowWeatherMapper nowWeatherMapper; private NowWeatherMapper nowWeatherMapper;
@Autowired @Autowired
@ -41,11 +46,11 @@ public class WeatherServiceImpl implements WeatherService {
if (!redisService.hasKey(NOW_WEATHER)) { if (!redisService.hasKey(NOW_WEATHER)) {
NowWeather nowWeather = gaodeNowWeatherFactory.weatherApi(); NowWeather nowWeather = gaodeNowWeatherFactory.weatherApi();
if (Objects.nonNull(nowWeather)) { if (Objects.nonNull(nowWeather)) {
nowWeatherMapper.insert(nowWeather); this.checkExistSave(nowWeather);
redisService.setCacheObject(NOW_WEATHER, nowWeather, NOW_WHEATHER_EXPIRE, TimeUnit.MINUTES); redisService.setCacheObject(NOW_WEATHER, nowWeather, NOW_WHEATHER_EXPIRE, TimeUnit.MINUTES);
return nowWeather; return nowWeather;
} else { } else {
throw new BusinessException("获取天气数据为空"); throw new BusinessException("获取实时天气数据为空");
} }
} else { } else {
return (NowWeather) redisService.getCacheObject(NOW_WEATHER); return (NowWeather) redisService.getCacheObject(NOW_WEATHER);
@ -55,9 +60,38 @@ public class WeatherServiceImpl implements WeatherService {
@Override @Override
public NowWeather save() { public NowWeather save() {
NowWeather nowWeather = Optional.ofNullable(gaodeNowWeatherFactory.weatherApi()).orElseGet(NowWeather::new); NowWeather nowWeather = Optional.ofNullable(gaodeNowWeatherFactory.weatherApi()).orElseGet(NowWeather::new);
nowWeatherMapper.insert(nowWeather); this.checkExistSave(nowWeather);
return 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…
Cancel
Save