xto-fronted 0.4.103 → 0.4.105
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-BsPaXQak.js → index-6kX8-rlc.js} +1 -1
- package/dist/{index-DEChud2_.js → index-B17roDLd.js} +1 -1
- package/dist/{index-BlN5yXBT.js → index-Bv8lJOd7.js} +1 -1
- package/dist/{index-DdACCkye.js → index-CkGnfjPz.js} +1 -1
- package/dist/{index-CNx4xpUG.js → index-DDZuw7Rp.js} +893 -893
- package/dist/index.js +1 -1
- package/dist/style.css +1 -1
- package/package.json +1 -1
- package/src/router/guards.ts +129 -127
- package/src/views/login/index.vue +4 -2
package/src/router/guards.ts
CHANGED
|
@@ -1,127 +1,129 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 路由守卫设置
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import type { Router } from 'vue-router'
|
|
6
|
-
import { getToken, clearToken } 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
|
-
// 应用ID
|
|
24
|
-
appId?: string
|
|
25
|
-
// 获取用户信息的回调(可选,默认调用 API)
|
|
26
|
-
fetchUserInfo?: () => Promise<any>
|
|
27
|
-
// 获取菜单的回调(可选,默认调用 API)
|
|
28
|
-
fetchMenu?: () => Promise<any>
|
|
29
|
-
// 登录成功后的回调
|
|
30
|
-
onLoginSuccess?: () => void
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* 设置路由守卫
|
|
35
|
-
* @param router 路由实例
|
|
36
|
-
* @param options 配置选项
|
|
37
|
-
*/
|
|
38
|
-
export function setupRouterGuards(router: Router, options: RouterGuardOptions = {}) {
|
|
39
|
-
const whiteList = options.whiteList || defaultWhiteList
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
1
|
+
/**
|
|
2
|
+
* 路由守卫设置
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type { Router } from 'vue-router'
|
|
6
|
+
import { getToken, clearToken } 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
|
+
// 应用ID
|
|
24
|
+
appId?: string
|
|
25
|
+
// 获取用户信息的回调(可选,默认调用 API)
|
|
26
|
+
fetchUserInfo?: () => Promise<any>
|
|
27
|
+
// 获取菜单的回调(可选,默认调用 API)
|
|
28
|
+
fetchMenu?: () => Promise<any>
|
|
29
|
+
// 登录成功后的回调
|
|
30
|
+
onLoginSuccess?: () => void
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* 设置路由守卫
|
|
35
|
+
* @param router 路由实例
|
|
36
|
+
* @param options 配置选项
|
|
37
|
+
*/
|
|
38
|
+
export function setupRouterGuards(router: Router, options: RouterGuardOptions = {}) {
|
|
39
|
+
const whiteList = options.whiteList || defaultWhiteList
|
|
40
|
+
|
|
41
|
+
router.beforeEach(async (to, _from, next) => {
|
|
42
|
+
const appStore = useAppStore()
|
|
43
|
+
const userStore = useUserStore()
|
|
44
|
+
const menuStore = useMenuStore()
|
|
45
|
+
|
|
46
|
+
// 从 store 获取路径配置(createXtoApp 已设置)
|
|
47
|
+
const loginPath = appStore.loginPath || options.loginPath || '/login'
|
|
48
|
+
const homePath = appStore.indexPath || options.homePath || '/dashboard'
|
|
49
|
+
|
|
50
|
+
// 初始化主题
|
|
51
|
+
appStore.initTheme()
|
|
52
|
+
|
|
53
|
+
// 检查是否有 token
|
|
54
|
+
const token = getToken()
|
|
55
|
+
|
|
56
|
+
if (token) {
|
|
57
|
+
// 已登录
|
|
58
|
+
if (to.path === loginPath) {
|
|
59
|
+
// 已登录访问登录页,跳转到首页
|
|
60
|
+
next({ path: homePath })
|
|
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(options.appId)
|
|
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
|
+
// 使用 clearToken 清除所有 token 相关数据(包括 expires_time、refresh_time 等)
|
|
109
|
+
clearToken()
|
|
110
|
+
next({ path: loginPath, query: { redirect: to.fullPath } })
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
} else {
|
|
115
|
+
// 未登录
|
|
116
|
+
if (whiteList.includes(to.path)) {
|
|
117
|
+
// 在白名单中,直接放行
|
|
118
|
+
next()
|
|
119
|
+
} else {
|
|
120
|
+
// 不在白名单中,跳转到登录页
|
|
121
|
+
next({ path: loginPath, query: { redirect: to.fullPath } })
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
router.afterEach(() => {
|
|
127
|
+
// 可以在这里添加进度条结束等
|
|
128
|
+
})
|
|
129
|
+
}
|
|
@@ -7,9 +7,11 @@ import { Message } from '@xto/feedback'
|
|
|
7
7
|
import { login } from '@/api/auth'
|
|
8
8
|
import { setTokenInfo } from '@/utils/auth'
|
|
9
9
|
import { getAppId, getClientId } from '@/utils/config'
|
|
10
|
+
import { useAppStore } from '@/stores/app'
|
|
10
11
|
|
|
11
12
|
const router = useRouter()
|
|
12
13
|
const route = useRoute()
|
|
14
|
+
const appStore = useAppStore()
|
|
13
15
|
|
|
14
16
|
const loading = ref(false)
|
|
15
17
|
const rememberMe = ref(false)
|
|
@@ -51,8 +53,8 @@ const handleLogin = async () => {
|
|
|
51
53
|
|
|
52
54
|
Message.success('登录成功')
|
|
53
55
|
|
|
54
|
-
//
|
|
55
|
-
const redirect = route.query.redirect as string || '/'
|
|
56
|
+
// 获取重定向地址,默认使用 indexPath
|
|
57
|
+
const redirect = route.query.redirect as string || appStore.indexPath || '/dashboard'
|
|
56
58
|
|
|
57
59
|
// 跳转到目标页面(路由守卫会自动获取用户信息和菜单)
|
|
58
60
|
router.push(redirect)
|