125 lines
4.4 KiB
JavaScript
125 lines
4.4 KiB
JavaScript
|
|
import { createRouter, createWebHistory } from 'vue-router'
|
|||
|
|
import { useUserStore } from '@/stores/user'
|
|||
|
|
import { getToken } from '@gold/utils/token-manager'
|
|||
|
|
|
|||
|
|
const routes = [
|
|||
|
|
{
|
|||
|
|
path: '/',
|
|||
|
|
redirect: '/content-style/benchmark'
|
|||
|
|
},
|
|||
|
|
// { path: '/home', name: '首页', component: () => import('../views/home/Home.vue') },
|
|||
|
|
{
|
|||
|
|
path: '/content-style',
|
|||
|
|
name: '内容风格分析',
|
|||
|
|
children: [
|
|||
|
|
{ path: '', redirect: '/content-style/benchmark' },
|
|||
|
|
{ path: 'benchmark', name: '对标分析', component: () => import('../views/content-style/Benchmark.vue') },
|
|||
|
|
{ path: 'copywriting', name: '文案创作', component: () => import('../views/content-style/Copywriting.vue') },
|
|||
|
|
]
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
path: '/trends',
|
|||
|
|
name: '热点趋势分析',
|
|||
|
|
children: [
|
|||
|
|
{ path: '', redirect: '/trends/heat' },
|
|||
|
|
{ path: 'heat', name: '热度分析', component: () => import('../views/trends/Heat.vue') },
|
|||
|
|
{ path: 'forecast', name: '热点预测', component: () => import('../views/trends/Forecast.vue') },
|
|||
|
|
{ path: 'copywriting', name: '趋势文案创作', component: () => import('../views/trends/Copywriting.vue') },
|
|||
|
|
]
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
path: '/digital-human',
|
|||
|
|
name: '数字人',
|
|||
|
|
children: [
|
|||
|
|
{ path: '', redirect: '/digital-human/voice-copy' },
|
|||
|
|
{ path: 'voice-copy', name: '人声克隆', component: () => import('../views/dh/VoiceCopy.vue') },
|
|||
|
|
{ path: 'avatar', name: '生成数字人', component: () => import('../views/dh/Avatar.vue') },
|
|||
|
|
{ path: 'video', name: '数字人视频', component: () => import('../views/dh/Video.vue') },
|
|||
|
|
]
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
path: '/material',
|
|||
|
|
name: '素材库',
|
|||
|
|
children: [
|
|||
|
|
{ path: '', redirect: '/material/list' },
|
|||
|
|
{ path: 'list', name: '素材列表', component: () => import('../views/material/MaterialList.vue') },
|
|||
|
|
{ path: 'group', name: '素材分组', component: () => import('../views/material/MaterialGroup.vue') },
|
|||
|
|
]
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
path: '/system',
|
|||
|
|
name: '系统',
|
|||
|
|
children: [
|
|||
|
|
{ path: '', redirect: '/system/style-settings' },
|
|||
|
|
{ path: 'style-settings', name: '风格设置', component: () => import('../views/system/StyleSettings.vue') },
|
|||
|
|
]
|
|||
|
|
},
|
|||
|
|
{ path: '/realtime-hot', name: '实时热点推送', component: () => import('../views/realtime/RealtimeHot.vue') },
|
|||
|
|
{ path: '/capcut-import', name: '剪映导入', component: () => import('../views/capcut/CapcutImport.vue') },
|
|||
|
|
{ path: '/help', name: '帮助', component: () => import('../views/misc/Help.vue') },
|
|||
|
|
{ path: '/download', name: '下载', component: () => import('../views/misc/Download.vue') },
|
|||
|
|
{ path: '/settings/theme', name: '主题设置', component: () => import('../views/misc/Theme.vue') },
|
|||
|
|
]
|
|||
|
|
|
|||
|
|
const router = createRouter({
|
|||
|
|
history: createWebHistory(import.meta.env.BASE_URL),
|
|||
|
|
routes,
|
|||
|
|
})
|
|||
|
|
|
|||
|
|
// 用户信息初始化标志(确保只初始化一次)
|
|||
|
|
let userInfoInitialized = false
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 路由导航守卫:初始化用户信息 + 登录验证
|
|||
|
|
* 在首次路由跳转时,如果已登录(有 token),则获取用户信息
|
|||
|
|
* 如果未登录访问系统相关路由,则重定向到首页
|
|||
|
|
*/
|
|||
|
|
router.beforeEach(async (to, from, next) => {
|
|||
|
|
const userStore = useUserStore()
|
|||
|
|
|
|||
|
|
// 只在首次路由跳转时初始化用户信息
|
|||
|
|
if (!userInfoInitialized) {
|
|||
|
|
userInfoInitialized = true
|
|||
|
|
|
|||
|
|
const token = getToken()
|
|||
|
|
if (token) {
|
|||
|
|
try {
|
|||
|
|
// 如果 store 中已标记为登录,则获取用户信息
|
|||
|
|
if (userStore.isLoggedIn) {
|
|||
|
|
userStore.fetchUserInfo()
|
|||
|
|
} else {
|
|||
|
|
// 如果有 token 但 store 中未标记为登录,可能是刷新页面
|
|||
|
|
// 先标记为已登录,然后获取用户信息
|
|||
|
|
userStore.isLoggedIn = true
|
|||
|
|
userStore.fetchUserInfo()
|
|||
|
|
}
|
|||
|
|
} catch (error) {
|
|||
|
|
console.error('初始化用户信息失败:', error)
|
|||
|
|
// 不阻止路由跳转,继续执行
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 检查访问系统相关路由时是否已登录
|
|||
|
|
if (to.path.startsWith('/system')) {
|
|||
|
|
// 等待 store 从本地存储恢复完成(最多等待500ms)
|
|||
|
|
let waitCount = 0
|
|||
|
|
while (!userStore.isHydrated && waitCount < 50) {
|
|||
|
|
await new Promise(resolve => setTimeout(resolve, 10))
|
|||
|
|
waitCount++
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 如果未登录,重定向到首页
|
|||
|
|
if (!userStore.isLoggedIn) {
|
|||
|
|
next({ path: '/content-style/benchmark', replace: true })
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 继续路由跳转
|
|||
|
|
next()
|
|||
|
|
})
|
|||
|
|
|
|||
|
|
export default router
|
|||
|
|
|