修复微信内嵌返回层级

This commit is contained in:
lyf
2026-07-14 10:58:41 +08:00
parent 267b415673
commit 1f72926f58
5 changed files with 409 additions and 50 deletions

View File

@@ -307,6 +307,7 @@ import {
isGuideTopTab,
type GuideTopTab
} from '@/utils/guideTopTabs'
import { isEmbeddedInWechatMiniProgram } from '@/utils/hostEnvironment'
import {
guideUseCase
} from '@/usecases/guideUseCase'
@@ -418,7 +419,7 @@ const guideMapShellRef = ref<{
} | null>(null)
const homeSearchPanelRef = ref<{
collapseHomePanel?: () => void
resetSearchState?: () => void
resetSearchState?: () => Promise<void> | void
returnToCollapsedAfterDetail?: () => Promise<void>
restoreResultListScroll?: () => Promise<void>
} | null>(null)
@@ -430,6 +431,14 @@ let searchTargetFocusRequestId = 0
let pendingCollapseSearchAfterDetail = false
let pendingSearchDetailFocusRequest: TargetPoiFocusRequestViewModel | null = null
type PoiSearchOverlayHistoryState = {
museumGuideOverlay?: 'poi-search'
[key: string]: unknown
}
let poiSearchHistoryPushed = false
let removingPoiSearchHistory = false
let launchOverlayFallbackTimer: ReturnType<typeof setTimeout> | null = null
let launchOverlayFadeTimer: ReturnType<typeof setTimeout> | null = null
let launchOverlayHideDelayTimer: ReturnType<typeof setTimeout> | null = null
@@ -725,6 +734,87 @@ const syncTopTabToUrl = (tabId: GuideTopTab) => {
}
}
const isPoiSearchOverlayHistoryState = (state: unknown): state is PoiSearchOverlayHistoryState => (
Boolean(
state
&& typeof state === 'object'
&& (state as PoiSearchOverlayHistoryState).museumGuideOverlay === 'poi-search'
)
)
const canManagePoiSearchOverlayHistory = () => (
typeof window !== 'undefined'
&& isIndexHashRoute()
&& isEmbeddedInWechatMiniProgram()
)
const removePoiSearchOverlayFromCurrentHistoryState = () => {
if (!canManagePoiSearchOverlayHistory()) return
if (!isPoiSearchOverlayHistoryState(window.history.state)) return
const nextState = { ...window.history.state }
delete nextState.museumGuideOverlay
window.history.replaceState(nextState, '', window.location.href)
}
const pushPoiSearchOverlayHistory = () => {
if (!canManagePoiSearchOverlayHistory() || !homeSearchExpanded.value) return
if (poiSearchHistoryPushed) return
if (isPoiSearchOverlayHistoryState(window.history.state)) {
poiSearchHistoryPushed = true
return
}
window.history.pushState({
...(window.history.state || {}),
museumGuideOverlay: 'poi-search'
} satisfies PoiSearchOverlayHistoryState, '', window.location.href)
poiSearchHistoryPushed = true
}
const removePoiSearchOverlayHistory = () => {
if (!canManagePoiSearchOverlayHistory() || !poiSearchHistoryPushed) return
if (!isPoiSearchOverlayHistoryState(window.history.state)) {
poiSearchHistoryPushed = false
return
}
poiSearchHistoryPushed = false
removingPoiSearchHistory = true
window.history.back()
}
const collapsePoiSearchAfterHistoryBack = async () => {
homeSearchPanelRef.value?.collapseHomePanel?.()
await homeSearchPanelRef.value?.resetSearchState?.()
clearHomeSearchMapState()
selectedGuidePoi.value = null
isPoiCardCollapsed.value = false
homeSearchExpanded.value = false
}
const handlePoiSearchHistoryPopState = (event: PopStateEvent) => {
if (!canManagePoiSearchOverlayHistory()) return
if (removingPoiSearchHistory) {
removingPoiSearchHistory = false
poiSearchHistoryPushed = false
if (homeSearchExpanded.value) pushPoiSearchOverlayHistory()
return
}
if (!homeSearchExpanded.value) return
if (isPoiSearchOverlayHistoryState(event.state)) {
poiSearchHistoryPushed = true
return
}
poiSearchHistoryPushed = false
void collapsePoiSearchAfterHistoryBack()
}
const loadTopTabData = (tabId: GuideTopTab) => {
if (tabId === 'explain') {
void loadExplainHalls()
@@ -786,7 +876,9 @@ onMounted(() => {
if (typeof window === 'undefined') return
syncTopTabFromHash()
removePoiSearchOverlayFromCurrentHistoryState()
window.addEventListener('hashchange', syncTopTabFromHash)
window.addEventListener('popstate', handlePoiSearchHistoryPopState)
})
onUnmounted(() => {
@@ -800,6 +892,7 @@ onUnmounted(() => {
if (typeof window === 'undefined') return
window.removeEventListener('hashchange', syncTopTabFromHash)
window.removeEventListener('popstate', handlePoiSearchHistoryPopState)
})
onLoad((options: any = {}) => {
@@ -1412,6 +1505,12 @@ const handleExplainQuickTap = () => {
const handleHomeSearchExpandedChange = (expanded: boolean) => {
homeSearchExpanded.value = expanded
if (expanded) {
pushPoiSearchOverlayHistory()
return
}
removePoiSearchOverlayHistory()
}
const clearHomeSearchMapState = () => {