92 lines
1.9 KiB
JavaScript
92 lines
1.9 KiB
JavaScript
import { createRouter, createWebHistory } from 'vue-router'
|
|
|
|
// 路由配置
|
|
const routes = [
|
|
{
|
|
path: '/',
|
|
name: 'Home',
|
|
component: () => import('../views/HomeView.vue'),
|
|
meta: { requiresAuth: true }
|
|
},
|
|
{
|
|
path: '/settings',
|
|
name: 'Settings',
|
|
component: () => import('../views/SettingsView.vue'),
|
|
meta: { requiresAuth: true }
|
|
},
|
|
{
|
|
path: '/auth',
|
|
name: 'Auth',
|
|
component: () => import('../views/AuthView.vue'),
|
|
meta: { requiresAuth: false }
|
|
},
|
|
{
|
|
path: '/conversations',
|
|
name: 'Conversations',
|
|
component: () => import('../views/ConversationView.vue'),
|
|
meta: { requiresAuth: true }
|
|
},
|
|
{
|
|
path: '/tools',
|
|
name: 'Tools',
|
|
component: () => import('../views/ToolsView.vue'),
|
|
meta: { requiresAuth: true }
|
|
},
|
|
// Agent 管理
|
|
{
|
|
path: '/agents',
|
|
name: 'Agents',
|
|
component: () => import('../views/AgentsView.vue'),
|
|
meta: { requiresAuth: true }
|
|
},
|
|
// 聊天室
|
|
{
|
|
path: '/rooms',
|
|
name: 'Rooms',
|
|
component: () => import('../views/RoomView.vue'),
|
|
meta: { requiresAuth: true }
|
|
},
|
|
// 首页重定向
|
|
{
|
|
path: '/home',
|
|
redirect: '/'
|
|
},
|
|
// 404 重定向
|
|
{
|
|
path: '/:pathMatch(.*)*',
|
|
redirect: '/'
|
|
}
|
|
]
|
|
|
|
// 创建路由实例
|
|
const router = createRouter({
|
|
history: createWebHistory(import.meta.env.BASE_URL),
|
|
routes,
|
|
scrollBehavior(to, from, savedPosition) {
|
|
if (savedPosition) {
|
|
return savedPosition
|
|
} else {
|
|
return { top: 0 }
|
|
}
|
|
}
|
|
})
|
|
|
|
// 路由守卫
|
|
router.beforeEach((to, from, next) => {
|
|
const token = localStorage.getItem('access_token')
|
|
const requiresAuth = to.matched.some(record => record.meta.requiresAuth)
|
|
|
|
if (requiresAuth && !token) {
|
|
next({
|
|
name: 'Auth',
|
|
query: { redirect: to.fullPath }
|
|
})
|
|
} else if (to.name === 'Auth' && token) {
|
|
next({ name: 'Home' })
|
|
} else {
|
|
next()
|
|
}
|
|
})
|
|
|
|
export default router
|