feat: 功能
This commit is contained in:
@@ -5,6 +5,8 @@ import SidebarNav from './components/SidebarNav.vue'
|
||||
import TopNav from './components/TopNav.vue'
|
||||
import { theme } from 'ant-design-vue'
|
||||
import SvgSprite from '@/components/icons/SvgSprite.vue'
|
||||
import { useUserStore } from '@/stores/user'
|
||||
import { getToken } from '@gold/utils/token-manager'
|
||||
|
||||
function readCssVar(name) {
|
||||
return getComputedStyle(document.documentElement).getPropertyValue(name).trim() || undefined
|
||||
@@ -24,7 +26,7 @@ const themeToken = ref({
|
||||
}
|
||||
})
|
||||
|
||||
onMounted(() => {
|
||||
onMounted(async () => {
|
||||
// 运行时从 :root 读取,若存在则覆盖默认值
|
||||
const next = { ...themeToken.value.token }
|
||||
next.colorPrimary = readCssVar('--color-primary') || next.colorPrimary
|
||||
@@ -35,6 +37,37 @@ onMounted(() => {
|
||||
next.colorTextSecondary = readCssVar('--color-text-secondary') || next.colorTextSecondary
|
||||
next.colorBorder = readCssVar('--color-border') || next.colorBorder
|
||||
themeToken.value = { algorithm: theme.darkAlgorithm, token: next }
|
||||
|
||||
// 检查登录状态:如果有token但store中未标记为登录,则恢复登录状态
|
||||
const userStore = useUserStore()
|
||||
|
||||
// 等待store从本地存储恢复完成(最多等待500ms)
|
||||
let waitCount = 0
|
||||
while (!userStore.isHydrated && waitCount < 50) {
|
||||
await new Promise(resolve => setTimeout(resolve, 10))
|
||||
waitCount++
|
||||
}
|
||||
|
||||
const token = getToken()
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
</script>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user