import { createRouter, createWebHistory } from 'vue-router' import { useAuthStore } from '@/stores/auth' const router = createRouter({ history: createWebHistory(import.meta.env.BASE_URL), routes: [ { path: '/login', component: () => import('@/views/auth/LoginView.vue'), }, { path: '/register', component: () => import('@/views/auth/RegisterView.vue'), }, { path: '/forgot-password', component: () => import('@/views/auth/ForgotPasswordView.vue'), }, { path: '/', component: () => import('@/views/layout/MainLayout.vue'), meta: { requiresAuth: true }, children: [ { path: '', redirect: '/dashboard', }, { path: 'dashboard', component: () => import('@/views/dashboard/DashboardView.vue'), }, { path: 'apps', component: () => import('@/views/apps/AppListView.vue'), }, { path: 'apps/:id', component: () => import('@/views/apps/AppDetailView.vue'), }, { path: 'apps/:appId/im', component: () => import('@/views/im/ImManagementView.vue'), }, { path: 'apps/:appId/update', component: () => import('@/views/update/VersionManagementView.vue'), }, { path: 'accounts', component: () => import('@/views/accounts/SubAccountView.vue'), }, ], }, ], }) router.beforeEach((to) => { const auth = useAuthStore() if (to.meta.requiresAuth && !auth.token) { return '/login' } if ((to.path === '/login' || to.path === '/register') && auth.token) { return '/dashboard' } }) export default router