diff --git a/tokisaki-api/src/main/java/io/micro/api/mq/robot/RobotCustomer.java b/tokisaki-api/src/main/java/io/micro/api/mq/robot/RobotCustomer.java index d3bfadc..00a8a88 100644 --- a/tokisaki-api/src/main/java/io/micro/api/mq/robot/RobotCustomer.java +++ b/tokisaki-api/src/main/java/io/micro/api/mq/robot/RobotCustomer.java @@ -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; @@ -15,9 +16,7 @@ public class RobotCustomer { @Incoming(RobotConstant.Queue.ROBOT_LOGIN_SUCCESS) public CompletionStage robotLoginSuccess(Message 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(); } diff --git a/tokisaki-core/src/main/kotlin/io/micro/core/robot/qq/QQRobot.kt b/tokisaki-core/src/main/kotlin/io/micro/core/robot/qq/QQRobot.kt index 996c4fa..eedcafd 100644 --- a/tokisaki-core/src/main/kotlin/io/micro/core/robot/qq/QQRobot.kt +++ b/tokisaki-core/src/main/kotlin/io/micro/core/robot/qq/QQRobot.kt @@ -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 } /** @@ -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 + /** * 登录成功事件 */ @@ -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) diff --git a/tokisaki-core/src/main/kotlin/io/micro/core/robot/qq/QRCodeLoginListenerHookSpi.kt b/tokisaki-core/src/main/kotlin/io/micro/core/robot/qq/QRCodeLoginListenerHookSpi.kt index 88de56f..fceec67 100644 --- a/tokisaki-core/src/main/kotlin/io/micro/core/robot/qq/QRCodeLoginListenerHookSpi.kt +++ b/tokisaki-core/src/main/kotlin/io/micro/core/robot/qq/QRCodeLoginListenerHookSpi.kt @@ -2,6 +2,7 @@ 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 @@ -9,21 +10,42 @@ 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) + } + } + } + } } } \ No newline at end of file diff --git a/tokisaki-server/src/main/kotlin/io/micro/server/robot/domain/service/impl/RobotManagerServiceImpl.kt b/tokisaki-server/src/main/kotlin/io/micro/server/robot/domain/service/impl/RobotManagerServiceImpl.kt index 3fe9039..c6cdc38 100644 --- a/tokisaki-server/src/main/kotlin/io/micro/server/robot/domain/service/impl/RobotManagerServiceImpl.kt +++ b/tokisaki-server/src/main/kotlin/io/micro/server/robot/domain/service/impl/RobotManagerServiceImpl.kt @@ -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 { 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) } } @@ -338,7 +338,7 @@ class RobotManagerServiceImpl( * @param robot QQ机器人 * @param em 事件触发器 */ - private fun qqRobotEventEmitBind(robotDO: RobotDO, robot: QQRobot, em: MultiEmitter) { + private fun qqRobotEventEmitBind(robotDO: RobotDO, robot: QQRobot, em: MultiEmitter) { val id = robot.id() robot.addStateChangeListener { event -> when (event) { @@ -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) }