From fd01b5a61f1a92db90c00e7bb1b4162a2b61bfa3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90Li?= <15040126243@163.com> Date: Wed, 4 Dec 2024 11:42:07 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E4=BC=98=E5=8C=96=20=E7=99=BD?= =?UTF-8?q?=E5=90=8D=E5=8D=95=E6=94=AF=E6=8C=81=E5=AF=B9=E9=80=9A=E9=85=8D?= =?UTF-8?q?=E7=AC=A6=E8=B7=AF=E5=BE=84=E5=8C=B9=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/permission.ts | 10 +++++++--- src/utils/validate.ts | 12 ++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/permission.ts b/src/permission.ts index 51a928a..86dd6c3 100644 --- a/src/permission.ts +++ b/src/permission.ts @@ -3,7 +3,7 @@ import router from './router'; import NProgress from 'nprogress'; import 'nprogress/nprogress.css'; import { getToken } from '@/utils/auth'; -import { isHttp } from '@/utils/validate'; +import { isHttp, isPathMatch } from '@/utils/validate'; import { isRelogin } from '@/utils/request'; import useUserStore from '@/store/modules/user'; import useSettingsStore from '@/store/modules/settings'; @@ -12,6 +12,10 @@ import usePermissionStore from '@/store/modules/permission'; NProgress.configure({ showSpinner: false }); const whiteList = ['/login', '/register', '/social-callback']; +const isWhiteList = (path: string) => { + return whiteList.some(pattern => isPathMatch(pattern, path)) +} + router.beforeEach(async (to, from, next) => { NProgress.start(); if (getToken()) { @@ -20,7 +24,7 @@ router.beforeEach(async (to, from, next) => { if (to.path === '/login') { next({ path: '/' }); NProgress.done(); - } else if (whiteList.indexOf(to.path as string) !== -1) { + } else if (isWhiteList(to.path)) { next(); } else { if (useUserStore().roles.length === 0) { @@ -49,7 +53,7 @@ router.beforeEach(async (to, from, next) => { } } else { // 没有token - if (whiteList.indexOf(to.path as string) !== -1) { + if (isWhiteList(to.path)) { // 在免登录白名单,直接进入 next(); } else { diff --git a/src/utils/validate.ts b/src/utils/validate.ts index 4d57894..788095c 100644 --- a/src/utils/validate.ts +++ b/src/utils/validate.ts @@ -1,3 +1,15 @@ +/** + * 路径匹配器 + * @param {string} pattern + * @param {string} path + * @returns {Boolean} + */ +export function isPathMatch(pattern: string, path: string) { + const regexPattern = pattern.replace(/\//g, '\\/').replace(/\*\*/g, '.*').replace(/\*/g, '[^\\/]*') + const regex = new RegExp(`^${regexPattern}$`) + return regex.test(path) +} + /** * 判断url是否是http或https * @returns {Boolean}