浏览代码

Merge remote-tracking branch 'origin/master'

bihuisong 5 月之前
父节点
当前提交
9f34eb3d2e

+ 47 - 0
zhjq-business/src/main/java/com/zhjq/controller/WeatherController.java

@@ -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\":\"获取气象信息失败,请稍候检查网络或稍后重试\"}");
+        }
+    }
+}

+ 1 - 1
zhjq-framework/src/main/java/com/zhjq/framework/config/SecurityConfig.java

@@ -111,7 +111,7 @@ public class SecurityConfig {
                     requests.antMatchers("/login", "/register", "/captchaImage").permitAll()
                             // 静态资源,可匿名访问
                             .antMatchers(HttpMethod.GET, "/", "/*.html", "/**/*.html", "/**/*.css", "/**/*.js", "/profile/**").permitAll()
-                            .antMatchers("/swagger-ui.html", "/swagger-resources/**", "/webjars/**", "/*/api-docs", "/druid/**").permitAll()
+                            .antMatchers("/swagger-ui.html", "/swagger-resources/**", "/webjars/**", "/*/api-docs", "/druid/**", "/weather/**").permitAll()
                             // 除上面外的所有请求全部需要鉴权认证
                             .anyRequest().authenticated();
                 })