修复讲解列表返回导览首页

This commit is contained in:
cxk
2026-07-20 02:43:37 +08:00
parent b58f838624
commit 153be754a9
2 changed files with 52 additions and 9 deletions

View File

@@ -79,6 +79,7 @@ type ExplainListHistoryState = {
}
let explainListHistoryPushed = false
let isConsumingExplainListHistory = false
let isReturningToGuideHome = false
type PageStackEntry = {
@@ -96,6 +97,14 @@ const guideHomeUrl = () => (
shouldUseHostNavigation.value ? guideTopTabUrl('guide') : GUIDE_HOME_URL
)
const isExplainListHistoryState = (state: unknown): state is ExplainListHistoryState => (
Boolean(
state
&& typeof state === 'object'
&& (state as ExplainListHistoryState).museumGuidePage === 'explain-list'
)
)
const showGuideHomeReturnError = () => {
isReturningToGuideHome = false
uni.showToast({
@@ -111,8 +120,22 @@ const reLaunchGuideHome = () => {
})
}
const consumeExplainListHistory = () => {
if (
typeof window === 'undefined'
|| !explainListHistoryPushed
|| !isExplainListHistoryState(window.history.state)
) return false
// 根栈压入了同 URL 标记,先消费它,避免回首页后再次回流到讲解列表。
isConsumingExplainListHistory = true
window.history.back()
return true
}
const returnToGuideHome = () => {
if (isReturningToGuideHome) return
if (isReturningToGuideHome || isConsumingExplainListHistory) return
if (consumeExplainListHistory()) return
isReturningToGuideHome = true
const pages = getPageStack()
@@ -138,14 +161,6 @@ const returnToGuideHome = () => {
reLaunchGuideHome()
}
const isExplainListHistoryState = (state: unknown): state is ExplainListHistoryState => (
Boolean(
state
&& typeof state === 'object'
&& (state as ExplainListHistoryState).museumGuidePage === 'explain-list'
)
)
const pushExplainListHistory = () => {
if (typeof window === 'undefined' || getPageStack().length > 1) return
if (explainListHistoryPushed) return
@@ -168,10 +183,12 @@ const handleExplainListHistoryPopState = (event: PopStateEvent) => {
if (isExplainListHistoryState(event.state)) {
explainListHistoryPushed = true
isConsumingExplainListHistory = false
return
}
explainListHistoryPushed = false
isConsumingExplainListHistory = false
returnToGuideHome()
}

View File

@@ -234,6 +234,32 @@ describe('讲解展厅列表返回', () => {
wrapper.unmount()
})
it('根栈页内返回先消费历史标记,再回到导览首页', async () => {
let currentHistoryState: Record<string, unknown> = {}
vi.spyOn(window.history, 'state', 'get').mockImplementation(() => currentHistoryState)
vi.spyOn(window.history, 'pushState').mockImplementation((state) => {
currentHistoryState = (state || {}) as Record<string, unknown>
})
const wrapper = mountExplainList()
const historyBack = vi.spyOn(window.history, 'back').mockImplementation(() => {
currentHistoryState = {}
window.dispatchEvent(new PopStateEvent('popstate', { state: currentHistoryState }))
})
testState.onLoadHandler?.()
expect(window.history.state).toMatchObject({ museumGuidePage: 'explain-list' })
await wrapper.get('[data-testid="explain-list-back"]').trigger('click')
await flushPromises()
expect(historyBack).toHaveBeenCalledTimes(1)
expect(uni.reLaunch).toHaveBeenCalledTimes(1)
expect(uni.reLaunch).toHaveBeenCalledWith(expect.objectContaining({
url: '/pages/index/index?tab=guide'
}))
wrapper.unmount()
})
it('普通 H5 浏览器返回也会回到导览首页,且不调用 history.back', async () => {
const historyBack = vi.spyOn(window.history, 'back')
const wrapper = mountExplainList()