|
@@ -31,37 +31,6 @@ const getUserMeta = async (token) => {
|
|
|
return result.data.menus
|
|
|
}
|
|
|
}
|
|
|
-// 注册前置守卫
|
|
|
-export const registerBeforeRouter = async (router) => {
|
|
|
- router.beforeEach(async (to, from, next) => {
|
|
|
- NProgress.start(); //开启进度条
|
|
|
- const token = localStorage.getItem('token')
|
|
|
- if (to.path === '/login') {
|
|
|
- next()
|
|
|
- return
|
|
|
- }
|
|
|
- if (token) {
|
|
|
- if (to.path === '/login') next()
|
|
|
- const menus = await getUserMeta(token)
|
|
|
- // 检查目的地路由是否注册
|
|
|
- const hasRoute = hasNecessaryRoute(to, router)
|
|
|
- if (hasRoute) {
|
|
|
- // 注册了直接进入
|
|
|
- next()
|
|
|
- } else {
|
|
|
- // 没注册就先注册再重定向进入直到进入为止
|
|
|
- await addUserRoutes(menus, router)
|
|
|
- next({ ...to, replace: true })
|
|
|
- }
|
|
|
- } else {
|
|
|
- next('/login')
|
|
|
- NProgress.done()
|
|
|
- }
|
|
|
- })
|
|
|
- router.afterEach(() => {
|
|
|
- NProgress.done(); //完成进度条
|
|
|
- });
|
|
|
-}
|
|
|
/**
|
|
|
* 将路由数组一维化
|
|
|
* @param {Array} routes 路由数组
|
|
@@ -146,3 +115,41 @@ const resetMenus = (menus) => {
|
|
|
}
|
|
|
return result
|
|
|
}
|
|
|
+
|
|
|
+// 注册前置守卫
|
|
|
+export const registerBeforeRouter = async (router) => {
|
|
|
+ router.beforeEach(async (to, from, next) => {
|
|
|
+ NProgress.start(); //开启进度条
|
|
|
+ const token = localStorage.getItem('token')
|
|
|
+ if (to.path === '/login') {
|
|
|
+ next()
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if (token) {
|
|
|
+ if (to.path === '/login') next()
|
|
|
+ const menus = await getUserMeta(token)
|
|
|
+ // 检查目的地路由是否注册
|
|
|
+ const hasRoute = hasNecessaryRoute(to, router)
|
|
|
+ if (hasRoute) {
|
|
|
+ // 注册了直接进入
|
|
|
+ next()
|
|
|
+ } else {
|
|
|
+ // 没注册就先注册再重定向进入直到进入为止
|
|
|
+ await addUserRoutes(menus, router)
|
|
|
+ next({ ...to, replace: true })
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ next('/login')
|
|
|
+ }
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+// 注册路由后置守卫
|
|
|
+
|
|
|
+export const registerAfterRouter = async (router) => {
|
|
|
+ router.afterEach((to, form) => {
|
|
|
+ NProgress.done(); //完成进度条
|
|
|
+ if (to.path === '/login') return
|
|
|
+ // 请求该页面的权限
|
|
|
+ })
|
|
|
+}
|