- 识别微信小程序 web-view 宿主环境 - 宿主导航存在时隐藏 H5 自绘返回与页面标题 - 收回详情、搜索、讲解列表页面顶部预留空间 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
34
src/utils/hostEnvironment.ts
Normal file
34
src/utils/hostEnvironment.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
const getCurrentUrlSearchParams = () => {
|
||||
if (typeof window === 'undefined') return new URLSearchParams()
|
||||
|
||||
const params = 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 (!params.has(key)) params.set(key, value)
|
||||
})
|
||||
}
|
||||
|
||||
return params
|
||||
}
|
||||
|
||||
export const isEmbeddedInWechatMiniProgram = () => {
|
||||
if (typeof window === 'undefined') return false
|
||||
|
||||
const params = getCurrentUrlSearchParams()
|
||||
const embedded = params.get('embedded') || params.get('embed') || params.get('host')
|
||||
if (embedded === 'wechat-mini-program' || embedded === 'weapp' || embedded === 'mini-program') {
|
||||
return true
|
||||
}
|
||||
|
||||
const wxEnvironment = (window as Window & {
|
||||
__wxjs_environment?: string
|
||||
}).__wxjs_environment
|
||||
|
||||
if (wxEnvironment === 'miniprogram') return true
|
||||
|
||||
return /miniProgram/i.test(window.navigator?.userAgent || '')
|
||||
}
|
||||
Reference in New Issue
Block a user