统一前端的访问地址格式为/#/
This commit is contained in:
@@ -11,6 +11,20 @@
|
||||
</head>
|
||||
<body>
|
||||
<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>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
@@ -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(() => {
|
||||
|
||||
39
web/src/utils/hashRouteUrl.ts
Normal file
39
web/src/utils/hashRouteUrl.ts
Normal 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')
|
||||
}
|
||||
Reference in New Issue
Block a user