ForcePilot/web/src/router/index.js

171 lines
4.6 KiB
JavaScript
Raw Normal View History

import { createRouter, createWebHistory } from 'vue-router'
2024-07-14 16:42:38 +08:00
import AppLayout from '@/layouts/AppLayout.vue';
import BlankLayout from '@/layouts/BlankLayout.vue';
2025-05-02 23:56:59 +08:00
import { useUserStore } from '@/stores/user';
const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
routes: [
{
path: '/',
2025-02-19 20:45:44 +08:00
name: 'main',
2024-07-14 16:42:38 +08:00
component: BlankLayout,
2025-03-29 19:14:03 +08:00
children: [
{
2024-07-14 16:42:38 +08:00
path: '',
name: 'Home',
2024-07-16 18:14:27 +08:00
component: () => import('../views/HomeView.vue'),
2025-05-02 23:56:59 +08:00
meta: { keepAlive: true, requiresAuth: false }
2024-07-14 16:42:38 +08:00
}
]
},
2025-05-02 23:56:59 +08:00
{
path: '/login',
name: 'login',
component: () => import('../views/LoginView.vue'),
meta: { requiresAuth: false }
},
{
path: '/chat',
name: 'chat',
2024-07-14 16:42:38 +08:00
component: AppLayout,
children: [
{
path: '',
2025-02-19 20:45:44 +08:00
name: 'ChatComp',
2024-07-16 18:14:27 +08:00
component: () => import('../views/ChatView.vue'),
2025-05-02 23:56:59 +08:00
meta: { keepAlive: true, requiresAuth: true, requiresAdmin: true }
2024-07-14 16:42:38 +08:00
}
]
},
2025-05-02 23:56:59 +08:00
{
path: '/agent',
name: 'AgentMain',
component: () => import('../views/AgentView.vue'),
meta: { keepAlive: true, requiresAuth: true, requiresAdmin: true }
},
2025-03-29 19:14:03 +08:00
{
path: '/agent/:agent_id',
name: 'AgentSinglePage',
2025-04-03 20:45:58 +08:00
component: () => import('../views/AgentSingleView.vue'),
2025-05-02 23:56:59 +08:00
meta: { requiresAuth: true }
2025-03-29 19:14:03 +08:00
},
2024-09-01 18:10:50 +08:00
{
path: '/graph',
name: 'graph',
component: AppLayout,
children: [
{
path: '',
2025-02-19 20:45:44 +08:00
name: 'GraphComp',
2024-09-01 18:10:50 +08:00
component: () => import('../views/GraphView.vue'),
2025-05-02 23:56:59 +08:00
meta: { keepAlive: false, requiresAuth: true, requiresAdmin: true }
2024-09-01 18:10:50 +08:00
}
]
},
2024-07-14 16:42:38 +08:00
{
2024-07-16 18:14:27 +08:00
path: '/database',
name: 'database',
2024-07-14 16:42:38 +08:00
component: AppLayout,
children: [
{
path: '',
2025-02-19 20:45:44 +08:00
name: 'DatabaseComp',
2024-07-16 18:14:27 +08:00
component: () => import('../views/DataBaseView.vue'),
2025-05-02 23:56:59 +08:00
meta: { keepAlive: true, requiresAuth: true, requiresAdmin: true }
2024-07-16 18:14:27 +08:00
},
{
path: ':database_id',
2025-02-19 20:45:44 +08:00
name: 'DatabaseInfoComp',
2024-07-16 18:14:27 +08:00
component: () => import('../views/DataBaseInfoView.vue'),
2025-05-02 23:56:59 +08:00
meta: { keepAlive: false, requiresAuth: true, requiresAdmin: true }
2024-07-14 16:42:38 +08:00
}
]
},
{
path: '/setting',
name: 'setting',
component: AppLayout,
children: [
{
path: '',
2025-02-19 20:45:44 +08:00
name: 'SettingComp',
component: () => import('../views/SettingView.vue'),
2025-05-02 23:56:59 +08:00
meta: { keepAlive: true, requiresAuth: true, requiresAdmin: true }
2025-03-28 00:48:33 +08:00
}
2024-09-14 02:47:04 +08:00
]
},
2024-07-14 16:42:38 +08:00
{
path: '/:pathMatch(.*)*',
name: 'NotFound',
2025-05-02 23:56:59 +08:00
component: () => import('../views/EmptyView.vue'),
meta: { requiresAuth: false }
2024-09-14 02:47:04 +08:00
},
]
})
2025-05-02 23:56:59 +08:00
// 全局前置守卫
router.beforeEach(async (to, from, next) => {
// 检查路由是否需要认证
const requiresAuth = to.matched.some(record => record.meta.requiresAuth === true);
const requiresAdmin = to.matched.some(record => record.meta.requiresAdmin);
const userStore = useUserStore();
const isLoggedIn = userStore.isLoggedIn;
const isAdmin = userStore.isAdmin;
// 如果路由需要认证但用户未登录
if (requiresAuth && !isLoggedIn) {
// 保存尝试访问的路径,登录后跳转
sessionStorage.setItem('redirect', to.fullPath);
next('/login');
return;
}
// 如果路由需要管理员权限但用户不是管理员
if (requiresAdmin && !isAdmin) {
// 如果是普通用户,跳转到默认智能体页面
try {
// 先尝试获取默认智能体
const response = await fetch('/api/chat/default_agent');
if (response.ok) {
const data = await response.json();
if (data.default_agent_id) {
// 如果存在默认智能体,直接跳转
next(`/agent/${data.default_agent_id}`);
return;
}
}
// 如果没有默认智能体,则获取第一个可用智能体
const agentResponse = await fetch('/api/chat/agent');
if (agentResponse.ok) {
const agentData = await agentResponse.json();
if (agentData.agents && agentData.agents.length > 0) {
const firstAgentId = agentData.agents[0].name;
next(`/agent/${firstAgentId}`);
} else {
next('/');
}
} else {
next('/');
}
} catch (error) {
console.error('获取智能体信息失败:', error);
next('/');
}
return;
}
// 如果用户已登录但访问登录页
if (to.path === '/login' && isLoggedIn) {
next('/');
return;
}
// 其他情况正常导航
next();
});
export default router