1、实现roll实时天气查询
This commit is contained in:
parent
8fddea4427
commit
0ee72c1206
|
|
@ -52,6 +52,8 @@ public class ApiConst {
|
|||
|
||||
public static final String ROLL_MOBILE_BELONG = "ROLL-手机归属地";
|
||||
|
||||
public static final String ROLL_WEATHER = "ROLL-天气预报";
|
||||
|
||||
|
||||
//-------------------url------------------------------
|
||||
|
||||
|
|
@ -106,6 +108,14 @@ public class ApiConst {
|
|||
*/
|
||||
public static final String ROLL_MOBILE_BELONG_URL = "https://www.mxnzp.com/api/mobile_location/aim_mobile";
|
||||
|
||||
/**
|
||||
* 接口文档:https://www.mxnzp.com/doc/detail?id=7
|
||||
*/
|
||||
public static final String ROLL_WEATHER_URL = "https://www.mxnzp.com/api/weather";
|
||||
|
||||
|
||||
|
||||
|
||||
//-----------------------api请求参数常量-----------------------------
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -5,15 +5,13 @@ import com.ruoyi.common.log.annotation.Log;
|
|||
import com.ruoyi.common.security.annotation.RequiresPermissions;
|
||||
import com.xjs.apitools.domain.ApiHoliday;
|
||||
import com.xjs.apitools.domain.ApiMobileBelong;
|
||||
import com.xjs.apitools.domain.ApiNowWeather;
|
||||
import com.xjs.apitools.service.ApiToolsService;
|
||||
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.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
|
|
@ -58,5 +56,15 @@ public class ApiToolsController {
|
|||
return R.ok(apiToolsService.getApiMobileBelong(mobile));
|
||||
}
|
||||
|
||||
@GetMapping("nowweather/{city}")
|
||||
@ApiOperation("获取实时天气信息")
|
||||
@Log(title = "获取实时天气")
|
||||
public R<ApiNowWeather> getNowWeatherApiData(@PathVariable("city") String city) {
|
||||
return R.ok(apiToolsService.getNowWeather(city));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,25 @@
|
|||
package com.xjs.apitools.domain;
|
||||
|
||||
import com.xjs.weather.domain.NowWeather;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* api天气实体
|
||||
*
|
||||
* @author xiejs
|
||||
* @since 2022-01-18
|
||||
*/
|
||||
@Data
|
||||
public class ApiNowWeather extends NowWeather {
|
||||
|
||||
/**
|
||||
* 城市具体信息,比如 “广东省 深圳市”
|
||||
*/
|
||||
private String address;
|
||||
|
||||
/**
|
||||
* 温度
|
||||
*/
|
||||
private String temp;
|
||||
|
||||
}
|
||||
|
|
@ -25,4 +25,9 @@ public class RequestBody {
|
|||
* 目标手机
|
||||
*/
|
||||
private String mobile;
|
||||
|
||||
/**
|
||||
* 目标城市
|
||||
*/
|
||||
private String city;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,42 @@
|
|||
package com.xjs.apitools.factory.impl;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.xjs.apitools.domain.ApiNowWeather;
|
||||
import com.xjs.apitools.domain.RequestBody;
|
||||
import com.xjs.apitools.factory.ApiToolsFactory;
|
||||
import com.xjs.common.client.api.roll.RollWeatherFeignClient;
|
||||
import com.xjs.config.RollProperties;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import static com.xjs.consts.ApiConst.DEMOTE_ERROR;
|
||||
import static com.xjs.consts.ApiConst.ROLL_CODE_SUCCESS;
|
||||
|
||||
/**
|
||||
* roll天气预报api工厂实现
|
||||
* @author xiejs
|
||||
* @since 2022-01-18
|
||||
*/
|
||||
@Component
|
||||
@Log4j2
|
||||
public class RollNowWeatherFactory implements ApiToolsFactory<ApiNowWeather, RequestBody> {
|
||||
|
||||
@Autowired
|
||||
private RollProperties rollProperties;
|
||||
@Autowired
|
||||
private RollWeatherFeignClient rollWeatherFeignClient;
|
||||
|
||||
|
||||
@Override
|
||||
public ApiNowWeather apiData(RequestBody requestBody) {
|
||||
requestBody.setApp_id(rollProperties.getApp_id());
|
||||
requestBody.setApp_secret(rollProperties.getApp_secret());
|
||||
JSONObject jsonObject = rollWeatherFeignClient.nowWeatherApi(requestBody, requestBody.getCity());
|
||||
if (!jsonObject.containsKey(DEMOTE_ERROR) && jsonObject.getInteger("code") == ROLL_CODE_SUCCESS.intValue()) {
|
||||
JSONObject jsonData = jsonObject.getJSONObject("data");
|
||||
return jsonData.toJavaObject(ApiNowWeather.class);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
@ -2,6 +2,7 @@ package com.xjs.apitools.service;
|
|||
|
||||
import com.xjs.apitools.domain.ApiHoliday;
|
||||
import com.xjs.apitools.domain.ApiMobileBelong;
|
||||
import com.xjs.apitools.domain.ApiNowWeather;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
|
@ -26,4 +27,12 @@ public interface ApiToolsService {
|
|||
*/
|
||||
ApiMobileBelong getApiMobileBelong(String mobile);
|
||||
|
||||
|
||||
/**
|
||||
* 获取实时天气
|
||||
* @param city 目标城市
|
||||
* @return ApiWeather
|
||||
*/
|
||||
ApiNowWeather getNowWeather(String city);
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,10 +2,12 @@ package com.xjs.apitools.service.impl;
|
|||
|
||||
import com.xjs.apitools.domain.ApiHoliday;
|
||||
import com.xjs.apitools.domain.ApiMobileBelong;
|
||||
import com.xjs.apitools.domain.ApiNowWeather;
|
||||
import com.xjs.apitools.domain.RequestBody;
|
||||
import com.xjs.apitools.factory.ApiToolsFactory;
|
||||
import com.xjs.apitools.factory.impl.RollHolidayFactory;
|
||||
import com.xjs.apitools.factory.impl.RollMobileBelongFactory;
|
||||
import com.xjs.apitools.factory.impl.RollNowWeatherFactory;
|
||||
import com.xjs.apitools.service.ApiToolsService;
|
||||
import com.xjs.exception.ApiException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
|
@ -27,6 +29,7 @@ public class ApiToolsServiceImpl implements ApiToolsService {
|
|||
|
||||
private ApiToolsFactory<ApiHoliday, Object> holidayFactory;
|
||||
private ApiToolsFactory<ApiMobileBelong, RequestBody> mobileBelongFactory;
|
||||
private ApiToolsFactory<ApiNowWeather, RequestBody> nowWeatherFactory;
|
||||
|
||||
@Autowired
|
||||
public void setHolidayFactory(RollHolidayFactory rollHolidayFactory) {
|
||||
|
|
@ -38,6 +41,12 @@ public class ApiToolsServiceImpl implements ApiToolsService {
|
|||
this.mobileBelongFactory = rollMobileBelongFactory;
|
||||
}
|
||||
|
||||
@Autowired
|
||||
public void setNowWeatherFactory(RollNowWeatherFactory rollNowWeatherFactory) {
|
||||
this.nowWeatherFactory = rollNowWeatherFactory;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public List<ApiHoliday> getApiHolidayList() {
|
||||
List<ApiHoliday> apiHolidayList = holidayFactory.apiDataList();
|
||||
|
|
@ -60,4 +69,12 @@ public class ApiToolsServiceImpl implements ApiToolsService {
|
|||
return Optional.ofNullable(mobileBelongFactory.apiData(requestBody))
|
||||
.orElseThrow(ApiException::new);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ApiNowWeather getNowWeather(String city) {
|
||||
RequestBody requestBody = new RequestBody();
|
||||
requestBody.setCity(city);
|
||||
return Optional.ofNullable(nowWeatherFactory.apiData(requestBody))
|
||||
.orElseThrow(ApiException::new);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,36 @@
|
|||
package com.xjs.common.client.api.roll;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.xjs.annotation.ApiLog;
|
||||
import com.xjs.apitools.domain.RequestBody;
|
||||
import com.xjs.common.client.factory.RollWeatherFeignFactory;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.cloud.openfeign.SpringQueryMap;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
|
||||
import static com.xjs.consts.ApiConst.ROLL_WEATHER;
|
||||
import static com.xjs.consts.ApiConst.ROLL_WEATHER_URL;
|
||||
|
||||
/**
|
||||
* roll 天气预报api调用feign
|
||||
* @author xiejs
|
||||
* @since 2022-01-18
|
||||
*/
|
||||
@FeignClient(name = "rollWeather", url = ROLL_WEATHER_URL, fallbackFactory = RollWeatherFeignFactory.class)
|
||||
public interface RollWeatherFeignClient {
|
||||
|
||||
@GetMapping("/current/{city}")
|
||||
@ApiLog(name = ROLL_WEATHER,
|
||||
url = ROLL_WEATHER_URL+"/current",
|
||||
method = "Get")
|
||||
JSONObject nowWeatherApi(@SpringQueryMap RequestBody requestBody, @PathVariable("city")String city);
|
||||
|
||||
|
||||
@GetMapping("/forecast/{city}")
|
||||
@ApiLog(name = ROLL_WEATHER,
|
||||
url = ROLL_WEATHER_URL+"/forecast",
|
||||
method = "Get")
|
||||
JSONObject forecastWeatherApi(@SpringQueryMap RequestBody requestBody,@PathVariable("city")String city);
|
||||
|
||||
}
|
||||
|
|
@ -10,6 +10,7 @@ import org.springframework.stereotype.Component;
|
|||
import static com.xjs.consts.ApiConst.DEMOTE_ERROR;
|
||||
|
||||
/**
|
||||
* roll ip信息api降级处理
|
||||
* @author xiejs
|
||||
* @since 2022-01-15
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -0,0 +1,41 @@
|
|||
package com.xjs.common.client.factory;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.ruoyi.common.core.domain.R;
|
||||
import com.xjs.apitools.domain.RequestBody;
|
||||
import com.xjs.common.client.api.roll.RollWeatherFeignClient;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.cloud.openfeign.FallbackFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import static com.xjs.consts.ApiConst.DEMOTE_ERROR;
|
||||
|
||||
/**
|
||||
* roll 天气服务api降级处理
|
||||
* @author xiejs
|
||||
* @since 2022-01-18
|
||||
*/
|
||||
@Component
|
||||
@Log4j2
|
||||
public class RollWeatherFeignFactory implements FallbackFactory<RollWeatherFeignClient> {
|
||||
@Override
|
||||
public RollWeatherFeignClient create(Throwable cause) {
|
||||
return new RollWeatherFeignClient() {
|
||||
@Override
|
||||
public JSONObject nowWeatherApi(RequestBody requestBody,String city) {
|
||||
log.error("api模块roll 实时天气服务调用失败:{},执行降级处理", cause.getMessage());
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
jsonObject.put(DEMOTE_ERROR, R.FAIL);
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject forecastWeatherApi(RequestBody requestBody, String city) {
|
||||
log.error("api模块roll 预报天气服务调用失败:{},执行降级处理", cause.getMessage());
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
jsonObject.put(DEMOTE_ERROR, R.FAIL);
|
||||
return jsonObject;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -62,8 +62,6 @@ public class WeatherController {
|
|||
|
||||
/**
|
||||
* week类型转换
|
||||
*
|
||||
* @return ForecastWeather
|
||||
*/
|
||||
private void weekConvert(ForecastWeather forecastWeather) {
|
||||
forecastWeather.getCasts().forEach(cast -> {
|
||||
|
|
|
|||
Loading…
Reference in New Issue