优化登陆态

This commit is contained in:
qaq
2025-06-19 17:19:39 +08:00
parent 61c6a287e7
commit f6a777da43
2 changed files with 70 additions and 6 deletions
+27 -6
View File
@@ -1,5 +1,6 @@
import Vue from 'vue';
import VueRouter from 'vue-router';
import { isLoggedIn } from '@/utils/auth';
// 路由懒加载
@@ -18,7 +19,13 @@ Vue.use(VueRouter);
// 路由配置
const routes = [
{path: '/', redirect: '/login'},
{
path: '/',
redirect: () => {
// 如果用户已登录,重定向到首页,否则重定向到登录页
return isLoggedIn() ? '/index' : '/login';
}
},
{path: '/login', component: Login},
{path: '/change-password', component: ChangeAccountPassword},
{
@@ -40,12 +47,26 @@ const router = new VueRouter({
})
router.beforeEach((to, from, next) => {
if (to.path === '/login' || to.path === '/change-password') return next();
const tokenStr = localStorage.getItem('token')
if (!tokenStr) {
return next('/login')
// 检查用户是否已登录
const userIsLoggedIn = isLoggedIn();
// 如果用户已登录且访问登录页面,直接跳转到首页
if (userIsLoggedIn && to.path === '/login') {
return next('/index');
}
next()
// 如果访问登录或修改密码页面,直接放行
if (to.path === '/login' || to.path === '/change-password') {
return next();
}
// 如果用户未登录且访问需要登录的页面,跳转到登录页面
if (!userIsLoggedIn) {
return next('/login');
}
// 其他情况正常放行
next();
})