|
@@ -2,10 +2,15 @@ package com.zhjq.controller;
|
|
|
|
|
|
import cn.hutool.http.HttpUtil;
|
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
|
+import com.zhjq.common.core.redis.RedisCache;
|
|
|
+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;
|
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
/**
|
|
|
* 百度天气API服务类
|
|
|
* 国内天气查询
|
|
@@ -34,18 +39,29 @@ public class WeatherController {
|
|
|
*/
|
|
|
private static final String API_URL = "https://api.map.baidu.com/weather/v1/";
|
|
|
|
|
|
+ @Resource
|
|
|
+ private RedisCache redisCache;
|
|
|
+
|
|
|
@GetMapping("/getWeather")
|
|
|
public JSONObject weather() {
|
|
|
JSONObject json = new JSONObject();
|
|
|
- String result = HttpUtil.get(API_URL + "?district_id=" + district_id + "&data_type=" + data_type + "&ak=" + ak);
|
|
|
- JSONObject jsonObject = JSONObject.parseObject(result);
|
|
|
- if (jsonObject.get("status").toString().equals("0")) {
|
|
|
+ if(redisCache.getCacheObject("weather") == null){
|
|
|
+ String result = HttpUtil.get(API_URL + "?district_id=" + district_id + "&data_type=" + data_type + "&ak=" + ak);
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(result);
|
|
|
+ if (jsonObject.get("status").toString().equals("0")) {
|
|
|
+ json.put("code",200);
|
|
|
+ json.put("msg","查询成功");
|
|
|
+ json.put("data",jsonObject.get("result"));
|
|
|
+ redisCache.setCacheObject("weather",jsonObject.get("result").toString(),120, TimeUnit.SECONDS);
|
|
|
+ return json;
|
|
|
+ } else {
|
|
|
+ return JSONObject.parseObject("{\"code\":1,\"msg\":\"获取气象信息失败,请稍候检查网络或稍后重试\"}");
|
|
|
+ }
|
|
|
+ }else{
|
|
|
json.put("code",200);
|
|
|
json.put("msg","查询成功");
|
|
|
- json.put("data",jsonObject.get("result"));
|
|
|
+ json.put("data",JSONObject.parseObject(redisCache.getCacheObject("weather").toString()));
|
|
|
return json;
|
|
|
- } else {
|
|
|
- return JSONObject.parseObject("{\"code\":1,\"msg\":\"获取气象信息失败,请稍候检查网络或稍后重试\"}");
|
|
|
}
|
|
|
}
|
|
|
}
|