hookehuyr

fix(router): 修复登录状态检查并优化滚动行为

移除未使用的滚动位置参数并简化滚动到顶部逻辑
添加已登录用户访问登录页时的重定向逻辑
...@@ -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,6 +28,7 @@ router.beforeEach(async (to, from, next) => { ...@@ -29,6 +28,7 @@ router.beforeEach(async (to, from, next) => {
29 } 28 }
30 29
31 // 检查用户是否已登录 30 // 检查用户是否已登录
31 + const currentUser = JSON.parse(localStorage.getItem('currentUser') || 'null')
32 32
33 // 登录权限检查 33 // 登录权限检查
34 const authResult = checkAuth(to) 34 const authResult = checkAuth(to)
...@@ -37,6 +37,12 @@ router.beforeEach(async (to, from, next) => { ...@@ -37,6 +37,12 @@ router.beforeEach(async (to, from, next) => {
37 return 37 return
38 } 38 }
39 39
40 + // 如果用户已经在登录页面,但已经登录了,重定向到首页
41 + if (to.path === '/login' && currentUser) {
42 + next('/')
43 + return
44 + }
45 +
40 next() 46 next()
41 }) 47 })
42 48
......