统一前端的访问地址格式为/#/
This commit is contained in:
@@ -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>
|
||||||
|
|||||||
@@ -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()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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(() => {
|
||||||
|
|||||||
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