mirror of
https://github.com/Sagit-chu/flvx.git
synced 2026-10-03 01:06:36 +08:00
增加网站配置
This commit is contained in:
@@ -1,15 +1,153 @@
|
||||
import { getConfigByName, getConfigs } from '@/api';
|
||||
|
||||
export type SiteConfig = typeof siteConfig;
|
||||
|
||||
export const siteConfig = {
|
||||
name: "哆啦A梦",
|
||||
description: "哆啦A梦",
|
||||
navItems: [],
|
||||
navMenuItems: [],
|
||||
links: {
|
||||
github: "https://github.com/go-gost/gost",
|
||||
twitter: "",
|
||||
docs: "",
|
||||
discord: "",
|
||||
sponsor: "",
|
||||
},
|
||||
// 缓存相关常量
|
||||
const CACHE_PREFIX = 'vite_config_';
|
||||
|
||||
// 缓存策略:
|
||||
// - 长期有效,不设置过期时间
|
||||
// - 只在以下情况清除:1. 更新配置时 2. 退出登录时(safeLogout会清除localStorage)
|
||||
|
||||
// 立即从缓存获取配置(同步操作,避免闪烁)
|
||||
const getInitialConfig = () => {
|
||||
if (typeof window === 'undefined') {
|
||||
return {
|
||||
name: "哆啦A梦",
|
||||
version: "1.0.9",
|
||||
};
|
||||
}
|
||||
|
||||
const cachedAppName = localStorage.getItem(CACHE_PREFIX + 'app_name');
|
||||
if (cachedAppName) {
|
||||
return {
|
||||
name: cachedAppName,
|
||||
version: "1.0.9",
|
||||
};
|
||||
}
|
||||
return {
|
||||
name: "哆啦A梦",
|
||||
version: "1.0.9",
|
||||
};
|
||||
};
|
||||
|
||||
export const siteConfig = getInitialConfig();
|
||||
|
||||
// 缓存工具函数
|
||||
export const configCache = {
|
||||
// 获取缓存的配置
|
||||
get: (key: string): string | null => {
|
||||
const cacheKey = CACHE_PREFIX + key;
|
||||
return localStorage.getItem(cacheKey);
|
||||
},
|
||||
|
||||
// 设置缓存的配置
|
||||
set: (key: string, value: string): void => {
|
||||
const cacheKey = CACHE_PREFIX + key;
|
||||
localStorage.setItem(cacheKey, value);
|
||||
},
|
||||
|
||||
// 删除指定配置的缓存
|
||||
remove: (key: string): void => {
|
||||
const cacheKey = CACHE_PREFIX + key;
|
||||
localStorage.removeItem(cacheKey);
|
||||
},
|
||||
|
||||
// 清空所有配置缓存
|
||||
clear: (): void => {
|
||||
// 获取所有localStorage的key
|
||||
const keys = Object.keys(localStorage);
|
||||
keys.forEach(key => {
|
||||
if (key.startsWith(CACHE_PREFIX)) {
|
||||
localStorage.removeItem(key);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// 获取单个配置(优先从缓存)
|
||||
export const getCachedConfig = async (key: string): Promise<string | null> => {
|
||||
const cachedValue = configCache.get(key);
|
||||
if (cachedValue !== null) {
|
||||
return cachedValue;
|
||||
}
|
||||
|
||||
const response = await getConfigByName(key);
|
||||
if (response.code === 0 && response.data?.value) {
|
||||
const value = response.data.value;
|
||||
configCache.set(key, value);
|
||||
return value;
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
// 获取所有配置(优先从缓存)
|
||||
export const getCachedConfigs = async (): Promise<Record<string, string>> => {
|
||||
// 尝试从缓存获取所有配置
|
||||
const configKeys = ['app_name', 'site_title', 'site_description', 'contact_email', 'footer_text', 'logo_url'];
|
||||
const cachedConfigs: Record<string, string> = {};
|
||||
let hasCachedData = false;
|
||||
|
||||
configKeys.forEach(key => {
|
||||
const cachedValue = configCache.get(key);
|
||||
if (cachedValue !== null) {
|
||||
cachedConfigs[key] = cachedValue;
|
||||
hasCachedData = true;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
// 从API获取最新配置
|
||||
try {
|
||||
const response = await getConfigs();
|
||||
if (response.code === 0 && response.data) {
|
||||
const configs = response.data;
|
||||
// 将所有配置存入缓存
|
||||
Object.entries(configs).forEach(([key, value]) => {
|
||||
configCache.set(key, value as string);
|
||||
});
|
||||
return configs;
|
||||
}
|
||||
} catch (error) {
|
||||
// API失败时返回缓存的数据
|
||||
if (hasCachedData) {
|
||||
return cachedConfigs;
|
||||
}
|
||||
}
|
||||
|
||||
return {};
|
||||
};
|
||||
|
||||
// 动态更新网站配置
|
||||
export const updateSiteConfig = async () => {
|
||||
const appName = await getCachedConfig('app_name');
|
||||
if (appName && appName !== siteConfig.name) {
|
||||
siteConfig.name = appName;
|
||||
// 更新页面标题
|
||||
document.title = appName;
|
||||
}
|
||||
};
|
||||
|
||||
// 清除配置缓存的工具函数
|
||||
// 缓存清除时机:
|
||||
// 1. 配置更新时:调用此函数清除所有缓存
|
||||
// 2. 退出登录时:safeLogout()中的localStorage.clear()会自动清除
|
||||
export const clearConfigCache = (keys?: string[]) => {
|
||||
if (keys && keys.length > 0) {
|
||||
// 删除指定的配置缓存
|
||||
keys.forEach(key => configCache.remove(key));
|
||||
} else {
|
||||
// 清空所有配置缓存
|
||||
configCache.clear();
|
||||
}
|
||||
};
|
||||
|
||||
// 在页面加载时异步更新配置(如果有更新的话)
|
||||
if (typeof window !== 'undefined') {
|
||||
// 延迟执行,避免阻塞初始渲染
|
||||
setTimeout(() => {
|
||||
updateSiteConfig();
|
||||
}, 200);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user