| | |
| | | */ |
| | | function clearWebViewCache() { |
| | | console.log('[clearWebViewCache] 函数被调用') |
| | | |
| | | let logMessages = [] |
| | | logMessages.push('开始清除缓存') |
| | | |
| | | try { |
| | | // 清除 uni.storage(所有环境通用) |
| | | try { |
| | | uni.clearStorageSync() |
| | | console.log('[clearWebViewCache] uni.storage 已清除') |
| | | logMessages.push('uni.storage 已清除') |
| | | } catch (e) { |
| | | console.error('[clearWebViewCache] 清除 uni.storage 失败:', e) |
| | | logMessages.push('uni.storage 清除失败') |
| | | } |
| | | |
| | | // #ifdef APP-PLUS |
| | | console.log('[clearWebViewCache] APP-PLUS 环境,开始清除 WebView 缓存') |
| | | logMessages.push('APP-PLUS 环境检测到') |
| | | |
| | | // 清除 localStorage |
| | | // 清除 plus.storage |
| | | if (plus.storage) { |
| | | plus.storage.clear() |
| | | console.log('[clearWebViewCache] localStorage 已清除') |
| | | console.log('[clearWebViewCache] plus.storage 已清除') |
| | | logMessages.push('plus.storage 已清除') |
| | | } |
| | | |
| | | // 清除 WebView 缓存 |
| | | const currentWebview = plus.webview.currentWebview() |
| | | if (currentWebview && typeof currentWebview.clearCache === 'function') { |
| | | currentWebview.clearCache() |
| | | console.log('[clearWebViewCache] WebView 缓存已清除') |
| | | console.log('[clearWebViewCache] 当前 WebView 缓存已清除') |
| | | logMessages.push('当前 WebView 缓存已清除') |
| | | } |
| | | |
| | | // 清除所有 WebView 的缓存 |
| | | const allWebviews = plus.webview.all() |
| | | if (allWebviews && allWebviews.length > 0) { |
| | | allWebviews.forEach((webview) => { |
| | | try { |
| | | // 清除缓存 |
| | | if (webview && typeof webview.clearCache === 'function') { |
| | | webview.clearCache() |
| | | } |
| | | }) |
| | | console.log('[clearWebViewCache] 所有 WebView 缓存已清除') |
| | | |
| | | // 清除 Cookie 和存储 |
| | | if (webview && typeof webview.evalJS === 'function') { |
| | | webview.evalJS(` |
| | | try { |
| | | // 清除所有 Cookie |
| | | 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('[clearWebViewCache] 处理 WebView 失败:', e) |
| | | } |
| | | }) |
| | | console.log('[clearWebViewCache] 所有 WebView 缓存和 Cookie 已清除,共 ' + allWebviews.length + ' 个') |
| | | logMessages.push(`所有 WebView 已清除 (${allWebviews.length}个)`) |
| | | } |
| | | // #endif |
| | | |
| | | // #ifndef APP-PLUS |
| | | console.log('[clearWebViewCache] 非 APP-PLUS 环境,跳过清除') |
| | | console.log('[clearWebViewCache] 非 APP-PLUS 环境,已清除 uni.storage') |
| | | logMessages.push('非 APP-PLUS 环境') |
| | | // #endif |
| | | |
| | | // 显示清除结果 |
| | | uni.showToast({ |
| | | title: '缓存已清除', |
| | | icon: 'success', |
| | | duration: 2000 |
| | | }) |
| | | |
| | | } catch (error) { |
| | | console.error('[clearWebViewCache] 清除缓存失败:', error) |
| | | logMessages.push('异常: ' + error.message) |
| | | |
| | | uni.showToast({ |
| | | title: '缓存清除失败', |
| | | icon: 'none', |
| | | duration: 2000 |
| | | }) |
| | | } |
| | | |
| | | // 开发调试:显示详细日志 |
| | | console.log('[clearWebViewCache] 执行完成,日志:', logMessages.join(' | ')) |
| | | } |
| | | |
| | | /** |