feat: 优化页面效果

This commit is contained in:
yarnom 2025-07-04 17:53:24 +08:00
parent 0d88e161d9
commit 94b91c1290

View File

@ -2320,7 +2320,7 @@
function showWeatherForecast(deviceInfo) { function showWeatherForecast(deviceInfo) {
currentWeatherDevice = deviceInfo; currentWeatherDevice = deviceInfo;
currentForecastIndex = 0; // 不在这里设置currentForecastIndex让fetchWeatherData自动选择最近的时间点
weatherData = null; weatherData = null;
var role = /*[[${role}]]*/ 'USER'; var role = /*[[${role}]]*/ 'USER';
@ -2330,7 +2330,7 @@
if (!weatherEnabled) { if (!weatherEnabled) {
layer.msg( layer.msg(
'天气预测功能未启用,请在地图功能菜单中开启', '天气预测功能未启用',
{time: 2000} {time: 2000}
); );
return; return;
@ -2400,10 +2400,11 @@
var requestBody = { var requestBody = {
"lat": parseFloat(lat.toFixed(2)), "lat": parseFloat(lat.toFixed(2)),
"lon": parseFloat(lon.toFixed(2)), "lon": parseFloat(lon.toFixed(2)),
"model": "gfs", "model": "gfs", // 使用全球预报系统模型
"parameters": ["temp", "wind", "precip", "pressure", "rh", "windGust"], "parameters": ["temp", "wind", "precip", "pressure", "rh", "windGust"],
"levels": ["surface"], "levels": ["surface"],
"key": weatherApiKey "key": weatherApiKey,
"hours": 72 // 请求未来72小时的预报数据确保有足够的未来预测
}; };
fetch('https://api.windy.com/api/point-forecast/v2', { fetch('https://api.windy.com/api/point-forecast/v2', {
@ -2422,7 +2423,43 @@
}) })
.then(function(data) { .then(function(data) {
weatherData = data; weatherData = data;
currentForecastIndex = 0;
// 找到最接近当前时间的预报索引,优先选择未来时间点
var currentTime = new Date().getTime();
var closestIndex = 0;
var futureIndex = -1; // 记录第一个未来时间点
var smallestDiff = Number.MAX_VALUE;
if (weatherData.ts && weatherData.ts.length > 0) {
// 首先尝试找到第一个未来时间点
for (var i = 0; i < weatherData.ts.length; i++) {
if (weatherData.ts[i] > currentTime) {
futureIndex = i;
break;
}
}
// 如果找到了未来时间点,直接使用
if (futureIndex >= 0) {
closestIndex = futureIndex;
} else {
// 否则找最接近的时间点
for (var i = 0; i < weatherData.ts.length; i++) {
var diff = Math.abs(weatherData.ts[i] - currentTime);
if (diff < smallestDiff) {
smallestDiff = diff;
closestIndex = i;
}
}
}
// 打印调试信息
console.log("当前时间:", new Date(currentTime).toLocaleString());
console.log("选择的预报时间:", new Date(weatherData.ts[closestIndex]).toLocaleString());
console.log("所有可用时间点:", weatherData.ts.map(ts => new Date(ts).toLocaleString()));
}
currentForecastIndex = closestIndex;
displayCurrentForecast(); displayCurrentForecast();
updateForecastNavigation(); updateForecastNavigation();
}) })