import 'package:get/get.dart'; import '../../im/im_manager.dart'; import 'package:im_flutter_sdk/im_flutter_sdk.dart'; class ChatController extends GetxController { final String userId; // 用户信息 final Rx userInfo = Rx(null); ChatController({required this.userId}); @override void onInit() { super.onInit(); // 初始化时获取用户信息 fetchUserInfo(); } /// 获取用户信息 Future 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 sendMessage(String content) async { try { final message = await IMManager.instance.sendTextMessage(content, userId); return message != null; } catch (e) { if (Get.isLogEnable) { Get.log('发送消息失败: $e'); } return false; } } }