|
|
@ -0,0 +1,169 @@ |
|
|
|
package com.zc.business.controller; |
|
|
|
|
|
|
|
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.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 javax.annotation.Resource; |
|
|
|
import java.io.IOException; |
|
|
|
import java.util.*; |
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* License |
|
|
|
* |
|
|
|
* @author Athena-zhaoxianglong |
|
|
|
*/ |
|
|
|
@Api(tags = "天气预报接口") |
|
|
|
@RestController |
|
|
|
@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="; |
|
|
|
@Resource |
|
|
|
private RedisCache redisCache; |
|
|
|
|
|
|
|
@Resource |
|
|
|
private DcRegionServiceImpl dcRegionService; |
|
|
|
|
|
|
|
/* |
|
|
|
* 天气实况查询 |
|
|
|
* */ |
|
|
|
@ApiOperation("天气实况查询") |
|
|
|
@PostMapping(value = "/weatherFacts") |
|
|
|
public AjaxResult weatherFacts() throws HttpException, IOException { |
|
|
|
return getAjaxResult(WEATHERFACTSURI, WEATHERFACTSKEY, WEATHERFACTS); |
|
|
|
} |
|
|
|
|
|
|
|
/* |
|
|
|
* 气象预警查询 |
|
|
|
* */ |
|
|
|
@ApiOperation("气象预警查询") |
|
|
|
@PostMapping(value = "/meteorologicalEarlyWarning") |
|
|
|
public AjaxResult meteorologicalEarlyWarning() throws HttpException, IOException { |
|
|
|
return getAjaxResult(METEOROLOGICALEARLYWARNINGURI, METEOROLOGICALEARLYWARNINGKEY, METEOROLOGICALEARLYWARNING); |
|
|
|
} |
|
|
|
|
|
|
|
/* |
|
|
|
* 气象预警查询 |
|
|
|
* */ |
|
|
|
@ApiOperation("逐小时天气查询") |
|
|
|
@PostMapping(value = "/hourlyWeather") |
|
|
|
public AjaxResult hourlyWeather() throws HttpException, IOException { |
|
|
|
return getAjaxResult(HOURLYWEATHERURI, HOURLYWEATHERKEY, HOURLYWEATHER); |
|
|
|
} |
|
|
|
|
|
|
|
private AjaxResult getAjaxResult(String uri, String accessKey, String redisKey) throws HttpException, IOException { |
|
|
|
|
|
|
|
JSONObject cacheObject = redisCache.getCacheObject(redisKey); |
|
|
|
|
|
|
|
if (cacheObject != null) { |
|
|
|
|
|
|
|
return AjaxResult.success(cacheObject); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
List<DcRegion> list = dcRegionService.list(); |
|
|
|
|
|
|
|
JSONObject jsonObject = new JSONObject(); |
|
|
|
|
|
|
|
for (DcRegion dcRegion : list) { |
|
|
|
OkHttp okHttp = new OkHttp(); |
|
|
|
|
|
|
|
Response response // 请求响应
|
|
|
|
= okHttp |
|
|
|
.headers(new HashMap<>()) |
|
|
|
.url(uri + dcRegion.getLongitude() + "," + dcRegion.getLatitude() + KEY + accessKey) // 请求地址
|
|
|
|
.get(); // 请求方法
|
|
|
|
|
|
|
|
if (response.body() != null) { |
|
|
|
|
|
|
|
JSONObject jsonResult = JSONObject.parseObject(response.body().string()); |
|
|
|
if (jsonResult.getInteger("code") == 200) { |
|
|
|
|
|
|
|
if (Objects.equals(redisKey, WEATHERFACTS)) { |
|
|
|
|
|
|
|
jsonObject.put(redisKey + dcRegion.getId(), extracted(jsonResult, "now")); |
|
|
|
|
|
|
|
} else if (Objects.equals(redisKey, METEOROLOGICALEARLYWARNING)) { |
|
|
|
|
|
|
|
jsonObject.put(redisKey + dcRegion.getId(), extracted(jsonResult, "warning")); |
|
|
|
|
|
|
|
} else if (Objects.equals(redisKey, HOURLYWEATHER)) { |
|
|
|
|
|
|
|
jsonObject.put(redisKey + dcRegion.getId(), extracted(jsonResult, "hourly")); |
|
|
|
} |
|
|
|
} else { |
|
|
|
return AjaxResult.error(jsonResult.getInteger("code"), "请求失败"); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (Objects.equals(redisKey, WEATHERFACTS)) { |
|
|
|
|
|
|
|
redisCache.setCacheObject(redisKey, jsonObject, 13, TimeUnit.MINUTES); |
|
|
|
|
|
|
|
} else if (Objects.equals(redisKey, METEOROLOGICALEARLYWARNING)) { |
|
|
|
|
|
|
|
redisCache.setCacheObject(redisKey, jsonObject, 13, TimeUnit.MINUTES); |
|
|
|
|
|
|
|
} else if (Objects.equals(redisKey, HOURLYWEATHER)) { |
|
|
|
|
|
|
|
redisCache.setCacheObject(redisKey, jsonObject, 13, TimeUnit.MINUTES); |
|
|
|
} |
|
|
|
|
|
|
|
return AjaxResult.success(jsonObject); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private static Object extracted(JSONObject jsonResult, String type) { |
|
|
|
JSONObject jsonObject = new JSONObject(); |
|
|
|
jsonObject.put("code", jsonResult.getString("code")); |
|
|
|
if (Objects.equals(jsonResult.getString("code"), "200")) { |
|
|
|
if (Objects.equals(type, "now")) { |
|
|
|
return jsonResult.getJSONObject(type); |
|
|
|
} else { |
|
|
|
return jsonResult.getJSONArray(type); |
|
|
|
} |
|
|
|
} else if (Objects.equals(jsonResult.getString("code"), "500")) { |
|
|
|
jsonObject.put("msg", "请求失败:无响应或超时"); |
|
|
|
} else if (Objects.equals(jsonResult.getString("code"), "429")) { |
|
|
|
jsonObject.put("msg", "请求失败:每分钟访问次数过多,请稍后重试"); |
|
|
|
} else if (Objects.equals(jsonResult.getString("code"), "404")) { |
|
|
|
jsonObject.put("msg", "请求失败:查询的地区或者数据不存在"); |
|
|
|
} else if (Objects.equals(jsonResult.getString("code"), "403")) { |
|
|
|
jsonObject.put("msg", "请求失败:无访问权限,可能是绑定的PackageName、BundleID、域名IP地址不一致"); |
|
|
|
} else if (Objects.equals(jsonResult.getString("code"), "402")) { |
|
|
|
jsonObject.put("msg", "请求失败:超过访问次数或余额不足以支持继续访问服务"); |
|
|
|
} else if (Objects.equals(jsonResult.getString("code"), "401")) { |
|
|
|
jsonObject.put("msg", "请求失败:认证失败,可能使用了错误的KEY、数字签名错误、KEY的类型错误"); |
|
|
|
} else if (Objects.equals(jsonResult.getString("code"), "400")) { |
|
|
|
jsonObject.put("msg", "请求失败:请求错误,可能包含错误的请求参数或缺少必选的请求参数"); |
|
|
|
} else if (Objects.equals(jsonResult.getString("code"), "204")) { |
|
|
|
jsonObject.put("msg", "请求失败:请求成功,但你查询的地区暂时没有你需要的数据"); |
|
|
|
} |
|
|
|
return jsonObject; |
|
|
|
} |
|
|
|
} |