修复微信内嵌返回层级

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

@@ -1,5 +1,12 @@
export type GuideTopTab = 'guide' | 'explain'
export const GUIDE_LOCATION_PREVIEW_STORAGE_KEY = 'museum-guide:last-location-preview-url'
const HOST_NAVIGATION_QUERY_KEYS = [
'embedded',
'embed',
'host',
'weapp',
'mini-program'
]
export const GUIDE_TOP_TABS: Array<{ id: GuideTopTab; label: string }> = [
{ id: 'guide', label: '馆内' },
@@ -10,7 +17,32 @@ export const isGuideTopTab = (value: unknown): value is GuideTopTab => {
return value === 'guide' || value === 'explain'
}
export const guideTopTabUrl = (tab: GuideTopTab) => `/pages/index/index?tab=${tab}`
const getHostNavigationQueryParams = () => {
if (typeof window === 'undefined') return new URLSearchParams()
const searchParams = new URLSearchParams(window.location.search)
const hash = window.location.hash || ''
const hashQueryStart = hash.indexOf('?')
if (hashQueryStart >= 0) {
const hashParams = new URLSearchParams(hash.slice(hashQueryStart + 1))
hashParams.forEach((value, key) => {
if (!searchParams.has(key)) searchParams.set(key, value)
})
}
const hostParams = new URLSearchParams()
HOST_NAVIGATION_QUERY_KEYS.forEach((key) => {
const value = searchParams.get(key)
if (value !== null) hostParams.set(key, value)
})
return hostParams
}
export const guideTopTabUrl = (tab: GuideTopTab) => {
const params = new URLSearchParams({ tab })
getHostNavigationQueryParams().forEach((value, key) => params.set(key, value))
return `/pages/index/index?${params.toString()}`
}
export const saveLastGuideLocationPreviewUrl = (url: string) => {
if (typeof window === 'undefined' || !url) return