From 4d925fb6ff4636559f5bfc9e5ba2a1541e1dcf90 Mon Sep 17 00:00:00 2001 From: Kroytz Date: Fri, 24 Jul 2026 11:27:55 +0800 Subject: [PATCH] fix(router): rematch redirect after dynamic routes --- yshop-drink-vue3/src/permission.ts | 40 +------------------ .../src/views/Login/SocialLogin.vue | 6 +-- .../src/views/Login/components/LoginForm.vue | 4 +- .../src/views/Login/components/MobileForm.vue | 4 +- 4 files changed, 6 insertions(+), 48 deletions(-) diff --git a/yshop-drink-vue3/src/permission.ts b/yshop-drink-vue3/src/permission.ts index e9b141b..70d3279 100644 --- a/yshop-drink-vue3/src/permission.ts +++ b/yshop-drink-vue3/src/permission.ts @@ -12,39 +12,6 @@ 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', @@ -56,7 +23,7 @@ const whiteList = [ ] // 路由加载前 -router.beforeEach(async (to, from, next) => { +router.beforeEach(async (to, _from, next) => { start() loadStart() const userStore = useUserStoreWithOut() @@ -89,10 +56,7 @@ 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 { paramsObject: query } = parseURL(redirect) - next(to.path === redirect ? { ...to, replace: true } : { path: redirect, query }) + next({ path: to.path, query: to.query, hash: to.hash, replace: true }) } catch { userStore.resetState() resetRouter() diff --git a/yshop-drink-vue3/src/views/Login/SocialLogin.vue b/yshop-drink-vue3/src/views/Login/SocialLogin.vue index 6361f6c..b81e4e8 100644 --- a/yshop-drink-vue3/src/views/Login/SocialLogin.vue +++ b/yshop-drink-vue3/src/views/Login/SocialLogin.vue @@ -154,7 +154,6 @@ import { ElLoading } from 'element-plus' import { useDesign } from '@/hooks/web/useDesign' import { useAppStore } from '@/store/modules/app' import { useIcon } from '@/hooks/web/useIcon' -import { usePermissionStore } from '@/store/modules/permission' import * as LoginApi from '@/api/login' import * as authUtil from '@/utils/auth' @@ -179,7 +178,6 @@ const formLogin = ref() const { validForm } = useFormValid(formLogin) const { getLoginState } = useLoginState() const { push } = useRouter() -const permissionStore = usePermissionStore() const loginLoading = ref(false) const verify = ref() const captchaType = ref('blockPuzzle') // blockPuzzle 滑块 clickWord 点击文字 @@ -253,7 +251,7 @@ const tryLogin = async () => { await LoginApi.socialLogin(type, code, state) - router.push({ path: redirect || '/' }) + await router.push(redirect || '/') } catch (err) {} } @@ -303,7 +301,7 @@ const handleLogin = async (params) => { if (redirect.indexOf('sso') !== -1) { window.location.href = window.location.href.replace('/login?redirect=', '') } else { - push({ path: redirect || permissionStore.addRouters[0].path }) + await push(redirect) } } finally { loginLoading.value = false diff --git a/yshop-drink-vue3/src/views/Login/components/LoginForm.vue b/yshop-drink-vue3/src/views/Login/components/LoginForm.vue index 5fd4061..ca855e6 100644 --- a/yshop-drink-vue3/src/views/Login/components/LoginForm.vue +++ b/yshop-drink-vue3/src/views/Login/components/LoginForm.vue @@ -90,7 +90,6 @@ import type { RouteLocationNormalizedLoaded } from 'vue-router' import { useIcon } from '@/hooks/web/useIcon' import * as authUtil from '@/utils/auth' -import { usePermissionStore } from '@/store/modules/permission' import * as LoginApi from '@/api/login' import { LoginStateEnum, useFormValid, useLoginState } from './useLogin' @@ -102,7 +101,6 @@ const formLogin = ref() const { validForm } = useFormValid(formLogin) const { getLoginState } = useLoginState() const { currentRoute, push } = useRouter() -const permissionStore = usePermissionStore() const redirect = ref('') const loginLoading = ref(false) const verify = ref() @@ -193,7 +191,7 @@ const handleLogin = async (params) => { if (redirect.value.indexOf('sso') !== -1) { window.location.href = window.location.href.replace('/login?redirect=', '') } else { - push({ path: redirect.value || permissionStore.addRouters[0].path }) + await push(redirect.value) } } catch { loginLoading.value = false diff --git a/yshop-drink-vue3/src/views/Login/components/MobileForm.vue b/yshop-drink-vue3/src/views/Login/components/MobileForm.vue index e8fb83b..6186e41 100644 --- a/yshop-drink-vue3/src/views/Login/components/MobileForm.vue +++ b/yshop-drink-vue3/src/views/Login/components/MobileForm.vue @@ -98,7 +98,6 @@ import type { RouteLocationNormalizedLoaded } from 'vue-router' import { useIcon } from '@/hooks/web/useIcon' import { setTenantId } from '@/utils/auth' -import { usePermissionStore } from '@/store/modules/permission' import { getTenantIdByName, sendSmsCode, smsLogin } from '@/api/login' import LoginFormTitle from './LoginFormTitle.vue' import { LoginStateEnum, useFormValid, useLoginState } from './useLogin' @@ -108,7 +107,6 @@ defineOptions({ name: 'MobileForm' }) const { t } = useI18n() const message = useMessage() -const permissionStore = usePermissionStore() const { currentRoute, push } = useRouter() const formSmsLogin = ref() const loginLoading = ref(false) @@ -199,7 +197,7 @@ const signIn = async () => { if (!redirect.value) { redirect.value = '/' } - push({ path: redirect.value || permissionStore.addRouters[0].path }) + await push(redirect.value) }) .catch(() => {}) .finally(() => {