xto-fronted 0.3.6 → 0.3.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index-CD364XjV.js +142 -0
- package/dist/index-D88fiqXR.js +475 -0
- package/dist/index-DWy_UGhI.js +345 -0
- package/dist/index-DcvRPHuy.js +372 -0
- package/dist/index-DdC1uV2v.js +1700 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.es.js +19 -18
- package/dist/index.umd.js +1 -1
- package/dist/router/guards.d.ts +16 -0
- package/package.json +1 -1
- package/src/index.ts +1 -0
- package/src/router/guards.ts +133 -0
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 路由守卫设置
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type { Router } from 'vue-router'
|
|
6
|
+
import { getToken } from '@/utils/auth'
|
|
7
|
+
import { useUserStore } from '@/stores/user'
|
|
8
|
+
import { useMenuStore } from '@/stores/menu'
|
|
9
|
+
import { useAppStore } from '@/stores/app'
|
|
10
|
+
import { getUserInfo } from '@/api/auth'
|
|
11
|
+
import { getMenuTree } from '@/api/system'
|
|
12
|
+
|
|
13
|
+
// 白名单路由
|
|
14
|
+
const defaultWhiteList = ['/login', '/404', '/403']
|
|
15
|
+
|
|
16
|
+
interface RouterGuardOptions {
|
|
17
|
+
// 白名单路由
|
|
18
|
+
whiteList?: string[]
|
|
19
|
+
// 登录页路径
|
|
20
|
+
loginPath?: string
|
|
21
|
+
// 首页路径
|
|
22
|
+
homePath?: string
|
|
23
|
+
// 获取用户信息的回调(可选,默认调用 API)
|
|
24
|
+
fetchUserInfo?: () => Promise<any>
|
|
25
|
+
// 获取菜单的回调(可选,默认调用 API)
|
|
26
|
+
fetchMenu?: () => Promise<any>
|
|
27
|
+
// 登录成功后的回调
|
|
28
|
+
onLoginSuccess?: () => void
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* 设置路由守卫
|
|
33
|
+
* @param router 路由实例
|
|
34
|
+
* @param options 配置选项
|
|
35
|
+
*/
|
|
36
|
+
export function setupRouterGuards(router: Router, options: RouterGuardOptions = {}) {
|
|
37
|
+
const whiteList = options.whiteList || defaultWhiteList
|
|
38
|
+
const loginPath = options.loginPath || '/login'
|
|
39
|
+
const homePath = options.homePath || '/'
|
|
40
|
+
|
|
41
|
+
router.beforeEach(async (to, _from, next) => {
|
|
42
|
+
// 启动进度条(可选)
|
|
43
|
+
// NProgress.start()
|
|
44
|
+
|
|
45
|
+
const appStore = useAppStore()
|
|
46
|
+
const userStore = useUserStore()
|
|
47
|
+
const menuStore = useMenuStore()
|
|
48
|
+
|
|
49
|
+
// 初始化主题
|
|
50
|
+
appStore.initTheme()
|
|
51
|
+
|
|
52
|
+
// 检查是否有 token
|
|
53
|
+
const token = getToken()
|
|
54
|
+
|
|
55
|
+
if (token) {
|
|
56
|
+
// 已登录
|
|
57
|
+
if (to.path === loginPath) {
|
|
58
|
+
// 已登录访问登录页,跳转到首页
|
|
59
|
+
next({ path: homePath })
|
|
60
|
+
// NProgress.done()
|
|
61
|
+
} else {
|
|
62
|
+
// 检查是否已获取用户信息
|
|
63
|
+
if (userStore.isLoggedIn) {
|
|
64
|
+
// 已有用户信息,直接放行
|
|
65
|
+
// 添加缓存页面
|
|
66
|
+
if (to.name && to.meta.keepAlive) {
|
|
67
|
+
appStore.addCachedView(to.name as string)
|
|
68
|
+
}
|
|
69
|
+
next()
|
|
70
|
+
} else {
|
|
71
|
+
// 尝试获取用户信息
|
|
72
|
+
try {
|
|
73
|
+
// 获取用户信息
|
|
74
|
+
if (options.fetchUserInfo) {
|
|
75
|
+
const userInfo = await options.fetchUserInfo()
|
|
76
|
+
userStore.setUserInfo(userInfo)
|
|
77
|
+
} else {
|
|
78
|
+
const userInfo = await getUserInfo()
|
|
79
|
+
userStore.setUserInfo(userInfo)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// 获取菜单
|
|
83
|
+
if (options.fetchMenu) {
|
|
84
|
+
const menuList = await options.fetchMenu()
|
|
85
|
+
menuStore.setMenuList(menuList)
|
|
86
|
+
} else {
|
|
87
|
+
const menuList = await getMenuTree()
|
|
88
|
+
menuStore.setMenuList(menuList)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// 登录成功回调
|
|
92
|
+
if (options.onLoginSuccess) {
|
|
93
|
+
options.onLoginSuccess()
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// 添加缓存页面
|
|
97
|
+
if (to.name && to.meta.keepAlive) {
|
|
98
|
+
appStore.addCachedView(to.name as string)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// 重新导航到目标路由,确保动态路由已添加
|
|
102
|
+
next({ ...to, replace: true })
|
|
103
|
+
} catch (error) {
|
|
104
|
+
// 获取用户信息失败,清除 token 并跳转到登录页
|
|
105
|
+
console.error('获取用户信息失败:', error)
|
|
106
|
+
userStore.clearUserInfo()
|
|
107
|
+
menuStore.clearMenu()
|
|
108
|
+
// 清除 token
|
|
109
|
+
localStorage.removeItem('token')
|
|
110
|
+
localStorage.removeItem('refreshToken')
|
|
111
|
+
next({ path: loginPath, query: { redirect: to.fullPath } })
|
|
112
|
+
// NProgress.done()
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
} else {
|
|
117
|
+
// 未登录
|
|
118
|
+
if (whiteList.includes(to.path)) {
|
|
119
|
+
// 在白名单中,直接放行
|
|
120
|
+
next()
|
|
121
|
+
} else {
|
|
122
|
+
// 不在白名单中,跳转到登录页
|
|
123
|
+
next({ path: loginPath, query: { redirect: to.fullPath } })
|
|
124
|
+
// NProgress.done()
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
})
|
|
128
|
+
|
|
129
|
+
router.afterEach(() => {
|
|
130
|
+
// 结束进度条
|
|
131
|
+
// NProgress.done()
|
|
132
|
+
})
|
|
133
|
+
}
|