xto-fronted 0.3.6 → 0.3.8
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-BkRneTya.js +142 -0
- package/dist/index-BxIL2hrt.js +475 -0
- package/dist/index-CD364XjV.js +142 -0
- package/dist/index-CIgWYERJ.js +1644 -0
- package/dist/index-Cgkqpyx2.js +345 -0
- package/dist/index-D7EzwTM5.js +372 -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/dist/stores/user.d.ts +2 -2
- package/dist/style.css +1 -1
- package/package.json +1 -1
- package/src/index.ts +1 -0
- package/src/router/guards.ts +133 -0
- package/src/views/login/index.vue +23 -86
package/src/index.ts
CHANGED
|
@@ -33,6 +33,7 @@ export { default as router, resetRouter } from './router'
|
|
|
33
33
|
export * from './router/staticRoutes'
|
|
34
34
|
export * from './router/dynamicRoutes'
|
|
35
35
|
export { createLayoutRoute, createRouter } from './router/layoutRoute'
|
|
36
|
+
export { setupRouterGuards } from './router/guards'
|
|
36
37
|
|
|
37
38
|
// API
|
|
38
39
|
export * from './api/auth'
|
|
@@ -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
|
+
}
|
|
@@ -1,17 +1,14 @@
|
|
|
1
1
|
<script setup lang="ts">
|
|
2
2
|
import { ref, reactive } from 'vue'
|
|
3
|
-
import { useRouter } from 'vue-router'
|
|
3
|
+
import { useRouter, useRoute } from 'vue-router'
|
|
4
4
|
import { Button } from '@xto/base'
|
|
5
5
|
import { Form, FormItem, Input, Checkbox } from '@xto/form'
|
|
6
6
|
import { Message } from '@xto/feedback'
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import { useMenuStore } from '@/stores/menu'
|
|
7
|
+
import { login } from '@/api/auth'
|
|
8
|
+
import { setTokenInfo } from '@/utils/auth'
|
|
10
9
|
|
|
11
10
|
const router = useRouter()
|
|
12
|
-
const
|
|
13
|
-
const userStore = useUserStore()
|
|
14
|
-
const menuStore = useMenuStore()
|
|
11
|
+
const route = useRoute()
|
|
15
12
|
|
|
16
13
|
const loading = ref(false)
|
|
17
14
|
const rememberMe = ref(false)
|
|
@@ -39,85 +36,25 @@ const handleLogin = async () => {
|
|
|
39
36
|
await formRef.value?.validate()
|
|
40
37
|
loading.value = true
|
|
41
38
|
|
|
42
|
-
//
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
phone: '13800138000',
|
|
59
|
-
status: 1,
|
|
60
|
-
roles: ['admin'],
|
|
61
|
-
permissions: ['*'],
|
|
62
|
-
createTime: new Date().toISOString()
|
|
63
|
-
})
|
|
64
|
-
|
|
65
|
-
// 设置菜单
|
|
66
|
-
menuStore.setMenuList([
|
|
67
|
-
{
|
|
68
|
-
id: 1,
|
|
69
|
-
name: 'Dashboard',
|
|
70
|
-
path: '/dashboard',
|
|
71
|
-
component: 'dashboard/index',
|
|
72
|
-
icon: 'dashboard',
|
|
73
|
-
title: '仪表盘',
|
|
74
|
-
keepAlive: true,
|
|
75
|
-
affix: true
|
|
76
|
-
},
|
|
77
|
-
{
|
|
78
|
-
id: 2,
|
|
79
|
-
name: 'System',
|
|
80
|
-
path: '/system',
|
|
81
|
-
redirect: '/system/user',
|
|
82
|
-
icon: 'setting',
|
|
83
|
-
title: '系统管理',
|
|
84
|
-
children: [
|
|
85
|
-
{
|
|
86
|
-
id: 21,
|
|
87
|
-
name: 'SystemUser',
|
|
88
|
-
path: '/system/user',
|
|
89
|
-
component: 'system/user/index',
|
|
90
|
-
icon: 'user',
|
|
91
|
-
title: '用户管理',
|
|
92
|
-
keepAlive: true
|
|
93
|
-
},
|
|
94
|
-
{
|
|
95
|
-
id: 22,
|
|
96
|
-
name: 'SystemRole',
|
|
97
|
-
path: '/system/role',
|
|
98
|
-
component: 'system/role/index',
|
|
99
|
-
icon: 'role',
|
|
100
|
-
title: '角色管理',
|
|
101
|
-
keepAlive: true
|
|
102
|
-
},
|
|
103
|
-
{
|
|
104
|
-
id: 23,
|
|
105
|
-
name: 'SystemMenu',
|
|
106
|
-
path: '/system/menu',
|
|
107
|
-
component: 'system/menu/index',
|
|
108
|
-
icon: 'menu',
|
|
109
|
-
title: '菜单管理',
|
|
110
|
-
keepAlive: true
|
|
111
|
-
}
|
|
112
|
-
]
|
|
113
|
-
}
|
|
114
|
-
])
|
|
115
|
-
|
|
116
|
-
Message.success('登录成功')
|
|
117
|
-
router.push('/')
|
|
118
|
-
loading.value = false
|
|
119
|
-
}, 1000)
|
|
39
|
+
// 调用登录 API
|
|
40
|
+
const result = await login({
|
|
41
|
+
username: formData.username,
|
|
42
|
+
password: formData.password
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
// 保存 token
|
|
46
|
+
setTokenInfo(result)
|
|
47
|
+
|
|
48
|
+
Message.success('登录成功')
|
|
49
|
+
|
|
50
|
+
// 获取重定向地址
|
|
51
|
+
const redirect = route.query.redirect as string || '/'
|
|
52
|
+
|
|
53
|
+
// 跳转到目标页面(路由守卫会自动获取用户信息和菜单)
|
|
54
|
+
router.push(redirect)
|
|
120
55
|
} catch (error) {
|
|
56
|
+
console.error('登录失败:', error)
|
|
57
|
+
} finally {
|
|
121
58
|
loading.value = false
|
|
122
59
|
}
|
|
123
60
|
}
|
|
@@ -178,7 +115,7 @@ const handleLogin = async () => {
|
|
|
178
115
|
</Form>
|
|
179
116
|
|
|
180
117
|
<div class="login__footer">
|
|
181
|
-
<p
|
|
118
|
+
<p>默认账号: admin / 123456</p>
|
|
182
119
|
</div>
|
|
183
120
|
</div>
|
|
184
121
|
</div>
|