// import router from './index' // 导入你的路由实例(与index.ts对应) // import { ElMessage } from 'element-plus' // import { useAuthStore } from '@/stores/auth' // 你的Pinia auth store路径 // // 路由前置守卫:每次路由跳转前执行 // router.beforeEach((to, from, next) => { // const authStore = useAuthStore() // 获取auth store实例 // const token = authStore.getToken() // 调用你的getToken()方法获取token // // 1. 如果访问登录页 // if (to.path === '/login') { // // 已登录状态下访问登录页,自动跳转到首页/dashboard // if (token) { // next('/dashboard') // ElMessage.success('已登录,自动跳转') // } else { // // 未登录,正常进入登录页 // next() // } // return // } // // 2. 访问非登录页,但未登录(无token) // if (!token) { // ElMessage.warning('请先登录') // next('/login') // 强制跳转到登录页 // return // } // // 3. 访问根路径,自动跳转到dashboard // if (to.path === '/') { // next('/dashboard') // return // } // // 4. 已登录且访问合法页面,正常放行 // next() // })