index.js
1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
* @Date: 2025-03-20 20:36:36
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2025-08-26 09:43:50
* @FilePath: /mlaj/src/router/index.js
* @Description: 路由实例创建和导出
*/
import { createRouter, createWebHashHistory } from 'vue-router'
import { routes } from './routes'
import { checkWxAuth, checkAuth } from './guards'
const router = createRouter({
history: createWebHashHistory(import.meta.env.VITE_BASE || '/'),
routes,
scrollBehavior() {
// 每次路由切换后,页面滚动到顶部
return { top: 0, left: 0 }
},
})
// 导航守卫
router.beforeEach(async (to, from, next) => {
// 微信授权检查
const wxAuthResult = await checkWxAuth()
if (wxAuthResult !== true) {
next(wxAuthResult)
return
}
// 检查用户是否已登录
const currentUser = JSON.parse(localStorage.getItem('currentUser') || 'null')
// 登录权限检查
const authResult = checkAuth(to)
if (authResult !== true) {
next(authResult)
return
}
// 如果用户已经在登录页面,但已经登录了,重定向到首页
if (to.path === '/login' && currentUser) {
next('/')
return
}
next()
})
export default router