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') +}