-
Notifications
You must be signed in to change notification settings - Fork 706
Expand file tree
/
Copy pathwatch.ts
More file actions
86 lines (84 loc) · 2.61 KB
/
watch.ts
File metadata and controls
86 lines (84 loc) · 2.61 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
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
import { ElMessage } from 'element-plus-secondary'
import { useCache } from '@/utils/useCache'
import { useAppearanceStoreWithOut } from '@/stores/appearance'
import { useUserStore } from '@/stores/user'
import { request } from '@/utils/request'
import type { Router } from 'vue-router'
import { generateDynamicRouters } from './dynamic'
import { toLoginPage } from '@/utils/utils'
const appearanceStore = useAppearanceStoreWithOut()
const userStore = useUserStore()
const { wsCache } = useCache()
const whiteList = ['/login', '/admin-login']
const assistantWhiteList = ['/assistant', '/embeddedPage', '/401']
export const watchRouter = (router: Router) => {
router.beforeEach(async (to: any, from: any, next: any) => {
await loadXpackStatic()
await appearanceStore.setAppearance()
LicenseGenerator.generateRouters(router)
if (to.path.startsWith('/login') && userStore.getUid) {
next(to?.query?.redirect || '/')
return
}
if (assistantWhiteList.includes(to.path)) {
next()
return
}
const token = wsCache.get('user.token')
if (whiteList.includes(to.path)) {
next()
return
}
if (!token) {
// ElMessage.error('Please login first')
next(toLoginPage(to.fullPath))
return
}
let isFirstDynamicPath = false
if (!userStore.getUid) {
await userStore.info()
generateDynamicRouters(router)
isFirstDynamicPath = to?.path && ['/ds/index', '/as/index'].includes(to.path)
if (isFirstDynamicPath) {
next({ ...to, replace: true })
return
}
}
if (to.path === '/' || accessCrossPermission(to)) {
next('/chat')
return
}
if (to.path === '/login' || to.path === '/admin-login') {
console.info(from)
next('/chat')
} else {
next()
}
})
}
const accessCrossPermission = (to: any) => {
if (!to?.path) return false
return (
(to.path.startsWith('/system') && !userStore.isAdmin) ||
(to.path.startsWith('/set') && !userStore.isSpaceAdmin)
)
}
const loadXpackStatic = () => {
if (document.getElementById('sqlbot_xpack_static')) {
return Promise.resolve()
}
const url = `/xpack_static/license-generator.umd.js?t=${Date.now()}`
return new Promise((resolve, reject) => {
request
.loadRemoteScript(url, 'sqlbot_xpack_static', () => {
LicenseGenerator?.init(import.meta.env.VITE_API_BASE_URL).then(() => {
resolve(true)
})
})
.catch((error) => {
console.error('Failed to load xpack_static script:', error)
ElMessage.error('Failed to load license generator script')
reject(error)
})
})
}