feat: 路由白名单功能支持匹配路由 Name
This commit is contained in:
parent
57d1df3f6e
commit
665bdbec7a
@ -1,4 +1,15 @@
|
|||||||
/** 免登录白名单 */
|
import { type RouteLocationNormalized } from "vue-router"
|
||||||
const whiteList = ["/login"]
|
|
||||||
|
|
||||||
export { whiteList }
|
/** 免登录白名单(匹配路由 path) */
|
||||||
|
const whiteListByPath: string[] = ["/login"]
|
||||||
|
|
||||||
|
/** 免登录白名单(匹配路由 name) */
|
||||||
|
const whiteListByName: string[] = []
|
||||||
|
|
||||||
|
/** 判断是否在白名单 */
|
||||||
|
const isWhiteList = (to: RouteLocationNormalized) => {
|
||||||
|
// path 和 name 任意一个匹配上即可
|
||||||
|
return whiteListByPath.indexOf(to.path) !== -1 || whiteListByName.indexOf(to.name as any) !== -1
|
||||||
|
}
|
||||||
|
|
||||||
|
export default isWhiteList
|
||||||
|
@ -2,9 +2,9 @@ import router from "@/router"
|
|||||||
import { useUserStoreHook } from "@/store/modules/user"
|
import { useUserStoreHook } from "@/store/modules/user"
|
||||||
import { usePermissionStoreHook } from "@/store/modules/permission"
|
import { usePermissionStoreHook } from "@/store/modules/permission"
|
||||||
import { ElMessage } from "element-plus"
|
import { ElMessage } from "element-plus"
|
||||||
import { whiteList } from "@/config/white-list"
|
|
||||||
import { getToken } from "@/utils/cache/cookies"
|
import { getToken } from "@/utils/cache/cookies"
|
||||||
import asyncRouteSettings from "@/config/async-route"
|
import asyncRouteSettings from "@/config/async-route"
|
||||||
|
import isWhiteList from "@/config/white-list"
|
||||||
import NProgress from "nprogress"
|
import NProgress from "nprogress"
|
||||||
import "nprogress/nprogress.css"
|
import "nprogress/nprogress.css"
|
||||||
|
|
||||||
@ -55,7 +55,7 @@ router.beforeEach(async (to, _from, next) => {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// 如果没有 Token
|
// 如果没有 Token
|
||||||
if (whiteList.indexOf(to.path) !== -1) {
|
if (isWhiteList(to)) {
|
||||||
// 如果在免登录的白名单中,则直接进入
|
// 如果在免登录的白名单中,则直接进入
|
||||||
next()
|
next()
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user