From 6e14d5a97241462002fa26f9b4f2948efe5a9d70 Mon Sep 17 00:00:00 2001
From: hejianhao <15708179461@qq.com>
Date: 星期五, 21 八月 2026 09:34:52 +0800
Subject: [PATCH] feat0806: 直播间用户清理缓存

---
 traditionalApp/pages/webview/webview.vue |   64 +++++++++++++++++++++++++++++++
 1 files changed, 63 insertions(+), 1 deletions(-)

diff --git a/traditionalApp/pages/webview/webview.vue b/traditionalApp/pages/webview/webview.vue
index 2b6aa91..f12bf32 100644
--- a/traditionalApp/pages/webview/webview.vue
+++ b/traditionalApp/pages/webview/webview.vue
@@ -9,9 +9,10 @@
 
 <script setup lang="ts">
 import { ref } from 'vue'
-import { onLoad } from '@dcloudio/uni-app'
+import { onLoad, onUnload } from '@dcloudio/uni-app'
 
 const webViewUrl = ref('')
+let currentWebviewInstance: any = null
 
 /**
  * 页面加载时读取外链地址与标题。
@@ -25,9 +26,70 @@
 			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) {

--
Gitblit v1.7.1