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

pull/254/head
xjs 4 years ago
parent dffb2aa293
commit a5cd1d7fe1

@ -1,13 +1,14 @@
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 = "有道翻译";
@ -46,10 +47,6 @@ public class ApiConst {
public static final String GAODE_WEATHER = "高德-天气预报";
//-------------------url------------------------------
public static final String BAIDU_FY_URL = "http://api.fanyi.baidu.com/api/trans/vip/translate";
@ -93,27 +90,19 @@ public class ApiConst {
public static final String GAODE_WEATHER_URL = "https://restapi.amap.com/v3/weather/weatherInfo";
//-----------------------api请求参数常量-----------------------------
/**
* Extensions
*/
public static final String GAODE_EXTENSIONS_ALL = "all";
/**
* Extensions
*/
public static final String GAODE_EXTENSIONS_BASE = "base";
//-----------------------api响应参数-----------------------------------
//-----------------------api响应参数及名称-----------------------------------
/**
@ -121,7 +110,18 @@ public class ApiConst {
*/
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";
//---------------------自定义相关请求响应常量----------------------------
@ -132,9 +132,4 @@ public class ApiConst {
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…
Cancel
Save