Skip to content

Commit

Permalink
🎨 feat: 优化QQ扫码登陆
Browse files Browse the repository at this point in the history
  • Loading branch information
cokie committed Jan 23, 2024
1 parent 603d6f1 commit aa256ff
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.micro.server.robot.infra.event.dto.RobotDTO;
import io.micro.server.robot.infra.event.sdk.RobotConstant;
import io.quarkus.logging.Log;
import io.vertx.core.json.JsonObject;
import jakarta.enterprise.context.ApplicationScoped;
import org.eclipse.microprofile.reactive.messaging.Incoming;
Expand All @@ -15,9 +16,7 @@ public class RobotCustomer {
@Incoming(RobotConstant.Queue.ROBOT_LOGIN_SUCCESS)
public CompletionStage<Void> robotLoginSuccess(Message<JsonObject> msg) {
RobotDTO robot = msg.getPayload().mapTo(RobotDTO.class);
System.out.println("==========");
System.out.println(robot.getId());
System.out.println("==========");
Log.info("mq消费: " + robot.getId());
return msg.ack();
}

Expand Down
20 changes: 18 additions & 2 deletions tokisaki-core/src/main/kotlin/io/micro/core/robot/qq/QQRobot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import net.mamoe.mirai.event.events.GroupMessageEvent
class QQRobot(private val id: Long, private val account: String) : Robot, Robot.LifeCycle {

companion object {
// 登录超时时间 2m
private const val LOGIN_TIMEOUT = 1000 * 60 * 1L
// 登录超时时间 5m
private const val LOGIN_TIMEOUT = 1000 * 60 * 5L
}

/**
Expand Down Expand Up @@ -57,6 +57,14 @@ class QQRobot(private val id: Long, private val account: String) : Robot, Robot.
*/
open class QRCodeStartEvent(val qr: ByteArray) : Robot.Event

open class QRCodeWaitingConfirmEvent : Robot.Event

open class QRCodeCancelledEvent : Robot.Event

open class QRCodeConfirmedEvent : Robot.Event

open class QRCodeTimeoutEvent : Robot.Event

/**
* 登录成功事件
*/
Expand Down Expand Up @@ -153,6 +161,14 @@ class QQRobot(private val id: Long, private val account: String) : Robot, Robot.
}
}

is QRCodeWaitingConfirmEvent, is QRCodeCancelledEvent, is QRCodeConfirmedEvent, is QRCodeTimeoutEvent -> {
try {
handle(event)
} catch (ex: Exception) {
Log.error(ex)
}
}

is LoginSuccessEvent -> {
try {
handle(event)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,50 @@ package io.micro.core.robot.qq

import io.micro.core.robot.Robot
import io.quarkus.arc.Arc
import io.quarkus.logging.Log
import net.mamoe.mirai.Bot
import net.mamoe.mirai.auth.QRCodeLoginListener
import net.mamoe.mirai.utils.QRCodeLoginListenerHook

class QRCodeLoginListenerHookSpi : QRCodeLoginListenerHook {

override fun onFetchQRCode(bot: Bot, data: ByteArray) {
Log.info("机器人: ${bot.id}(${bot.nick})开始登陆")
val bizId = bot.getBizId()
if (bizId != null) {
val instance = Arc.container().instance(QQRobotManager::class.java)
if (instance.isAvailable) {
val manager = instance.get()
val lifeCycle = manager.getRobot(bizId) as Robot.LifeCycle?
lifeCycle?.loggingInListener(QQRobot.QRCodeStartEvent(data))
val robot = manager.getRobot(bizId)
if (robot != null && robot is Robot.LifeCycle) {
robot.loggingInListener(QQRobot.QRCodeStartEvent(data))
}
}
}
}

override fun onStateChanged(bot: Bot, state: QRCodeLoginListener.State) {
println("===============")
println("当前二维码登陆状态: ${state.name}")
println("===============")
Log.debug("机器人: ${bot.id}(${bot.nick}), 当前二维码登陆状态: ${state.name}")
val bizId = bot.getBizId()
if (bizId != null) {
val instance = Arc.container().instance(QQRobotManager::class.java)
if (instance.isAvailable) {
val manager = instance.get()
val robot = manager.getRobot(bizId)
if (robot != null && robot is Robot.LifeCycle) {
val event = when (state) {
QRCodeLoginListener.State.WAITING_FOR_CONFIRM -> QQRobot.QRCodeWaitingConfirmEvent()
QRCodeLoginListener.State.CANCELLED -> QQRobot.QRCodeCancelledEvent()
QRCodeLoginListener.State.TIMEOUT -> QQRobot.QRCodeTimeoutEvent()
QRCodeLoginListener.State.CONFIRMED -> QQRobot.QRCodeConfirmedEvent()
else -> null
}
if (event != null) {
robot.loggingInListener(event)
}
}
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -252,16 +252,16 @@ class RobotManagerServiceImpl(
// 创建QQ机器人
val robot = factory.create(Credential(robotId!!, robotDO.account!!)) as QQRobot
val vertxContext = Vertx.currentContext()
return Multi.createFrom().emitter<String> { em ->
return Multi.createFrom().emitter { em ->
// 绑定登录回调函数
qqRobotEventEmitBind(robotDO, robot, em)
// 群消息处理
onGroupMessage(robot, robotId, vertxContext)
// 注册机器人并开始登录
manager.registerRobot(robot)
}.map {
}.map { str ->
// 发送消息事件
sse.newEvent(it)
sse.newEvent(str)
}
}

Expand Down Expand Up @@ -338,7 +338,7 @@ class RobotManagerServiceImpl(
* @param robot QQ机器人
* @param em 事件触发器
*/
private fun qqRobotEventEmitBind(robotDO: RobotDO, robot: QQRobot, em: MultiEmitter<in String?>) {
private fun qqRobotEventEmitBind(robotDO: RobotDO, robot: QQRobot, em: MultiEmitter<in String>) {
val id = robot.id()
robot.addStateChangeListener { event ->
when (event) {
Expand All @@ -347,8 +347,24 @@ class RobotManagerServiceImpl(
em.emit("qr#$qrCode")
}

is QQRobot.QRCodeWaitingConfirmEvent -> {
em.emit("waiting_confirm#")
}

is QQRobot.QRCodeConfirmedEvent -> {
em.emit("confirmed#")
}

is QQRobot.QRCodeCancelledEvent -> {
em.emit("cancelled#")
}

is QQRobot.QRCodeTimeoutEvent -> {
em.emit("qr_timeout#")
}

is QQRobot.LoginTimeoutEvent -> {
em.emit("timeout#")
em.emit("login_timeout#")
em.complete()
manager.unregisterRobot(id)
}
Expand Down

0 comments on commit aa256ff

Please sign in to comment.