File size: 2,224 Bytes
96dd062 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | import { backgroundWallpaper } from "../config";
// 背景图片处理工具函数
export const getBackgroundImages = () => {
const bgSrc = backgroundWallpaper.src;
if (
typeof bgSrc === "object" &&
bgSrc !== null &&
!Array.isArray(bgSrc) &&
("desktop" in bgSrc || "mobile" in bgSrc)
) {
const srcObj = bgSrc as {
desktop?: string | string[];
mobile?: string | string[];
};
return {
desktop: srcObj.desktop || srcObj.mobile || "",
mobile: srcObj.mobile || srcObj.desktop || "",
};
}
// 如果是字符串,同时用于桌面端和移动端
return {
desktop: bgSrc,
mobile: bgSrc,
};
};
// 类型守卫函数
export const isBannerSrcObject = (
src:
| string
| string[]
| { desktop?: string | string[]; mobile?: string | string[] },
): src is { desktop?: string | string[]; mobile?: string | string[] } => {
return (
typeof src === "object" &&
src !== null &&
!Array.isArray(src) &&
("desktop" in src || "mobile" in src)
);
};
// 获取默认背景图片
export const getDefaultBackground = (): string => {
const src = backgroundWallpaper.src;
if (typeof src === "string") {
return src;
}
if (src && typeof src === "object" && !Array.isArray(src)) {
// 优先使用desktop,如果没有则使用mobile
const desktopSrc = src.desktop;
const mobileSrc = src.mobile;
if (typeof desktopSrc === "string") {
return desktopSrc;
}
if (typeof mobileSrc === "string") {
return mobileSrc;
}
}
return "";
};
// 检查是否为首页
export const isHomePage = (pathname: string): boolean => {
// 获取 base URL
const baseUrl = import.meta.env.BASE_URL || "/";
const baseUrlNoSlash = baseUrl.endsWith("/") ? baseUrl.slice(0, -1) : baseUrl;
if (pathname === baseUrl) return true;
if (pathname === baseUrlNoSlash) return true;
if (pathname === "/") return true;
return false;
};
// 获取横幅偏移量
export const getBannerOffset = (position = "center") => {
const bannerOffsetByPosition = {
top: "100vh",
center: "50vh",
bottom: "0",
};
return (
bannerOffsetByPosition[position as keyof typeof bannerOffsetByPosition] ||
"50vh"
);
};
|