80 lines
2.8 KiB
JavaScript
80 lines
2.8 KiB
JavaScript
|
|
import { message } from "antd";
|
|||
|
|
import SunIcon from "~/assets/images/public/weather/1.png";
|
|||
|
|
import CloudIcon from "~/assets/images/public/weather/2.png";
|
|||
|
|
import CloudSunIcon from "~/assets/images/public/weather/3.png";
|
|||
|
|
import ThunderstormIcon from "~/assets/images/public/weather/4.png";
|
|||
|
|
import MildRainIcon from "~/assets/images/public/weather/5.png";
|
|||
|
|
import ModerateRainIcon from "~/assets/images/public/weather/6.png";
|
|||
|
|
import HeavyRainIcon from "~/assets/images/public/weather/7.png";
|
|||
|
|
import StormRainIcon from "~/assets/images/public/weather/8.png";
|
|||
|
|
import SnowIcon from "~/assets/images/public/weather/9.png";
|
|||
|
|
import ModerateSnowIcon from "~/assets/images/public/weather/10.png";
|
|||
|
|
import HeavySnowIcon from "~/assets/images/public/weather/11.png";
|
|||
|
|
import SnowRainIcon from "~/assets/images/public/weather/12.png";
|
|||
|
|
import FogIcon from "~/assets/images/public/weather/13.png";
|
|||
|
|
import HazeIcon from "~/assets/images/public/weather/14.png";
|
|||
|
|
import DustIcon from "~/assets/images/public/weather/15.png";
|
|||
|
|
import DuststormIcon from "~/assets/images/public/weather/16.png";
|
|||
|
|
|
|||
|
|
const WEATHER_URL = "https://api.map.baidu.com/weather/v1/?district_id=130300&data_type=all&ak=dIqOi34IlTg5FkNck1vqoBpLhPAj36S1";
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 获取秦皇岛市天气数据。
|
|||
|
|
* 统一校验 HTTP 状态和百度接口业务状态,调用方只处理页面展示状态。
|
|||
|
|
* @param {{ signal?: AbortSignal }} options 请求配置,可传入 signal 取消请求。
|
|||
|
|
* @returns {Promise<object>} 百度天气接口 result 数据。
|
|||
|
|
*/
|
|||
|
|
export async function getWeather(options = {}) {
|
|||
|
|
return {}; // TODO: Remove this line
|
|||
|
|
|
|||
|
|
const response = await fetch(WEATHER_URL, { signal: options.signal });
|
|||
|
|
if (!response.ok) {
|
|||
|
|
message.error("天气接口请求失败,请检查网络或稍后再试");
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
const data = await response.json();
|
|||
|
|
if (data.status !== 0 || !data.result?.now) {
|
|||
|
|
message.error(data.message || "天气接口返回数据异常");
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return data.result;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
export const weatherIconMap = {
|
|||
|
|
晴天: SunIcon,
|
|||
|
|
晴: SunIcon,
|
|||
|
|
多云: CloudSunIcon,
|
|||
|
|
阴: CloudIcon,
|
|||
|
|
雷阵雨: ThunderstormIcon,
|
|||
|
|
小雨: MildRainIcon,
|
|||
|
|
中雨: ModerateRainIcon,
|
|||
|
|
大雨: HeavyRainIcon,
|
|||
|
|
暴雨: StormRainIcon,
|
|||
|
|
小雪: SnowIcon,
|
|||
|
|
中雪: ModerateSnowIcon,
|
|||
|
|
大雪: HeavySnowIcon,
|
|||
|
|
雨夹雪: SnowRainIcon,
|
|||
|
|
雾: FogIcon,
|
|||
|
|
霾: HazeIcon,
|
|||
|
|
浮尘: DustIcon,
|
|||
|
|
沙尘暴: DuststormIcon,
|
|||
|
|
默认: SunIcon,
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
export const getWeatherIcon = (weatherName) => {
|
|||
|
|
if (!weatherName)
|
|||
|
|
return weatherIconMap["默认"];
|
|||
|
|
|
|||
|
|
for (const [key, icon] of Object.entries(weatherIconMap)) {
|
|||
|
|
if (weatherName.includes(key))
|
|||
|
|
return icon;
|
|||
|
|
}
|
|||
|
|
return weatherIconMap["默认"];
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
export const getAlertColor = (level) => {
|
|||
|
|
return { 蓝色预警: "#1E90FF", 黄色预警: "#FFA500", 橙色预警: "#FF4500", 红色预警: "#FF0000" }[level] || "#fff";
|
|||
|
|
};
|