You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
288 lines
7.4 KiB
288 lines
7.4 KiB
import 'package:get/get.dart';
|
|
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
|
|
|
|
import '../../im/im_manager.dart';
|
|
import 'package:im_flutter_sdk/im_flutter_sdk.dart';
|
|
import 'conversation_controller.dart';
|
|
|
|
class ChatController extends GetxController {
|
|
final String userId;
|
|
|
|
// 用户信息
|
|
final Rx<EMUserInfo?> userInfo = Rx<EMUserInfo?>(null);
|
|
|
|
// 消息列表
|
|
final RxList<EMMessage> messages = RxList<EMMessage>([]);
|
|
|
|
// 加载更多的游标
|
|
String? _cursor;
|
|
|
|
// 视频发送状态
|
|
final RxBool isSendingVideo = RxBool(false);
|
|
final RxString sendingStatus = RxString('');
|
|
|
|
ChatController({required this.userId});
|
|
|
|
@override
|
|
void onInit() {
|
|
super.onInit();
|
|
// 注册到 IMManager,以便接收消息时能通知到此 Controller
|
|
IMManager.instance.registerChatController(this);
|
|
// 初始化时获取用户信息和消息列表
|
|
fetchUserInfo();
|
|
fetchMessages();
|
|
}
|
|
|
|
@override
|
|
void onClose() {
|
|
// 注销 ChatController
|
|
IMManager.instance.unregisterChatController(userId);
|
|
super.onClose();
|
|
}
|
|
|
|
/// 获取用户信息
|
|
Future<void> fetchUserInfo() async {
|
|
try {
|
|
final EMUserInfo? info = await IMManager.instance.getUserInfo(userId);
|
|
if (info != null) {
|
|
userInfo.value = info;
|
|
if (Get.isLogEnable) {
|
|
Get.log('获取用户信息成功: ${info.nickName}');
|
|
}
|
|
} else {
|
|
if (Get.isLogEnable) {
|
|
Get.log('未找到用户信息: $userId');
|
|
}
|
|
}
|
|
} catch (e) {
|
|
if (Get.isLogEnable) {
|
|
Get.log('获取用户信息失败: $e');
|
|
}
|
|
}
|
|
}
|
|
|
|
/// 发送消息
|
|
Future<bool> sendMessage(String content) async {
|
|
try {
|
|
final message = await IMManager.instance.sendTextMessage(content, userId);
|
|
if (message != null) {
|
|
// 发送成功后将消息添加到列表开头
|
|
messages.insert(0, message);
|
|
update();
|
|
// 更新会话列表
|
|
_refreshConversationList();
|
|
return true;
|
|
}
|
|
return false;
|
|
} catch (e) {
|
|
if (Get.isLogEnable) {
|
|
Get.log('发送消息失败: $e');
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// 发送图片消息
|
|
Future<bool> sendImageMessage(String imagePath) async {
|
|
try {
|
|
final message = await IMManager.instance.sendImageMessage(
|
|
imagePath,
|
|
userId,
|
|
);
|
|
if (message != null) {
|
|
// 发送成功后将消息添加到列表开头
|
|
messages.insert(0, message);
|
|
update();
|
|
// 更新会话列表
|
|
_refreshConversationList();
|
|
return true;
|
|
}
|
|
return false;
|
|
} catch (e) {
|
|
if (Get.isLogEnable) {
|
|
Get.log('发送图片消息失败: $e');
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// 发送语音消息
|
|
Future<bool> sendVoiceMessage(String filePath, int seconds) async {
|
|
try {
|
|
final message = await IMManager.instance.sendVoiceMessage(
|
|
filePath,
|
|
userId,
|
|
seconds,
|
|
);
|
|
if (message != null) {
|
|
// 发送成功后将消息添加到列表开头
|
|
messages.insert(0, message);
|
|
update();
|
|
// 更新会话列表
|
|
_refreshConversationList();
|
|
return true;
|
|
}
|
|
return false;
|
|
} catch (e) {
|
|
if (Get.isLogEnable) {
|
|
Get.log('发送语音消息失败: $e');
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// 发送视频消息
|
|
Future<bool> sendVideoMessage(String filePath, int duration) async {
|
|
// 如果正在发送,防止重复发送
|
|
if (isSendingVideo.value) {
|
|
SmartDialog.showToast('视频正在发送中,请稍候...');
|
|
return false;
|
|
}
|
|
|
|
try {
|
|
// 设置发送状态
|
|
isSendingVideo.value = true;
|
|
sendingStatus.value = '正在准备视频...';
|
|
update();
|
|
|
|
print('🎬 [ChatController] 准备发送视频消息');
|
|
print('视频路径: $filePath');
|
|
print('视频时长: $duration 秒');
|
|
|
|
sendingStatus.value = '正在上传视频...';
|
|
update();
|
|
|
|
final message = await IMManager.instance.sendVideoMessage(
|
|
filePath,
|
|
userId,
|
|
duration,
|
|
);
|
|
|
|
if (message != null) {
|
|
print('✅ [ChatController] 视频消息创建成功');
|
|
print('消息类型: ${message.body.type}');
|
|
|
|
sendingStatus.value = '发送成功';
|
|
update();
|
|
|
|
// 发送成功后将消息添加到列表开头
|
|
messages.insert(0, message);
|
|
update();
|
|
|
|
// 更新会话列表
|
|
_refreshConversationList();
|
|
|
|
// 显示成功提示
|
|
SmartDialog.showToast('✅ 视频发送成功');
|
|
|
|
return true;
|
|
}
|
|
|
|
print('❌ [ChatController] 视频消息创建失败');
|
|
sendingStatus.value = '发送失败';
|
|
update();
|
|
|
|
SmartDialog.showToast('❌ 视频消息发送失败,请重试');
|
|
|
|
return false;
|
|
} catch (e) {
|
|
print('❌ [ChatController] 发送视频消息异常: $e');
|
|
|
|
sendingStatus.value = '发送失败: $e';
|
|
update();
|
|
|
|
if (Get.isLogEnable) {
|
|
Get.log('发送视频消息失败: $e');
|
|
}
|
|
|
|
SmartDialog.showToast('❌ 视频消息发送失败: ${e.toString()}');
|
|
|
|
return false;
|
|
} finally {
|
|
// 重置发送状态
|
|
isSendingVideo.value = false;
|
|
sendingStatus.value = '';
|
|
update();
|
|
}
|
|
}
|
|
|
|
/// 获取消息列表
|
|
Future<void> fetchMessages({bool loadMore = false}) async {
|
|
try {
|
|
final List<EMMessage?> fetchedMessages = await IMManager.instance
|
|
.getMessages(
|
|
userId,
|
|
pageSize: 20,
|
|
startMsgId: loadMore ? _cursor : null,
|
|
);
|
|
|
|
// 过滤掉null消息
|
|
final List<EMMessage> validMessages = fetchedMessages
|
|
.whereType<EMMessage>()
|
|
.toList();
|
|
|
|
if (loadMore) {
|
|
// 加载更多时添加到列表末尾
|
|
messages.addAll(validMessages);
|
|
} else {
|
|
// 刷新时替换整个列表
|
|
messages.assignAll(validMessages);
|
|
}
|
|
|
|
// 通知UI更新
|
|
update();
|
|
|
|
// 更新游标
|
|
if (validMessages.isNotEmpty) {
|
|
_cursor = validMessages.last.msgId;
|
|
}
|
|
|
|
if (Get.isLogEnable) {
|
|
Get.log('获取消息成功,数量: ${validMessages.length}');
|
|
}
|
|
} catch (e) {
|
|
if (Get.isLogEnable) {
|
|
Get.log('获取消息失败: $e');
|
|
}
|
|
}
|
|
}
|
|
|
|
/// 加载更多消息
|
|
Future<void> loadMoreMessages() async {
|
|
if (_cursor != null) {
|
|
await fetchMessages(loadMore: true);
|
|
}
|
|
}
|
|
|
|
/// 添加接收到的消息
|
|
void addReceivedMessage(EMMessage message) {
|
|
// 检查消息是否已存在(避免重复添加)
|
|
if (!messages.any((msg) => msg.msgId == message.msgId)) {
|
|
// 将新消息添加到列表开头
|
|
messages.insert(0, message);
|
|
update();
|
|
// 更新会话列表
|
|
_refreshConversationList();
|
|
|
|
if (Get.isLogEnable) {
|
|
Get.log('收到新消息并添加到列表: ${message.msgId}');
|
|
}
|
|
}
|
|
}
|
|
|
|
/// 刷新会话列表
|
|
void _refreshConversationList() {
|
|
try {
|
|
// 尝试获取 ConversationController 并刷新会话列表
|
|
if (Get.isRegistered<ConversationController>()) {
|
|
final conversationController = Get.find<ConversationController>();
|
|
conversationController.refreshConversations();
|
|
}
|
|
} catch (e) {
|
|
// ConversationController 可能未注册,忽略错误
|
|
if (Get.isLogEnable) {
|
|
Get.log('刷新会话列表失败: $e');
|
|
}
|
|
}
|
|
}
|
|
}
|