permission.js
3.69 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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import NProgress from "nprogress"
import en from "../i18n/lang/en"
import Layout from "@/views/layout/layout"
import CommerViews from "@/views/commerViews"
import router from "./index"
import store from "../store"
let routeName = en.routeName
const Erji = () => import("@/views/duoji/erji")
const Sanji = () => import("@/views/duoji/sanji")
let addrouters = [ //测试用,后续后端获取
{
path: "/",
iconCls: "fa fa-server",
// name: routeName.multiDirectory,
name: '多级',
component: Layout,
children: [
{
path: "/erji1",
iconCls: "fa fa-server",
// name: routeName["menu2-1"],
name: '二级',
component: Erji,
meta:{
role:['admin']
},
children: []
},
{
path: "/erji3",
iconCls: "fa fa-server",
name: "三级",
component: CommerViews, // 无限极菜单的容器
children: [
{
path: "/sanji1",
iconCls: "fa fa-server",
name: "三级子页面",
component: Sanji,
children: []
},
]
}
]
},
{ path: "*",
redirect: "/404",
hidden: true,
children: []
}
]
// 获取角色信息,根据用户权限动态加载路由
router.beforeEach((to, from, next) => {
NProgress.start()
if (store.getters.token) {
if (to.path === "/login") {
next({path: "/"})
} else {
if (!store.getters.info.role) {
!(async function getAddRouters () {
// 省略 axios 请求代码 通过 token 向后台请求用户权限等信息,这里用假数据赋值
await store.dispatch("setInfo", {
role: "superAdmin",
permissions: "超级管理员",
name:"张老师",
})
await store.dispatch("newRoutes",addrouters)
let newAddRouters = store.getters.addRouters
await router.addRoutes(newAddRouters)
next({path: to.path})
}())
} else {
let is404 = to.matched.some(record => {
if (record.meta.role) {
return store.getters.info.authorityRouter === -1
}
})
if (is404) {
next({path: "/404"})
return false
}
next()
}
}
} else {
if (to.path === "/login") {
next()
}
next({path: "/login"})
}
})
router.afterEach(() => {
NProgress.done()
})
// // 真实使用
// import fetchUser from "@/api/apis/user"
//
// router.beforeEach((to, from, next) => {
// if (store.getters.token && store.getters.token !== "undefined") {
// // store.dispatch('setToken', store.getters.token)
// if (to.path === "/login") {
// next({path: "/"})
// } else {
// if (!store.getters.info) {
// (async function getAddRouters () {
// fetchUser.fetchGetUserInfo().then(async function (response) {
// await store.dispatch("setInfo", response.data)
// await store.dispatch("newRoutes", store.getters.info.authorityRouter)
// await router.addRoutes(store.getters.addRouters)
// next({path: "/index"})
// }).catch(function (error) {
// console.log(error)
// })
// }())
// } else {
// let is404 = to.matched.some(record => {
// console.log(record)
// if (record.meta.role) {
// return store.getters.info.authorityRouter === -1
// }
// })
// if (is404) {
// next({path: "/404"})
// return false
// }
// next()
// }
// }
// } else {
// if (to.path === "/login") {
// next()
// }
// next({path: "/login"})
// }
// })