diff --git a/weather-data-ui/src/utils/windDirection.ts b/weather-data-ui/src/utils/windDirection.ts new file mode 100644 index 0000000..3c7315d --- /dev/null +++ b/weather-data-ui/src/utils/windDirection.ts @@ -0,0 +1,22 @@ +/** + * 风向角度 -> 风向代码(N/NE/E/SE/S/SW/W/NW) + */ +export function windDirectionCode(deg: number | null | string): string | null { + if (deg == null || deg === "" || isNaN(Number(deg))) return null; + const d = Number(deg); + if (d < 0 || d > 360) return null; + return ["N", "NE", "E", "SE", "S", "SW", "W", "NW"][Math.floor((d + 22.5) / 45) % 8]; +} + +/** + * 风向角度 -> 风向中文标签(通过字典 wind_direction_type 映射) + * @param deg 风向角度 + * @param dicts 字典列表 + */ +export function windDirectionLabel(deg: number | null | string, dicts: any[]): string { + const code = windDirectionCode(deg); + if (!code) return "—"; // em dash + const type = dicts.find((d: any) => d.dictType === "wind_direction_type"); + const entry = type?.dataList?.find((e: any) => e.dictValue === code); + return entry?.dictLabel || code; +}