From d054908b30e8eb7b147acad24ff3d19319f8ea73 Mon Sep 17 00:00:00 2001 From: zhenhui <1276357500@qq.com> Date: Mon, 27 Jul 2026 10:21:35 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=9F=E4=B8=80=E5=89=8D=E7=AB=AF=E7=9A=84?= =?UTF-8?q?=E8=AE=BF=E9=97=AE=E5=9C=B0=E5=9D=80=E6=A0=BC=E5=BC=8F=E4=B8=BA?= =?UTF-8?q?/#/?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web/index.html | 14 +++++++++++++ web/src/router/index.ts | 5 +++++ web/src/utils/axios.ts | 9 ++++++-- web/src/utils/hashRouteUrl.ts | 39 +++++++++++++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 web/src/utils/hashRouteUrl.ts diff --git a/web/index.html b/web/index.html index badfa36..b97d533 100644 --- a/web/index.html +++ b/web/index.html @@ -11,6 +11,20 @@
+ diff --git a/web/src/router/index.ts b/web/src/router/index.ts index 694dbde..354b9a1 100644 --- a/web/src/router/index.ts +++ b/web/src/router/index.ts @@ -9,8 +9,12 @@ import { mergeMessage } from '/@/lang/index' import { useConfig } from '/@/stores/config' import { useAdminInfo } from '/@/stores/adminInfo' import { isAdminApp } from '/@/utils/common' +import { ensureStandardHashUrl, redirectRootToAdminLoginIfNoHash } from '/@/utils/hashRouteUrl' import { uniq } from 'lodash-es' +redirectRootToAdminLoginIfNoHash() +ensureStandardHashUrl() + const router = createRouter({ history: createWebHashHistory(), routes: staticRoutes, @@ -84,6 +88,7 @@ router.beforeEach((to, from, next) => { // 路由加载后 router.afterEach(() => { + ensureStandardHashUrl() if (window.existLoading) { loading.hide() } diff --git a/web/src/utils/axios.ts b/web/src/utils/axios.ts index 8cd0e54..76ad2f2 100644 --- a/web/src/utils/axios.ts +++ b/web/src/utils/axios.ts @@ -285,12 +285,17 @@ function redirectToLoginIfNeedLoginFromHttp303(error: any): boolean { } const isAdminAppFlag = isAdminApp() const loginRouteName = isAdminAppFlag ? 'adminLogin' : 'userLogin' + const adminInfo = useAdminInfo() + const userInfo = useUserInfo() if (router.currentRoute.value.name === loginRouteName) { + if (isAdminAppFlag) { + adminInfo.removeToken() + } else { + userInfo.removeToken() + } return true } window.authRedirecting = true - const adminInfo = useAdminInfo() - const userInfo = useUserInfo() if (isAdminAppFlag) { adminInfo.removeToken() router.replace({ name: loginRouteName }).finally(() => { diff --git a/web/src/utils/hashRouteUrl.ts b/web/src/utils/hashRouteUrl.ts new file mode 100644 index 0000000..75897ab --- /dev/null +++ b/web/src/utils/hashRouteUrl.ts @@ -0,0 +1,39 @@ +import { adminBaseRoutePath } from '/@/router/static/adminBase' + +function shouldStripQuestionBeforeHash(): boolean { + const { search, hash } = window.location + if (!hash.startsWith('#/')) { + return false + } + if (search === '?') { + return true + } + return window.location.href.includes('/?#/') +} + +/** 将 `/?#/path` 规范为 `/#/path` */ +export function ensureStandardHashUrl(): void { + if (typeof window === 'undefined') { + return + } + const { pathname } = window.location + const onAppRoot = pathname === '/' || pathname === '' + if (!onAppRoot || !shouldStripQuestionBeforeHash()) { + return + } + const hash = window.location.hash + window.history.replaceState(window.history.state, '', pathname + hash) +} + +/** 根路径无 hash 时跳转到后台登录(`/#/admin/login`) */ +export function redirectRootToAdminLoginIfNoHash(): void { + if (typeof window === 'undefined') { + return + } + const { pathname, hash } = window.location + const onAppRoot = pathname === '/' || pathname === '' + if (!onAppRoot || hash) { + return + } + window.location.replace(pathname + '#' + adminBaseRoutePath + '/login') +}