Graphic VerificationCode

This commit is contained in:
ryoii 2020-03-20 00:08:44 +08:00
parent 6952443776
commit 5ece8e95f9
3 changed files with 60 additions and 6 deletions

View File

@ -3,12 +3,15 @@ package net.mamoe.mirai.console.graphical.controller
import javafx.application.Platform
import javafx.collections.ObservableList
import javafx.stage.Modality
import javafx.stage.StageStyle
import kotlinx.coroutines.delay
import net.mamoe.mirai.Bot
import net.mamoe.mirai.console.MiraiConsole
import net.mamoe.mirai.console.graphical.model.*
import net.mamoe.mirai.console.graphical.view.VerificationCodeFragment
import net.mamoe.mirai.console.plugins.PluginManager
import net.mamoe.mirai.console.utils.MiraiConsoleUI
import net.mamoe.mirai.network.WrongPasswordException
import net.mamoe.mirai.utils.LoginSolver
import net.mamoe.mirai.utils.SimpleLogger.LogPriority
import tornadofx.*
@ -97,7 +100,27 @@ class MiraiGraphicalUIController : Controller(), MiraiConsoleUI {
class GraphicalLoginSolver : LoginSolver() {
override suspend fun onSolvePicCaptcha(bot: Bot, data: ByteArray): String? {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
val code = VerificationCodeModel(VerificationCode(data))
// 界面需要运行在主线程
Platform.runLater {
find<VerificationCodeFragment>(Scope(code)).openModal(
stageStyle = StageStyle.UNDECORATED,
escapeClosesWindow = false,
modality = Modality.NONE,
resizable = false,
block = true
)
}
// 阻塞协程直到验证码已经输入
while (code.isDirty || code.code.value == null) {
delay(1000)
if (code.code.value === VerificationCodeFragment.MAGIC_KEY) {
throw WrongPasswordException("取消登录")
}
}
return code.code.value
}

View File

@ -1,17 +1,26 @@
package net.mamoe.mirai.console.graphical.model
import javafx.beans.property.SimpleObjectProperty
import javafx.beans.property.SimpleStringProperty
import tornadofx.ItemViewModel
import tornadofx.getValue
import tornadofx.setValue
class VerificationCode {
val codeProperty = SimpleStringProperty("")
var code: String by codeProperty
class VerificationCode(data: ByteArray = ByteArray(0)) {
val codeProperty = SimpleStringProperty(null)
var code: String? by codeProperty
val dataProperty: SimpleObjectProperty<ByteArray> = SimpleObjectProperty()
val data: ByteArray by dataProperty
init {
dataProperty.set(data)
}
}
class VerificationCodeModel(code: VerificationCode) : ItemViewModel<VerificationCode>(code) {
constructor() : this(VerificationCode())
val code = bind(VerificationCode::codeProperty)
val data = bind(VerificationCode::dataProperty)
}

View File

@ -1,16 +1,38 @@
package net.mamoe.mirai.console.graphical.view
import javafx.scene.image.Image
import net.mamoe.mirai.console.graphical.model.VerificationCodeModel
import tornadofx.*
import java.io.ByteArrayInputStream
class VerificationCodeFragment : Fragment() {
companion object {
val MAGIC_KEY = String("CANCEL".toByteArray())
}
val code = find<VerificationCodeModel>()
override val root = vbox {
//TODO: 显示验证码
// 显示验证码
imageview(Image(ByteArrayInputStream(code.data.value)))
form {
fieldset {
field("验证码") {
textfield()
textfield(code.code)
}
}
buttonbar {
button("提交").action {
code.commit()
this@VerificationCodeFragment.close()
}
button("取消").action {
code.code.value = MAGIC_KEY
code.commit()
this@VerificationCodeFragment.close()
}
}
}