fix: 修复系统设置的动态注入问题
- 创建 useSiteSettings composable 在应用启动时加载系统设置 - 从 window.__SITE_CONFIG__(后端注入)和 localStorage 初始化设置 - 修复 favicon 更新逻辑,正确移除旧 favicon 并添加新的 - 在路由守卫中设置页面标题,格式为 '页面名 - 网站名' - 简化 admin.vue 和 admin-sidebar 组件,使用统一的设置管理 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -1,13 +1,11 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, onMounted, onUnmounted } from 'vue'
|
||||
import { computed } from 'vue'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import { useRouter } from 'vue-router'
|
||||
|
||||
import AdminSidebar from '@/components/admin-sidebar/index.vue'
|
||||
import LanguageChange from '@/components/language-change.vue'
|
||||
import ToggleTheme from '@/components/toggle-theme.vue'
|
||||
import api from '@/services/api'
|
||||
import { getFileUrl } from '@/utils/config'
|
||||
|
||||
const router = useRouter()
|
||||
const { t } = useI18n()
|
||||
@@ -73,74 +71,6 @@ const breadcrumbs = computed(() => {
|
||||
|
||||
return crumbs
|
||||
})
|
||||
|
||||
function getAssetUrl(path: string) {
|
||||
return getFileUrl(path)
|
||||
}
|
||||
|
||||
function updateFavicon(favicon: string) {
|
||||
if (!favicon)
|
||||
return
|
||||
const link: HTMLLinkElement = document.querySelector('link[rel*=\'icon\']') || document.createElement('link')
|
||||
link.type = 'image/x-icon'
|
||||
link.rel = 'shortcut icon'
|
||||
link.href = getAssetUrl(favicon)
|
||||
document.getElementsByTagName('head')[0].appendChild(link)
|
||||
}
|
||||
|
||||
function updateSiteName(name: string) {
|
||||
if (name)
|
||||
document.title = name
|
||||
}
|
||||
|
||||
function applySettings(settings: { site_name?: string, site_logo?: string, site_favicon?: string }) {
|
||||
if (settings.site_favicon)
|
||||
updateFavicon(settings.site_favicon)
|
||||
if (settings.site_name)
|
||||
updateSiteName(settings.site_name)
|
||||
}
|
||||
|
||||
function handleSettingsChange(event: CustomEvent) {
|
||||
applySettings(event.detail)
|
||||
}
|
||||
|
||||
async function loadSystemSettings() {
|
||||
const storedSettings = localStorage.getItem('systemSettings')
|
||||
if (storedSettings) {
|
||||
try {
|
||||
applySettings(JSON.parse(storedSettings))
|
||||
}
|
||||
catch (e) {
|
||||
console.error('Failed to parse systemSettings', e)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
const data = await api.get<any>('/dev/system-settings')
|
||||
if (data) {
|
||||
const settings = {
|
||||
site_name: data.site_name || '',
|
||||
site_logo: data.site_logo || '',
|
||||
site_favicon: data.site_favicon || '',
|
||||
}
|
||||
localStorage.setItem('systemSettings', JSON.stringify(settings))
|
||||
window.dispatchEvent(new CustomEvent('system-settings-changed', { detail: settings }))
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
console.error('加载系统设置失败:', error)
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
loadSystemSettings()
|
||||
window.addEventListener('system-settings-changed', handleSettingsChange as EventListener)
|
||||
})
|
||||
|
||||
onUnmounted(() => {
|
||||
window.removeEventListener('system-settings-changed', handleSettingsChange as EventListener)
|
||||
})
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@@ -179,4 +109,4 @@ onUnmounted(() => {
|
||||
</main>
|
||||
</UiSidebarInset>
|
||||
</UiSidebarProvider>
|
||||
</template>
|
||||
</template>
|
||||
Reference in New Issue
Block a user