|
|
|
@ -51,6 +51,7 @@ class IMManager { |
|
|
|
appKey: appKey, |
|
|
|
autoLogin: false, |
|
|
|
acceptInvitationAlways: false, |
|
|
|
debugMode: true |
|
|
|
); |
|
|
|
|
|
|
|
// 初始化SDK |
|
|
|
@ -130,15 +131,11 @@ class IMManager { |
|
|
|
// 通知对应的 ChatController 更新消息列表 |
|
|
|
_notifyChatControllers(messages); |
|
|
|
}, |
|
|
|
onCmdMessagesReceived: (cmdMessages) { |
|
|
|
onMessageContentChanged: (EMMessage message, String operatorId, int operationTime){ |
|
|
|
if (Get.isLogEnable) { |
|
|
|
Get.log('📨 [IMManager] 收到 ${cmdMessages.length} 条CMD消息'); |
|
|
|
Get.log('📨 [IMManager] 消息内容已修改: ${message.localTime}'); |
|
|
|
} |
|
|
|
// 处理CMD消息(如撤回消息等) |
|
|
|
for (var msg in cmdMessages) { |
|
|
|
_handleCmdMessage(msg); |
|
|
|
} |
|
|
|
}, |
|
|
|
} |
|
|
|
), |
|
|
|
); |
|
|
|
EMClient.getInstance.chatManager.addMessageEvent(_chatHandlerKey, ChatMessageEvent( |
|
|
|
@ -1194,157 +1191,6 @@ class IMManager { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// 处理CMD消息 |
|
|
|
void _handleCmdMessage(EMMessage cmdMessage) { |
|
|
|
try { |
|
|
|
// 这里可以处理各种CMD消息,如撤回消息等 |
|
|
|
if (Get.isLogEnable) { |
|
|
|
Get.log('处理CMD消息: ${cmdMessage.msgId}'); |
|
|
|
} |
|
|
|
} catch (e) { |
|
|
|
if (Get.isLogEnable) { |
|
|
|
Get.log('处理CMD消息失败: $e'); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 注意:以下方法保留用于未来扩展,当SDK支持更多回调时可以使用 |
|
|
|
// 目前环信SDK的EMChatEventHandler可能不支持这些回调,所以暂时注释掉 |
|
|
|
/* |
|
|
|
/// 通知消息已读 |
|
|
|
void _notifyMessageRead(List<EMMessage> messages, String from, String to) { |
|
|
|
try { |
|
|
|
// 找到对应的 ChatController 并通知消息已读 |
|
|
|
final controller = _activeChatControllers[from]; |
|
|
|
if (controller != null) { |
|
|
|
// TODO: 在 ChatController 中添加更新消息已读状态的方法 |
|
|
|
if (Get.isLogEnable) { |
|
|
|
Get.log('通知消息已读: $from'); |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (e) { |
|
|
|
if (Get.isLogEnable) { |
|
|
|
Get.log('通知消息已读失败: $e'); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// 通知消息已送达 |
|
|
|
void _notifyMessageDelivered(List<EMMessage> messages, String to) { |
|
|
|
try { |
|
|
|
// 找到对应的 ChatController 并通知消息已送达 |
|
|
|
for (var message in messages) { |
|
|
|
final targetId = message.to; |
|
|
|
if (targetId != null) { |
|
|
|
final controller = _activeChatControllers[targetId]; |
|
|
|
if (controller != null) { |
|
|
|
// TODO: 在 ChatController 中添加更新消息送达状态的方法 |
|
|
|
if (Get.isLogEnable) { |
|
|
|
Get.log('通知消息已送达: $targetId'); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (e) { |
|
|
|
if (Get.isLogEnable) { |
|
|
|
Get.log('通知消息已送达失败: $e'); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// 通知消息撤回 |
|
|
|
void _notifyMessageRecalled(List<EMMessage> messages) { |
|
|
|
try { |
|
|
|
// 通知所有相关的 ChatController 更新消息列表 |
|
|
|
for (var message in messages) { |
|
|
|
final fromId = message.from; |
|
|
|
final toId = message.to; |
|
|
|
|
|
|
|
// 通知发送方的 ChatController |
|
|
|
if (fromId != null) { |
|
|
|
final controller = _activeChatControllers[fromId]; |
|
|
|
if (controller != null) { |
|
|
|
// TODO: 在 ChatController 中添加处理消息撤回的方法 |
|
|
|
if (Get.isLogEnable) { |
|
|
|
Get.log('通知消息撤回(发送方): $fromId'); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 通知接收方的 ChatController |
|
|
|
if (toId != null) { |
|
|
|
final controller = _activeChatControllers[toId]; |
|
|
|
if (controller != null) { |
|
|
|
// TODO: 在 ChatController 中添加处理消息撤回的方法 |
|
|
|
if (Get.isLogEnable) { |
|
|
|
Get.log('通知消息撤回(接收方): $toId'); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 刷新会话列表 |
|
|
|
_refreshConversationList(); |
|
|
|
} catch (e) { |
|
|
|
if (Get.isLogEnable) { |
|
|
|
Get.log('通知消息撤回失败: $e'); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// 通知消息状态变更 |
|
|
|
void _notifyMessageStatusChanged(EMMessage message, MessageStatus status, EMError? error) { |
|
|
|
try { |
|
|
|
final targetId = message.to; |
|
|
|
if (targetId != null) { |
|
|
|
final controller = _activeChatControllers[targetId]; |
|
|
|
if (controller != null) { |
|
|
|
// 更新消息状态和错误码 |
|
|
|
final index = controller.messages.indexWhere((msg) => msg.msgId == message.msgId); |
|
|
|
if (index != -1) { |
|
|
|
// 更新消息对象 |
|
|
|
final updatedMessage = message; |
|
|
|
controller.messages[index] = updatedMessage; |
|
|
|
controller.update(); |
|
|
|
|
|
|
|
if (Get.isLogEnable) { |
|
|
|
final errorCode = error?.code; |
|
|
|
Get.log('✅ [IMManager] 已通知ChatController更新消息状态: $targetId, status=$status, errorCode=$errorCode'); |
|
|
|
} |
|
|
|
} else { |
|
|
|
// 如果找不到消息,尝试通过内容匹配(处理消息ID可能改变的情况) |
|
|
|
if (message.body.type == MessageType.TXT) { |
|
|
|
final textBody = message.body as EMTextMessageBody; |
|
|
|
final content = textBody.content; |
|
|
|
final contentIndex = controller.messages.indexWhere((msg) => |
|
|
|
msg.body.type == MessageType.TXT && |
|
|
|
(msg.body as EMTextMessageBody).content == content && |
|
|
|
msg.direction == MessageDirection.SEND && |
|
|
|
msg.status == MessageStatus.PROGRESS |
|
|
|
); |
|
|
|
if (contentIndex != -1) { |
|
|
|
// 更新消息对象,包括错误码 |
|
|
|
final updatedMessage = message; |
|
|
|
controller.messages[contentIndex] = updatedMessage; |
|
|
|
controller.update(); |
|
|
|
|
|
|
|
if (Get.isLogEnable) { |
|
|
|
final errorCode = error?.code; |
|
|
|
Get.log('✅ [IMManager] 已通过内容匹配更新消息状态: $targetId, status=$status, errorCode=$errorCode'); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (e) { |
|
|
|
if (Get.isLogEnable) { |
|
|
|
Get.log('通知消息状态变更失败: $e'); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
*/ |
|
|
|
|
|
|
|
/// 撤回消息 |
|
|
|
Future<bool> recallMessage(EMMessage message) async { |
|
|
|
try { |
|
|
|
|