-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauth.js
62 lines (59 loc) · 1.48 KB
/
auth.js
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
import { authStore } from '@/stores'
import router from '@/router'
import { ElMessageBox } from 'element-plus'
// 路由挂载比store挂载早。不能直接初始化
let auth = null
const getToken = () => {
if (auth === null) {
auth = authStore()
}
return !!auth.token
}
const getRole = (path) => {
if (auth === null) {
auth = authStore()
}
if (!auth.routes) return false
if (auth.routes === '*') return true
return auth.routes.includes(path)
}
router.beforeEach(async (to, from, next) => {
if (to.meta && to.meta.white === true) { // 放行白名单
document.title = import.meta.env.VE_NAME
next()
return
}
// 用户登陆过
if (getToken()) {
if (getRole(to.path)) { // 验证权限
document.title = `${import.meta.env.VE_NAME}-${to.meta.title}`
next()
} else { // 提示权限不足
try {
await ElMessageBox.confirm(
`当前用户暂时无权访问,请联系管理员添加[${to.name}]权限。或尝试重新登录获取权限`,
'访问失败',
{
confirmButtonText: '返回',
cancelButtonText: '重新登录',
type: 'error'
}
)
next(Error('没有访问权限'))
} catch (error) {
next({
name: 'login',
query: {
redirect: to.fullPath
}
})
}
}
return
}
// 第一次登录的用户。
next({
name: 'login'
})
})
export default router