From 76a73188383649e1bf6c27cf67540d68ad0378cd Mon Sep 17 00:00:00 2001
From: quling <377956897@qq.com>
Date: 星期五, 21 八月 2026 09:43:34 +0800
Subject: [PATCH] fix0806: 进入微赞直播间时清理缓存

---
 traditionalApp/pages/webview/webview.vue |   62 ++++++++++++++++--------------
 1 files changed, 33 insertions(+), 29 deletions(-)

diff --git a/traditionalApp/pages/webview/webview.vue b/traditionalApp/pages/webview/webview.vue
index f12bf32..2314e1a 100644
--- a/traditionalApp/pages/webview/webview.vue
+++ b/traditionalApp/pages/webview/webview.vue
@@ -15,11 +15,18 @@
 let currentWebviewInstance: any = null
 
 /**
- * 页面加载时读取外链地址与标题。
+ * 页面加载时读取外链地址与标题,并在加载 URL 前清理一次缓存避免上次登录态残留。
  */
 onLoad((options) => {
 	const resolvedUrl = decodeQueryValue(options?.url)
 	const resolvedTitle = decodeQueryValue(options?.title)
+
+	// #ifdef APP-PLUS
+	// 加载 URL 前先清理所有 webview 的缓存、Cookie 和存储,避免上次登录态残留
+	clearWebViewData()
+	console.log('[WebView] 进入前缓存已清理,URL:', resolvedUrl)
+	// #endif
+
 	webViewUrl.value = resolvedUrl
 	if (resolvedTitle) {
 		uni.setNavigationBarTitle({
@@ -45,43 +52,40 @@
 })
 
 /**
- * 清除 WebView 的缓存、Cookie 和存储数据
+ * 清除所有 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()
+				try {
+					// 清除 WebView 缓存
+					if (webview && typeof webview.clearCache === 'function') {
+						webview.clearCache()
+					}
+
+					// 清除 Cookie、localStorage 和 sessionStorage
+					if (webview && typeof webview.evalJS === 'function') {
+						webview.evalJS(`
+							try {
+								document.cookie.split(";").forEach(function(c) {
+									document.cookie = c.replace(/^ +/, "").replace(/=.*/, "=;expires=" + new Date().toUTCString() + ";path=/");
+								});
+								if (window.localStorage) localStorage.clear();
+								if (window.sessionStorage) sessionStorage.clear();
+								console.log('[WebView.evalJS] Cookie 和存储已清除');
+							} catch (e) {
+								console.error('[WebView.evalJS] 清除失败:', e);
+							}
+						`)
+					}
+				} catch (e) {
+					console.error('[WebView] 处理 WebView 失败:', e)
 				}
 			})
-			console.log('[WebView] 所有 WebView 缓存已清除')
+			console.log('[WebView] 所有 WebView 缓存、Cookie 和存储已清除,共 ' + allWebviews.length + ' 个')
 		}
 		// #endif
 	} catch (error) {

--
Gitblit v1.7.1