-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
90 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,20 @@ | ||
import type { Router } from 'vue-router'; | ||
import { getToken } from '@/utils/auth'; | ||
|
||
import { createPermissionGuard } from './permission'; | ||
|
||
const appTitle = import.meta.env.VITE_APP_TITLE; | ||
|
||
export function setupRouterGuard(router: Router) { | ||
const isLogin = Boolean(getToken()); | ||
|
||
router.beforeEach((to, _from, next) => { | ||
// 登录鉴权 | ||
if (!isLogin) { | ||
if (to.name === 'login') { | ||
next(); | ||
} else { | ||
const redirect = to.fullPath; | ||
next({ path: '/login', query: { redirect } }); | ||
} | ||
return false; | ||
} | ||
router.beforeEach(async (to, from, next) => { | ||
// 开始 loadingBar | ||
window.$loadingBar?.start(); | ||
// 权限操作 | ||
await createPermissionGuard(to, from, next); | ||
}); | ||
router.afterEach((to) => { | ||
// 修改网页标题 | ||
document.title = `${to.meta.title}——${appTitle}`; | ||
next(); | ||
// 结束 loadingBar | ||
window.$loadingBar?.finish(); | ||
}); | ||
// router.afterEach((_to) => {}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { RouteLocationNormalized, NavigationGuardNext } from 'vue-router'; | ||
import { getToken } from '@/utils/auth'; | ||
import { useRouteStore, useAuthStore } from '@/store'; | ||
|
||
export async function createPermissionGuard( | ||
to: RouteLocationNormalized, | ||
from: RouteLocationNormalized, | ||
next: NavigationGuardNext, | ||
) { | ||
const isLogin = Boolean(getToken()); | ||
const routeStore = useRouteStore(); | ||
|
||
// 判断路由有无进行初始化 | ||
if (!routeStore.isInitAuthRoute) { | ||
// 没有初始化路由 => 登录鉴权 | ||
// 登录鉴权 | ||
if (!isLogin) { | ||
if (to.name === 'login') { | ||
next(); | ||
} else { | ||
const redirect = to.fullPath; | ||
next({ path: '/login', query: { redirect } }); | ||
} | ||
return false; | ||
} | ||
|
||
// 有登录但是没有路由,初始化路由等 | ||
await routeStore.initAuthRoute(); | ||
} | ||
// 权限路由已经加载,仍然未找到,重定向到not-found | ||
// if (to.name === 'not-found-page') { | ||
// next({ name: 'not-found-page', replace: true }); | ||
// } | ||
|
||
// next({ name: 'root' }); | ||
next(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './app'; | ||
export * from './auth'; | ||
export * from './route'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4acb525
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
ench-admin – ./
ench-admin-whyhenin.vercel.app
ench-admin-git-main-whyhenin.vercel.app
ench-admin.vercel.app