diff --git a/im-uniapp/App.vue b/im-uniapp/App.vue index c58d3fb..f6cfd3b 100644 --- a/im-uniapp/App.vue +++ b/im-uniapp/App.vue @@ -79,6 +79,67 @@ export default { this.configStore.setAppInit(false); } }) + wsApi.onMessage((cmd, msgInfo) => { + if (cmd == 2) { + // 异地登录 + this.exit(); + } else if (cmd == 3) { + // 私聊消息 + this.handlePrivateMessage(msgInfo); + } else if (cmd == 4) { + // 群聊消息 + this.handleGroupMessage(msgInfo); + } else if (cmd == 5) { + // 系统消息 + this.handleSystemMessage(msgInfo); + } else if (cmd == 6) { + // 新增:客服变更通知 + this.handleCustomerChanged(msgInfo); + } + }); + }, + // 处理客服变更 + handleCustomerChanged(msgInfo) { + console.log('客服已变更,刷新聊天记录:', msgInfo); + + // 刷新好友列表(会获取最新的客服信息) + this.friendStore.loadFriend().then(() => { + // 刷新聊天列表 + this.chatStore.refreshChats(); + + // 如果当前正在聊天页面,重新加载消息 + const pages = getCurrentPages(); + const currentPage = pages[pages.length - 1]; + + if (currentPage.route === 'pages/chat/chat-box') { + // 重新加载当前会话的消息 + const targetId = currentPage.options.targetId; + this.reloadChatMessages(targetId); + } + }); + }, + + // 重新加载聊天消息 + reloadChatMessages(targetId) { + // 找到对应的会话 + const chat = this.chatStore.chats.find(c => + c.targetId == targetId && c.type === 'PRIVATE' + ); + + if (chat) { + // 清空消息 + chat.messages = []; + + // 重新加载历史消息 + this.$http({ + url: `/message/private/history/${targetId}?full=true`, + method: 'GET' + }).then(messages => { + messages.forEach(msg => { + this.chatStore.insertMessage(msg, chat); + }); + }); + } }, loadStore() { return this.userStore.loadUser().then(() => { @@ -144,6 +205,32 @@ export default { msg.selfSend = msg.sendId == this.userStore.userInfo.id; // 好友id let friendId = msg.selfSend ? msg.recvId : msg.sendId; + // 检查是否为未知用户(只检查收到的消息) + let existingFriend = this.friendStore.findFriend(friendId); + if (!existingFriend && !msg.selfSend) { + console.log("收到未知用户消息,刷新应用:", friendId); + + // 重新加载数据 + this.loadStore().then(() => { + // 刷新整个应用 + // #ifdef H5 + window.location.reload(); + // #endif + + // #ifdef APP-PLUS + plus.runtime.restart(); + // #endif + + // #ifdef MP-WEIXIN + // 小程序重新启动 + uni.reLaunch({ + url: '/pages/chat/chat' + }); + // #endif + }); + + return; + } // 会话信息 let chatInfo = { type: 'PRIVATE', diff --git a/im-uniapp/store/chatStore.js b/im-uniapp/store/chatStore.js index bad8dee..b7c73ae 100644 --- a/im-uniapp/store/chatStore.js +++ b/im-uniapp/store/chatStore.js @@ -30,6 +30,37 @@ export default defineStore('chatStore', { this.privateMsgMaxId = chatsData.privateMsgMaxId || 0; this.groupMsgMaxId = chatsData.groupMsgMaxId || 0; }, + + // 客服转接:合并旧客服会话到新客服(你要的功能) + mergeOldCustomerToNew(oldKfId, newKfId) { + const oldChat = this.findChatByFriend(oldKfId); + const newChat = this.findChatByFriend(newKfId); + + if (!oldChat || !newChat) return; + + newChat.messages = [...oldChat.messages, ...newChat.messages]; + + newChat.unreadCount += oldChat.unreadCount; + newChat.lastContent = oldChat.lastContent || newChat.lastContent; + newChat.lastSendTime = Math.max(oldChat.lastSendTime || 0, newChat.lastSendTime || 0); + + newChat.stored = false; + oldChat.stored = false; + + this.removePrivateChat(oldKfId); + + this.saveToStorage(true); + }, + + // 强制清理某个用户的本地缓存(清理旧客服) + clearCustomerCache(userId) { + const userStore = useUserStore(); + const currentUserId = userStore.userInfo.id; + const key = `chats-app-${currentUserId}-PRIVATE-${userId}`; + + uni.removeStorageSync(key); + uni.removeStorageSync(key + '-hot'); + }, openChat(chatInfo) { let chats = this.curChats; let chat = null; diff --git a/im-web/src/api/date.js b/im-web/src/api/date.js index f215f3b..3407a13 100644 --- a/im-web/src/api/date.js +++ b/im-web/src/api/date.js @@ -2,9 +2,9 @@ let toTimeText = (timeStamp, simple) => { var dateTime = new Date(timeStamp) var currentTime = Date.parse(new Date()); //当前时间 var timeDiff = currentTime - dateTime; //与当前时间误差 - var timeText = 'Just now'; + var timeText = ''; if (timeDiff <= 60000) { //一分钟内 - timeText = '刚刚'; + timeText = 'Just now'; } else if (timeDiff > 60000 && timeDiff < 3600000) { //1小时内 timeText = Math.floor(timeDiff / 60000) + ' minutes ago'; diff --git a/im-web/src/components/chat/ChatBox.vue b/im-web/src/components/chat/ChatBox.vue index 2f6938c..a25e3ea 100644 --- a/im-web/src/components/chat/ChatBox.vue +++ b/im-web/src/components/chat/ChatBox.vue @@ -59,11 +59,11 @@
- + + + +