From 5f23a260650cb486f2c83fa2e6ab2d242dab2998 Mon Sep 17 00:00:00 2001 From: tanjianbin <632190820@qq.com> Date: Wed, 25 Mar 2026 19:53:00 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E7=A7=BB=E9=99=A4=E6=9C=AA?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E7=9A=84=E6=B6=88=E6=81=AF=E5=BC=95=E7=94=A8?= =?UTF-8?q?=E5=9B=9E=E5=A4=8D=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 删除 WeworkController 中的 lastReceivedMessage 和 lastReceivedName 变量 - 移除 WeworkController.replyMessage 方法,合并逻辑到 sendMessage - 删除 WeworkOperationImpl.replyMessage 方法及相关调用 - 清理 WeworkLoopImpl 中保存最后消息用于引用的代码 - 简化 sendChatMessage 方法,移除 reply 参数 --- .../worktool/service/WeworkController.kt | 51 +-------- .../worktool/service/WeworkLoopImpl.kt | 14 +-- .../worktool/service/WeworkOperationImpl.kt | 101 +----------------- 3 files changed, 4 insertions(+), 162 deletions(-) diff --git a/app/src/main/java/org/yameida/worktool/service/WeworkController.kt b/app/src/main/java/org/yameida/worktool/service/WeworkController.kt index 04febaa..85d9bc4 100644 --- a/app/src/main/java/org/yameida/worktool/service/WeworkController.kt +++ b/app/src/main/java/org/yameida/worktool/service/WeworkController.kt @@ -17,10 +17,6 @@ object WeworkController { var mainLoopRunning = false /** 是否正在等待回复 - 等待时会暂停主循环扫描新消息 */ var waitingForReply = false - /** 最后收到的消息内容,用于回复时引用 */ - var lastReceivedMessage: String? = null - /** 最后收到消息的发送者名称 */ - var lastReceivedName: String? = null /** * 交互通知 @@ -67,52 +63,7 @@ object WeworkController { @RequestMapping fun sendMessage(message: WeworkMessageBean): Boolean { LogUtils.d("REQUEST sendMessage(): ${message.messageId} ${message.titleList} ${message.receivedContent} ${message.at} ${message.atList?.joinToString()}") - // 如果有待引用消息且有回复内容,才使用引用回复 - val result = if (!lastReceivedMessage.isNullOrBlank() - && !lastReceivedName.isNullOrBlank() - && !message.receivedContent.isNullOrBlank()) { - LogUtils.d("使用引用回复: ${lastReceivedName} -> $lastReceivedMessage") - val replyResult = WeworkOperationImpl.replyMessage( - message, - message.titleList, - lastReceivedName, - lastReceivedMessage!!, - null, - message.receivedContent - ) - // 发送完成后清除引用信息 - lastReceivedMessage = null - lastReceivedName = null - replyResult - } else { - WeworkOperationImpl.sendMessage(message, message.titleList, message.receivedContent, message.at, message.atList) - } - // 发送完成,通知等待结束 - waitingForReply = false - return result - } - - /** - * 在房间内指定回复消息 - * @see WeworkMessageBean.REPLY_MESSAGE - * @param message#titleList 房间名称 - * @param message#receivedName 原始消息的发送者姓名 - * @param message#originalContent 原始消息的内容 - * @param message#textType 原始消息的消息类型 - * @param message#receivedContent 回复内容 - * @see WeworkMessageBean.TEXT_TYPE - */ - @RequestMapping - fun replyMessage(message: WeworkMessageBean): Boolean { - LogUtils.d("REQUEST replyMessage(): ${message.messageId} ${message.receivedName} ${message.originalContent} ${message.textType} ${message.receivedContent}") - val result = WeworkOperationImpl.replyMessage( - message, - message.titleList, - message.receivedName, - message.originalContent, - message.textType, - message.receivedContent - ) + val result = WeworkOperationImpl.sendMessage(message, message.titleList, message.receivedContent, message.at, message.atList) // 发送完成,通知等待结束 waitingForReply = false return result diff --git a/app/src/main/java/org/yameida/worktool/service/WeworkLoopImpl.kt b/app/src/main/java/org/yameida/worktool/service/WeworkLoopImpl.kt index ddb106b..16eb0c9 100644 --- a/app/src/main/java/org/yameida/worktool/service/WeworkLoopImpl.kt +++ b/app/src/main/java/org/yameida/worktool/service/WeworkLoopImpl.kt @@ -104,7 +104,7 @@ object WeworkLoopImpl { sleep(1000) if (isAtHome()) { - LogUtils.d("用户已离开聊天页面") + LogUtils.d("已离开聊天页面") WeworkController.waitingForReply = false return } @@ -327,18 +327,6 @@ object WeworkLoopImpl { null ) WeworkController.weworkService.webSocketManager.send(messageBean) - // 保存最后收到的消息内容,用于回复时引用 - val lastMsg = messageList.lastOrNull() - if (lastMsg != null && lastMsg.sender == 0 && lastMsg.itemMessageList != null) { - val msgContent = lastMsg.itemMessageList.lastOrNull()?.text?.takeIf { it.isNotBlank() } ?: "" - val senderName = lastMsg.nameList?.firstOrNull()?.takeIf { it.isNotBlank() } ?: "" - if (msgContent.isNotBlank() && senderName.isNotBlank()) { - WeworkController.lastReceivedMessage = msgContent - WeworkController.lastReceivedName = senderName - LogUtils.d("保存最后消息用于引用: $senderName: $msgContent") - } - } - //推测是否回复并在房间等待指令 // 方案A:发送消息后立即返回主页,继续检测下一条消息,不等待 if (needInfer) { val lastMessage = messageList.lastOrNull() diff --git a/app/src/main/java/org/yameida/worktool/service/WeworkOperationImpl.kt b/app/src/main/java/org/yameida/worktool/service/WeworkOperationImpl.kt index ff203f4..e9671e5 100644 --- a/app/src/main/java/org/yameida/worktool/service/WeworkOperationImpl.kt +++ b/app/src/main/java/org/yameida/worktool/service/WeworkOperationImpl.kt @@ -100,103 +100,6 @@ object WeworkOperationImpl { return true } - /** - * 在房间内指定回复消息 - * @param titleList 房间名称 - * @param receivedName 原始消息的发送者姓名 - * @param originalContent 原始消息的内容 - * @param textType 原始消息的消息类型 - * @param receivedContent 回复内容 - * @see WeworkMessageBean.TEXT_TYPE - */ - fun replyMessage( - message: WeworkMessageBean, - titleList: List, - receivedName: String?, - originalContent: String, - textType: Int?, - receivedContent: String? - ): Boolean { - val startTime = System.currentTimeMillis() - if (receivedContent.isNullOrEmpty()) { - LogUtils.d("未发现回复内容") - uploadCommandResult(message, ExecCallbackBean.ERROR_ILLEGAL_DATA, "回复内容为空", startTime, listOf(), titleList) - goHome() - return false - } - - // 验证titleList是否在当前会话列表中存在 - val currentRoomList = WeworkRoomUtil.getRoomTitle(print = false) - if (currentRoomList.isEmpty()) { - LogUtils.d("无法获取当前会话列表") - uploadCommandResult(message, ExecCallbackBean.ERROR_SEND_MESSAGE, "无法获取当前会话列表,请确保在微信首页", startTime, listOf(), titleList) - return false - } - val notFoundList = titleList.filter { title -> - currentRoomList.none { it == title || it.replace(Constant.digitalRegex, "") == title || title.replace(Constant.digitalRegex, "") == it } - } - if (notFoundList.isNotEmpty()) { - LogUtils.d("以下会话不存在: ${notFoundList.joinToString()}") - uploadCommandResult(message, ExecCallbackBean.ERROR_SEND_MESSAGE, "会话不存在: ${notFoundList.joinToString()}", startTime, listOf(), titleList) - return false - } - - val successList = arrayListOf() - val failList = arrayListOf() - for (title in LinkedHashSet(titleList)) { - if (WeworkRoomUtil.intoRoom(title) || WeworkRoomUtil.intoRoom(title, fastIn = false)) { - if (textType?.let { - WeworkTextUtil.longClickMessageItem( - //聊天消息列表 1ListView 0RecycleView xViewGroup - AccessibilityUtil.findOneByClazz(getRoot(), Views.ListView), - it, - receivedName, - originalContent, - "回复", "引用" - ) - } == true - ) { - LogUtils.v("开始回复") - if (sendChatMessage(receivedContent, reply = true, title = title)) { - LogUtils.d("$title: 回复成功") - successList.add(title) - uploadCommandResult(message, ExecCallbackBean.SUCCESS, "", startTime, successList, failList) - return true - } else { - LogUtils.d("$title: 回复发送失败") - failList.add(title) - uploadCommandResult(message, ExecCallbackBean.ERROR_SEND_MESSAGE, "回复发送失败", startTime, successList, failList) - return false - } - } else { - LogUtils.d("$title: 回复失败 直接发送答案") - error("$title: 回复失败 直接发送答案 $receivedContent") - val text = if (originalContent.isNotEmpty()) "【$originalContent】\n$receivedContent" else receivedContent - if (sendChatMessage(text, receivedName, title = title)) { - LogUtils.d("$title: 直接发送答案成功") - successList.add(title) - uploadCommandResult(message, ExecCallbackBean.SUCCESS, "", startTime, successList, failList) - return true - } else { - LogUtils.d("$title: 直接发送答案失败") - failList.add(title) - uploadCommandResult(message, ExecCallbackBean.ERROR_SEND_MESSAGE, "直接发送答案失败", startTime, successList, failList) - return false - } - } - } else { - error("$title: 回复失败 $receivedContent") - LogUtils.d("进入房间失败 $title") - failList.add(title) - uploadCommandResult(message, ExecCallbackBean.ERROR_INTO_ROOM, "进入房间失败 $title", startTime, successList, failList) - return false - } - } - LogUtils.d("房间名为空") - uploadCommandResult(message, ExecCallbackBean.ERROR_ILLEGAL_DATA, "房间名为空", startTime, listOf(), titleList) - return false - } - /** * 在房间内转发消息 * @see WeworkMessageBean.RELAY_MESSAGE @@ -2868,7 +2771,7 @@ object WeworkOperationImpl { /** * 发送消息+@at */ - private fun sendChatMessage(text: String, at: String? = null, atList: List? = null, reply: Boolean? = false, title: String): Boolean { + private fun sendChatMessage(text: String, at: String? = null, atList: List? = null, title: String): Boolean { val roomType = WeworkRoomUtil.getRoomType() var titleList = arrayListOf(title) ?: WeworkRoomUtil.getRoomTitle() if (titleList.count { it.endsWith("…") } > 0) { @@ -2976,7 +2879,7 @@ object WeworkOperationImpl { } LogUtils.v("atFailed: $atFailed") val content = if (atFailed) "@${atList?.joinToString()} $text" else text - val append = (reply == true) || (!atList.isNullOrEmpty() && !atFailed) + val append = !atList.isNullOrEmpty() && !atFailed if (AccessibilityUtil.findTextInput(getRoot(), content, append = append)) { AccessibilityUtil.findOneByText(getRoot(), "发送", exact = true, timeout = 2000) val sendButton = AccessibilityUtil.findAllByClazz(getRoot(), Views.Button)