|
|
@ -0,0 +1,60 @@ |
|
|
|
|
|
import 'package:get/get.dart'; |
|
|
|
|
|
import 'package:im_flutter_sdk/im_flutter_sdk.dart'; |
|
|
|
|
|
import '../../im/im_manager.dart'; |
|
|
|
|
|
|
|
|
|
|
|
class FriendController extends GetxController { |
|
|
|
|
|
// 好友列表数据 |
|
|
|
|
|
final friendList = <EMContact>[].obs; |
|
|
|
|
|
// 加载状态 |
|
|
|
|
|
final isLoading = false.obs; |
|
|
|
|
|
// 错误消息 |
|
|
|
|
|
final errorMessage = ''.obs; |
|
|
|
|
|
|
|
|
|
|
|
@override |
|
|
|
|
|
void onInit() { |
|
|
|
|
|
super.onInit(); |
|
|
|
|
|
// 初始化时加载好友列表 |
|
|
|
|
|
loadFriends(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// 加载好友列表 |
|
|
|
|
|
Future<void> loadFriends() async { |
|
|
|
|
|
try { |
|
|
|
|
|
isLoading.value = true; |
|
|
|
|
|
errorMessage.value = ''; |
|
|
|
|
|
|
|
|
|
|
|
// 检查 IM 登录状态 |
|
|
|
|
|
if (!IMManager.instance.isLoggedIn) { |
|
|
|
|
|
if (Get.isLogEnable) { |
|
|
|
|
|
Get.log('⚠️ [FriendController] IM 未登录,无法加载好友列表'); |
|
|
|
|
|
} |
|
|
|
|
|
errorMessage.value = 'IM 未登录,无法加载好友列表'; |
|
|
|
|
|
isLoading.value = false; |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 从 IMManager 获取所有联系人 |
|
|
|
|
|
final contacts = await IMManager.instance.getAllContacts(); |
|
|
|
|
|
|
|
|
|
|
|
// 更新好友列表 |
|
|
|
|
|
friendList.value = contacts; |
|
|
|
|
|
|
|
|
|
|
|
if (Get.isLogEnable) { |
|
|
|
|
|
Get.log('✅ [FriendController] 加载好友列表成功,共 ${contacts.length} 个好友'); |
|
|
|
|
|
} |
|
|
|
|
|
} catch (e) { |
|
|
|
|
|
if (Get.isLogEnable) { |
|
|
|
|
|
Get.log('❌ [FriendController] 加载好友列表失败: $e'); |
|
|
|
|
|
} |
|
|
|
|
|
errorMessage.value = '加载好友列表失败,请稍后重试'; |
|
|
|
|
|
} finally { |
|
|
|
|
|
isLoading.value = false; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// 刷新好友列表 |
|
|
|
|
|
Future<void> refreshFriends() async { |
|
|
|
|
|
await loadFriends(); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|