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 * 和风天气KEY
*/ */
HEFENG_KEY(true, 20,""), HEFENG_KEY(true, 20,""),
/**
* 和风天气KEY
*/
HEFENG_HOST(false, 100,""),
/** /**
* GITEE key * GITEE key

View File

@ -36,10 +36,10 @@ import java.util.concurrent.TimeUnit;
public class WeatherManager { public class WeatherManager {
private static final Logger log = LoggerFactory.getLogger(WeatherManager.class); 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_CITY = "https://%s/geo/v2/city/lookup";
private static final String URL_NOW = "https://devapi.heweather.net/v7/weather/now"; private static final String URL_NOW = "https://%s/v7/weather/now";
private static final String URL_DAILY = "https://devapi.heweather.net/v7/weather/3d"; private static final String URL_DAILY = "https://%s/v7/weather/3d";
private static final String URL_HOURLY = "https://devapi.heweather.net/v7/weather/24h"; private static final String URL_HOURLY = "https://%s/v7/weather/24h";
private static final String SUCCESS = "200"; private static final String SUCCESS = "200";
/** /**
@ -66,8 +66,8 @@ public class WeatherManager {
return cache; return cache;
} }
log.info("[BLOSSOM] refresh weather: {}", location); log.info("[BLOSSOM] refresh weather: {}", location);
Map<String, String> maps = initParam(location); HeFengReq params = initParam(location);
if (maps == null) { if (params == null) {
log.info("未配置天气信息, 忽略天气查询"); log.info("未配置天气信息, 忽略天气查询");
WeatherRes weather = new WeatherRes(); WeatherRes weather = new WeatherRes();
CityRes.Location l = new CityRes.Location(); CityRes.Location l = new CityRes.Location();
@ -81,22 +81,22 @@ public class WeatherManager {
DailyRes daily = null; DailyRes daily = null;
HourlyRes hourly = null; HourlyRes hourly = null;
try { try {
cityStr = HttpUtil.get(URL_CITY, maps); cityStr = HttpUtil.get(String.format(URL_CITY, params.getHost()), params.getApiParam());
if (StrUtil.isNotBlank(cityStr)) { if (StrUtil.isNotBlank(cityStr)) {
city = JsonUtil.toObj(cityStr, CityRes.class); 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)) { if (StrUtil.isNotBlank(nowStr)) {
now = JsonUtil.toObj(nowStr, NowRes.class); 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)) { if (StrUtil.isNotBlank(dailyStr)) {
daily = JsonUtil.toObj(dailyStr, DailyRes.class); 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)) { if (StrUtil.isNotBlank(hourlyStr)) {
hourly = JsonUtil.toObj(hourlyStr, HourlyRes.class); hourly = JsonUtil.toObj(hourlyStr, HourlyRes.class);
} }
@ -159,13 +159,20 @@ public class WeatherManager {
* *
* @return 返回查询参数 * @return 返回查询参数
*/ */
public Map<String, String> initParam(String location) { public HeFengReq initParam(String location) {
Map<String, String> paramMap = paramService.selectMap(false, ParamEnum.HEFENG_KEY); 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()))) { 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<String, String> map = new HashMap<>(2);
map.put("location", location); map.put("location", location);
map.put("key", paramMap.get(ParamEnum.HEFENG_KEY.name())); map.put("key", paramMap.get(ParamEnum.HEFENG_KEY.name()));
return map; req.setApiParam(map);
return req;
} }
return null; 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: spring:
datasource: datasource:
driver-class-name: com.mysql.cj.jdbc.Driver 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 username: root
password: jasmine888 password: jasmine888
hikari: 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) INSERT INTO base_sys_param (id, param_name, param_value, param_desc, open_state, cre_time, upd_time)
SELECT * 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, FROM (select 21 as id,
'GITEE_ACCESS_TOKEN' as param_name, 'GITEE_ACCESS_TOKEN' as param_name,
'' as param_value, '' as param_value,

View File

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