fix: 修复前端切换页面时logo跟标题闪烁的问题

This commit is contained in:
shaw
2025-12-19 22:33:36 +08:00
parent 14b155c66b
commit 22414326cc
5 changed files with 103 additions and 44 deletions

View File

@@ -5,8 +5,9 @@
import { defineStore } from 'pinia';
import { ref, computed } from 'vue';
import type { Toast, ToastType } from '@/types';
import type { Toast, ToastType, PublicSettings } from '@/types';
import { checkUpdates as checkUpdatesAPI, type VersionInfo, type ReleaseInfo } from '@/api/admin/system';
import { getPublicSettings as fetchPublicSettingsAPI } from '@/api/auth';
export const useAppStore = defineStore('app', () => {
// ==================== State ====================
@@ -16,6 +17,15 @@ export const useAppStore = defineStore('app', () => {
const loading = ref<boolean>(false);
const toasts = ref<Toast[]>([]);
// Public settings cache state
const publicSettingsLoaded = ref<boolean>(false);
const publicSettingsLoading = ref<boolean>(false);
const siteName = ref<string>('Sub2API');
const siteLogo = ref<string>('');
const siteVersion = ref<string>('');
const contactInfo = ref<string>('');
const apiBaseUrl = ref<string>('');
// Version cache state
const versionLoaded = ref<boolean>(false);
const versionLoading = ref<boolean>(false);
@@ -268,6 +278,59 @@ export const useAppStore = defineStore('app', () => {
hasUpdate.value = false;
}
// ==================== Public Settings Management ====================
/**
* Fetch public settings (uses cache unless force=true)
* @param force - Force refresh from API
*/
async function fetchPublicSettings(force = false): Promise<PublicSettings | null> {
// Return cached data if available and not forcing refresh
if (publicSettingsLoaded.value && !force) {
return {
registration_enabled: false,
email_verify_enabled: false,
turnstile_enabled: false,
turnstile_site_key: '',
site_name: siteName.value,
site_logo: siteLogo.value,
site_subtitle: '',
api_base_url: apiBaseUrl.value,
contact_info: contactInfo.value,
version: siteVersion.value,
};
}
// Prevent duplicate requests
if (publicSettingsLoading.value) {
return null;
}
publicSettingsLoading.value = true;
try {
const data = await fetchPublicSettingsAPI();
siteName.value = data.site_name || 'Sub2API';
siteLogo.value = data.site_logo || '';
siteVersion.value = data.version || '';
contactInfo.value = data.contact_info || '';
apiBaseUrl.value = data.api_base_url || '';
publicSettingsLoaded.value = true;
return data;
} catch (error) {
console.error('Failed to fetch public settings:', error);
return null;
} finally {
publicSettingsLoading.value = false;
}
}
/**
* Clear public settings cache
*/
function clearPublicSettingsCache(): void {
publicSettingsLoaded.value = false;
}
// ==================== Return Store API ====================
return {
@@ -277,6 +340,14 @@ export const useAppStore = defineStore('app', () => {
loading,
toasts,
// Public settings state
publicSettingsLoaded,
siteName,
siteLogo,
siteVersion,
contactInfo,
apiBaseUrl,
// Version state
versionLoaded,
versionLoading,
@@ -309,5 +380,9 @@ export const useAppStore = defineStore('app', () => {
// Version actions
fetchVersion,
clearVersionCache,
// Public settings actions
fetchPublicSettings,
clearPublicSettingsCache,
};
});