zcloud-gbs-bi-react/src/utils/index.js

104 lines
3.3 KiB
JavaScript

import dayjs from "dayjs";
import quarterOfYear from "dayjs/plugin/quarterOfYear";
import SunIcon from "~/assets/images/weather/1.png";
import CloudIcon from "~/assets/images/weather/2.png";
import CloudSunIcon from "~/assets/images/weather/3.png";
import ThunderstormIcon from "~/assets/images/weather/4.png";
import MildRainIcon from "~/assets/images/weather/5.png";
import ModerateRainIcon from "~/assets/images/weather/6.png";
import HeavyRainIcon from "~/assets/images/weather/7.png";
import StormRainIcon from "~/assets/images/weather/8.png";
import SnowIcon from "~/assets/images/weather/9.png";
import ModerateSnowIcon from "~/assets/images/weather/10.png";
import HeavySnowIcon from "~/assets/images/weather/11.png";
import SnowRainIcon from "~/assets/images/weather/12.png";
import FogIcon from "~/assets/images/weather/13.png";
import HazeIcon from "~/assets/images/weather/14.png";
import DustIcon from "~/assets/images/weather/15.png";
import DuststormIcon from "~/assets/images/weather/16.png";
dayjs.extend(quarterOfYear);
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";
};
export const closeWindow = () => {
window.close();
setTimeout(() => {
if (!window.closed && !window.opener) {
window.location.href = "https://gbs-gateway.qhdsafety.com/";
// window.location.href = "http://192.168.198.8:30140/";
}
}, 500);
};
const DATE_TIME_FORMAT = "YYYY-MM-DD HH:mm:ss";
/**
* 根据统计周期获取当前年度、季度或月份的展示文本及完整时间范围。
* referenceDate 用于后续按指定日期查询,未传时使用当前时间。
*/
export function getPeriodRange(period, referenceDate = dayjs()) {
const currentDate = dayjs(referenceDate);
let startDate;
let endDate;
let periodDate;
if (period === "年度") {
startDate = currentDate.startOf("year");
endDate = currentDate.endOf("year");
periodDate = currentDate.format("YYYY年");
}
else if (period === "季度") {
startDate = currentDate.startOf("quarter");
endDate = currentDate.endOf("quarter");
periodDate = `${currentDate.format("YYYY年")}${currentDate.quarter()}季度`;
}
else {
startDate = currentDate.startOf("month");
endDate = currentDate.endOf("month");
periodDate = currentDate.format("YYYY年M月");
}
return {
dateRange: [startDate, endDate],
periodLabel: period === "月份" ? "月度" : period,
periodDate,
startTime: startDate.format(DATE_TIME_FORMAT),
endTime: endDate.format(DATE_TIME_FORMAT),
};
}