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.
54 lines
1.3 KiB
54 lines
1.3 KiB
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<EMUserInfo?> userInfo = Rx<EMUserInfo?>(null);
|
|
|
|
ChatController({required this.userId});
|
|
|
|
@override
|
|
void onInit() {
|
|
super.onInit();
|
|
// 初始化时获取用户信息
|
|
fetchUserInfo();
|
|
}
|
|
|
|
/// 获取用户信息
|
|
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);
|
|
return message != null;
|
|
} catch (e) {
|
|
if (Get.isLogEnable) {
|
|
Get.log('发送消息失败: $e');
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
}
|