大屏-天气

This commit is contained in:
XinWei 2024-08-02 15:28:23 +08:00
parent edb974c3ce
commit 25a8f51223
3 changed files with 206 additions and 0 deletions

View File

@ -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<WeatherInfoVO> getWeather(String cityCode) {
String weatherData = GoodWeatherUtil.getWeatherData(cityCode);
WeatherInfoVO weatherInfo = GoodWeatherUtil.GetWeather(weatherData);
return CommonResult.success(weatherInfo);
}
}

View File

@ -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;//风向
}

View File

@ -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);
}
}