2025-11-10 00:59:40 +08:00
|
|
|
|
<script setup>
|
|
|
|
|
|
import { RouterView } from 'vue-router'
|
|
|
|
|
|
import { ref, onMounted } from 'vue'
|
|
|
|
|
|
import { theme } from 'ant-design-vue'
|
|
|
|
|
|
import SvgSprite from '@/components/icons/SvgSprite.vue'
|
2025-11-12 22:45:29 +08:00
|
|
|
|
import { useUserStore } from '@/stores/user'
|
2025-11-25 00:58:51 +08:00
|
|
|
|
import tokenManager from '@gold/utils/token-manager'
|
2025-11-10 00:59:40 +08:00
|
|
|
|
|
|
|
|
|
|
function readCssVar(name) {
|
|
|
|
|
|
return getComputedStyle(document.documentElement).getPropertyValue(name).trim() || undefined
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const themeToken = ref({
|
|
|
|
|
|
algorithm: theme.darkAlgorithm,
|
|
|
|
|
|
token: {
|
|
|
|
|
|
colorPrimary: '#3B82F6',
|
|
|
|
|
|
colorInfo: '#1A66E0',
|
|
|
|
|
|
colorBgBase: '#0D0D0D',
|
|
|
|
|
|
colorBgContainer: '#1A1A1A',
|
|
|
|
|
|
colorTextBase: '#F2F2F2',
|
|
|
|
|
|
colorTextSecondary: '#CCCCCC',
|
|
|
|
|
|
colorBorder: '#333333',
|
|
|
|
|
|
borderRadius: 6,
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
|
2025-11-12 22:45:29 +08:00
|
|
|
|
onMounted(async () => {
|
2025-11-10 00:59:40 +08:00
|
|
|
|
// 运行时从 :root 读取,若存在则覆盖默认值
|
|
|
|
|
|
const next = { ...themeToken.value.token }
|
|
|
|
|
|
next.colorPrimary = readCssVar('--color-primary') || next.colorPrimary
|
|
|
|
|
|
next.colorInfo = readCssVar('--color-blue') || next.colorInfo
|
|
|
|
|
|
next.colorBgBase = readCssVar('--color-bg') || next.colorBgBase
|
|
|
|
|
|
next.colorBgContainer = readCssVar('--color-surface') || next.colorBgContainer
|
|
|
|
|
|
next.colorTextBase = readCssVar('--color-text') || next.colorTextBase
|
|
|
|
|
|
next.colorTextSecondary = readCssVar('--color-text-secondary') || next.colorTextSecondary
|
|
|
|
|
|
next.colorBorder = readCssVar('--color-border') || next.colorBorder
|
|
|
|
|
|
themeToken.value = { algorithm: theme.darkAlgorithm, token: next }
|
2025-11-12 22:45:29 +08:00
|
|
|
|
|
|
|
|
|
|
// 检查登录状态:如果有token但store中未标记为登录,则恢复登录状态
|
|
|
|
|
|
const userStore = useUserStore()
|
2025-11-25 00:58:51 +08:00
|
|
|
|
|
2025-11-12 22:45:29 +08:00
|
|
|
|
// 等待store从本地存储恢复完成(最多等待500ms)
|
|
|
|
|
|
let waitCount = 0
|
|
|
|
|
|
while (!userStore.isHydrated && waitCount < 50) {
|
|
|
|
|
|
await new Promise(resolve => setTimeout(resolve, 10))
|
|
|
|
|
|
waitCount++
|
|
|
|
|
|
}
|
2025-11-25 00:58:51 +08:00
|
|
|
|
|
|
|
|
|
|
const token = tokenManager.getAccessToken()
|
2025-11-12 22:45:29 +08:00
|
|
|
|
if (token) {
|
|
|
|
|
|
// 如果有token但未登录,可能是刷新页面,需要恢复登录状态
|
|
|
|
|
|
if (!userStore.isLoggedIn) {
|
|
|
|
|
|
userStore.isLoggedIn = true
|
|
|
|
|
|
// 尝试获取用户信息
|
|
|
|
|
|
try {
|
|
|
|
|
|
await userStore.fetchUserInfo()
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('初始化用户信息失败:', error)
|
|
|
|
|
|
}
|
|
|
|
|
|
} else if (!userStore.nickname && !userStore.userId) {
|
|
|
|
|
|
// 如果已登录但没有用户信息,尝试获取
|
|
|
|
|
|
try {
|
|
|
|
|
|
await userStore.fetchUserInfo()
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('获取用户信息失败:', error)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-11-10 00:59:40 +08:00
|
|
|
|
})
|
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
|
|
<template>
|
|
|
|
|
|
<a-config-provider :theme="themeToken">
|
2025-11-25 01:24:12 +08:00
|
|
|
|
<SvgSprite />
|
|
|
|
|
|
<RouterView />
|
2025-11-10 00:59:40 +08:00
|
|
|
|
</a-config-provider>
|
|
|
|
|
|
</template>
|
|
|
|
|
|
|
2025-11-25 01:24:12 +08:00
|
|
|
|
<style>
|
|
|
|
|
|
/* 全局样式保持不变 */
|
2025-11-10 00:59:40 +08:00
|
|
|
|
</style>
|