|
@@ -0,0 +1,47 @@
|
|
|
+package com.zhjq.controller;
|
|
|
+
|
|
|
+import cn.hutool.http.HttpUtil;
|
|
|
+import com.alibaba.fastjson2.JSONObject;
|
|
|
+import org.springframework.web.bind.annotation.GetMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 百度天气API服务类
|
|
|
+ * 国内天气查询
|
|
|
+ */
|
|
|
+@RestController
|
|
|
+@RequestMapping("/weather")
|
|
|
+public class WeatherController {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 区县的行政区划编码
|
|
|
+ */
|
|
|
+ private static final String district_id = "220211";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 请求数据类型。数据类型有:now/fc/index/alert/fc_hour/all,控制返回内容
|
|
|
+ */
|
|
|
+ private static final String data_type = "all";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 开发者密钥,可在API控制台申请获得
|
|
|
+ */
|
|
|
+ private static final String ak = "gP0zhor16hI4qZIolfffsgXI8G8vI2Xw";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 开发者密钥,可在API控制台申请获得
|
|
|
+ */
|
|
|
+ private static final String API_URL = "https://api.map.baidu.com/weather/v1/";
|
|
|
+
|
|
|
+ @GetMapping("/getWeather")
|
|
|
+ public JSONObject weather() {
|
|
|
+ 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")) {
|
|
|
+ return jsonObject;
|
|
|
+ } else {
|
|
|
+ return JSONObject.parseObject("{\"status\":1,\"message\":\"获取气象信息失败,请稍候检查网络或稍后重试\"}");
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|