Files
gitlab-instance-0a899031_cn…/app/lib/city-weather.ts
2026-06-08 06:05:31 -05:00

65 lines
1.4 KiB
TypeScript

export interface CityWeatherLive {
temperature: number;
humidity?: number;
apparentTemperature?: number;
weatherCode?: number;
observedAt?: string;
}
export type CityWeatherMap = Record<string, CityWeatherLive>;
/** WMO weather code → 简短展示 */
export function weatherCodeLabel(code?: number, locale: "zh" | "en" = "zh"): string | null {
if (code == null) return null;
const zh: Record<number, string> = {
0: "晴",
1: "多云",
2: "多云",
3: "阴",
45: "雾",
48: "雾",
51: "小雨",
53: "小雨",
55: "小雨",
61: "雨",
63: "雨",
65: "大雨",
71: "雪",
73: "雪",
75: "大雪",
80: "阵雨",
81: "阵雨",
82: "暴雨",
95: "雷雨",
};
const en: Record<number, string> = {
0: "Clear",
1: "Cloudy",
2: "Cloudy",
3: "Overcast",
45: "Fog",
48: "Fog",
51: "Drizzle",
53: "Drizzle",
55: "Drizzle",
61: "Rain",
63: "Rain",
65: "Heavy rain",
71: "Snow",
73: "Snow",
75: "Heavy snow",
80: "Showers",
81: "Showers",
82: "Storms",
95: "Thunder",
};
const table = locale === "zh" ? zh : en;
return table[code] ?? (locale === "zh" ? "天气" : "Weather");
}
export function cityWeatherKey(city: { slug?: string; id?: number; name?: string }): string {
if (city.slug) return city.slug;
if (city.id != null) return String(city.id);
return city.name || "";
}