From c000c2e7abdff31255111bf03db5387c5626f7a4 Mon Sep 17 00:00:00 2001 From: "xie.bx" Date: Sun, 5 Nov 2023 22:19:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B7=B2=E8=AF=BB=E6=9C=AA=E8=AF=BB=E6=98=BE?= =?UTF-8?q?=E7=A4=BA(=E5=AE=8C=E6=88=90)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/bx/imclient/sender/IMSender.java | 10 +- .../com/bx/imcommon/contant/IMRedisKey.java | 4 +- .../listener/GroupMessageListener.java | 1 + .../service/impl/FriendServiceImpl.java | 2 +- .../service/impl/GroupMessageServiceImpl.java | 19 +- .../impl/PrivateMessageServiceImpl.java | 2 +- .../task/PullUnreadGroupMessageTask.java | 2 +- .../task/PullUnreadPrivateMessageTask.java | 2 +- im-ui/src/components/chat/ChatBox.vue | 49 ++++-- im-ui/src/components/chat/ChatMessageItem.vue | 4 +- im-ui/src/store/chatStore.js | 52 +++--- im-ui/src/view/Home.vue | 25 ++- im-uniapp/App.vue | 166 ++++++++++++------ im-uniapp/common/emotion.js | 23 +-- im-uniapp/common/enums.js | 11 +- im-uniapp/common/request.js | 3 - .../chat-message-item/chat-message-item.vue | 55 +++--- im-uniapp/components/loading/loading.vue | 91 ++++++---- im-uniapp/pages/chat/chat-box.vue | 83 +++++++-- im-uniapp/pages/chat/chat.vue | 26 ++- im-uniapp/pages/login/login.vue | 9 +- im-uniapp/store/chatStore.js | 141 ++++++++++----- 22 files changed, 511 insertions(+), 269 deletions(-) diff --git a/im-client/src/main/java/com/bx/imclient/sender/IMSender.java b/im-client/src/main/java/com/bx/imclient/sender/IMSender.java index 1d3dc87..f4d0b83 100644 --- a/im-client/src/main/java/com/bx/imclient/sender/IMSender.java +++ b/im-client/src/main/java/com/bx/imclient/sender/IMSender.java @@ -14,8 +14,6 @@ import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import java.util.*; -import java.util.concurrent.atomic.AtomicInteger; -import java.util.stream.Collectors; @Service public class IMSender { @@ -34,7 +32,7 @@ public class IMSender { Integer serverId = (Integer)redisTemplate.opsForValue().get(key); // 如果对方在线,将数据存储至redis,等待拉取推送 if (serverId != null) { - String sendKey = String.join(":", IMRedisKey.IM_UNREAD_PRIVATE_QUEUE, serverId.toString()); + String sendKey = String.join(":", IMRedisKey.IM_MESSAGE_PRIVATE_QUEUE, serverId.toString()); IMRecvInfo recvInfo = new IMRecvInfo(); recvInfo.setCmd(IMCmdType.PRIVATE_MESSAGE.code()); recvInfo.setSendResult(message.getSendResult()); @@ -63,7 +61,7 @@ public class IMSender { Integer serverId = (Integer)redisTemplate.opsForValue().get(key); // 如果终端在线,将数据存储至redis,等待拉取推送 if (serverId != null) { - String sendKey = String.join(":", IMRedisKey.IM_UNREAD_PRIVATE_QUEUE, serverId.toString()); + String sendKey = String.join(":", IMRedisKey.IM_MESSAGE_PRIVATE_QUEUE, serverId.toString()); IMRecvInfo recvInfo = new IMRecvInfo(); // 自己的消息不需要回推消息结果 recvInfo.setSendResult(false); @@ -112,7 +110,7 @@ public class IMSender { recvInfo.setSendResult(message.getSendResult()); recvInfo.setData(message.getData()); // 推送至队列 - String key = String.join(":", IMRedisKey.IM_UNREAD_GROUP_QUEUE, entry.getKey().toString()); + String key = String.join(":", IMRedisKey.IM_MESSAGE_GROUP_QUEUE, entry.getKey().toString()); redisTemplate.opsForList().rightPush(key, recvInfo); } // 对离线用户回复消息状态 @@ -144,7 +142,7 @@ public class IMSender { // 自己的消息不需要回推消息结果 recvInfo.setSendResult(false); recvInfo.setData(message.getData()); - String sendKey = String.join(":", IMRedisKey.IM_UNREAD_GROUP_QUEUE, serverId.toString()); + String sendKey = String.join(":", IMRedisKey.IM_MESSAGE_GROUP_QUEUE, serverId.toString()); redisTemplate.opsForList().rightPush(sendKey, recvInfo); } } diff --git a/im-commom/src/main/java/com/bx/imcommon/contant/IMRedisKey.java b/im-commom/src/main/java/com/bx/imcommon/contant/IMRedisKey.java index a3d7ad4..a40f8d0 100644 --- a/im-commom/src/main/java/com/bx/imcommon/contant/IMRedisKey.java +++ b/im-commom/src/main/java/com/bx/imcommon/contant/IMRedisKey.java @@ -7,9 +7,9 @@ public class IMRedisKey { // 用户ID所连接的IM-server的ID public final static String IM_USER_SERVER_ID = "im:user:server_id"; // 未读私聊消息队列 - public final static String IM_UNREAD_PRIVATE_QUEUE = "im:unread:private"; + public final static String IM_MESSAGE_PRIVATE_QUEUE = "im:message:private"; // 未读群聊消息队列 - public final static String IM_UNREAD_GROUP_QUEUE = "im:unread:group"; + public final static String IM_MESSAGE_GROUP_QUEUE = "im:message:group"; // 私聊消息发送结果队列 public final static String IM_RESULT_PRIVATE_QUEUE = "im:result:private"; // 群聊消息发送结果队列 diff --git a/im-platform/src/main/java/com/bx/implatform/listener/GroupMessageListener.java b/im-platform/src/main/java/com/bx/implatform/listener/GroupMessageListener.java index a99522c..0648a13 100644 --- a/im-platform/src/main/java/com/bx/implatform/listener/GroupMessageListener.java +++ b/im-platform/src/main/java/com/bx/implatform/listener/GroupMessageListener.java @@ -22,6 +22,7 @@ public class GroupMessageListener implements MessageListener { @Override public void process(IMSendResult result){ GroupMessageVO messageInfo = result.getData(); + // todo 删除 // 保存该用户已拉取的最大消息id if(result.getCode().equals(IMSendCode.SUCCESS.code())) { String key = String.join(":",RedisKey.IM_GROUP_READED_POSITION,messageInfo.getGroupId().toString(),result.getReceiver().getId().toString()); diff --git a/im-platform/src/main/java/com/bx/implatform/service/impl/FriendServiceImpl.java b/im-platform/src/main/java/com/bx/implatform/service/impl/FriendServiceImpl.java index c40cf10..3d450a8 100644 --- a/im-platform/src/main/java/com/bx/implatform/service/impl/FriendServiceImpl.java +++ b/im-platform/src/main/java/com/bx/implatform/service/impl/FriendServiceImpl.java @@ -79,7 +79,7 @@ public class FriendServiceImpl extends ServiceImpl impleme @Override public void delFriend(Long friendId) { long userId = SessionContext.getSession().getUserId(); - // 互相解除好友关系 + // 互相解除好友关系,走代理清理缓存 FriendServiceImpl proxy = (FriendServiceImpl)AopContext.currentProxy(); proxy.unbindFriend(userId,friendId); proxy.unbindFriend(friendId,userId); 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 8a90f4e..176bf23 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 @@ -1,5 +1,6 @@ package com.bx.implatform.service.impl; +import cn.hutool.core.util.StrUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; @@ -27,6 +28,7 @@ import com.bx.implatform.session.UserSession; import com.bx.implatform.util.BeanUtils; import com.bx.implatform.dto.GroupMessageDTO; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; @@ -50,7 +52,7 @@ public class GroupMessageServiceImpl extends ServiceImpl vo.getGroupId().equals(id)).forEach(vo -> { if (vo.getId() <= sendMaxId) { - // 已推送过 - vo.setStatus(MessageStatus.SENDED.code()); + // 已读 + vo.setStatus(MessageStatus.READED.code()); } else { // 未推送 vo.setStatus(MessageStatus.UNSEND.code()); @@ -250,6 +252,17 @@ public class GroupMessageServiceImpl extends ServiceImpl wrapper = Wrappers.lambdaQuery(); + wrapper.eq(GroupMessage::getGroupId, groupId) + .orderByDesc(GroupMessage::getId) + .last("limit 1") + .select(GroupMessage::getId); + GroupMessage message = this.getOne(wrapper); + String key = StrUtil.join(":",RedisKey.IM_GROUP_READED_POSITION,groupId,session.getUserId()); + redisTemplate.opsForValue().set(key, message.getId()); + } /** 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 7de2679..314c2a6 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 @@ -45,7 +45,7 @@ public class PrivateMessageServiceImpl extends ServiceImpl - +
  • - @@ -115,7 +115,8 @@ y: 0 }, showHistory: false, // 是否显示历史聊天记录 - lockMessage: false // 是否锁定发送 + lockMessage: false, // 是否锁定发送, + showMinIdx: 0 // 下标低于showMinIdx的消息不显示,否则页面会很卡 } }, methods: { @@ -236,6 +237,22 @@ handleCloseSide() { this.showSide = false; }, + handleScrollToTop() { + // 多展示10条信息 + this.showMinIdx = this.showMinIdx > 10 ? this.showMinIdx - 10 : 0; + }, + handleScroll(e) { + + let scrollElement = e.target + let scrollTop = scrollElement.scrollTop + if (scrollTop <30 ) { // 在顶部,不滚动的情况 + console.log("next") + // 多展示20条信息 + this.showMinIdx = this.showMinIdx > 20 ? this.showMinIdx - 20 : 0; + + } + + }, switchEmotionBox() { this.showEmotion = !this.showEmotion; let width = this.$refs.emotion.offsetWidth; @@ -329,7 +346,6 @@ }, sendTextMessage() { if (!this.sendText.trim()) { - this.$message.error("不能发送空白信息"); return } let msgInfo = { @@ -396,16 +412,16 @@ }); }, readedMessage() { - if(this.chat.type == "GROUP"){ + if (this.chat.type == "GROUP") { var url = `/message/group/readed?groupId=${this.chat.targetId}` - }else{ + } else { url = `/message/private/readed?friendId=${this.chat.targetId}` } this.$http({ url: url, method: 'put' }).then(() => { - this.$store.commit("resetUnreadCount",this.chat) + this.$store.commit("resetUnreadCount", this.chat) this.scrollToBottom(); }) }, @@ -457,7 +473,7 @@ }, scrollToBottom() { this.$nextTick(() => { - const div = document.getElementById("chatScrollBox"); + let div = document.getElementById("chatScrollBox"); div.scrollTop = div.scrollHeight; }); } @@ -490,8 +506,9 @@ watch: { chat: { handler(newChat, oldChat) { - if (newChat.targetId > 0 && (!oldChat || newChat.type != oldChat.type || newChat.targetId != oldChat - .targetId)) { + if (newChat.targetId > 0 && (!oldChat || newChat.type != oldChat.type || + newChat.targetId != oldChat.targetId)) { + if (this.chat.type == "GROUP") { this.loadGroup(this.chat.targetId); } else { @@ -499,6 +516,9 @@ } this.scrollToBottom(); this.sendText = ""; + // 初始状态只显示30条消息 + let size = this.chat.messages.length; + this.showMinIdx = size > 30 ? size - 30 : 0; // 保持输入框焦点 this.$nextTick(() => { this.$refs.sendBox.focus(); @@ -509,12 +529,16 @@ }, unreadCount: { handler(newCount, oldCount) { - if(newCount > 0){ + if (newCount > 0) { // 消息已读 this.readedMessage() } } } + }, + mounted() { + let div = document.getElementById("chatScrollBox"); + div.addEventListener('scroll', this.handleScroll) } } @@ -606,8 +630,7 @@ color: black; background-color: #f8f8f8 !important; outline-color: rgba(83, 160, 231, 0.61); - text-align: left; - border: 0; + } .send-image-area { diff --git a/im-ui/src/components/chat/ChatMessageItem.vue b/im-ui/src/components/chat/ChatMessageItem.vue index 4fde6eb..b1a2e41 100644 --- a/im-ui/src/components/chat/ChatMessageItem.vue +++ b/im-ui/src/components/chat/ChatMessageItem.vue @@ -342,10 +342,10 @@ color: #f23c0f; font-weight: 600; } - + .chat-readed { font-size: 10px; - color: #aaa; + color: #888; font-weight: 600; } } diff --git a/im-ui/src/store/chatStore.js b/im-ui/src/store/chatStore.js index b65c2da..d7d6111 100644 --- a/im-ui/src/store/chatStore.js +++ b/im-ui/src/store/chatStore.js @@ -15,22 +15,16 @@ export default { }, mutations: { - initChats(state, chats) { - state.chats = chats||[]; + initChats(state, chatsData) { + state.chats = chatsData.chats || []; + state.privateMsgMaxId = chatsData.privateMsgMaxId || 0; + state.groupMsgMaxId = chatsData.groupMsgMaxId || 0; + // 防止图片一直处在加载中状态 state.chats.forEach((chat) => { chat.messages.forEach((msg) => { - // 防止图片一直处在加载中状态 if (msg.loadStatus == "loading") { msg.loadStatus = "fail" } - // 记录最大私聊消息id - if(chat.type == "PRIVATE" && msg.id && msg.id>state.privateMsgMaxId){ - state.privateMsgMaxId = msg.id; - } - // 记录最大群聊消息id - if(chat.type == "GROUP" && msg.id && msg.id>state.groupMsgMaxId){ - state.groupMsgMaxId = msg.id; - } }) }) }, @@ -76,17 +70,17 @@ export default { }, resetUnreadCount(state, chatInfo) { for (let idx in state.chats) { - if (state.chats[idx].type == chatInfo.type - && state.chats[idx].targetId == chatInfo.targetId) { - state.chats[idx].unreadCount=0; + if (state.chats[idx].type == chatInfo.type && + state.chats[idx].targetId == chatInfo.targetId) { + state.chats[idx].unreadCount = 0; } } this.commit("saveToStorage"); }, readedMessage(state, friendId) { for (let idx in state.chats) { - if (state.chats[idx].type == 'PRIVATE' - && state.chats[idx].targetId == friendId) { + if (state.chats[idx].type == 'PRIVATE' && + state.chats[idx].targetId == friendId) { state.chats[idx].messages.forEach((m) => { if (m.selfSend && m.status != MESSAGE_STATUS.RECALL) { m.status = MESSAGE_STATUS.READED @@ -154,12 +148,11 @@ export default { if (!msgInfo.selfSend && msgInfo.status != MESSAGE_STATUS.READED) { chat.unreadCount++; } - // 记录消息的最大id - if (msgInfo.id && type=="PRIVATE" && msgInfo.id > state.privateMsgMaxId) { + if (msgInfo.id && type == "PRIVATE" && msgInfo.id > state.privateMsgMaxId) { state.privateMsgMaxId = msgInfo.id; - } - if (msgInfo.id && type=="GROUP" && msgInfo.id > state.groupMsgMaxId) { + } + if (msgInfo.id && type == "GROUP" && msgInfo.id > state.groupMsgMaxId) { state.groupMsgMaxId = msgInfo.id; } // 如果是已存在消息,则覆盖旧的消息数据 @@ -239,17 +232,22 @@ export default { } this.commit("saveToStorage"); }, - - loadingPrivateMsg(state,loadding){ + + loadingPrivateMsg(state, loadding) { state.loadingPrivateMsg = loadding; }, - loadingGroupMsg(state,loadding){ + loadingGroupMsg(state, loadding) { state.loadingGroupMsg = loadding; }, saveToStorage(state) { let userId = userStore.state.userInfo.id; let key = "chats-" + userId; - localStorage.setItem(key, JSON.stringify(state.chats)); + let chatsData = { + privateMsgMaxId: state.privateMsgMaxId, + groupMsgMaxId: state.groupMsgMaxId, + chats: state.chats + } + localStorage.setItem(key, JSON.stringify(chatsData)); }, clear(state) { state.activeIndex = -1; @@ -262,8 +260,10 @@ export default { let userId = userStore.state.userInfo.id; let key = "chats-" + userId; let item = localStorage.getItem(key) - let chats = JSON.parse(localStorage.getItem(key)); - context.commit("initChats", chats); + if (item) { + let chatsData = JSON.parse(item); + context.commit("initChats", chatsData); + } resolve(); }) } diff --git a/im-ui/src/view/Home.vue b/im-ui/src/view/Home.vue index 77a5083..d84eafb 100644 --- a/im-ui/src/view/Home.vue +++ b/im-ui/src/view/Home.vue @@ -74,13 +74,13 @@ data() { return { showSettingDialog: false, - + lastPlayAudioTime: new Date()-1000 } }, methods: { init() { this.$store.dispatch("load").then(() => { - // 加载未拉取的消息 + // 加载离线消息 this.loadPrivateMessage(this.$store.state.chatStore.privateMsgMaxId); this.loadGroupMessage(this.$store.state.chatStore.groupMsgMaxId); // ws初始化 @@ -202,8 +202,9 @@ // 插入消息 this.$store.commit("insertMessage", msg); // 播放提示音 - !msg.selfSend && this.playAudioTip(); - + if(!msg.selfSend && msg.status != this.$enums.MESSAGE_STATUS.READED){ + this.playAudioTip(); + } }, handleGroupMessage(msg) { // 标记这条消息是不是自己发的 @@ -236,7 +237,9 @@ // 插入消息 this.$store.commit("insertMessage", msg); // 播放提示音 - !msg.selfSend && this.playAudioTip(); + if(!msg.selfSend && msg.status != this.$enums.MESSAGE_STATUS.READED){ + this.playAudioTip(); + } }, handleExit() { this.$wsApi.close(); @@ -244,10 +247,14 @@ location.href = "/"; }, playAudioTip() { - let audio = new Audio(); - let url = require(`@/assets/audio/tip.wav`); - audio.src = url; - audio.play(); + if(new Date() - this.lastPlayAudioTime > 1000){ + this.lastPlayAudioTime = new Date(); + let audio = new Audio(); + let url = require(`@/assets/audio/tip.wav`); + audio.src = url; + audio.play(); + } + }, showSetting() { this.showSettingDialog = true; diff --git a/im-uniapp/App.vue b/im-uniapp/App.vue index 938cb27..be4ed1d 100644 --- a/im-uniapp/App.vue +++ b/im-uniapp/App.vue @@ -18,6 +18,9 @@ this.initAudit(); // 初始化websocket this.initWebSocket(); + // 加载离线消息 + this.loadPrivateMessage(store.state.chatStore.privateMsgMaxId); + this.loadGroupMessage(store.state.chatStore.groupMsgMaxId); }).catch((e) => { console.log(e); this.exit(); @@ -28,10 +31,7 @@ let userId = store.state.userStore.userInfo.id; wsApi.init(process.env.WS_URL, loginInfo.accessToken); wsApi.connect(); - wsApi.onOpen(()=>{ - // 拉取未读消息 - this.pullUnreadMessage(); - }) + wsApi.onOpen() wsApi.onMessage((cmd, msgInfo) => { if (cmd == 2) { // 异地登录,强制下线 @@ -41,58 +41,85 @@ }) this.exit(); } else if (cmd == 3) { - // 标记这条消息是不是自己发的 - msgInfo.selfSend = userId == msgInfo.sendId; - // 插入私聊消息 + // 私聊消息 this.handlePrivateMessage(msgInfo); } else if (cmd == 4) { - // 标记这条消息是不是自己发的 - msgInfo.selfSend = userId == msgInfo.sendId; - // 插入群聊消息 + // 群聊消息 this.handleGroupMessage(msgInfo); } }); - wsApi.onClose((res)=>{ + wsApi.onClose((res) => { // 1006是服务器主动断开,3000是APP主动关闭 - if(res.code == 1006){ + if (res.code == 1006) { uni.showToast({ title: '连接已断开,请重新登录', icon: 'none', }) this.exit(); - }else if(res.code != 3000){ + } else if (res.code != 3000) { // 重新连接 wsApi.connect(); } }) }, - pullUnreadMessage() { - // 拉取未读私聊消息 + loadPrivateMessage(minId) { + store.commit("loadingPrivateMsg", true) http({ - url: "/message/private/pullUnreadMessage", - method: 'POST' - }); - // 拉取未读群聊消息 + url: "/message/private/loadMessage?minId=" + minId, + method: 'get' + }).then((msgInfos) => { + msgInfos.forEach((msgInfo) => { + this.handlePrivateMessage(msgInfo); + }) + if (msgInfos.length == 100) { + // 继续拉取 + this.loadPrivateMessage(msgInfos[99].id); + } else { + store.commit("loadingPrivateMsg", false) + } + }) + }, + loadGroupMessage(minId) { + store.commit("loadingGroupMsg", true) http({ - url: "/message/group/pullUnreadMessage", - method: 'POST' - }); + url: "/message/group/loadMessage?minId=" + minId, + method: 'get' + }).then((msgInfos) => { + msgInfos.forEach((msgInfo) => { + this.handleGroupMessage(msgInfo); + }) + if (msgInfos.length == 100) { + // 继续拉取 + this.loadGroupMessage(msgInfos[99].id); + } else { + store.commit("loadingGroupMsg", false) + } + }) }, handlePrivateMessage(msg) { + // 标记这条消息是不是自己发的 + msg.selfSend = msg.sendId == store.state.userStore.userInfo.id; + // 好友id let friendId = msg.selfSend ? msg.recvId : msg.sendId; - let friend = store.state.friendStore.friends.find((f) => f.id == friendId); - if (!friend) { - http({ - url: `/friend/find/${msg.sendId}`, - method: 'GET' - }).then((friend) => { - this.insertPrivateMessage(friend, msg); - store.commit("addFriend", friend); - }) - } else { - // 好友列表不存在好友信息,则发请求获取好友信息 - this.insertPrivateMessage(friend, msg); + // 消息已读处理 + if (msg.type == enums.MESSAGE_TYPE.READED) { + if (msg.selfSend) { + // 我已读对方的消息,清空已读数量 + let chatInfo = { + type: 'PRIVATE', + targetId: friendId + } + store.commit("resetUnreadCount", chatInfo) + } else { + // 对方已读我的消息,修改消息状态为已读 + store.commit("readedMessage", friendId) + + } + return; } + this.loadFriendInfo(friendId).then((friend) => { + this.insertPrivateMessage(friend, msg); + }) }, insertPrivateMessage(friend, msg) { @@ -115,19 +142,23 @@ }, handleGroupMessage(msg) { - let group = store.state.groupStore.groups.find((g) => g.id == msg.groupId); - if (!group) { - http({ - url: `/group/find/${msg.groupId}`, - method: 'get' - }).then((group) => { - this.insertGroupMessage(group, msg); - store.commit("addGroup", group); - }) - } else { - // 群聊缓存存在,直接插入群聊消息 - this.insertGroupMessage(group, msg); + // 标记这条消息是不是自己发的 + msg.selfSend = msg.sendId == store.state.userStore.userInfo.id; + let groupId = msg.groupId; + // 消息已读处理 + if (msg.type == enums.MESSAGE_TYPE.READED) { + // 我已读对方的消息,清空已读数量 + let chatInfo = { + type: 'GROUP', + targetId: groupId + } + store.commit("resetUnreadCount", chatInfo) + return; } + this.loadGroupInfo(groupId).then((group) => { + // 插入群聊消息 + this.insertGroupMessage(group, msg); + }) }, insertGroupMessage(group, msg) { @@ -144,11 +175,43 @@ // 播放提示音 !msg.selfSend && this.playAudioTip(); }, + loadFriendInfo(id) { + return new Promise((resolve, reject) => { + let friend = store.state.friendStore.friends.find((f) => f.id == id); + if (friend) { + resolve(friend); + } else { + http({ + url: `/friend/find/${id}`, + method: 'get' + }).then((friend) => { + store.commit("addFriend", friend); + resolve(friend) + }) + } + }); + }, + loadGroupInfo(id) { + return new Promise((resolve, reject) => { + let group = store.state.groupStore.groups.find((g) => g.id == id); + if (group) { + resolve(group); + } else { + http({ + url: `/group/find/${id}`, + method: 'get' + }).then((group) => { + resolve(group) + store.commit("addGroup", group); + }) + } + }); + }, exit() { console.log("exit"); wsApi.close(); uni.removeStorageSync("loginInfo"); - uni.navigateTo({ + uni.reLaunch({ url: "/pages/login/login" }) store.dispatch("unload"); @@ -161,18 +224,18 @@ }, initAudit() { console.log("initAudit") - if(store.state.userStore.userInfo.type == 1){ + if (store.state.userStore.userInfo.type == 1) { // 显示群组功能 uni.setTabBarItem({ index: 2, text: "群聊" - }) - }else{ + }) + } else { // 隐藏群组功能 uni.setTabBarItem({ index: 2, text: "搜索" - }) + }) } } }, @@ -214,5 +277,4 @@ // #endif background-color: #f8f8f8; } - \ No newline at end of file diff --git a/im-uniapp/common/emotion.js b/im-uniapp/common/emotion.js index b655db4..178909a 100644 --- a/im-uniapp/common/emotion.js +++ b/im-uniapp/common/emotion.js @@ -7,41 +7,26 @@ const emoTextList = ['憨笑', '媚眼', '开心', '坏笑', '可怜', '爱心', ]; -let emoImageUrlList = []; - -// 备注:经过测试,小程序的无法显示相对路径的图片,所以在这里对图片提前全部转成绝对路径 -// 提前初始化图片的url -for (let i = 0; i < emoTextList.length; i++) { - let path = `/static/emoji2/${i}.gif`; - uni.getImageInfo({ - src: path, - success(res) { - emoImageUrlList[i] = res.path - }, - fail(res) { - emoImageUrlList = path; - } - }); -} - let transform = (content) => { return content.replace(/\#[\u4E00-\u9FA5]{1,3}\;/gi, textToImg); } - // 将匹配结果替换表情图片 let textToImg = (emoText) => { let word = emoText.replace(/\#|\;/gi, ''); let idx = emoTextList.indexOf(word); + if (idx == -1) { + return ""; + } let path = textToPath(emoText); // #ifdef MP // 微信小程序不能有前面的'/' path = path.slice(1); // #endif let img = ``; + margin: 0 -2px;vertical-align:bottom;"/>`; return img; } diff --git a/im-uniapp/common/enums.js b/im-uniapp/common/enums.js index ce4daf9..6beeb24 100644 --- a/im-uniapp/common/enums.js +++ b/im-uniapp/common/enums.js @@ -6,6 +6,7 @@ const MESSAGE_TYPE = { AUDIO:3, VIDEO:4, RECALL:10, + READED:11, TIP_TIME:20, RTC_CALL: 101, RTC_ACCEPT: 102, @@ -27,8 +28,16 @@ const TERMINAL_TYPE = { APP: 1 } +const MESSAGE_STATUS = { + UNSEND: 0, + SENDED: 1, + RECALL:2, + READED:3 +} + export { MESSAGE_TYPE, USER_STATE, - TERMINAL_TYPE + TERMINAL_TYPE, + MESSAGE_STATUS } diff --git a/im-uniapp/common/request.js b/im-uniapp/common/request.js index 42c8ff8..606c841 100644 --- a/im-uniapp/common/request.js +++ b/im-uniapp/common/request.js @@ -43,9 +43,6 @@ const request = (options) => { requestList.forEach(cb => cb()); requestList = []; isRefreshToken = false; - // 保存token - console.log(res.data.data.accessToken) - // 重新发送刚才的请求 return resolve(request(options)) 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 7a4893b..dad63a1 100644 --- a/im-uniapp/components/chat-message-item/chat-message-item.vue +++ b/im-uniapp/components/chat-message-item/chat-message-item.vue @@ -2,12 +2,12 @@ {{msgInfo.content}} - {{$date.toTimeText(msgInfo.sendTime)}} - + {{$date.toTimeText(msgInfo.sendTime)}} + + - + {{showName}} @@ -18,8 +18,8 @@ :nodes="$emo.transform(msgInfo.content)"> - + @@ -40,6 +40,11 @@ + 已读 + 未读 +