fix(router): 修复登录状态检查并优化滚动行为
移除未使用的滚动位置参数并简化滚动到顶部逻辑 添加已登录用户访问登录页时的重定向逻辑
Showing
1 changed file
with
9 additions
and
3 deletions
| ... | @@ -12,9 +12,8 @@ import { checkWxAuth, checkAuth } from './guards' | ... | @@ -12,9 +12,8 @@ import { checkWxAuth, checkAuth } from './guards' |
| 12 | const router = createRouter({ | 12 | const router = createRouter({ |
| 13 | history: createWebHashHistory(import.meta.env.VITE_BASE || '/'), | 13 | history: createWebHashHistory(import.meta.env.VITE_BASE || '/'), |
| 14 | routes, | 14 | routes, |
| 15 | - scrollBehavior(to, from, savedPosition) { | 15 | + scrollBehavior() { |
| 16 | // 每次路由切换后,页面滚动到顶部 | 16 | // 每次路由切换后,页面滚动到顶部 |
| 17 | - // return savedPosition || { top: 0, left: 0 } | ||
| 18 | return { top: 0, left: 0 } | 17 | return { top: 0, left: 0 } |
| 19 | }, | 18 | }, |
| 20 | }) | 19 | }) |
| ... | @@ -29,13 +28,20 @@ router.beforeEach(async (to, from, next) => { | ... | @@ -29,13 +28,20 @@ router.beforeEach(async (to, from, next) => { |
| 29 | } | 28 | } |
| 30 | 29 | ||
| 31 | // 检查用户是否已登录 | 30 | // 检查用户是否已登录 |
| 32 | - | 31 | + const currentUser = JSON.parse(localStorage.getItem('currentUser') || 'null') |
| 32 | + | ||
| 33 | // 登录权限检查 | 33 | // 登录权限检查 |
| 34 | const authResult = checkAuth(to) | 34 | const authResult = checkAuth(to) |
| 35 | if (authResult !== true) { | 35 | if (authResult !== true) { |
| 36 | next(authResult) | 36 | next(authResult) |
| 37 | return | 37 | return |
| 38 | } | 38 | } |
| 39 | + | ||
| 40 | + // 如果用户已经在登录页面,但已经登录了,重定向到首页 | ||
| 41 | + if (to.path === '/login' && currentUser) { | ||
| 42 | + next('/') | ||
| 43 | + return | ||
| 44 | + } | ||
| 39 | 45 | ||
| 40 | next() | 46 | next() |
| 41 | }) | 47 | }) | ... | ... |
-
Please register or login to post a comment