1、后端获取实时天气接口实现
This commit is contained in:
parent
dffb2aa293
commit
a5cd1d7fe1
|
|
@ -1,98 +1,93 @@
|
|||
package com.xjs.consts;
|
||||
|
||||
/**
|
||||
* api名称常量类
|
||||
*
|
||||
* @author xiejs
|
||||
* @desc api名称常量类
|
||||
* @create 2021-12-28
|
||||
*/
|
||||
public class ApiConst {
|
||||
|
||||
//--------------------name------------------------------
|
||||
//--------------------name_url------------------------------
|
||||
|
||||
public static final String YOUDAO_FY= "有道翻译";
|
||||
public static final String YOUDAO_FY = "有道翻译";
|
||||
|
||||
public static final String BAIDU_FY= "百度翻译";
|
||||
public static final String BAIDU_FY = "百度翻译";
|
||||
|
||||
public static final String ROLL_FY= "ROLL翻译";
|
||||
public static final String ROLL_FY = "ROLL翻译";
|
||||
|
||||
public static final String TIANXING_PYQ= "天行-朋友圈";
|
||||
public static final String TIANXING_PYQ = "天行-朋友圈";
|
||||
|
||||
public static final String TIANXING_WYY= "天行-网易云";
|
||||
public static final String TIANXING_WYY = "天行-网易云";
|
||||
|
||||
public static final String TIANXING_JDTC= "天行-经典台词";
|
||||
public static final String TIANXING_JDTC = "天行-经典台词";
|
||||
|
||||
public static final String TIANXING_MMMY= "天行-名人名言";
|
||||
public static final String TIANXING_MMMY = "天行-名人名言";
|
||||
|
||||
public static final String TIANXING_TRAN_DICT= "天行-翻译字典";
|
||||
public static final String TIANXING_TRAN_DICT = "天行-翻译字典";
|
||||
|
||||
public static final String TIANXING_ONE_ENGLISH= "天行-英语一言";
|
||||
public static final String TIANXING_ONE_ENGLISH = "天行-英语一言";
|
||||
|
||||
public static final String ROLL_A_WORD= "ROLL-每日一句";
|
||||
public static final String ROLL_A_WORD = "ROLL-每日一句";
|
||||
|
||||
public static final String TIANXING_A_WORD= "天行-每日一句";
|
||||
public static final String TIANXING_A_WORD = "天行-每日一句";
|
||||
|
||||
public static final String TIANXING_TOPSEARCHALLNETWORK= "天行-全网热搜榜";
|
||||
public static final String TIANXING_TOPSEARCHALLNETWORK = "天行-全网热搜榜";
|
||||
|
||||
public static final String TIANXING_TOPSEARCHWECHAT= "天行-微信热搜榜";
|
||||
public static final String TIANXING_TOPSEARCHWECHAT = "天行-微信热搜榜";
|
||||
|
||||
public static final String TIANXING_TOPSEARCHBAIDU= "天行-百度热搜榜";
|
||||
|
||||
public static final String TIANXING_TOPSEARCHWEIBO= "天行-微博热搜榜";
|
||||
|
||||
public static final String TIANXING_TOPSEARCHDOUYIN= "天行-抖音热搜榜";
|
||||
|
||||
public static final String ROLL_IP= "ROLL-IP信息";
|
||||
|
||||
public static final String GAODE_WEATHER= "高德-天气预报";
|
||||
public static final String TIANXING_TOPSEARCHBAIDU = "天行-百度热搜榜";
|
||||
|
||||
public static final String TIANXING_TOPSEARCHWEIBO = "天行-微博热搜榜";
|
||||
|
||||
public static final String TIANXING_TOPSEARCHDOUYIN = "天行-抖音热搜榜";
|
||||
|
||||
public static final String ROLL_IP = "ROLL-IP信息";
|
||||
|
||||
public static final String GAODE_WEATHER = "高德-天气预报";
|
||||
|
||||
|
||||
//-------------------url------------------------------
|
||||
|
||||
public static final String BAIDU_FY_URL= "http://api.fanyi.baidu.com/api/trans/vip/translate";
|
||||
public static final String BAIDU_FY_URL = "http://api.fanyi.baidu.com/api/trans/vip/translate";
|
||||
|
||||
public static final String YOUDAO_FY_URL= "http://fanyi.youdao.com/translate";
|
||||
public static final String YOUDAO_FY_URL = "http://fanyi.youdao.com/translate";
|
||||
|
||||
public static final String ROLL_FY_URL= "https://www.mxnzp.com/api/convert/translate";
|
||||
public static final String ROLL_FY_URL = "https://www.mxnzp.com/api/convert/translate";
|
||||
|
||||
public static final String TIANXING_PYQ_URL= "http://api.tianapi.com/pyqwenan/index";
|
||||
public static final String TIANXING_PYQ_URL = "http://api.tianapi.com/pyqwenan/index";
|
||||
|
||||
public static final String TIANXING_WYY_URL= "http://api.tianapi.com/hotreview/index";
|
||||
public static final String TIANXING_WYY_URL = "http://api.tianapi.com/hotreview/index";
|
||||
|
||||
public static final String TIANXING_JDTC_URL= "http://api.tianapi.com/dialogue/index";
|
||||
public static final String TIANXING_JDTC_URL = "http://api.tianapi.com/dialogue/index";
|
||||
|
||||
public static final String TIANXING_MMMY_URL= "http://api.tianapi.com/mingyan/index";
|
||||
public static final String TIANXING_MMMY_URL = "http://api.tianapi.com/mingyan/index";
|
||||
|
||||
public static final String TIANXING_TRAN_DICT_URL= "http://api.tianapi.com/enwords/index";
|
||||
public static final String TIANXING_TRAN_DICT_URL = "http://api.tianapi.com/enwords/index";
|
||||
|
||||
public static final String TIANXING_ONE_ENGLISH_URL= "http://api.tianapi.com/ensentence/index";
|
||||
public static final String TIANXING_ONE_ENGLISH_URL = "http://api.tianapi.com/ensentence/index";
|
||||
|
||||
public static final String ROLL_A_WORD_URL= "https://www.mxnzp.com/api/daily_word/recommend";
|
||||
public static final String ROLL_A_WORD_URL = "https://www.mxnzp.com/api/daily_word/recommend";
|
||||
|
||||
public static final String TIANXING_A_WORD_URL= "http://api.tianapi.com/everyday/index";
|
||||
public static final String TIANXING_A_WORD_URL = "http://api.tianapi.com/everyday/index";
|
||||
|
||||
public static final String TIANXING_TOPSEARCHALLNETWORK_URL= "http://api.tianapi.com/networkhot/index";
|
||||
public static final String TIANXING_TOPSEARCHALLNETWORK_URL = "http://api.tianapi.com/networkhot/index";
|
||||
|
||||
public static final String TIANXING_TOPSEARCHWECHAT_URL= "http://api.tianapi.com/wxhottopic/index";
|
||||
public static final String TIANXING_TOPSEARCHWECHAT_URL = "http://api.tianapi.com/wxhottopic/index";
|
||||
|
||||
public static final String TIANXING_TOPSEARCHBAIDU_URL= "http://api.tianapi.com/nethot/index";
|
||||
public static final String TIANXING_TOPSEARCHBAIDU_URL = "http://api.tianapi.com/nethot/index";
|
||||
|
||||
public static final String TIANXING_TOPSEARCHWEIBO_URL= "http://api.tianapi.com/weibohot/index";
|
||||
public static final String TIANXING_TOPSEARCHWEIBO_URL = "http://api.tianapi.com/weibohot/index";
|
||||
|
||||
public static final String TIANXING_TOPSEARCHDOUYIN_URL= "http://api.tianapi.com/douyinhot/index";
|
||||
public static final String TIANXING_TOPSEARCHDOUYIN_URL = "http://api.tianapi.com/douyinhot/index";
|
||||
|
||||
public static final String ROLL_IP_URL= "https://www.mxnzp.com/api/ip/aim_ip";
|
||||
public static final String ROLL_IP_URL = "https://www.mxnzp.com/api/ip/aim_ip";
|
||||
|
||||
/**
|
||||
* 接口文档:
|
||||
* https://lbs.amap.com/api/webservice/guide/api/weatherinfo
|
||||
*/
|
||||
public static final String GAODE_WEATHER_URL= "https://restapi.amap.com/v3/weather/weatherInfo";
|
||||
|
||||
|
||||
public static final String GAODE_WEATHER_URL = "https://restapi.amap.com/v3/weather/weatherInfo";
|
||||
|
||||
|
||||
//-----------------------api请求参数常量-----------------------------
|
||||
|
|
@ -100,28 +95,33 @@ public class ApiConst {
|
|||
/**
|
||||
* 高德Extensions请求参数条件
|
||||
*/
|
||||
public static final String GAODE_EXTENSIONS_ALL="all" ;
|
||||
public static final String GAODE_EXTENSIONS_BASE="base" ;
|
||||
public static final String GAODE_EXTENSIONS_ALL = "all";
|
||||
/**
|
||||
* 高德Extensions请求参数条件
|
||||
*/
|
||||
public static final String GAODE_EXTENSIONS_BASE = "base";
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//-----------------------api响应参数-----------------------------------
|
||||
//-----------------------api响应参数及名称-----------------------------------
|
||||
|
||||
|
||||
/**
|
||||
* roll平台返回值code成功参数
|
||||
*/
|
||||
public static final Integer ROLL_CODE_SUCCESS=1 ;
|
||||
|
||||
public static final Integer ROLL_CODE_SUCCESS = 1;
|
||||
|
||||
/**
|
||||
* 高德返回值infocode
|
||||
*/
|
||||
public static final String INFOCODE = "infocode";
|
||||
/**
|
||||
* 高德返回值infocode的值(成功值)
|
||||
*/
|
||||
public static final String INFOCODE_VALUE = "10000";
|
||||
/**
|
||||
* 高德返回值lives名称(需要的天气参数)
|
||||
*/
|
||||
public static final String LIVES= "lives";
|
||||
|
||||
|
||||
//---------------------自定义相关请求响应常量----------------------------
|
||||
|
|
@ -129,12 +129,7 @@ public class ApiConst {
|
|||
/**
|
||||
* 降级返回结果key
|
||||
*/
|
||||
public static final String DEMOTE_ERROR= "error";
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static final String DEMOTE_ERROR = "error";
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
package com.xjs.consts;
|
||||
|
||||
/**
|
||||
* redis key常量
|
||||
* @author xiejs
|
||||
* @desc redis key常量
|
||||
* @create 2021-12-30
|
||||
* @since 2021-12-30
|
||||
*/
|
||||
public class RedisConst {
|
||||
|
||||
|
|
@ -34,6 +34,11 @@ public class RedisConst {
|
|||
*/
|
||||
public static final String IP_INFO = "IPInfo";
|
||||
|
||||
/**
|
||||
* 天气常量信息key
|
||||
*/
|
||||
public static final String NOW_WEATHER = "nowWeather";
|
||||
|
||||
|
||||
//-------------------有效时间-----------------------
|
||||
public static final Integer TRAN_DICT_EXPIRE = 1; //小时
|
||||
|
|
@ -44,5 +49,7 @@ public class RedisConst {
|
|||
|
||||
public static final Long IP_INFO_EXPIRE = 30L; //分钟
|
||||
|
||||
public static final Long NOW_WHEATHER_EXPIRE = 10L; //分钟
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ import static com.xjs.consts.ApiConst.GAODE_WEATHER_URL;
|
|||
* @author xiejs
|
||||
* @since 2022-01-16
|
||||
*/
|
||||
@FeignClient(name = "gaodeWeather", url = GAODE_WEATHER, fallbackFactory = GaodeWeatherFeignFactory.class)
|
||||
@FeignClient(name = "gaodeWeather", url = GAODE_WEATHER_URL, fallbackFactory = GaodeWeatherFeignFactory.class)
|
||||
public interface GaodeWeatherFeignClient {
|
||||
|
||||
@GetMapping()
|
||||
|
|
|
|||
|
|
@ -91,13 +91,10 @@ public class CopyWritingController extends BaseController {
|
|||
factories.add(tianXingWYYCopyWritingFactory);
|
||||
factories.add(tianXingJDTCCopyWritingFactory);
|
||||
factories.add(tianXingMMMYCopyWritingFactory);
|
||||
factories.add(rollMMYJCopyWritingFactory);
|
||||
factories.add(rollMMYJCopyWritingFactory);
|
||||
factories.add(rollMMYJCopyWritingFactory);
|
||||
factories.add(rollMMYJCopyWritingFactory);
|
||||
factories.add(rollMMYJCopyWritingFactory);
|
||||
factories.add(rollMMYJCopyWritingFactory);
|
||||
factories.add(rollMMYJCopyWritingFactory);
|
||||
factories.add(tianXingPYQCopyWritingFactory);
|
||||
factories.add(tianXingWYYCopyWritingFactory);
|
||||
factories.add(tianXingJDTCCopyWritingFactory);
|
||||
factories.add(tianXingMMMYCopyWritingFactory);
|
||||
factories.add(rollMMYJCopyWritingFactory);
|
||||
//--------add----------------------------;-
|
||||
//随机调用集合中的接口
|
||||
|
|
|
|||
|
|
@ -0,0 +1,37 @@
|
|||
package com.xjs.weather.controller;
|
||||
|
||||
import com.ruoyi.common.core.domain.R;
|
||||
import com.ruoyi.common.log.annotation.Log;
|
||||
import com.ruoyi.common.security.annotation.RequiresLogin;
|
||||
import com.xjs.weather.domain.NowWeather;
|
||||
import com.xjs.weather.service.WeatherService;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* 天气控制器
|
||||
* @author xiejs
|
||||
* @since 2022-01-16
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("weather")
|
||||
@Api(tags = "业务模块-天气管理")
|
||||
@Log4j2
|
||||
public class WeatherController {
|
||||
|
||||
@Autowired
|
||||
private WeatherService weatherService;
|
||||
|
||||
@GetMapping
|
||||
@ApiOperation("获取天气信息")
|
||||
@Log(title = "获取天气")
|
||||
@RequiresLogin
|
||||
public R<NowWeather> getWeatherApiData() {
|
||||
return R.ok(weatherService.saveNowWeather());
|
||||
}
|
||||
}
|
||||
|
|
@ -82,7 +82,7 @@ public class NowWeather implements Serializable {
|
|||
|
||||
@Excel(name = "创建时间" ,dateFormat = "yyyy-MM-dd HH:mm:ss")
|
||||
@TableField(fill = FieldFill.INSERT)
|
||||
private Date create_time;
|
||||
private Date createTime;
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -12,10 +12,11 @@ import lombok.extern.log4j.Log4j2;
|
|||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import static com.xjs.consts.ApiConst.GAODE_EXTENSIONS_BASE;
|
||||
import static com.xjs.consts.ApiConst.*;
|
||||
|
||||
/**
|
||||
* 高德实时天气工厂实现
|
||||
*
|
||||
* @author xiejs
|
||||
* @since 2022-01-16
|
||||
*/
|
||||
|
|
@ -40,9 +41,11 @@ public class GaodeNowWeatherFactory implements WeatherFactory<NowWeather> {
|
|||
.setCity(ipApiData.getCityId())
|
||||
.setExtensions(GAODE_EXTENSIONS_BASE);
|
||||
JSONObject jsonObject = gaodeWeatherFeignClient.WeatherApi(requestBody);
|
||||
|
||||
|
||||
|
||||
if (!jsonObject.containsKey(DEMOTE_ERROR)) {
|
||||
if (INFOCODE_VALUE.equals(jsonObject.getString(INFOCODE))) {
|
||||
return jsonObject.getJSONArray(LIVES).getJSONObject(0).toJavaObject(NowWeather.class);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,12 @@
|
|||
package com.xjs.weather.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.xjs.weather.domain.NowWeather;
|
||||
|
||||
/**
|
||||
* nowWeather mapper
|
||||
* @author xiejs
|
||||
* @since 2022-01-16
|
||||
*/
|
||||
public interface NowWeatherMapper extends BaseMapper<NowWeather> {
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
package com.xjs.weather.service;
|
||||
|
||||
import com.xjs.weather.domain.NowWeather;
|
||||
|
||||
/**
|
||||
* 天气服务
|
||||
* @author xiejs
|
||||
* @since 2022-01-16
|
||||
*/
|
||||
public interface WeatherService {
|
||||
|
||||
/**
|
||||
* 保存实时天气并放入缓存
|
||||
* @return NowWeather
|
||||
*/
|
||||
NowWeather saveNowWeather();
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,55 @@
|
|||
package com.xjs.weather.service.impl;
|
||||
|
||||
import com.ruoyi.common.redis.service.RedisService;
|
||||
import com.xjs.exception.BusinessException;
|
||||
import com.xjs.weather.domain.NowWeather;
|
||||
import com.xjs.weather.factory.WeatherFactory;
|
||||
import com.xjs.weather.mapper.NowWeatherMapper;
|
||||
import com.xjs.weather.service.WeatherService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static com.xjs.consts.RedisConst.NOW_WEATHER;
|
||||
import static com.xjs.consts.RedisConst.NOW_WHEATHER_EXPIRE;
|
||||
|
||||
/**
|
||||
* 天气服务实现
|
||||
*
|
||||
* @author xiejs
|
||||
* @since 2022-01-16
|
||||
*/
|
||||
@Service
|
||||
public class WeatherServiceImpl implements WeatherService {
|
||||
|
||||
@Autowired
|
||||
private WeatherFactory<NowWeather> gaodeNowWeatherFactory;
|
||||
@Resource
|
||||
private NowWeatherMapper nowWeatherMapper;
|
||||
@Autowired
|
||||
private RedisService redisService;
|
||||
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public NowWeather saveNowWeather() {
|
||||
if (!redisService.hasKey(NOW_WEATHER)) {
|
||||
NowWeather nowWeather = gaodeNowWeatherFactory.weatherApi();
|
||||
if (Objects.nonNull(nowWeather)) {
|
||||
nowWeatherMapper.insert(nowWeather);
|
||||
redisService.setCacheObject(NOW_WEATHER, nowWeather, NOW_WHEATHER_EXPIRE, TimeUnit.MINUTES);
|
||||
return nowWeather;
|
||||
} else {
|
||||
throw new BusinessException("获取天气数据为空");
|
||||
}
|
||||
} else {
|
||||
return (NowWeather) redisService.getCacheObject(NOW_WEATHER);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue