修复讲解展厅列表返回导览首页
Some checks failed
CI / verify (push) Has been cancelled

This commit is contained in:
lyf
2026-07-14 15:23:19 +08:00
parent ec4c3e36cd
commit 04c6ccd40b
2 changed files with 210 additions and 54 deletions

View File

@@ -21,7 +21,7 @@
<script setup lang="ts">
import { computed, onMounted, onUnmounted, ref } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import { onBackPress, onLoad } from '@dcloudio/uni-app'
import GuidePageFrame from '@/components/navigation/GuidePageFrame.vue'
import ExplainHallSelect, {
type ExplainHallSelectItem
@@ -33,6 +33,7 @@ import type {
MuseumHall
} from '@/domain/museum'
import {
guideTopTabUrl,
navigateToGuideTopTab,
type GuideTopTab
} from '@/utils/guideTopTabs'
@@ -68,13 +69,71 @@ const buildExplainHallItems = (
const shouldUseHostNavigation = computed(() => isEmbeddedInWechatMiniProgram())
const GUIDE_HOME_URL = '/pages/index/index?tab=guide'
type ExplainListHistoryState = {
museumGuidePage?: 'explain-list'
[key: string]: unknown
}
let explainListHistoryPushed = false
let returningFromExplainListHistory = false
let isReturningToGuideHome = false
type PageStackEntry = {
route?: string
options?: {
tab?: string | string[]
}
}
const getPageStack = () => (
typeof getCurrentPages === 'function' ? getCurrentPages() as PageStackEntry[] : []
)
const guideHomeUrl = () => (
shouldUseHostNavigation.value ? guideTopTabUrl('guide') : GUIDE_HOME_URL
)
const showGuideHomeReturnError = () => {
isReturningToGuideHome = false
uni.showToast({
title: '返回导览首页失败,请重试',
icon: 'none'
})
}
const reLaunchGuideHome = () => {
uni.reLaunch({
url: guideHomeUrl(),
fail: showGuideHomeReturnError
})
}
const returnToGuideHome = () => {
if (isReturningToGuideHome) return
isReturningToGuideHome = true
const pages = getPageStack()
const previousPage = pages.length > 1 ? pages[pages.length - 2] : null
const previousRoute = previousPage?.route || ''
const previousTab = Array.isArray(previousPage?.options?.tab)
? previousPage.options.tab[0]
: previousPage?.options?.tab
const canNavigateBackToGuideHome = (
previousRoute === 'pages/index/index'
&& (!previousTab || previousTab === 'guide')
)
if (canNavigateBackToGuideHome) {
uni.navigateBack({
delta: 1,
fail: reLaunchGuideHome
})
return
}
reLaunchGuideHome()
}
const isExplainListHistoryState = (state: unknown): state is ExplainListHistoryState => (
Boolean(
@@ -85,7 +144,7 @@ const isExplainListHistoryState = (state: unknown): state is ExplainListHistoryS
)
const pushExplainListHistory = () => {
if (!shouldUseHostNavigation.value || typeof window === 'undefined') return
if (typeof window === 'undefined' || getPageStack().length > 1) return
if (explainListHistoryPushed) return
if (isExplainListHistoryState(window.history.state)) {
@@ -101,7 +160,8 @@ const pushExplainListHistory = () => {
}
const handleExplainListHistoryPopState = (event: PopStateEvent) => {
if (!shouldUseHostNavigation.value || returningFromExplainListHistory) return
if (isReturningToGuideHome) return
if (!explainListHistoryPushed) return
if (isExplainListHistoryState(event.state)) {
explainListHistoryPushed = true
@@ -109,8 +169,7 @@ const handleExplainListHistoryPopState = (event: PopStateEvent) => {
}
explainListHistoryPushed = false
returningFromExplainListHistory = true
navigateToGuideTopTab('explain')
returnToGuideHome()
}
const loadExplainHalls = async () => {
@@ -153,26 +212,12 @@ onUnmounted(() => {
window.removeEventListener('popstate', handleExplainListHistoryPopState)
})
const handleBack = () => {
if (shouldUseHostNavigation.value) {
navigateToGuideTopTab('explain')
return
}
onBackPress(() => {
returnToGuideHome()
return true
})
uni.navigateBack({
delta: 1,
fail: () => {
uni.redirectTo({
url: '/pages/index/index?tab=explain',
fail: () => {
uni.reLaunch({
url: '/pages/index/index?tab=explain'
})
}
})
}
})
}
const handleBack = returnToGuideHome
const handleTopTabChange = (tab: GuideTopTab) => {
navigateToGuideTopTab(tab)