统一前端的访问地址格式为/#/

This commit is contained in:
2026-07-27 10:21:35 +08:00
parent e3d4ec4624
commit d054908b30
4 changed files with 65 additions and 2 deletions

View File

@@ -11,6 +11,20 @@
</head> </head>
<body> <body>
<div id="app"></div> <div id="app"></div>
<script>
;(function () {
var path = window.location.pathname
if (path !== '/' && path !== '') return
var hash = window.location.hash
if (!hash) {
window.location.replace(path + '#/admin/login')
return
}
if (hash.indexOf('#/') === 0 && (window.location.search === '?' || window.location.href.indexOf('/?#/') !== -1)) {
window.history.replaceState(null, '', path + hash)
}
})()
</script>
<script type="module" src="/src/main.ts"></script> <script type="module" src="/src/main.ts"></script>
</body> </body>
</html> </html>

View File

@@ -9,8 +9,12 @@ import { mergeMessage } from '/@/lang/index'
import { useConfig } from '/@/stores/config' import { useConfig } from '/@/stores/config'
import { useAdminInfo } from '/@/stores/adminInfo' import { useAdminInfo } from '/@/stores/adminInfo'
import { isAdminApp } from '/@/utils/common' import { isAdminApp } from '/@/utils/common'
import { ensureStandardHashUrl, redirectRootToAdminLoginIfNoHash } from '/@/utils/hashRouteUrl'
import { uniq } from 'lodash-es' import { uniq } from 'lodash-es'
redirectRootToAdminLoginIfNoHash()
ensureStandardHashUrl()
const router = createRouter({ const router = createRouter({
history: createWebHashHistory(), history: createWebHashHistory(),
routes: staticRoutes, routes: staticRoutes,
@@ -84,6 +88,7 @@ router.beforeEach((to, from, next) => {
// 路由加载后 // 路由加载后
router.afterEach(() => { router.afterEach(() => {
ensureStandardHashUrl()
if (window.existLoading) { if (window.existLoading) {
loading.hide() loading.hide()
} }

View File

@@ -285,12 +285,17 @@ function redirectToLoginIfNeedLoginFromHttp303(error: any): boolean {
} }
const isAdminAppFlag = isAdminApp() const isAdminAppFlag = isAdminApp()
const loginRouteName = isAdminAppFlag ? 'adminLogin' : 'userLogin' const loginRouteName = isAdminAppFlag ? 'adminLogin' : 'userLogin'
const adminInfo = useAdminInfo()
const userInfo = useUserInfo()
if (router.currentRoute.value.name === loginRouteName) { if (router.currentRoute.value.name === loginRouteName) {
if (isAdminAppFlag) {
adminInfo.removeToken()
} else {
userInfo.removeToken()
}
return true return true
} }
window.authRedirecting = true window.authRedirecting = true
const adminInfo = useAdminInfo()
const userInfo = useUserInfo()
if (isAdminAppFlag) { if (isAdminAppFlag) {
adminInfo.removeToken() adminInfo.removeToken()
router.replace({ name: loginRouteName }).finally(() => { router.replace({ name: loginRouteName }).finally(() => {

View File

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