|
|
@ -5,17 +5,19 @@ import com.alibaba.fastjson.JSONObject; |
|
|
|
import com.ruoyi.common.core.controller.BaseController; |
|
|
|
import com.ruoyi.common.core.domain.AjaxResult; |
|
|
|
import com.ruoyi.common.core.redis.RedisCache; |
|
|
|
import com.ruoyi.system.service.ISysConfigService; |
|
|
|
import com.zc.business.domain.DcRegion; |
|
|
|
import com.zc.business.service.impl.DcRegionServiceImpl; |
|
|
|
import com.zc.common.core.httpclient.OkHttp; |
|
|
|
import com.zc.common.core.httpclient.exception.HttpException; |
|
|
|
import io.swagger.annotations.Api; |
|
|
|
import io.swagger.annotations.ApiOperation; |
|
|
|
import okhttp3.Response; |
|
|
|
import org.springframework.web.bind.annotation.PostMapping; |
|
|
|
import org.springframework.web.bind.annotation.RequestMapping; |
|
|
|
import org.springframework.web.bind.annotation.RestController; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.web.bind.annotation.*; |
|
|
|
|
|
|
|
import javax.annotation.Resource; |
|
|
|
import java.io.IOException; |
|
|
|
import java.util.*; |
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
|
|
|
@ -30,29 +32,33 @@ import java.util.concurrent.TimeUnit; |
|
|
|
@RequestMapping("/weatherForecast") |
|
|
|
public class WeatherForecastController extends BaseController { |
|
|
|
|
|
|
|
private static final String WEATHERFACTSURI = "https://devapi.qweather.com/v7/weather/now?location="; |
|
|
|
private static final String WEATHERFACTS = "weatherFacts"; |
|
|
|
private static final String WEATHERFACTSKEY = "f72f6415b3f04515be1cceb868b33a7e"; //谢朴峰
|
|
|
|
private static final String METEOROLOGICALEARLYWARNINGURI = "https://devapi.qweather.com/v7/warning/now?location="; |
|
|
|
private static final String METEOROLOGICALEARLYWARNING = "meteorologicalEarlyWarning"; |
|
|
|
private static final String METEOROLOGICALEARLYWARNINGKEY = "8b5e521388154799a86e73444db76b1d"; // 高广超
|
|
|
|
private static final String HOURLYWEATHERURI = "https://devapi.qweather.com/v7/weather/24h?location="; |
|
|
|
private static final String HOURLYWEATHER = "hourlyWeather"; |
|
|
|
private static final String HOURLYWEATHERKEY = "cfd87e0502684ac3a681eb19a712aec6"; // 孟凡峰
|
|
|
|
private static final String KEY = "&key="; |
|
|
|
//private static final String WEATHERFACTSURI = "https://devapi.qweather.com/v7/weather/now?location=";
|
|
|
|
//private static final String WEATHERFACTS = "weatherFacts";
|
|
|
|
//private static final String WEATHERFACTSKEY = "f72f6415b3f04515be1cceb868b33a7e"; //谢朴峰
|
|
|
|
//private static final String METEOROLOGICALEARLYWARNINGURI = "https://devapi.qweather.com/v7/warning/now?location=";
|
|
|
|
//private static final String METEOROLOGICALEARLYWARNING = "meteorologicalEarlyWarning";
|
|
|
|
//private static final String METEOROLOGICALEARLYWARNINGKEY = "8b5e521388154799a86e73444db76b1d"; // 高广超
|
|
|
|
//private static final String HOURLYWEATHERURI = "https://devapi.qweather.com/v7/weather/24h?location=";
|
|
|
|
//private static final String HOURLYWEATHER = "hourlyWeather";
|
|
|
|
//private static final String HOURLYWEATHERKEY = "cfd87e0502684ac3a681eb19a712aec6"; // 孟凡峰
|
|
|
|
//private static final String KEY = "&key=";
|
|
|
|
@Resource |
|
|
|
private RedisCache redisCache; |
|
|
|
|
|
|
|
@Resource |
|
|
|
private DcRegionServiceImpl dcRegionService; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private ISysConfigService configService; |
|
|
|
|
|
|
|
/* |
|
|
|
* 天气实况查询 |
|
|
|
* */ |
|
|
|
@ApiOperation("天气实况查询") |
|
|
|
@PostMapping(value = "/weatherFacts") |
|
|
|
public AjaxResult weatherFacts() { |
|
|
|
return getAjaxResult(WEATHERFACTSURI, WEATHERFACTSKEY, WEATHERFACTS); |
|
|
|
JSONObject weather = JSONObject.parseObject(configService.selectConfigByKey("weather")); |
|
|
|
return getAjaxResult(weather.getString("WEATHERFACTSURI"), weather.getString("WEATHERFACTSKEY"), weather.getString("WEATHERFACTS")); |
|
|
|
} |
|
|
|
|
|
|
|
/* |
|
|
@ -61,7 +67,8 @@ public class WeatherForecastController extends BaseController { |
|
|
|
@ApiOperation("气象预警查询") |
|
|
|
@PostMapping(value = "/meteorologicalEarlyWarning") |
|
|
|
public AjaxResult meteorologicalEarlyWarning() { |
|
|
|
return getAjaxResult(METEOROLOGICALEARLYWARNINGURI, METEOROLOGICALEARLYWARNINGKEY, METEOROLOGICALEARLYWARNING); |
|
|
|
JSONObject weather = JSONObject.parseObject(configService.selectConfigByKey("weather")); |
|
|
|
return getAjaxResult(weather.getString("METEOROLOGICALEARLYWARNINGURI"), weather.getString("METEOROLOGICALEARLYWARNINGKEY"), weather.getString("METEOROLOGICALEARLYWARNING")); |
|
|
|
} |
|
|
|
|
|
|
|
/* |
|
|
@ -70,8 +77,9 @@ public class WeatherForecastController extends BaseController { |
|
|
|
@ApiOperation("气象预警数量查询") |
|
|
|
@PostMapping(value = "/queryTheNumberOfMeteorologicalWarning") |
|
|
|
public AjaxResult queryTheNumberOfMeteorologicalWarning() { |
|
|
|
JSONObject weather = JSONObject.parseObject(configService.selectConfigByKey("weather")); |
|
|
|
JSONObject jsonObject = new JSONObject(); |
|
|
|
JSONObject cacheObject = redisCache.getCacheObject(METEOROLOGICALEARLYWARNING); |
|
|
|
JSONObject cacheObject = redisCache.getCacheObject(weather.getString("METEOROLOGICALEARLYWARNING")); |
|
|
|
|
|
|
|
if (cacheObject != null) { |
|
|
|
jsonObject = cacheObject; |
|
|
@ -88,7 +96,7 @@ public class WeatherForecastController extends BaseController { |
|
|
|
Response response // 请求响应
|
|
|
|
= okHttp |
|
|
|
.headers(new HashMap<>()) |
|
|
|
.url(METEOROLOGICALEARLYWARNINGURI + dcRegion.getLongitude() + "," + dcRegion.getLatitude() + KEY + METEOROLOGICALEARLYWARNINGKEY) // 请求地址
|
|
|
|
.url(weather.getString("METEOROLOGICALEARLYWARNINGURI") + dcRegion.getLongitude() + "," + dcRegion.getLatitude() + weather.getString("KEY") + weather.getString("METEOROLOGICALEARLYWARNINGKEY")) // 请求地址
|
|
|
|
.get(); // 请求方法
|
|
|
|
|
|
|
|
if (response.body() != null) { |
|
|
@ -96,7 +104,7 @@ public class WeatherForecastController extends BaseController { |
|
|
|
JSONObject jsonResult = JSONObject.parseObject(response.body().string()); |
|
|
|
if (jsonResult.getInteger("code") == 200) { |
|
|
|
|
|
|
|
jsonObject.put(METEOROLOGICALEARLYWARNING + dcRegion.getId(), extracted(jsonResult, "warning")); |
|
|
|
jsonObject.put(weather.getString("METEOROLOGICALEARLYWARNING") + dcRegion.getId(), extracted(jsonResult, "warning")); |
|
|
|
|
|
|
|
} else { |
|
|
|
return AjaxResult.error(jsonResult.getInteger("code"), "请求失败"); |
|
|
@ -107,7 +115,7 @@ public class WeatherForecastController extends BaseController { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
redisCache.setCacheObject(METEOROLOGICALEARLYWARNING, jsonObject, 13, TimeUnit.MINUTES); |
|
|
|
redisCache.setCacheObject(weather.getString("METEOROLOGICALEARLYWARNING"), jsonObject, 13, TimeUnit.MINUTES); |
|
|
|
JSONArray meteorologicalEarlyWarning1 = jsonObject.getJSONArray("meteorologicalEarlyWarning1"); |
|
|
|
JSONArray meteorologicalEarlyWarning2 = jsonObject.getJSONArray("meteorologicalEarlyWarning2"); |
|
|
|
JSONArray meteorologicalEarlyWarning3 = jsonObject.getJSONArray("meteorologicalEarlyWarning3"); |
|
|
@ -133,10 +141,12 @@ public class WeatherForecastController extends BaseController { |
|
|
|
@ApiOperation("逐小时天气查询") |
|
|
|
@PostMapping(value = "/hourlyWeather") |
|
|
|
public AjaxResult hourlyWeather() { |
|
|
|
return getAjaxResult(HOURLYWEATHERURI, HOURLYWEATHERKEY, HOURLYWEATHER); |
|
|
|
JSONObject weather = JSONObject.parseObject(configService.selectConfigByKey("weather")); |
|
|
|
return getAjaxResult(weather.getString("HOURLYWEATHERURI"), weather.getString("HOURLYWEATHERKEY"), weather.getString("HOURLYWEATHER")); |
|
|
|
} |
|
|
|
|
|
|
|
private AjaxResult getAjaxResult(String uri, String accessKey, String redisKey) { |
|
|
|
JSONObject weather = JSONObject.parseObject(configService.selectConfigByKey("weather")); |
|
|
|
|
|
|
|
JSONObject cacheObject = redisCache.getCacheObject(redisKey); |
|
|
|
|
|
|
@ -159,7 +169,7 @@ public class WeatherForecastController extends BaseController { |
|
|
|
Response response // 请求响应
|
|
|
|
= okHttp |
|
|
|
.headers(new HashMap<>()) |
|
|
|
.url(uri + dcRegion.getLongitude() + "," + dcRegion.getLatitude() + KEY + accessKey) // 请求地址
|
|
|
|
.url(uri + dcRegion.getLongitude() + "," + dcRegion.getLatitude() + weather.getString("KEY") + accessKey) // 请求地址
|
|
|
|
.get(); // 请求方法
|
|
|
|
|
|
|
|
if (response.body() != null) { |
|
|
@ -167,15 +177,15 @@ public class WeatherForecastController extends BaseController { |
|
|
|
JSONObject jsonResult = JSONObject.parseObject(response.body().string()); |
|
|
|
if (jsonResult.getInteger("code") == 200) { |
|
|
|
|
|
|
|
if (Objects.equals(redisKey, WEATHERFACTS)) { |
|
|
|
if (Objects.equals(redisKey, weather.getString("WEATHERFACTS"))) { |
|
|
|
|
|
|
|
jsonObject.put(redisKey + dcRegion.getId(), extracted(jsonResult, "now")); |
|
|
|
|
|
|
|
} else if (Objects.equals(redisKey, METEOROLOGICALEARLYWARNING)) { |
|
|
|
} else if (Objects.equals(redisKey, weather.getString("METEOROLOGICALEARLYWARNING"))) { |
|
|
|
|
|
|
|
jsonObject.put(redisKey + dcRegion.getId(), extracted(jsonResult, "warning")); |
|
|
|
|
|
|
|
} else if (Objects.equals(redisKey, HOURLYWEATHER)) { |
|
|
|
} else if (Objects.equals(redisKey, weather.getString("HOURLYWEATHER"))) { |
|
|
|
|
|
|
|
jsonObject.put(redisKey + dcRegion.getId(), extracted(jsonResult, "hourly")); |
|
|
|
} |
|
|
@ -191,15 +201,15 @@ public class WeatherForecastController extends BaseController { |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (Objects.equals(redisKey, WEATHERFACTS)) { |
|
|
|
if (Objects.equals(redisKey, weather.getString("WEATHERFACTS"))) { |
|
|
|
|
|
|
|
redisCache.setCacheObject(redisKey, jsonObject, 13, TimeUnit.MINUTES); |
|
|
|
|
|
|
|
} else if (Objects.equals(redisKey, METEOROLOGICALEARLYWARNING)) { |
|
|
|
} else if (Objects.equals(redisKey, weather.getString("METEOROLOGICALEARLYWARNING"))) { |
|
|
|
|
|
|
|
redisCache.setCacheObject(redisKey, jsonObject, 13, TimeUnit.MINUTES); |
|
|
|
|
|
|
|
} else if (Objects.equals(redisKey, HOURLYWEATHER)) { |
|
|
|
} else if (Objects.equals(redisKey, weather.getString("HOURLYWEATHER"))) { |
|
|
|
|
|
|
|
redisCache.setCacheObject(redisKey, jsonObject, 13, TimeUnit.MINUTES); |
|
|
|
} |
|
|
@ -236,4 +246,35 @@ public class WeatherForecastController extends BaseController { |
|
|
|
} |
|
|
|
return jsonObject; |
|
|
|
} |
|
|
|
|
|
|
|
/* |
|
|
|
* 逐小时天气查询 |
|
|
|
* */ |
|
|
|
@ApiOperation("查询指定经纬度天气") |
|
|
|
@GetMapping(value = "/queryTheSpecifiedLatitudeAndLongitudeWeather/{longitude}/{latitude}") |
|
|
|
public AjaxResult queryTheSpecifiedLatitudeAndLongitudeWeather(@PathVariable String longitude, @PathVariable String latitude) throws HttpException, IOException { |
|
|
|
|
|
|
|
JSONObject weather = JSONObject.parseObject(configService.selectConfigByKey("weather")); |
|
|
|
JSONObject jsonObject = new JSONObject(); |
|
|
|
|
|
|
|
OkHttp okHttp = new OkHttp(); |
|
|
|
|
|
|
|
Response response // 请求响应
|
|
|
|
= okHttp |
|
|
|
.headers(new HashMap<>()) |
|
|
|
.url(weather.getString("WEATHERFACTSURI") + longitude + "," + latitude + weather.getString("KEY") + weather.getString("WEATHERFACTSKEY")) // 请求地址
|
|
|
|
.get(); // 请求方法
|
|
|
|
|
|
|
|
if (response.body() != null) { |
|
|
|
|
|
|
|
JSONObject jsonResult = JSONObject.parseObject(response.body().string()); |
|
|
|
if (jsonResult.getInteger("code") == 200) { |
|
|
|
jsonObject.put("weatherInTheAreaWhereTheIncidentOccurred",jsonResult); |
|
|
|
} else { |
|
|
|
return AjaxResult.error(jsonResult.getInteger("code"), "请求失败"); |
|
|
|
} |
|
|
|
} |
|
|
|
return AjaxResult.success(jsonObject); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|