| | |
| | | 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({ |
| | |
| | | }) |
| | | |
| | | /** |
| | | * 清除 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) { |