| | |
| | | import { clearWebViewData } from '@/utils/webview-cache.js' |
| | | import { registerWeizanPayBridge, unregisterWeizanPayBridge } from '@/utils/weizan-pay-bridge.js' |
| | | |
| | | // 微信开放平台 appId(见 manifest.json app-plus.distribute.sdkConfigs.share.weixin) |
| | | const WEIXIN_APP_ID = 'wx074c1f0cf5a0ecf4' |
| | | |
| | | const webViewUrl = ref('') |
| | | let currentWebviewInstance: any = null |
| | | // 进入微赞直播页前记录原始 UA,退出时还原,避免全局 UA 常驻影响其它 webview |
| | | let originalUserAgent = '' |
| | | |
| | | /** |
| | | * 页面加载时读取外链地址与标题,并在加载 URL 前清理一次缓存避免上次登录态残留。 |
| | |
| | | if (shouldClearCache) { |
| | | clearWebViewData() |
| | | console.log('[WebView] 进入前缓存已清理,URL:', resolvedUrl) |
| | | // 仅微赞直播间入口注入带 webchatPay/{appId} 的 UA,微赞 H5 靠它开启唤起支付逻辑。 |
| | | // 必须在给 web-view 赋值 URL(触发渲染)之前设置,底层 WebView 创建时才会带上该 UA。 |
| | | injectWeizanUserAgent() |
| | | logCurrentUserAgent() |
| | | } |
| | | // #endif |
| | | |
| | |
| | | if (currentWebviewInstance) { |
| | | unregisterWeizanPayBridge(currentWebviewInstance) |
| | | } |
| | | // #ifdef APP-PLUS |
| | | // 还原全局 UA,避免影响之后创建的其它 webview |
| | | restoreUserAgent() |
| | | // #endif |
| | | }) |
| | | |
| | | /** |
| | | * 注入带 webchatPay/{appId} 的全局 UA(方式 B)。 |
| | | * 微赞 H5 通过 UA 中的 webchatPay 标识判断当前处于可唤起小程序支付的 APP 环境。 |
| | | * 用全局 setUserAgent 是因为通用 <web-view> 组件无法单独设置 UA;副作用由 onUnload 还原兜住。 |
| | | */ |
| | | function injectWeizanUserAgent() { |
| | | // #ifdef APP-PLUS |
| | | try { |
| | | const baseUA = plus.navigator.getUserAgent() || '' |
| | | // 已包含标识则不重复追加,避免多次进入叠加 |
| | | if (baseUA.indexOf('webchatPay/') >= 0) { |
| | | originalUserAgent = baseUA.replace(/\s*webchatPay\/\S+/g, '') |
| | | console.log('[WebView] UA 已含 webchatPay,跳过追加。当前 UA:', baseUA) |
| | | return |
| | | } |
| | | originalUserAgent = baseUA |
| | | const nextUA = `${baseUA} webchatPay/${WEIXIN_APP_ID}`.trim() |
| | | // 第二参 false:不持久化,仅本次运行期生效 |
| | | plus.navigator.setUserAgent(nextUA, false) |
| | | console.log('[WebView] 已注入微赞支付 UA:', nextUA) |
| | | } catch (e) { |
| | | console.error('[WebView] 注入 UA 失败:', e) |
| | | } |
| | | // #endif |
| | | } |
| | | |
| | | /** |
| | | * 还原进入直播页前的原始 UA。 |
| | | */ |
| | | function restoreUserAgent() { |
| | | // #ifdef APP-PLUS |
| | | try { |
| | | if (!originalUserAgent) { |
| | | return |
| | | } |
| | | plus.navigator.setUserAgent(originalUserAgent, false) |
| | | console.log('[WebView] 已还原原始 UA:', originalUserAgent) |
| | | originalUserAgent = '' |
| | | } catch (e) { |
| | | console.error('[WebView] 还原 UA 失败:', e) |
| | | } |
| | | // #endif |
| | | } |
| | | |
| | | /** |
| | | * 输出进入直播间后底层 WebView 当前实际使用的完整 UA。 |
| | | */ |
| | | function logCurrentUserAgent() { |
| | | // #ifdef APP-PLUS |
| | | try { |
| | | console.log('[WebView] 进入直播间完整 UA:', plus.navigator.getUserAgent() || '') |
| | | } catch (e) { |
| | | console.error('[WebView] 读取完整 UA 失败:', e) |
| | | } |
| | | // #endif |
| | | } |
| | | |
| | | /** |
| | | * 解码路由参数,异常时回退为空字符串。 |
| | | */ |
| | | function decodeQueryValue(value: unknown) { |