From 25a8f5122371d74a60ca13148ced4aa4fa52da19 Mon Sep 17 00:00:00 2001 From: XinWei <2718030729@qq.com> Date: Fri, 2 Aug 2024 15:28:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=A7=E5=B1=8F-=E5=A4=A9=E6=B0=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/admin/weather/WeatherApi.java | 26 +++ .../admin/weather/vo/WeatherInfoVO.java | 17 ++ .../datacenter/utlis/GoodWeatherUtil.java | 163 ++++++++++++++++++ 3 files changed, 206 insertions(+) create mode 100644 ludu-module-datacenter/ludu-module-datacenter-biz/src/main/java/cn/iocoder/yudao/module/datacenter/controller/admin/weather/WeatherApi.java create mode 100644 ludu-module-datacenter/ludu-module-datacenter-biz/src/main/java/cn/iocoder/yudao/module/datacenter/controller/admin/weather/vo/WeatherInfoVO.java create mode 100644 ludu-module-datacenter/ludu-module-datacenter-biz/src/main/java/cn/iocoder/yudao/module/datacenter/utlis/GoodWeatherUtil.java diff --git a/ludu-module-datacenter/ludu-module-datacenter-biz/src/main/java/cn/iocoder/yudao/module/datacenter/controller/admin/weather/WeatherApi.java b/ludu-module-datacenter/ludu-module-datacenter-biz/src/main/java/cn/iocoder/yudao/module/datacenter/controller/admin/weather/WeatherApi.java new file mode 100644 index 000000000..eba435660 --- /dev/null +++ b/ludu-module-datacenter/ludu-module-datacenter-biz/src/main/java/cn/iocoder/yudao/module/datacenter/controller/admin/weather/WeatherApi.java @@ -0,0 +1,26 @@ +package cn.iocoder.yudao.module.datacenter.controller.admin.weather; + +import cn.iocoder.yudao.framework.common.pojo.CommonResult; +import cn.iocoder.yudao.module.datacenter.controller.admin.weather.vo.WeatherInfoVO; +import cn.iocoder.yudao.module.datacenter.utlis.GoodWeatherUtil; +import io.swagger.v3.oas.annotations.tags.Tag; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * @Description 大屏服务-天气接口 + */ +@Tag(name = "大屏服务 - 天气接口") +@RestController +@RequestMapping("/h5/weather") +@Validated +public class WeatherApi { + @GetMapping(value = "/getWeather") + public CommonResult getWeather(String cityCode) { + String weatherData = GoodWeatherUtil.getWeatherData(cityCode); + WeatherInfoVO weatherInfo = GoodWeatherUtil.GetWeather(weatherData); + return CommonResult.success(weatherInfo); + } +} diff --git a/ludu-module-datacenter/ludu-module-datacenter-biz/src/main/java/cn/iocoder/yudao/module/datacenter/controller/admin/weather/vo/WeatherInfoVO.java b/ludu-module-datacenter/ludu-module-datacenter-biz/src/main/java/cn/iocoder/yudao/module/datacenter/controller/admin/weather/vo/WeatherInfoVO.java new file mode 100644 index 000000000..4acf33b24 --- /dev/null +++ b/ludu-module-datacenter/ludu-module-datacenter-biz/src/main/java/cn/iocoder/yudao/module/datacenter/controller/admin/weather/vo/WeatherInfoVO.java @@ -0,0 +1,17 @@ +package cn.iocoder.yudao.module.datacenter.controller.admin.weather.vo; + +import lombok.Data; + +/** + * @Description 天气数据实体 + */ +@Data +public class WeatherInfoVO { + private String weatherInfo;//天气 + private Integer temperature;//气温 + private String ultravioletRay;//紫外线 + private String visibility;//能见度 + private String typhoonInfo;//台风信息 + private String windPower;//风力 + private String windDirection;//风向 +} diff --git a/ludu-module-datacenter/ludu-module-datacenter-biz/src/main/java/cn/iocoder/yudao/module/datacenter/utlis/GoodWeatherUtil.java b/ludu-module-datacenter/ludu-module-datacenter-biz/src/main/java/cn/iocoder/yudao/module/datacenter/utlis/GoodWeatherUtil.java new file mode 100644 index 000000000..2f82bf9f8 --- /dev/null +++ b/ludu-module-datacenter/ludu-module-datacenter-biz/src/main/java/cn/iocoder/yudao/module/datacenter/utlis/GoodWeatherUtil.java @@ -0,0 +1,163 @@ +package cn.iocoder.yudao.module.datacenter.utlis; + +import cn.iocoder.yudao.module.datacenter.controller.admin.weather.vo.WeatherInfoVO; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; + +/** + * 获取天气数据 + */ + +public class GoodWeatherUtil { + + //高德地图 + private static final String WEATHER_SERVICES_URL = "https://restapi.amap.com/v3/weather/weatherInfo?city="; + private static final String WEATHER_KEY = "&key=172993e9541be0a4f824feedeb210b7e"; + + //和风天气 + private static final String WEATHER_SERVICES_URL2 = "https://geoapi.qweather.com/v2/city/lookup?"; + private static final String LOCATION = "location="; //区县名称 + private static final String ADM = "&adm="; //城市名称 + private static final String PRIVATE_KEY = "&key=1c0db79d181441258030a633197fb429"; + + + + public static String getWeatherData(String cityCode){ + int codelength = cityCode.length(); + if (6 != codelength){ + return "500"; + } + StringBuffer sb = new StringBuffer(); + try { + String weather_url = WEATHER_SERVICES_URL+cityCode+WEATHER_KEY; + URL url = new URL(weather_url); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); + connection.setRequestProperty("Charset", "utf-8"); + connection.connect(); + //读取URL的响应 + BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8")); + String line = null; + while ((line = reader.readLine()) != null) + sb.append(line + " "); + reader.close(); + }catch (Exception e){ + e.printStackTrace(); + } + return sb.toString(); + } + + + /* + * 将JSON格式数据进行解析 ,返回一个weather对象 + */ + public static WeatherInfoVO GetWeather(String weatherInfobyJson) { + if ("500".equals(weatherInfobyJson)){ + return null; + } + + JSONObject dataOfJson = JSONObject.parseObject(weatherInfobyJson); + System.out.println(dataOfJson); + //创建WeatherInfo对象,提取所需的天气信息 + WeatherInfoVO weatherInfoVO = new WeatherInfoVO(); + + //从json数据中提取数据 + JSONArray forecasts = dataOfJson.getJSONArray("lives"); + JSONObject weather = forecasts.getJSONObject(0); + + // 取得当天的天气信息 + weatherInfoVO.setWeatherInfo(weather.getString("weather")); + + // 设置气温 + String temperature = weather.getString("temperature"); + weatherInfoVO.setTemperature(Integer.parseInt(temperature)); + + //设置风力信息 + String windPower = weather.getString("windpower"); + weatherInfoVO.setWindPower(windPower); + + //设置风向信息 + String windDirection = weather.getString("winddirection"); + weatherInfoVO.setWindDirection(windDirection); + + return weatherInfoVO; + } + +// +// public static String getWeatherDataHF(String areaName,String cityName){ +// if (areaName == null && cityName == null){ +// return "500"; +// } +// StringBuffer sb = new StringBuffer(); +// try { +// String weather_url = WEATHER_SERVICES_URL2+LOCATION+areaName+ADM+cityName+PRIVATE_KEY; +// URL url = new URL(weather_url); +// HttpURLConnection connection = (HttpURLConnection) url.openConnection(); +// connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); +// connection.setRequestProperty("Charset", "utf-8"); +// connection.connect(); +// //读取URL的响应 +// BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8")); +// String line = null; +// while ((line = reader.readLine()) != null) +// sb.append(line + " "); +// reader.close(); +// }catch (Exception e){ +// e.printStackTrace(); +// } +// return sb.toString(); +// } + + /* + * 将JSON格式数据进行解析 ,返回一个weather对象 + */ +// public static String GetWeatherHF(String weatherInfobyJson) { +// if ("500".equals(weatherInfobyJson)){ +// return null; +// } +// JSONObject dataOfJson = JSONObject.parseObject(weatherInfobyJson); +// System.out.println(dataOfJson); +// //创建WeatherInfo对象,提取所需的天气信息 +// WeatherInfoVO weatherInfoVO = new WeatherInfoVO(); +// +// //从json数据中提取数据 +// JSONArray location = dataOfJson.getJSONArray("location"); +// JSONObject weather = location.getJSONObject(0); +// +// // 取得当天的天气信息 +// weatherInfoVO.setWeatherInfo(weather.getString("weather")); +// +// // 设置气温 +// String temperature = weather.getString("temperature"); +// weatherInfoVO.setTemperature(Integer.parseInt(temperature)); +// +// //设置风力信息 +// String windPower = weather.getString("windpower"); +// weatherInfoVO.setWindPower(windPower); +// +// //设置风向信息 +// String windDirection = weather.getString("winddirection"); +// weatherInfoVO.setWindDirection(windDirection); +// +// return weatherInfoVO; +// } + + + + public static void main(String [] args) { + String weatherData = GoodWeatherUtil.getWeatherData("360730"); + + + WeatherInfoVO weatherInfoVO = GoodWeatherUtil.GetWeather(weatherData); + + System.out.println(weatherInfoVO); + + } + + +} \ No newline at end of file