From 85eb5ac72c5ab32508553246238e84c5f8dbbb5e Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 29 Jun 2024 19:18:30 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E9=AA=8C=E8=AF=81=E5=90=8E?= =?UTF-8?q?=E9=87=8D=E6=96=B0=E6=B7=BB=E5=8A=A0=E7=A6=BB=E7=BA=BF=E9=93=BE?= =?UTF-8?q?=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../nap511/screen/AllWebViewScreen.kt | 31 +++++++++++++++++-- .../nap511/screen/OfflineDownloadScreen.kt | 1 + .../nap511/viewmodel/OfflineFileViewModel.kt | 16 ++++++++++ 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/github/zerorooot/nap511/screen/AllWebViewScreen.kt b/app/src/main/java/github/zerorooot/nap511/screen/AllWebViewScreen.kt index 6a1e318..7afbe35 100644 --- a/app/src/main/java/github/zerorooot/nap511/screen/AllWebViewScreen.kt +++ b/app/src/main/java/github/zerorooot/nap511/screen/AllWebViewScreen.kt @@ -2,7 +2,6 @@ package github.zerorooot.nap511.screen import android.annotation.SuppressLint import android.webkit.CookieManager -import android.webkit.WebResourceRequest import android.webkit.WebResourceResponse import android.webkit.WebSettings import android.webkit.WebView @@ -21,10 +20,15 @@ import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier import androidx.compose.ui.res.stringResource import androidx.compose.ui.viewinterop.AndroidView +import androidx.work.Data +import androidx.work.OneTimeWorkRequest +import androidx.work.WorkManager import com.acsbendi.requestinspectorwebview.RequestInspectorWebViewClient import com.acsbendi.requestinspectorwebview.WebViewRequest import com.google.gson.Gson +import com.google.gson.reflect.TypeToken import github.zerorooot.nap511.R +import github.zerorooot.nap511.activity.OfflineTaskWorker import github.zerorooot.nap511.bean.LoginBean import github.zerorooot.nap511.ui.theme.Purple80 import github.zerorooot.nap511.util.App @@ -227,16 +231,37 @@ fun captchaWebViewClient(webView: WebView): WebViewClient { .url("https://webapi.115.com/user/captcha") .method("POST", webViewRequest.body.toRequestBody()) webViewRequest.headers.forEach { (t, u) -> a.addHeader(t, u) } + //移除web添加的cookie + a.removeHeader("cookie") + a.addHeader("cookie",App.cookie) + val response = httpClient.newCall(a.build()).execute() val string = response.body.string() - if (string.contains("{\"state\":true}")) { App.selectedItem = "我的文件" - App.instance.toast("验证账号成功~,请重新添加链接") + addTask() + App.instance.toast("验证账号成功~,重新添加链接中.......") } } return null } } +} +private fun addTask() { + val currentOfflineTaskList = + DataStoreUtil.getData(ConfigUtil.currentOfflineTask, "") + .split("\n") + .filter { i -> i != "" && i != " " } + .toSet() + .toMutableList() + val listType = object : TypeToken?>() {}.type + val list = Gson().toJson(currentOfflineTaskList, listType) + val data: Data = + Data.Builder().putString("cookie", App.cookie).putString("list", list) + .build() + val request: OneTimeWorkRequest = + OneTimeWorkRequest.Builder(OfflineTaskWorker::class.java).setInputData(data) + .build() + WorkManager.getInstance(App.instance.applicationContext).enqueue(request) } diff --git a/app/src/main/java/github/zerorooot/nap511/screen/OfflineDownloadScreen.kt b/app/src/main/java/github/zerorooot/nap511/screen/OfflineDownloadScreen.kt index c5a7e0d..1b0fcbc 100644 --- a/app/src/main/java/github/zerorooot/nap511/screen/OfflineDownloadScreen.kt +++ b/app/src/main/java/github/zerorooot/nap511/screen/OfflineDownloadScreen.kt @@ -2,6 +2,7 @@ package github.zerorooot.nap511.screen import androidx.compose.foundation.layout.* +//noinspection UsingMaterialAndMaterial3Libraries import androidx.compose.material.Surface import androidx.compose.material.icons.Icons import androidx.compose.material.icons.rounded.Menu diff --git a/app/src/main/java/github/zerorooot/nap511/viewmodel/OfflineFileViewModel.kt b/app/src/main/java/github/zerorooot/nap511/viewmodel/OfflineFileViewModel.kt index 039ebc5..e7c2eba 100644 --- a/app/src/main/java/github/zerorooot/nap511/viewmodel/OfflineFileViewModel.kt +++ b/app/src/main/java/github/zerorooot/nap511/viewmodel/OfflineFileViewModel.kt @@ -19,6 +19,7 @@ import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.launch import java.text.SimpleDateFormat import java.util.Locale +import java.util.StringJoiner import kotlin.system.exitProcess class OfflineFileViewModel(private val cookie: String, private val application: Application) : @@ -153,6 +154,21 @@ class OfflineFileViewModel(private val cookie: String, private val application: //打开验证页面 App.selectedItem = "captchaWebView" } + //把失败的离线链接保存起来 + val currentOfflineTaskList = + DataStoreUtil.getData(ConfigUtil.currentOfflineTask, "") + .split("\n") + .filter { i -> i != "" && i != " " } + .toSet() + .toMutableList() + currentOfflineTaskList.addAll(list) + val stringJoiner = StringJoiner("\n") + currentOfflineTaskList.toSet().forEach { stringJoiner.add(it) } + //写入缓存 + DataStoreUtil.putData( + ConfigUtil.currentOfflineTask, + stringJoiner.toString() + ) "任务添加失败,${addTask.errorMsg}" } Toast.makeText(application, message, Toast.LENGTH_SHORT).show()