-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpermission.ts
52 lines (40 loc) · 1.01 KB
/
permission.ts
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
import { useUserAccountStore } from '@/modules/UserAccount/store'
import Cookie from 'js-cookie'
import { allowlist } from '@/router/auth-list'
import { systemTitle } from '@/base'
import NProgress from 'nprogress'
import type { Router } from 'vue-router'
NProgress.configure({
showSpinner: false
})
export function createRouterGuards(router: Router) {
router.beforeEach(async (to, from, next) => {
const userAccountStore = useUserAccountStore()
NProgress.start()
document.title = `${ to.meta.title || '' } - ${ systemTitle }`
console.log('😄😄😄 ', to)
if (
allowlist.find(
name => to.name === name
)
) {
next()
return
}
if (!Cookie.get('token')) {
next(`/user/login`)
return
}
// 获取用户信息
const { data, error } = await userAccountStore.getUserInfo()
if (error) {
Cookie.remove('token')
next(`/user/login`)
return
}
next()
})
router.afterEach((to) => {
NProgress.done()
})
}