Skip to content
This repository has been archived by the owner on Jan 18, 2024. It is now read-only.

Commit

Permalink
cancel login
Browse files Browse the repository at this point in the history
  • Loading branch information
MrXiaoM committed Apr 27, 2023
1 parent 71b05b8 commit 2d66ba5
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
13 changes: 9 additions & 4 deletions app/src/main/java/top/mrxiaom/mirai/aoki/ExceptionAnalyzer.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package top.mrxiaom.mirai.aoki

import top.mrxiaom.mirai.aoki.ui.model.UserCancelledLoginException

object ExceptionAnalyzer {
fun Throwable.analyze(): String {
val msg = message ?: ""
return when {
var stacktrace = true
val analyze = when {
this is UserCancelledLoginException -> "用户主动取消了登录操作".also { stacktrace = false }
msg.contains("code=235") -> """
出现了 235 错误,你的账号可能已被风控,请尝试以下方法解决:
* 先使用官方QQ客户端登录机器人账号
Expand Down Expand Up @@ -44,11 +48,12 @@ object ExceptionAnalyzer {
请尝试到「账号管理」删除登录会话
""".trimIndent()
else -> null
}?.run { """
$this
}?.run { if (stacktrace) this +
"""
==============================
以下为原始异常信息,非开发者无需阅读
==============================
""".trimIndent() } ?: ""
""".trimIndent() else this } ?: ""
return analyze + if (analyze.isEmpty() || stacktrace) stackTraceToString() else ""
}
}
4 changes: 1 addition & 3 deletions app/src/main/java/top/mrxiaom/mirai/aoki/ui/LoginActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,10 @@ class LoginActivity : AppCompatActivity() {
}
*/
buttonPositive(R.string.ok) {
loginViewModel.viewModelScope.launch { loginViewModel.login(bot) }
loginViewModel.login(bot)
dismiss()
}
buttonNegative(R.string.cancel) {
loading.visibility = View.INVISIBLE
login.isClickable = true
loginViewModel.cancelLogin(bot)
dismiss()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import top.mrxiaom.mirai.aoki.util.*

class QRLoginDialog(
val activity: AppCompatActivity,
loginViewModel: LoginViewModel
val loginViewModel: LoginViewModel
) {
init {
activity.observe(loginViewModel.qrloginRequest) { pushInfo(this) }
Expand Down Expand Up @@ -44,7 +44,8 @@ class QRLoginDialog(
})
buttonNegative(R.string.cancel) {
image.contentDescription.toString().toLongOrNull()?.also {
Bot.getInstanceOrNull(it)?.close(object: CustomLoginFailedException(true, "用户主动取消登录") { })
val bot = Bot.getInstanceOrNull(it) ?: return@also
loginViewModel.cancelLogin(bot)
}
dismiss()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package top.mrxiaom.mirai.aoki.ui.model
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.launch
import net.mamoe.mirai.Bot
import net.mamoe.mirai.auth.QRCodeLoginListener
import net.mamoe.mirai.utils.DeviceVerificationRequests
Expand Down Expand Up @@ -43,7 +45,7 @@ class LoginViewModel : ViewModel() {
val scanRequest: LiveData<ScanRequest> = _scanRequest
internal val _qrloginRequest = MutableLiveData<QRLoginRequest>()
val qrloginRequest: LiveData<QRLoginRequest> = _qrloginRequest
suspend fun login(bot: Bot) {
fun login(bot: Bot) = viewModelScope.launch {
try {
bot.login()
_loginResult.value = LoginResult(success = bot)
Expand All @@ -52,7 +54,8 @@ class LoginViewModel : ViewModel() {
}
}
fun cancelLogin(bot: Bot) {
_loginResult.value = LoginResult(error = InterruptedException("用户取消了登录操作"))
_loginResult.value = LoginResult(error = UserCancelledLoginException())
bot.close()
}
}
}
class UserCancelledLoginException : InterruptedException()

0 comments on commit 2d66ba5

Please sign in to comment.