import {createRouter, createWebHistory} from 'vue-router' import {useUserStore} from '@/stores/user' import {ElMessage} from 'element-plus' import NProgress from 'nprogress' const routes = [ { path: '/login', name: 'Login', component: () => import('@/views/Login.vue'), meta: { title: '登录 - 炬融圈', requiresAuth: false } }, { path: '/', component: () => import('@/layout/Layout.vue'), redirect: '/dashboard', meta: { requiresAuth: true }, children: [ { path: 'dashboard', name: 'Dashboard', component: () => import('@/views/Dashboard.vue'), meta: { title: '仪表盘 - 炬融圈', icon: 'Odometer' } }, { path: 'users', name: 'Users', component: () => import('@/views/Users.vue'), meta: { title: '用户管理 - 炬融圈', icon: 'User', requiresAdmin: true } }, { path: 'user-audit', name: 'UserAudit', component: () => import('@/views/UserAudit.vue'), meta: { title: '用户审核 - 炬融圈', icon: 'DocumentChecked', requiresAdmin: true } }, { path: 'points', name: 'Points', component: () => import('@/views/Points.vue'), meta: { title: '积分管理 - 积分商城管理系统', icon: 'Coin', requiresAdmin: true } }, { path: 'transfers', name: 'Transfers', component: () => import('@/views/Transfers.vue'), meta: { title: '转账管理 - 炬融圈', icon: 'Money', } }, { path: 'beans', name: 'Beans', component: () => import('@/views/Beans.vue'), meta: { title: '融豆管理 - 炬融圈', icon: 'Promotion', requiresAdmin: true } }, { path: 'projects', name: 'Projects', component: () => import('@/views/Project.vue'), meta: { title: '项目管理 - 炬融圈', icon: 'Project', requiresAdmin: true } }, { path: 'profile', name: 'Profile', component: () => import('@/views/Profile.vue'), meta: { title: '个人资料 - 炬融圈', icon: 'UserFilled' } }, { path: 'program', name: 'Program', component: () => import('@/views/Program.vue'), meta: { title: '项目管理 - 炬融圈', icon: 'Project' } }, { path: 'chat', name: 'Chat', component: () => import('@/views/Chat.vue'), meta: { title: '聊天 - 炬融圈', icon: 'ChatDotRound' } } ] }, { path: '/404', name: 'NotFound', component: () => import('@/views/404.vue'), meta: { title: '页面未找到 - 炬融圈' } }, { path: '/:pathMatch(.*)*', redirect: '/404' } ] const router = createRouter({ history: createWebHistory('/admin/'), routes }) // 路由守卫 router.beforeEach(async (to, from, next) => { NProgress.start() const userStore = useUserStore() // 设置页面标题 if (to.meta.title) { document.title = to.meta.title } // 检查是否需要认证 if (to.meta.requiresAuth !== false) { if (!userStore.isAuthenticated) { // 只在没有token或用户信息时才尝试从本地存储恢复登录状态 const token = localStorage.getItem('admin_token') const userStr = localStorage.getItem('admin_user') if (token && userStr) { try { const user = JSON.parse(userStr) userStore.token = token userStore.user = user } catch (error) { console.error('解析用户信息失败:', error) localStorage.removeItem('admin_token') localStorage.removeItem('admin_user') } } if (!userStore.isAuthenticated) { next('/login') return } } // 检查管理员权限 if (to.meta.requiresAdmin && !userStore.isAdmin) { ElMessage.error('您没有权限访问此页面') next('/dashboard') return } } // 如果已登录用户访问登录页,重定向到仪表盘 if (to.name === 'Login' && userStore.isAuthenticated) { next('/dashboard') return } next() }) router.afterEach(() => { NProgress.done() }) export default router