Skip to content

Commit

Permalink
支持验证后重新添加离线链接
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Jun 29, 2024
1 parent 91d0987 commit 85eb5ac
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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<List<String?>?>() {}.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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) :
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 85eb5ac

Please sign in to comment.