From d75b8fb8cf9ed78356eddb88c28cba1d05cc9193 Mon Sep 17 00:00:00 2001 From: wangdongbo <949818794@qq.com> Date: Tue, 3 Feb 2026 14:35:05 +0800 Subject: [PATCH] =?UTF-8?q?=E8=81=8A=E5=A4=A9=E5=AE=A4=E5=90=8D=E5=AD=97?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/message/hooks/use-group-chat.js | 11 +- utils/tim-chat.js | 179 +++++++++++++------------- 2 files changed, 100 insertions(+), 90 deletions(-) diff --git a/pages/message/hooks/use-group-chat.js b/pages/message/hooks/use-group-chat.js index beca78e..64a6836 100644 --- a/pages/message/hooks/use-group-chat.js +++ b/pages/message/hooks/use-group-chat.js @@ -23,6 +23,14 @@ export default function useGroupChat(groupID) { avatar: member.avatar, isTeamMember: member.isTeamMember // 标记是否为团队成员 } + // 如果成员有 miniAppId(患者的聊天 userID),也添加一个映射(用于消息的 from 字段) + if (member.miniAppId) { + res[member.miniAppId] = { + name: member.name, + avatar: member.avatar, + isTeamMember: member.isTeamMember + } + } }) return res }) @@ -92,7 +100,8 @@ export default function useGroupChat(groupID) { id: pid, name: groupResult.data.patient.name || '患者', avatar: '', // 患者不设置头像,使用默认 - isTeamMember: false + isTeamMember: false, + miniAppId: groupResult.data.patient.miniAppId || '' // 患者的聊天 userID }) } } diff --git a/utils/tim-chat.js b/utils/tim-chat.js index d2fa14f..acbb643 100644 --- a/utils/tim-chat.js +++ b/utils/tim-chat.js @@ -2170,93 +2170,6 @@ class TimChatManager { } } - // 发送自定义消息 - async sendCustomMessage(customPayload, description = '') { - if (!this.tim) { - this.triggerCallback('onError', 'IM未初始化') - return { success: false, error: 'IM未初始化' } - } - - // 检查登录状态 - if (!this.isLoggedIn) { - console.error('IM未登录,无法发送消息'); - this.triggerCallback('onError', 'IM未登录,请稍后重试') - return { success: false, error: 'IM未登录' } - } - - // 优先使用 currentConversationID,如果没有则尝试从 conversation 获取 - let conversationID = this.currentConversationID; - if (!conversationID && this.conversation) { - conversationID = this.conversation.conversationID; - } - - if (!conversationID) { - console.error('会话ID不存在'); - this.triggerCallback('onError', '会话不存在,请重新进入聊天') - return { success: false, error: '会话ID不存在' } - } - - // 从 conversationID 提取 groupID - let groupID = null; - if (conversationID.startsWith('GROUP')) { - groupID = conversationID.replace('GROUP', ''); - } else if (this.conversation?.groupProfile?.groupID) { - groupID = this.conversation.groupProfile.groupID; - } - - if (!groupID) { - console.error('无法获取群聊ID,conversationID:', conversationID); - this.triggerCallback('onError', '无法获取群聊ID') - return { success: false, error: '无法获取群聊ID' } - } - - // customPayload 已经是完整的 payload 对象 { data, description, extension } - // 直接使用,不需要再包装 - const payload = customPayload; - - const localMessage = { - ID: `local_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`, - flow: 'out', - type: 'TIMCustomElem', - payload: payload, - lastTime: Math.floor(Date.now() / 1000), - status: 'sending', - avatar: '/static/center/user-avatar.png', - conversationID: conversationID, - from: this.currentUserID - } - - console.log('发送自定义消息,本地消息对象:', localMessage); - - // 触发本地消息回调,立即显示在聊天列表中 - this.triggerCallback('onMessageReceived', localMessage) - - const message = this.tim.createCustomMessage({ - to: groupID, - conversationType: TIM.TYPES.CONV_GROUP, - payload: payload - }) - - try { - await this.tim.sendMessage(message) - localMessage.status = 'success' - console.log('自定义消息发送成功'); - return { success: true, message: localMessage } - } catch (error) { - console.error('自定义消息发送失败:', error) - localMessage.status = 'failed' - - // 如果是因为未登录导致的失败,尝试重连 - if (error.message && (error.message.includes('not login') || error.message.includes('sdk not ready'))) { - console.log('检测到未登录错误,尝试重连...'); - this.isLoggedIn = false; - this.ensureIMConnection(); - } - - return { success: false, error } - } - } - // 发送图片消息 async sendImageMessage(imageFile) { if (!this.tim) { @@ -2412,6 +2325,95 @@ class TimChatManager { } } + // 发送自定义消息 + async sendCustomMessage(messageData) { + if (!this.tim) { + this.triggerCallback('onError', 'IM未初始化') + return { success: false, error: 'IM未初始化' } + } + + // 检查登录状态 + if (!this.isLoggedIn) { + console.error('IM未登录,无法发送消息'); + this.triggerCallback('onError', 'IM未登录,请稍后重试') + return { success: false, error: 'IM未登录' } + } + + // 优先使用 currentConversationID,如果没有则尝试从 conversation 获取 + let conversationID = this.currentConversationID; + if (!conversationID && this.conversation) { + conversationID = this.conversation.conversationID; + } + + if (!conversationID) { + console.error('会话ID不存在'); + this.triggerCallback('onError', '会话不存在,请重新进入聊天') + return { success: false, error: '会话ID不存在' } + } + + // 从 conversationID 提取 groupID + let groupID = null; + if (conversationID.startsWith('GROUP')) { + groupID = conversationID.replace('GROUP', ''); + } else if (this.conversation?.groupProfile?.groupID) { + groupID = this.conversation.groupProfile.groupID; + } + + if (!groupID) { + console.error('无法获取群聊ID,conversationID:', conversationID); + this.triggerCallback('onError', '无法获取群聊ID') + return { success: false, error: '无法获取群聊ID' } + } + + const localMessage = { + ID: `local_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`, + flow: 'out', + type: 'TIMCustomElem', + payload: { + data: JSON.stringify(messageData), + description: messageData.content || '自定义消息1', + extension: messageData.messageType || 'custom' + }, + lastTime: Date.now(), + status: 'sending', + avatar: '/static/center/user-avatar.png', + conversationID: conversationID, + from: this.currentUserID + } + + // 缓存功能已移除 + + this.triggerCallback('onMessageReceived', localMessage) + + const message = this.tim.createCustomMessage({ + to: groupID, + conversationType: TIM.TYPES.CONV_GROUP, + payload: { + data: JSON.stringify(messageData), + description: messageData.content || '自定义消息', + extension: messageData.messageType || 'custom' + } + }) + + try { + await this.tim.sendMessage(message) + localMessage.status = 'success' + return { success: true, message: localMessage } + } catch (error) { + console.error('自定义消息发送失败:', error) + localMessage.status = 'failed' + + // 如果是因为未登录导致的失败,尝试重连 + if (error.message && (error.message.includes('not login') || error.message.includes('sdk not ready'))) { + console.log('检测到未登录错误,尝试重连...'); + this.isLoggedIn = false; + this.ensureIMConnection(); + } + + return { success: false, error } + } + } + // 工具方法 // 判断是否为系统消息 isSystemMessage(message) { @@ -2881,8 +2883,7 @@ export { setGlobalIMCallback, getGroupList, clearConversationCache, - clearAllMessageCache, - TIM + clearAllMessageCache } export default globalTimChatManager