风向计算方法封装

This commit is contained in:
2026-07-01 11:50:53 +08:00
parent a1bd48dac3
commit f1d44e7dbe
@@ -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;
}