From af5ef1f2874f31658c81eb111e1e6ccfcf3aefe2 Mon Sep 17 00:00:00 2001 From: AKING <2734339436@qq.com> Date: Wed, 24 Jan 2024 02:44:33 +0000 Subject: [PATCH] =?UTF-8?q?update=20src/permission.ts.=20=E8=A7=A3?= =?UTF-8?q?=E5=86=B3=E4=BA=86=E8=B7=B3=E8=BD=AC=E4=B8=8D=E5=B8=A6=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: AKING <2734339436@qq.com> (cherry picked from commit 024d08555b5d0168d9eb29168e970bb2f38d2829) --- src/permission.ts | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/src/permission.ts b/src/permission.ts index 0698dc88..d538303b 100644 --- a/src/permission.ts +++ b/src/permission.ts @@ -12,6 +12,40 @@ import { usePermissionStoreWithOut } from '@/store/modules/permission' const { start, done } = useNProgress() const { loadStart, loadDone } = usePageLoading() + +const parseURL = ( + url: string | null | undefined +): { basePath: string; paramsObject: { [key: string]: string } } => { + // 如果输入为 null 或 undefined,返回空字符串和空对象 + if (url == null) { + return { basePath: '', paramsObject: {} } + } + + // 找到问号 (?) 的位置,它之前是基础路径,之后是查询参数 + const questionMarkIndex = url.indexOf('?') + let basePath = url + const paramsObject: { [key: string]: string } = {} + + // 如果找到了问号,说明有查询参数 + if (questionMarkIndex !== -1) { + // 获取 basePath + basePath = url.substring(0, questionMarkIndex) + + // 从 URL 中获取查询字符串部分 + const queryString = url.substring(questionMarkIndex + 1) + + // 使用 URLSearchParams 遍历参数 + const searchParams = new URLSearchParams(queryString) + searchParams.forEach((value, key) => { + // 封装进 paramsObject 对象 + paramsObject[key] = value + }) + } + + // 返回 basePath 和 paramsObject + return { basePath, paramsObject } +} + // 路由不重定向白名单 const whiteList = [ '/login', @@ -47,8 +81,10 @@ router.beforeEach(async (to, from, next) => { router.addRoute(route as unknown as RouteRecordRaw) // 动态添加可访问路由表 }) const redirectPath = from.query.redirect || to.path + // 修复跳转时不带参数的问题 const redirect = decodeURIComponent(redirectPath as string) - const nextData = to.path === redirect ? { ...to, replace: true } : { path: redirect } + const { basePath, paramsObject: query } = parseURL(redirect) + const nextData = to.path === redirect ? { ...to, replace: true } : { path: redirect, query } next(nextData) } else { next()