fix: 更新天气API

This commit is contained in:
xiaozzzi 2025-06-04 16:54:57 +08:00
parent 57d7aa5335
commit 6cc8a66243
6 changed files with 66 additions and 15 deletions

View File

@ -28,6 +28,10 @@ public enum ParamEnum {
* 和风天气KEY
*/
HEFENG_KEY(true, 20,""),
/**
* 和风天气KEY
*/
HEFENG_HOST(false, 100,""),
/**
* GITEE key

View File

@ -36,10 +36,10 @@ import java.util.concurrent.TimeUnit;
public class WeatherManager {
private static final Logger log = LoggerFactory.getLogger(WeatherManager.class);
private static final String URL_CITY = "https://geoapi.qweather.com/v2/city/lookup";
private static final String URL_NOW = "https://devapi.heweather.net/v7/weather/now";
private static final String URL_DAILY = "https://devapi.heweather.net/v7/weather/3d";
private static final String URL_HOURLY = "https://devapi.heweather.net/v7/weather/24h";
private static final String URL_CITY = "https://%s/geo/v2/city/lookup";
private static final String URL_NOW = "https://%s/v7/weather/now";
private static final String URL_DAILY = "https://%s/v7/weather/3d";
private static final String URL_HOURLY = "https://%s/v7/weather/24h";
private static final String SUCCESS = "200";
/**
@ -66,8 +66,8 @@ public class WeatherManager {
return cache;
}
log.info("[BLOSSOM] refresh weather: {}", location);
Map<String, String> maps = initParam(location);
if (maps == null) {
HeFengReq params = initParam(location);
if (params == null) {
log.info("未配置天气信息, 忽略天气查询");
WeatherRes weather = new WeatherRes();
CityRes.Location l = new CityRes.Location();
@ -81,22 +81,22 @@ public class WeatherManager {
DailyRes daily = null;
HourlyRes hourly = null;
try {
cityStr = HttpUtil.get(URL_CITY, maps);
cityStr = HttpUtil.get(String.format(URL_CITY, params.getHost()), params.getApiParam());
if (StrUtil.isNotBlank(cityStr)) {
city = JsonUtil.toObj(cityStr, CityRes.class);
}
nowStr = HttpUtil.get(URL_NOW, maps);
nowStr = HttpUtil.get(String.format(URL_NOW, params.getHost()), params.getApiParam());
if (StrUtil.isNotBlank(nowStr)) {
now = JsonUtil.toObj(nowStr, NowRes.class);
}
dailyStr = HttpUtil.get(URL_DAILY, maps);
dailyStr = HttpUtil.get(String.format(URL_DAILY, params.getHost()), params.getApiParam());
if (StrUtil.isNotBlank(dailyStr)) {
daily = JsonUtil.toObj(dailyStr, DailyRes.class);
}
hourlyStr = HttpUtil.get(URL_HOURLY, maps);
hourlyStr = HttpUtil.get(String.format(URL_HOURLY, params.getHost()), params.getApiParam());
if (StrUtil.isNotBlank(hourlyStr)) {
hourly = JsonUtil.toObj(hourlyStr, HourlyRes.class);
}
@ -159,13 +159,20 @@ public class WeatherManager {
*
* @return 返回查询参数
*/
public Map<String, String> initParam(String location) {
Map<String, String> paramMap = paramService.selectMap(false, ParamEnum.HEFENG_KEY);
if (MapUtil.isNotEmpty(paramMap) && StrUtil.isNotBlank(paramMap.get(ParamEnum.HEFENG_KEY.name()))) {
public HeFengReq initParam(String location) {
Map<String, String> paramMap = paramService.selectMap(false, ParamEnum.HEFENG_KEY, ParamEnum.HEFENG_HOST);
if (MapUtil.isNotEmpty(paramMap)
&& StrUtil.isNotBlank(paramMap.get(ParamEnum.HEFENG_KEY.name()))
&& StrUtil.isNotBlank(paramMap.get(ParamEnum.HEFENG_HOST.name()))
) {
HeFengReq req = new HeFengReq();
req.setHost(paramMap.get(ParamEnum.HEFENG_HOST.name()));
Map<String, String> map = new HashMap<>(2);
map.put("location", location);
map.put("key", paramMap.get(ParamEnum.HEFENG_KEY.name()));
return map;
req.setApiParam(map);
return req;
}
return null;
}

View File

@ -0,0 +1,22 @@
package com.blossom.backend.thirdparty.hefeng.pojo;
import lombok.Data;
import java.util.Map;
/**
* 和风天气请求接口
*/
@Data
public class HeFengReq {
/**
* 和风用户域名
*/
private String host;
/**
* 和风接口请求参数
*/
private Map<String,String> apiParam;
}

View File

@ -1,7 +1,7 @@
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.31.99:3306/xzzz-blossom?useUnicode=true&characterEncoding=utf-8&allowPublicKeyRetrieval=true&allowMultiQueries=true&useSSL=false&&serverTimezone=GMT%2B8
url: jdbc:mysql://115.120.218.188:3306/xzzz-blossom?useUnicode=true&characterEncoding=utf-8&allowPublicKeyRetrieval=true&allowMultiQueries=true&useSSL=false&&serverTimezone=GMT%2B8
username: root
password: jasmine888
hikari:

View File

@ -77,6 +77,19 @@ WHERE NOT EXISTS(SELECT 1
--
INSERT INTO base_sys_param (id, param_name, param_value, param_desc, open_state, cre_time, upd_time)
SELECT *
FROM (select 12 as id,
'HEFENG_HOST' as param_name,
'abc.com' as param_value,
'和风天气用户 HOST' as param_desc,
1 as open_state,
CURRENT_TIMESTAMP as cre_time,
CURRENT_TIMESTAMP as upd_time) as temp
WHERE NOT EXISTS(SELECT 1
FROM base_sys_param
WHERE id = 12);
--
INSERT INTO base_sys_param (id, param_name, param_value, param_desc, open_state, cre_time, upd_time)
SELECT *
FROM (select 21 as id,
'GITEE_ACCESS_TOKEN' as param_name,
'' as param_value,

View File

@ -70,6 +70,10 @@
</div>
</el-form-item>
<el-form-item label="和风天气 Host">
<el-input size="default" v-model="serverParamForm.HEFENG_HOST" @change="(cur: any) => updParam('HEFENG_HOST', cur)"></el-input>
</el-form-item>
<el-form-item label="和风天气 Key">
<el-input size="default" v-model="serverParamForm.HEFENG_KEY" @change="(cur: any) => updParam('HEFENG_KEY', cur)"></el-input>
<div class="conf-tip">
@ -128,6 +132,7 @@ const serverParamForm = ref({
ARTICLE_LOG_EXP_DAYS: 0,
ARTICLE_RECYCLE_EXP_DAYS: 0,
BACKUP_EXP_DAYS: 0,
HEFENG_HOST: '',
HEFENG_KEY: '',
BLOSSOM_OBJECT_STORAGE_DOMAIN: '',
SERVER_MACHINE_EXPIRE: '',