106 lines
4.3 KiB
JavaScript
106 lines
4.3 KiB
JavaScript
/* Погода Астаны через открытый API Open-Meteo, без ключей. */
|
||
const ASTANA = { latitude:51.1694, longitude:71.4491, timezone:"Asia/Almaty" };
|
||
const WEATHER_URL = "https://api.open-meteo.com/v1/forecast?latitude=51.1694&longitude=71.4491¤t=temperature_2m,apparent_temperature,relative_humidity_2m,weather_code,wind_speed_10m&daily=weather_code,temperature_2m_max,temperature_2m_min,uv_index_max&timezone=Asia%2FAlmaty&forecast_days=7";
|
||
const AIR_URL = "https://air-quality-api.open-meteo.com/v1/air-quality?latitude=51.1694&longitude=71.4491¤t=european_aqi,pm2_5&timezone=Asia%2FAlmaty";
|
||
|
||
function parseWeather(raw, air) {
|
||
return {
|
||
temp: Math.round(raw.current.temperature_2m),
|
||
feels: Math.round(raw.current.apparent_temperature),
|
||
humidity: Math.round(raw.current.relative_humidity_2m),
|
||
wind: Math.round(raw.current.wind_speed_10m * 10) / 10,
|
||
code: raw.current.weather_code,
|
||
updated: raw.current.time,
|
||
aqi: air && air.current ? Math.round(air.current.european_aqi) : null,
|
||
pm25: air && air.current ? air.current.pm2_5 : null,
|
||
isDemo: false,
|
||
daily: raw.daily.time.map((date, i) => ({
|
||
date,
|
||
min: Math.round(raw.daily.temperature_2m_min[i]),
|
||
max: Math.round(raw.daily.temperature_2m_max[i]),
|
||
uv: Math.round(raw.daily.uv_index_max[i] * 10) / 10,
|
||
code: raw.daily.weather_code[i]
|
||
}))
|
||
};
|
||
}
|
||
|
||
function sampleWeather() {
|
||
const now = new Date();
|
||
const dates = Array.from({length:7}, (_,i) => {
|
||
const date = new Date(now); date.setDate(now.getDate()+i);
|
||
return date.toISOString().slice(0,10);
|
||
});
|
||
return {
|
||
temp:3, feels:-1, humidity:70, wind:4.2, code:3, aqi:78, pm25:null, isDemo:true, updated:now.toISOString(),
|
||
daily:dates.map((date,i) => ({date,min:-3-i,max:4-i,uv:1,code:i===2?71:3}))
|
||
};
|
||
}
|
||
|
||
async function fetchJson(url) {
|
||
const controller = new AbortController();
|
||
const timer = setTimeout(() => controller.abort(), 12000);
|
||
try {
|
||
const response = await fetch(url, {signal:controller.signal});
|
||
if (!response.ok) throw new Error("Weather HTTP " + response.status);
|
||
return await response.json();
|
||
} finally { clearTimeout(timer); }
|
||
}
|
||
|
||
const WeatherStore = {
|
||
data:null,
|
||
async load() {
|
||
try {
|
||
const local = await fetchJson("api/environment");
|
||
if (!local.daily || !Number.isFinite(local.temp)) throw new Error("Неверный формат API");
|
||
this.data = local;
|
||
return this.data;
|
||
} catch (_) { /* В статическом предпросмотре прогноз берём напрямую. */ }
|
||
const [weatherResult, airResult] = await Promise.allSettled([fetchJson(WEATHER_URL), fetchJson(AIR_URL)]);
|
||
this.data = weatherResult.status === "fulfilled"
|
||
? parseWeather(weatherResult.value, airResult.status === "fulfilled" ? airResult.value : null)
|
||
: sampleWeather();
|
||
return this.data;
|
||
}
|
||
};
|
||
|
||
function weatherCodeKey(code) {
|
||
if (code === 0 || code === 1) return "conditionClear";
|
||
if (code === 2) return "conditionCloudy";
|
||
if (code === 3) return "conditionOvercast";
|
||
if (code >= 45 && code <= 48) return "conditionFog";
|
||
if ((code >= 51 && code <= 67) || (code >= 80 && code <= 82)) return "conditionRain";
|
||
if ((code >= 71 && code <= 77) || code === 85 || code === 86) return "conditionSnow";
|
||
if (code >= 95) return "conditionStorm";
|
||
return "conditionCloudy";
|
||
}
|
||
function weatherIcon(code) {
|
||
const key = weatherCodeKey(code);
|
||
if (key === "conditionSnow") return "sparkles";
|
||
if (key === "conditionRain" || key === "conditionStorm") return "alert";
|
||
if (key === "conditionFog") return "info";
|
||
return "sun";
|
||
}
|
||
function aqiKey(aqi) {
|
||
if (aqi === null || aqi === undefined) return "aqiModerate";
|
||
if (aqi <= 20) return "aqiGood";
|
||
if (aqi <= 40) return "aqiModerate";
|
||
if (aqi <= 80) return "aqiHigh";
|
||
return "aqiVeryHigh";
|
||
}
|
||
function uvKey(uv) {
|
||
if (uv < 3) return "uvLow";
|
||
if (uv < 6) return "uvModerate";
|
||
if (uv < 8) return "uvHigh";
|
||
return "uvVeryHigh";
|
||
}
|
||
function weatherSummary(w) {
|
||
if (w.temp <= 0 || w.feels <= -3) return t("weatherAdviceCold");
|
||
if (w.temp <= 13) return t("weatherAdviceMild");
|
||
return t("weatherAdviceWarm");
|
||
}
|
||
function formatDay(dateString, index) {
|
||
if (index === 0) return t("today");
|
||
if (index === 1) return t("tomorrow");
|
||
return localizedDate(new Date(dateString+"T12:00:00"),true);
|
||
}
|