fix(router): rematch redirect after dynamic routes

This commit is contained in:
Kroytz committed 2026-07-24 11:27:55 +08:00
1 parent a8cc96e88b
commit 4d925fb6ff
4 files changed
+6 -48

No files matched your search

+2 -38
View File
@@ -12,39 +12,6 @@ const { start, done } = useNProgress()
const { loadStart, loadDone } = usePageLoading() 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 = [ const whiteList = [
'/login', '/login',
@@ -56,7 +23,7 @@ const whiteList = [
] ]
// 路由加载前 // 路由加载前
router.beforeEach(async (to, from, next) => { router.beforeEach(async (to, _from, next) => {
start() start()
loadStart() loadStart()
const userStore = useUserStoreWithOut() const userStore = useUserStoreWithOut()
@@ -89,10 +56,7 @@ router.beforeEach(async (to, from, next) => {
router.addRoute(route as unknown as RouteRecordRaw) router.addRoute(route as unknown as RouteRecordRaw)
}) })
const redirectPath = from.query.redirect || to.path next({ path: to.path, query: to.query, hash: to.hash, replace: true })
const redirect = decodeURIComponent(redirectPath as string)
const { paramsObject: query } = parseURL(redirect)
next(to.path === redirect ? { ...to, replace: true } : { path: redirect, query })
} catch { } catch {
userStore.resetState() userStore.resetState()
resetRouter() resetRouter()
@@ -154,7 +154,6 @@ import { ElLoading } from 'element-plus'
import { useDesign } from '@/hooks/web/useDesign' import { useDesign } from '@/hooks/web/useDesign'
import { useAppStore } from '@/store/modules/app' import { useAppStore } from '@/store/modules/app'
import { useIcon } from '@/hooks/web/useIcon' import { useIcon } from '@/hooks/web/useIcon'
import { usePermissionStore } from '@/store/modules/permission'
import * as LoginApi from '@/api/login' import * as LoginApi from '@/api/login'
import * as authUtil from '@/utils/auth' import * as authUtil from '@/utils/auth'
@@ -179,7 +178,6 @@ const formLogin = ref<any>()
const { validForm } = useFormValid(formLogin) const { validForm } = useFormValid(formLogin)
const { getLoginState } = useLoginState() const { getLoginState } = useLoginState()
const { push } = useRouter() const { push } = useRouter()
const permissionStore = usePermissionStore()
const loginLoading = ref(false) const loginLoading = ref(false)
const verify = ref() const verify = ref()
const captchaType = ref('blockPuzzle') // blockPuzzle 滑块 clickWord 点击文字 const captchaType = ref('blockPuzzle') // blockPuzzle 滑块 clickWord 点击文字
@@ -253,7 +251,7 @@ const tryLogin = async () => {
await LoginApi.socialLogin(type, code, state) await LoginApi.socialLogin(type, code, state)
router.push({ path: redirect || '/' }) await router.push(redirect || '/')
} catch (err) {} } catch (err) {}
} }
@@ -303,7 +301,7 @@ const handleLogin = async (params) => {
if (redirect.indexOf('sso') !== -1) { if (redirect.indexOf('sso') !== -1) {
window.location.href = window.location.href.replace('/login?redirect=', '') window.location.href = window.location.href.replace('/login?redirect=', '')
} else { } else {
push({ path: redirect || permissionStore.addRouters[0].path }) await push(redirect)
} }
} finally { } finally {
loginLoading.value = false loginLoading.value = false
@@ -90,7 +90,6 @@ import type { RouteLocationNormalizedLoaded } from 'vue-router'
import { useIcon } from '@/hooks/web/useIcon' import { useIcon } from '@/hooks/web/useIcon'
import * as authUtil from '@/utils/auth' import * as authUtil from '@/utils/auth'
import { usePermissionStore } from '@/store/modules/permission'
import * as LoginApi from '@/api/login' import * as LoginApi from '@/api/login'
import { LoginStateEnum, useFormValid, useLoginState } from './useLogin' import { LoginStateEnum, useFormValid, useLoginState } from './useLogin'
@@ -102,7 +101,6 @@ const formLogin = ref()
const { validForm } = useFormValid(formLogin) const { validForm } = useFormValid(formLogin)
const { getLoginState } = useLoginState() const { getLoginState } = useLoginState()
const { currentRoute, push } = useRouter() const { currentRoute, push } = useRouter()
const permissionStore = usePermissionStore()
const redirect = ref<string>('') const redirect = ref<string>('')
const loginLoading = ref(false) const loginLoading = ref(false)
const verify = ref() const verify = ref()
@@ -193,7 +191,7 @@ const handleLogin = async (params) => {
if (redirect.value.indexOf('sso') !== -1) { if (redirect.value.indexOf('sso') !== -1) {
window.location.href = window.location.href.replace('/login?redirect=', '') window.location.href = window.location.href.replace('/login?redirect=', '')
} else { } else {
push({ path: redirect.value || permissionStore.addRouters[0].path }) await push(redirect.value)
} }
} catch { } catch {
loginLoading.value = false loginLoading.value = false
@@ -98,7 +98,6 @@ import type { RouteLocationNormalizedLoaded } from 'vue-router'
import { useIcon } from '@/hooks/web/useIcon' import { useIcon } from '@/hooks/web/useIcon'
import { setTenantId } from '@/utils/auth' import { setTenantId } from '@/utils/auth'
import { usePermissionStore } from '@/store/modules/permission'
import { getTenantIdByName, sendSmsCode, smsLogin } from '@/api/login' import { getTenantIdByName, sendSmsCode, smsLogin } from '@/api/login'
import LoginFormTitle from './LoginFormTitle.vue' import LoginFormTitle from './LoginFormTitle.vue'
import { LoginStateEnum, useFormValid, useLoginState } from './useLogin' import { LoginStateEnum, useFormValid, useLoginState } from './useLogin'
@@ -108,7 +107,6 @@ defineOptions({ name: 'MobileForm' })
const { t } = useI18n() const { t } = useI18n()
const message = useMessage() const message = useMessage()
const permissionStore = usePermissionStore()
const { currentRoute, push } = useRouter() const { currentRoute, push } = useRouter()
const formSmsLogin = ref() const formSmsLogin = ref()
const loginLoading = ref(false) const loginLoading = ref(false)
@@ -199,7 +197,7 @@ const signIn = async () => {
if (!redirect.value) { if (!redirect.value) {
redirect.value = '/' redirect.value = '/'
} }
push({ path: redirect.value || permissionStore.addRouters[0].path }) await push(redirect.value)
}) })
.catch(() => {}) .catch(() => {})
.finally(() => { .finally(() => {