@@ -38,7 +38,10 @@ import {
|
||||
navigateToGuideTopTab,
|
||||
type GuideTopTab
|
||||
} from '@/utils/guideTopTabs'
|
||||
import { isEmbeddedInWechatMiniProgram } from '@/utils/hostEnvironment'
|
||||
import {
|
||||
isEmbeddedInWechatMiniProgram,
|
||||
navigateBackToWechatMiniProgram
|
||||
} from '@/utils/hostEnvironment'
|
||||
import { syncHostNavigationTitle } from '@/utils/hostNavigationTitle'
|
||||
import { explainGuideStopListUrl } from '@/utils/explainNavigation'
|
||||
|
||||
@@ -71,8 +74,6 @@ const buildExplainHallItems = (
|
||||
|
||||
const shouldUseHostNavigation = computed(() => isEmbeddedInWechatMiniProgram())
|
||||
|
||||
const GUIDE_HOME_URL = '/pages/index/index?tab=guide'
|
||||
|
||||
type ExplainListHistoryState = {
|
||||
museumGuidePage?: 'explain-list'
|
||||
[key: string]: unknown
|
||||
@@ -80,7 +81,7 @@ type ExplainListHistoryState = {
|
||||
|
||||
let explainListHistoryPushed = false
|
||||
let isConsumingExplainListHistory = false
|
||||
let isReturningToGuideHome = false
|
||||
let isReturningFromExplainList = false
|
||||
|
||||
type PageStackEntry = {
|
||||
route?: string
|
||||
@@ -93,9 +94,8 @@ const getPageStack = () => (
|
||||
typeof getCurrentPages === 'function' ? getCurrentPages() as PageStackEntry[] : []
|
||||
)
|
||||
|
||||
const guideHomeUrl = () => (
|
||||
shouldUseHostNavigation.value ? guideTopTabUrl('guide') : GUIDE_HOME_URL
|
||||
)
|
||||
const guideHomeUrl = () => guideTopTabUrl('guide')
|
||||
const explainHomeUrl = () => guideTopTabUrl('explain')
|
||||
|
||||
const isExplainListHistoryState = (state: unknown): state is ExplainListHistoryState => (
|
||||
Boolean(
|
||||
@@ -106,7 +106,7 @@ const isExplainListHistoryState = (state: unknown): state is ExplainListHistoryS
|
||||
)
|
||||
|
||||
const showGuideHomeReturnError = () => {
|
||||
isReturningToGuideHome = false
|
||||
isReturningFromExplainList = false
|
||||
uni.showToast({
|
||||
title: '返回导览首页失败,请重试',
|
||||
icon: 'none'
|
||||
@@ -120,6 +120,13 @@ const reLaunchGuideHome = () => {
|
||||
})
|
||||
}
|
||||
|
||||
const reLaunchExplainHome = () => {
|
||||
uni.reLaunch({
|
||||
url: explainHomeUrl(),
|
||||
fail: showGuideHomeReturnError
|
||||
})
|
||||
}
|
||||
|
||||
const consumeExplainListHistory = () => {
|
||||
if (
|
||||
typeof window === 'undefined'
|
||||
@@ -133,31 +140,55 @@ const consumeExplainListHistory = () => {
|
||||
return true
|
||||
}
|
||||
|
||||
const returnToGuideHome = () => {
|
||||
if (isReturningToGuideHome || isConsumingExplainListHistory) return
|
||||
if (consumeExplainListHistory()) return
|
||||
|
||||
isReturningToGuideHome = true
|
||||
const getPreviousPage = () => {
|
||||
const pages = getPageStack()
|
||||
const previousPage = pages.length > 1 ? pages[pages.length - 2] : null
|
||||
const previousRoute = previousPage?.route || ''
|
||||
return pages.length > 1 ? pages[pages.length - 2] : null
|
||||
}
|
||||
|
||||
const cameFromProjectExplainHome = () => {
|
||||
const previousPage = getPreviousPage()
|
||||
const previousTab = Array.isArray(previousPage?.options?.tab)
|
||||
? previousPage.options.tab[0]
|
||||
: previousPage?.options?.tab
|
||||
const canNavigateBackToGuideHome = (
|
||||
!shouldUseHostNavigation.value
|
||||
&& previousRoute === 'pages/index/index'
|
||||
&& (!previousTab || previousTab === 'guide')
|
||||
)
|
||||
|
||||
if (canNavigateBackToGuideHome) {
|
||||
uni.navigateBack({
|
||||
delta: 1,
|
||||
fail: reLaunchGuideHome
|
||||
})
|
||||
return previousPage?.route === 'pages/index/index' && previousTab === 'explain'
|
||||
}
|
||||
|
||||
const returnToProjectExplainHome = () => {
|
||||
isReturningFromExplainList = true
|
||||
uni.navigateBack({
|
||||
delta: 1,
|
||||
fail: reLaunchExplainHome
|
||||
})
|
||||
}
|
||||
|
||||
const returnFromStandaloneMiniProgramEntry = () => {
|
||||
isReturningFromExplainList = true
|
||||
if (navigateBackToWechatMiniProgram()) return
|
||||
|
||||
isReturningFromExplainList = false
|
||||
uni.showToast({
|
||||
title: '返回小程序失败,请重试',
|
||||
icon: 'none'
|
||||
})
|
||||
}
|
||||
|
||||
const returnToGuideHome = () => {
|
||||
if (isReturningFromExplainList || isConsumingExplainListHistory) return
|
||||
if (consumeExplainListHistory()) return
|
||||
|
||||
// 首页讲解 Tab 是项目内入口,优先恢复原页面而不是退出小程序。
|
||||
if (cameFromProjectExplainHome()) {
|
||||
returnToProjectExplainHome()
|
||||
return
|
||||
}
|
||||
|
||||
if (shouldUseHostNavigation.value) {
|
||||
returnFromStandaloneMiniProgramEntry()
|
||||
return
|
||||
}
|
||||
|
||||
isReturningFromExplainList = true
|
||||
reLaunchGuideHome()
|
||||
}
|
||||
|
||||
@@ -178,7 +209,7 @@ const pushExplainListHistory = () => {
|
||||
}
|
||||
|
||||
const handleExplainListHistoryPopState = (event: PopStateEvent) => {
|
||||
if (isReturningToGuideHome) return
|
||||
if (isReturningFromExplainList) return
|
||||
if (!explainListHistoryPushed) return
|
||||
|
||||
if (isExplainListHistoryState(event.state)) {
|
||||
|
||||
@@ -43,6 +43,28 @@ export const isEmbeddedInWechatMiniProgram = () => {
|
||||
return /miniProgram/i.test(window.navigator?.userAgent || '')
|
||||
}
|
||||
|
||||
type WechatMiniProgramNavigationBridge = {
|
||||
navigateBack?: (options: { delta: number }) => void
|
||||
}
|
||||
|
||||
export const navigateBackToWechatMiniProgram = () => {
|
||||
if (!isEmbeddedInWechatMiniProgram() || typeof window === 'undefined') return false
|
||||
|
||||
const bridge = (window as Window & {
|
||||
wx?: { miniProgram?: WechatMiniProgramNavigationBridge }
|
||||
}).wx?.miniProgram
|
||||
|
||||
if (typeof bridge?.navigateBack !== 'function') return false
|
||||
|
||||
try {
|
||||
bridge.navigateBack({ delta: 1 })
|
||||
return true
|
||||
} catch (error) {
|
||||
console.error('返回小程序失败:', error)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
export const isWechatMiniProgramRuntime = () => (
|
||||
isNativeWechatMiniProgram() || isEmbeddedInWechatMiniProgram()
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user