大屏未来三天天气接口

This commit is contained in:
sonjinyon 2024-08-22 09:59:39 +08:00
parent 2bd72f906f
commit decde473dd
7 changed files with 61 additions and 93 deletions

View File

@ -3,6 +3,8 @@ package cn.iocoder.yudao.module.datacenter.controller.app.weather;
import cn.iocoder.yudao.module.datacenter.controller.app.weather.vo.WeatherInfoVO;
import cn.iocoder.yudao.module.datacenter.utlis.GoodWeatherUtil;
import cn.iocoder.yudao.module.datacenter.utlis.ResponseVO;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
@ -18,6 +20,7 @@ import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/h5/weather")
@Validated
public class WeatherApi {
@GetMapping(value = "/getWeather")
@Operation(summary = "获得输入城市的天气")
public ResponseVO getWeather(String cityCode) {
@ -25,4 +28,18 @@ public class WeatherApi {
WeatherInfoVO weatherInfo = GoodWeatherUtil.GetWeather(weatherData);
return new ResponseVO(200,"",weatherInfo,0);
}
/**
* 获取未来三天天气预报
* @param cityCode
* @return
*/
@GetMapping(value = "/getFutureWeather")
public ResponseVO getFutureWeather(String cityCode) {
String weatherData = GoodWeatherUtil.getWeatherData(cityCode);
JSONObject dataOfJson = JSONObject.parseObject(weatherData);
JSONArray forecasts = dataOfJson.getJSONArray("forecasts");
return new ResponseVO(200,"",forecasts,0);
}
}

View File

@ -18,15 +18,14 @@ 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";
private static final String WEATHER_TYPE = "&extensions=all";
/**
* 今日实况天气
* @param cityCode
* @return
*/
public static String getWeatherData(String cityCode){
int codelength = cityCode.length();
if (6 != codelength){
@ -53,6 +52,37 @@ public class GoodWeatherUtil {
}
/**
* 获取未来三天天气预报
* @param cityCode
* @return
*/
public static String getFutureWeather(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 + WEATHER_TYPE;
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对象
*/
@ -88,76 +118,4 @@ public class GoodWeatherUtil {
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);
}
}

View File

@ -3,7 +3,7 @@ spring:
name: ticket-server
profiles:
active: dev
active: local
server:
port: 48088

View File

@ -3,7 +3,7 @@ spring:
name: gateway-server
profiles:
active: dev
active: local
server:
port: 48080

View File

@ -66,7 +66,7 @@ spring:
username: root
password: 123456
ticketing:
url: jdbc:mysql://101.43.112.107:3306/ludu_ticketing?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true&nullCatalogMeansCurrent=true # MySQL Connector/J 8.X 连接的示例
url: jdbc:mysql://120.46.37.243:3306/ludu_ticketing?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true&nullCatalogMeansCurrent=true # MySQL Connector/J 8.X 连接的示例
username: root
password: xpower1234
@ -97,16 +97,9 @@ spring:
--- #################### 定时任务相关配置 ####################
xxl:
job:
enabled: false # 是否开启调度中心,默认为 true 开启
admin:
addresses: http://127.0.0.1:9090/xxl-job-admin
executor:
appname: ${spring.application.name} # 执行器 AppName
address:
ip: # 执行器IP [选填]默认为空表示自动获取IP多网卡时可手动设置指定IP该IP不会绑定Host仅作为通讯实用地址信息用于 "执行器注册" 和 "调度中心请求并触发任务"
port: 0 # ### 执行器端口号 [选填]小于等于0则自动获取默认端口为9999单机部署多个执行器时注意要配置不同执行器端口
logpath: ${user.home}/logs/xxl-job/${spring.application.name} # 执行器运行日志文件存储磁盘路径
logretentiondays: 30 # 执行器日志文件保存天数 [选填] 过期日志自动清理, 限制值大于等于3时生效; 否则, 如-1, 关闭自动清理功能;
accessToken: # 执行器通讯TOKEN
addresses: http://127.0.0.1:9090/xxl-job-admin # 调度中心部署跟地址
--- #################### 服务保障相关配置 ####################

View File

@ -3,7 +3,7 @@ spring:
name: infra-server
profiles:
active: dev
active: local
server:
port: 48082

View File

@ -3,7 +3,7 @@ spring:
name: system-server
profiles:
active: dev
active: local
server:
port: 48081