hejianhao
2026-08-21 6e14d5a97241462002fa26f9b4f2948efe5a9d70
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<template>
    <view class="container">
        <web-view v-if="webViewUrl" class="webview-content" :src="webViewUrl"></web-view>
        <view v-else class="empty-box">
            <text class="empty-text">链接地址无效</text>
        </view>
    </view>
</template>
 
<script setup lang="ts">
import { ref } from 'vue'
import { onLoad, onUnload } from '@dcloudio/uni-app'
 
const webViewUrl = ref('')
let currentWebviewInstance: any = null
 
/**
 * 页面加载时读取外链地址与标题。
 */
onLoad((options) => {
    const resolvedUrl = decodeQueryValue(options?.url)
    const resolvedTitle = decodeQueryValue(options?.title)
    webViewUrl.value = resolvedUrl
    if (resolvedTitle) {
        uni.setNavigationBarTitle({
            title: resolvedTitle,
        })
    }
 
    // #ifdef APP-PLUS
    // 获取当前 WebView 实例
    setTimeout(() => {
        currentWebviewInstance = plus.webview.currentWebview()
        console.log('[WebView] 页面加载完成,URL:', resolvedUrl)
    }, 100)
    // #endif
})
 
/**
 * 页面卸载时清除 WebView 缓存和 Cookie
 */
onUnload(() => {
    console.log('[WebView] 页面卸载,开始清除缓存')
    clearWebViewData()
})
 
/**
 * 清除 WebView 的缓存、Cookie 和存储数据
 */
function clearWebViewData() {
    try {
        // #ifdef APP-PLUS
        if (currentWebviewInstance) {
            // 清除 WebView 缓存
            if (typeof currentWebviewInstance.clearCache === 'function') {
                currentWebviewInstance.clearCache()
                console.log('[WebView] 缓存已清除')
            }
 
            // 清除 Cookie
            if (typeof currentWebviewInstance.evalJS === 'function') {
                // 通过 JS 清除 Cookie
                currentWebviewInstance.evalJS(`
                    document.cookie.split(";").forEach(function(c) {
                        document.cookie = c.replace(/^ +/, "").replace(/=.*/, "=;expires=" + new Date().toUTCString() + ";path=/");
                    });
                    // 清除 localStorage 和 sessionStorage
                    if (window.localStorage) localStorage.clear();
                    if (window.sessionStorage) sessionStorage.clear();
                    console.log('[WebView.evalJS] Cookie 和存储已清除');
                `)
                console.log('[WebView] Cookie 和存储已清除')
            }
        }
 
        // 清除所有 WebView 的缓存
        const allWebviews = plus.webview.all()
        if (allWebviews && allWebviews.length > 0) {
            allWebviews.forEach((webview) => {
                if (webview && typeof webview.clearCache === 'function') {
                    webview.clearCache()
                }
            })
            console.log('[WebView] 所有 WebView 缓存已清除')
        }
        // #endif
    } catch (error) {
        console.error('[WebView] 清除数据失败:', error)
    }
}
 
/**
 * 解码路由参数,异常时回退为空字符串。
 */
function decodeQueryValue(value: unknown) {
    if (typeof value !== 'string' || !value.trim()) {
        return ''
    }
    try {
        return decodeURIComponent(value)
    } catch {
        return value.trim()
    }
}
</script>
 
<style lang="scss" scoped>
.container {
    width: 100vw;
    min-height: 100vh;
    background-color: #ffffff;
}
 
.webview-content {
    width: 100%;
    height: 100vh;
}
 
.empty-box {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
}
 
.empty-text {
    font-size: 28rpx;
    color: #999999;
}
</style>