panel/src/router/index.js

44 lines
1.2 KiB
JavaScript
Raw Normal View History

2025-09-27 16:42:15 +08:00
import { createRouter, createWebHistory } from 'vue-router'
2025-11-18 10:40:54 +08:00
import { ElMessage } from 'element-plus'
2025-09-27 16:42:15 +08:00
import Login from '../views/Login.vue'
import Dashboard from '../views/Dashboard.vue'
2025-11-18 10:40:54 +08:00
import WxCallback from '../views/wx_callback.vue'
import { isLoggedIn, clearTokens } from '../utils/auth'
2025-09-27 16:42:15 +08:00
const routes = [
{ path: '/', redirect: '/login' },
2025-11-18 10:40:54 +08:00
{ path: '/login', component: Login, meta: { public: true } },
{ path: '/dashboard', component: Dashboard },
{ path: '/wx_callback', component: WxCallback, meta: { public: true } }
2025-09-27 16:42:15 +08:00
]
const router = createRouter({
history: createWebHistory(),
routes
})
2025-11-18 10:40:54 +08:00
// 路由守卫:无效或缺失 token 时跳转登录
router.beforeEach((to, from, next) => {
// 标记为 public 的路由直接放行
if (to.meta && to.meta.public) return next()
if (to.path === '/login') {
// 已登录且未过期,访问登录页则跳转首页
if (isLoggedIn()) {
ElMessage.info('您已登录,正在跳转到首页...')
return next('/dashboard')
}
return next()
}
// 其他受保护路由:检查登录状态
if (!isLoggedIn()) {
ElMessage.warning('请先登录')
return next('/login')
}
return next()
})
2025-09-27 16:42:15 +08:00
export default router