ForcePilot/web/src/router/index.js

174 lines
4.7 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';
import { useAgentStore } from '@/stores/agent';
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: '/agent',
name: 'AgentMain',
2025-05-04 21:04:01 +08:00
component: AppLayout,
children: [
{
path: '',
name: 'AgentComp',
component: () => import('../views/AgentView.vue'),
meta: { keepAlive: true, requiresAuth: true, requiresAdmin: true }
}
]
2025-05-02 23:56:59 +08:00
},
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
]
},
{
path: '/dashboard',
name: 'dashboard',
component: AppLayout,
children: [
{
path: '',
name: 'DashboardComp',
component: () => import('../views/DashboardView.vue'),
meta: { keepAlive: false, requiresAuth: true, requiresAdmin: true }
}
]
},
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 agentStore = useAgentStore();
// 等待 store 初始化完成
if (!agentStore.isInitialized) {
await agentStore.initialize();
2025-05-02 23:56:59 +08:00
}
const defaultAgent = agentStore.defaultAgent;
if (defaultAgent && defaultAgent.id) {
next(`/agent/${defaultAgent.id}`);
2025-05-02 23:56:59 +08:00
} else {
// 如果没有默认智能体,可以考虑跳转到第一个可用的智能体,或者一个特定的页面
const agentIds = Object.keys(agentStore.agents);
if (agentIds.length > 0) {
next(`/agent/${agentIds[0]}`);
} else {
// 没有可用的智能体,跳转到首页
next('/');
}
2025-05-02 23:56:59 +08:00
}
} catch (error) {
console.error('获取智能体信息失败:', error);
next('/');
}
return;
}
// 如果用户已登录但访问登录页
if (to.path === '/login' && isLoggedIn) {
next('/');
return;
}
// 其他情况正常导航
next();
});
export default router