From 641babd2bea567e631746c82023e05404f6d0f80 Mon Sep 17 00:00:00 2001 From: xsx <825657193@qq.com> Date: Wed, 26 Mar 2025 18:19:46 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B6=88=E6=81=AF=E6=92=A4=E5=9B=9E=E4=BC=98?= =?UTF-8?q?=E5=8C=96=EF=BC=8C=E6=94=AF=E6=8C=81=E7=A6=BB=E7=BA=BF=E6=92=A4?= =?UTF-8?q?=E5=9B=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/GroupMessageController.java | 5 +- .../controller/PrivateMessageController.java | 5 +- .../service/GroupMessageService.java | 2 +- .../service/PrivateMessageService.java | 2 +- .../service/impl/GroupMessageServiceImpl.java | 34 +- .../impl/PrivateMessageServiceImpl.java | 30 +- im-uniapp/App.vue | 47 +- .../chat-message-item/chat-message-item.vue | 7 +- im-uniapp/main.js | 16 +- im-uniapp/pages/chat/chat-box.vue | 9 +- im-uniapp/store/chatStore.js | 38 +- im-web/src/components/chat/ChatBox.vue | 10 +- .../src/components/chat/ChatMessageItem.vue | 6 +- im-web/src/store/chatStore.js | 44 +- im-web/src/view/Chat.vue | 2 +- im-web/src/view/Home.vue | 946 +++++++++--------- 16 files changed, 629 insertions(+), 574 deletions(-) diff --git a/im-platform/src/main/java/com/bx/implatform/controller/GroupMessageController.java b/im-platform/src/main/java/com/bx/implatform/controller/GroupMessageController.java index 007a3de..a1c5845 100644 --- a/im-platform/src/main/java/com/bx/implatform/controller/GroupMessageController.java +++ b/im-platform/src/main/java/com/bx/implatform/controller/GroupMessageController.java @@ -30,9 +30,8 @@ public class GroupMessageController { @DeleteMapping("/recall/{id}") @Operation(summary = "撤回消息", description = "撤回群聊消息") - public Result recallMessage(@NotNull(message = "消息id不能为空") @PathVariable Long id) { - groupMessageService.recallMessage(id); - return ResultUtils.success(); + public Result recallMessage(@NotNull(message = "消息id不能为空") @PathVariable Long id) { + return ResultUtils.success(groupMessageService.recallMessage(id)); } @GetMapping("/pullOfflineMessage") diff --git a/im-platform/src/main/java/com/bx/implatform/controller/PrivateMessageController.java b/im-platform/src/main/java/com/bx/implatform/controller/PrivateMessageController.java index 622c5dc..19536a7 100644 --- a/im-platform/src/main/java/com/bx/implatform/controller/PrivateMessageController.java +++ b/im-platform/src/main/java/com/bx/implatform/controller/PrivateMessageController.java @@ -30,9 +30,8 @@ public class PrivateMessageController { @DeleteMapping("/recall/{id}") @Operation(summary = "撤回消息", description = "撤回私聊消息") - public Result recallMessage(@NotNull(message = "消息id不能为空") @PathVariable Long id) { - privateMessageService.recallMessage(id); - return ResultUtils.success(); + public Result recallMessage(@NotNull(message = "消息id不能为空") @PathVariable Long id) { + return ResultUtils.success( privateMessageService.recallMessage(id)); } @GetMapping("/pullOfflineMessage") diff --git a/im-platform/src/main/java/com/bx/implatform/service/GroupMessageService.java b/im-platform/src/main/java/com/bx/implatform/service/GroupMessageService.java index dc4efda..1a3ed38 100644 --- a/im-platform/src/main/java/com/bx/implatform/service/GroupMessageService.java +++ b/im-platform/src/main/java/com/bx/implatform/service/GroupMessageService.java @@ -22,7 +22,7 @@ public interface GroupMessageService extends IService { * * @param id 消息id */ - void recallMessage(Long id); + GroupMessageVO recallMessage(Long id); /** * 拉取离线消息,只能拉取最近1个月的消息,最多拉取1000条 diff --git a/im-platform/src/main/java/com/bx/implatform/service/PrivateMessageService.java b/im-platform/src/main/java/com/bx/implatform/service/PrivateMessageService.java index 261a5e0..8ebed75 100644 --- a/im-platform/src/main/java/com/bx/implatform/service/PrivateMessageService.java +++ b/im-platform/src/main/java/com/bx/implatform/service/PrivateMessageService.java @@ -23,7 +23,7 @@ public interface PrivateMessageService extends IService { * * @param id 消息id */ - void recallMessage(Long id); + PrivateMessageVO recallMessage(Long id); /** * 拉取历史聊天记录 diff --git a/im-platform/src/main/java/com/bx/implatform/service/impl/GroupMessageServiceImpl.java b/im-platform/src/main/java/com/bx/implatform/service/impl/GroupMessageServiceImpl.java index 66c0c88..244c062 100644 --- a/im-platform/src/main/java/com/bx/implatform/service/impl/GroupMessageServiceImpl.java +++ b/im-platform/src/main/java/com/bx/implatform/service/impl/GroupMessageServiceImpl.java @@ -38,6 +38,7 @@ import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.time.DateUtils; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import java.util.*; import java.util.concurrent.atomic.AtomicInteger; @@ -92,8 +93,9 @@ public class GroupMessageServiceImpl extends ServiceImpl userIds = groupMemberService.findUserIdsByGroupId(msg.getGroupId()); - // 不用发给自己 - userIds = userIds.stream().filter(uid -> !session.getUserId().equals(uid)).collect(Collectors.toList()); - GroupMessageVO msgInfo = BeanUtils.copyProperties(msg, GroupMessageVO.class); - msgInfo.setType(MessageType.RECALL.code()); - String content = String.format("'%s'撤回了一条消息", member.getShowNickName()); - msgInfo.setContent(content); - msgInfo.setSendTime(new Date()); - + GroupMessageVO msgInfo = BeanUtils.copyProperties(recallMsg, GroupMessageVO.class); IMGroupMessage sendMessage = new IMGroupMessage<>(); sendMessage.setSender(new IMUserInfo(session.getUserId(), session.getTerminal())); sendMessage.setRecvIds(userIds); sendMessage.setData(msgInfo); - sendMessage.setSendResult(false); - sendMessage.setSendToSelf(false); - imClient.sendGroupMessage(sendMessage); - - // 推给自己其他终端 - msgInfo.setContent("你撤回了一条消息"); - sendMessage.setSendToSelf(true); - sendMessage.setRecvIds(Collections.emptyList()); - sendMessage.setRecvTerminals(Collections.emptyList()); imClient.sendGroupMessage(sendMessage); log.info("撤回群聊消息,发送id:{},群聊id:{},内容:{}", session.getUserId(), msg.getGroupId(), msg.getContent()); + return msgInfo; } @@ -165,7 +162,6 @@ public class GroupMessageServiceImpl extends ServiceImpl messages = this.list(wrapper); // 通过群聊对消息进行分组 diff --git a/im-platform/src/main/java/com/bx/implatform/service/impl/PrivateMessageServiceImpl.java b/im-platform/src/main/java/com/bx/implatform/service/impl/PrivateMessageServiceImpl.java index 8380184..7e357c2 100644 --- a/im-platform/src/main/java/com/bx/implatform/service/impl/PrivateMessageServiceImpl.java +++ b/im-platform/src/main/java/com/bx/implatform/service/impl/PrivateMessageServiceImpl.java @@ -74,8 +74,9 @@ public class PrivateMessageServiceImpl extends ServiceImpl sendMessage = new IMPrivateMessage<>(); sendMessage.setSender(new IMUserInfo(session.getUserId(), session.getTerminal())); sendMessage.setRecvId(msgInfo.getRecvId()); - sendMessage.setSendToSelf(false); sendMessage.setData(msgInfo); - sendMessage.setSendResult(false); - imClient.sendPrivateMessage(sendMessage); - - // 推给自己其他终端 - msgInfo.setContent("你撤回了一条消息"); - sendMessage.setSendToSelf(true); - sendMessage.setRecvTerminals(Collections.emptyList()); imClient.sendPrivateMessage(sendMessage); log.info("撤回私聊消息,发送id:{},接收id:{},内容:{}", msg.getSendId(), msg.getRecvId(), msg.getContent()); + return msgInfo; } @Override @@ -154,8 +153,7 @@ public class PrivateMessageServiceImpl extends ServiceImpl wrap.and( + queryWrapper.gt(PrivateMessage::getId, minId).ge(PrivateMessage::getSendTime, minDate).and(wrap -> wrap.and( wp -> wp.eq(PrivateMessage::getSendId, session.getUserId()).in(PrivateMessage::getRecvId, friendIds)) .or(wp -> wp.eq(PrivateMessage::getRecvId, session.getUserId()).in(PrivateMessage::getSendId, friendIds))) .orderByAsc(PrivateMessage::getId); diff --git a/im-uniapp/App.vue b/im-uniapp/App.vue index 0f62016..649eaef 100644 --- a/im-uniapp/App.vue +++ b/im-uniapp/App.vue @@ -107,6 +107,15 @@ export default { }) }, handlePrivateMessage(msg) { + // 标记这条消息是不是自己发的 + msg.selfSend = msg.sendId == this.userStore.userInfo.id; + // 好友id + let friendId = msg.selfSend ? msg.recvId : msg.sendId; + // 会话信息 + let chatInfo = { + type: 'PRIVATE', + targetId: friendId + } // 消息加载标志 if (msg.type == enums.MESSAGE_TYPE.LOADING) { this.chatStore.setLoadingPrivateMsg(JSON.parse(msg.content)) @@ -114,10 +123,7 @@ export default { } // 消息已读处理,清空已读数量 if (msg.type == enums.MESSAGE_TYPE.READED) { - this.chatStore.resetUnreadCount({ - type: 'PRIVATE', - targetId: msg.recvId - }) + this.chatStore.resetUnreadCount(chatInfo); return; } // 消息回执处理,改消息状态为已读 @@ -127,10 +133,12 @@ export default { }) return; } - // 标记这条消息是不是自己发的 - msg.selfSend = msg.sendId == this.userStore.userInfo.id; - // 好友id - let friendId = msg.selfSend ? msg.recvId : msg.sendId; + // 消息撤回 + if (msg.type == enums.MESSAGE_TYPE.RECALL) { + this.chatStore.recallMessage(msg, chatInfo); + return; + } + // 消息插入 this.loadFriendInfo(friendId, (friend) => { this.insertPrivateMessage(friend, msg); }) @@ -177,6 +185,12 @@ export default { }, handleGroupMessage(msg) { + // 标记这条消息是不是自己发的 + msg.selfSend = msg.sendId == this.userStore.userInfo.id; + let chatInfo = { + type: 'GROUP', + targetId: msg.groupId + } // 消息加载标志 if (msg.type == enums.MESSAGE_TYPE.LOADING) { this.chatStore.setLoadingGroupMsg(JSON.parse(msg.content)) @@ -185,19 +199,11 @@ export default { // 消息已读处理 if (msg.type == enums.MESSAGE_TYPE.READED) { // 我已读对方的消息,清空已读数量 - let chatInfo = { - type: 'GROUP', - targetId: msg.groupId - } this.chatStore.resetUnreadCount(chatInfo) return; } // 消息回执处理 if (msg.type == enums.MESSAGE_TYPE.RECEIPT) { - let chatInfo = { - type: 'GROUP', - targetId: msg.groupId - } // 更新消息已读人数 let msgInfo = { id: msg.id, @@ -205,11 +211,14 @@ export default { readedCount: msg.readedCount, receiptOk: msg.receiptOk }; - this.chatStore.updateMessage(msgInfo,chatInfo) + this.chatStore.updateMessage(msgInfo, chatInfo) + return; + } + // 消息撤回 + if (msg.type == this.$enums.MESSAGE_TYPE.RECALL) { + this.chatStore.recallMessage(msg, chatInfo) return; } - // 标记这条消息是不是自己发的 - msg.selfSend = msg.sendId == this.userStore.userInfo.id; this.loadGroupInfo(msg.groupId, (group) => { // 插入群聊消息 this.insertGroupMessage(group, msg); diff --git a/im-uniapp/components/chat-message-item/chat-message-item.vue b/im-uniapp/components/chat-message-item/chat-message-item.vue index ae6f0c2..0904bad 100644 --- a/im-uniapp/components/chat-message-item/chat-message-item.vue +++ b/im-uniapp/components/chat-message-item/chat-message-item.vue @@ -1,13 +1,12 @@