hejianhao
2026-08-25 4246e1ca86d5c74e6395d94bbfb1538f166a2f4f
fix0806: 安卓插件升级
1个文件已修改
94 ■■■■■ 已修改文件
traditionalApp/pages/webview/webview.vue 94 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
traditionalApp/pages/webview/webview.vue
@@ -9,36 +9,16 @@
<script setup lang="ts">
import { ref } from 'vue'
import { onLoad, onUnload } from '@dcloudio/uni-app'
import { clearWebViewData } from '@/utils/webview-cache.js'
// 微信开放平台 appId(见 manifest.json app-plus.distribute.sdkConfigs.share.weixin)
const WEIXIN_APP_ID = 'wx074c1f0cf5a0ecf4'
import { onLoad } from '@dcloudio/uni-app'
const webViewUrl = ref('')
// 进入微赞直播页前记录原始 UA,退出时还原,避免全局 UA 常驻影响其它 webview
let originalUserAgent = ''
/**
 * 页面加载时读取外链地址与标题,并在加载 URL 前清理一次缓存避免上次登录态残留。
 * 页面加载时读取外链地址与标题。
 */
onLoad((options) => {
    const resolvedUrl = decodeQueryValue(options?.url)
    const resolvedTitle = decodeQueryValue(options?.title)
    // 仅在入口显式要求时清理(如进入微赞直播间);直播订单、直播统计等无需清理
    const shouldClearCache = String(options?.clearCache || '') === '1'
    // #ifdef APP-PLUS
    // 加载 URL 前先清理所有 webview 的缓存、Cookie 和存储,避免上次登录态残留
    if (shouldClearCache) {
        clearWebViewData()
        console.log('[WebView] 进入前缓存已清理,URL:', resolvedUrl)
        // 仅微赞直播间入口注入带 webchatPay/{appId} 的 UA,微赞 H5 靠它开启唤起支付逻辑。
        // 必须在给 web-view 赋值 URL(触发渲染)之前设置,底层 WebView 创建时才会带上该 UA。
        injectWeizanUserAgent()
        logCurrentUserAgent()
    }
    // #endif
    webViewUrl.value = resolvedUrl
    if (resolvedTitle) {
@@ -46,77 +26,7 @@
            title: resolvedTitle,
        })
    }
    console.log('[WebView] 页面加载完成,URL:', resolvedUrl)
})
/**
 * 页面卸载时不再清理缓存:清理集中在进入直播间前与退出登录时执行。
 */
onUnload(() => {
    console.log('[WebView] 页面卸载')
    // #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
}
/**
 * 解码路由参数,异常时回退为空字符串。