Skip to content

Commit

Permalink
loginSolver
Browse files Browse the repository at this point in the history
  • Loading branch information
lz1998 committed Sep 23, 2020
1 parent 89eea7b commit a372954
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class BotController {
@RequestMapping("/getLoginUrl")
fun getLoginUrl(botId: Long): String? {
val loginData = myLoginSolver.getLoginData(botId) ?: return null
return if (loginData.type == LoginDataType.PIC_CAPTCHA) {
return if (loginData.type != LoginDataType.PIC_CAPTCHA) {
loginData.url
} else {
null
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/net/lz1998/mirai/entity/RemoteBot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ interface RemoteBot {
suspend fun initBot() {
bot = Bot(botId, password) {
fileBasedDeviceInfo("device.json")
// loginSolver = myLoginSolver
loginSolver = myLoginSolver
noNetworkLog()
}.alsoLogin()
bot.subscribeAlways<BotEvent> {
Expand Down
7 changes: 6 additions & 1 deletion src/main/kotlin/net/lz1998/mirai/service/MyLoginSolver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,31 @@ class MyLoginSolver : LoginSolver() {
// TODO 通过轮询查询 loginMap
val loginMap = mutableMapOf<Long, LoginData>()

// 图片验证码登陆
override suspend fun onSolvePicCaptcha(bot: Bot, data: ByteArray): String? {
val def = CompletableDeferred<String>()
loginMap[bot.id] = LoginData(LoginDataType.PIC_CAPTCHA, def, data, null)
return def.await().trim()
}

// 滑动验证
override suspend fun onSolveSliderCaptcha(bot: Bot, url: String): String? {
val def = CompletableDeferred<String>()
loginMap[bot.id] = LoginData(LoginDataType.SLIDER_CAPTCHA, def, null, url)
return def.await().trim()
}

// 设备锁扫码验证
override suspend fun onSolveUnsafeDeviceLoginVerify(bot: Bot, url: String): String? {
val def = CompletableDeferred<String>()
loginMap[bot.id] = LoginData(LoginDataType.UNSAFE_DEVICE_LOGIN_VERIFY, def, null, url)
return def.await().trim()
}

fun solveLogin(botId: Long, result: String) {
loginMap[botId]?.def?.complete(result)
val loginData = loginMap[botId] ?: return
loginMap.remove(botId)
loginData.def.complete(result)
}

fun getLoginData(botId: Long): LoginData? {
Expand Down
7 changes: 5 additions & 2 deletions src/main/kotlin/net/lz1998/mirai/utils/MsgConverter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ suspend fun OnebotBase.Message.toMiraiMessage(bot: Bot, contact: Contact): Messa
"image" -> {
return try {
withContext(Dispatchers.IO) {
URL(dataMap["file"]?:"").openConnection().getInputStream().uploadAsImage(contact)
URL(dataMap["file"] ?: "").openConnection().getInputStream().uploadAsImage(contact)
}
} catch (e: Exception) {
MSG_EMPTY
Expand All @@ -45,9 +45,12 @@ fun MessageChain.toOnebotMessage(): List<OnebotBase.Message> {
val message = when (content) {
is At -> OnebotBase.Message.newBuilder().setType("at").putAllData(mapOf("qq" to content.target.toString())).build()
is PlainText -> OnebotBase.Message.newBuilder().setType("text").putAllData(mapOf("text" to content.content)).build()
is Face -> OnebotBase.Message.newBuilder().setType("face").putAllData(mapOf("id" to content.id.toString())).build()
is Image -> OnebotBase.Message.newBuilder().setType("image").putAllData(mapOf("file" to content.imageId)).build()
is Voice -> OnebotBase.Message.newBuilder().setType("record").putAllData(mapOf("file" to content.fileName)).build()
else -> OnebotBase.Message.newBuilder().setType("unknown").build()
}
messageChain.add(message)
}
return messageChain
}
}

0 comments on commit a372954

Please sign in to comment.