Procházet zdrojové kódy

加载 url 添加时间戳

cc12458 před 1 měsícem
rodič
revize
80c890efbd

+ 16 - 1
library/browser/src/main/java/com/hzliuzhi/applet/browser/webview/WebViewNavigator.kt

@@ -4,11 +4,13 @@ import android.webkit.WebView
 import androidx.compose.runtime.getValue
 import androidx.compose.runtime.mutableStateOf
 import androidx.compose.runtime.setValue
+import androidx.core.net.toUri
 import kotlinx.coroutines.CoroutineScope
 import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.flow.MutableSharedFlow
 import kotlinx.coroutines.launch
 import kotlinx.coroutines.withContext
+import kotlin.random.Random
 
 class WebViewNavigator(
   private val coroutineScope: CoroutineScope,
@@ -40,7 +42,7 @@ class WebViewNavigator(
         Event.Forward -> goForward()
         Event.Reload -> reload()
         Event.Stop -> stopLoading()
-        is Event.LoadUrl -> loadUrl(it.url, it.additionalHttpHeaders ?: emptyMap())
+        is Event.LoadUrl -> loadUrl(it.url.timestamp(), it.additionalHttpHeaders ?: emptyMap())
         else -> {}
       }
     }
@@ -69,4 +71,17 @@ class WebViewNavigator(
       }
     }
   }
+
+  private fun String.timestamp(): String {
+    return try {
+      val uri = toUri()
+      val timestamp = System.currentTimeMillis()
+      return uri.buildUpon().apply {
+        appendQueryParameter("_t", timestamp.toString())
+        appendQueryParameter("_r", Random.nextInt(1000).toString())
+      }.build().toString()
+    } catch (e: Exception) {
+      this
+    }
+  }
 }