permission.js
3.84 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
import NProgress from "nprogress"
import router from "./index"
import store from "../store"
// 获取角色信息,根据用户权限动态加载路由
router.beforeEach((to, from, next) => {
NProgress.start()
if (store.getters.token) {
// if (to.path === "/login") {
next()
// } else {
// if (!store.getters.info.permissions) {
// (async function getAddRouters() {
// // 省略 axios 请求代码 通过 token 向后台请求用户权限等信息,这里用假数据赋值
// const userInfo = {
// permissions: [
// {
// role: "superAdmin",
// roleName: "超级管理员"
// }
// ],
// name: "李老师",
// // avatar: data.avatar ? data.avatar : "",
// // uid: data.id,
// authorityRouter: ["examinationPaper",
// "examinationPaperAdd",
// "examinationPaperEdit",
// "examinationPaperRecycle", "ask",
// "askAnalysis",
// "test",
// "testAnalysis", "portrait"],
// }
// await store.commit("setInfo", { ...userInfo })
// await store.commit("setRouters", userInfo.authorityRouter)
// let newAddRouters = store.getters.addRouters
// newAddRouters.forEach(res => {
// router.addRoute(res)
// })
// router.addRoute({
// path: "*",
// redirect: "/404",
// hidden: true,
// children: []
// })
// next({ path: to.fullPath })
// }())
// } else {
// next()
// }
// }
} else {
if (store.getters.info && store.getters.addRouters) {
console.log(store.getters.addRouters)
store.commit("setToken", "isLogin");
store.commit(
"setRouters",
store.getters.info.authorityRouter
);
let newAddRouters = store.getters.addRouters;
newAddRouters.forEach((res) => {
router.addRoute(res);
});
router.addRoute({
path: "*",
redirect: "/404",
hidden: true,
children: [],
});
next({ path: to.fullPath })
} else {
if (store.getters.code) {
next()
} else {
if (to.path === "/login") {
next()
} else {
next({ path: "/login" })
}
}
}
}
})
router.afterEach(() => {
NProgress.done()
})
router.onError((error) => {
const pattern = /Loading chunk (\d)+ failed/g;
const isChunkLoadFailed = error.message.match(pattern);
const targetPath = router.history.pending.fullPath;
if (isChunkLoadFailed) { router.replace(targetPath); }
});
// // 真实使用
// 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"})
// }
// })