Browse Source

修复bug

ios
Jolie 3 months ago
parent
commit
5b682d090d
1 changed files with 257 additions and 38 deletions
  1. 295
      lib/widget/live/live_room_guest_list_dialog.dart

295
lib/widget/live/live_room_guest_list_dialog.dart

@ -2,6 +2,7 @@ import 'dart:convert';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:dating_touchme_app/controller/discover/room_controller.dart';
import 'package:dating_touchme_app/controller/message/conversation_controller.dart';
import 'package:dating_touchme_app/im/im_manager.dart';
import 'package:dating_touchme_app/network/network_service.dart';
import 'package:dating_touchme_app/rtc/rtc_manager.dart';
@ -9,6 +10,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:im_flutter_sdk/im_flutter_sdk.dart';
///
class LiveRoomGuestListDialog extends StatefulWidget {
@ -41,6 +43,7 @@ class _LiveRoomGuestListDialogState extends State<LiveRoomGuestListDialog> {
try {
//
final conversations = await IMManager.instance.getConversations();
if (conversations.isEmpty) {
setState(() {
_guestList = [];
@ -53,15 +56,119 @@ class _LiveRoomGuestListDialogState extends State<LiveRoomGuestListDialog> {
final List<Map<String, dynamic>> guestList = [];
//
for (final conversation in conversations) {
for (int i = 0; i < conversations.length; i++) {
final conversation = conversations[i];
try {
final userId = conversation.id;
if (userId.isEmpty) continue;
if (userId.isEmpty) {
continue;
}
//
final contactsMap = await IMManager.instance.getContacts(userId);
final emUserInfo = contactsMap[userId];
Get.log('获取会话列表: ${emUserInfo?.gender}');
// ConversationController
ExtendedUserInfo? cachedUserInfo;
// ConversationController
if (Get.isRegistered<ConversationController>()) {
try {
final conversationController = Get.find<ConversationController>();
// 使 loadContact
// 1.
// 2.
// 3.
cachedUserInfo = await conversationController.loadContact(userId);
} catch (e) {
//
}
}
// ConversationController
final hasNickName = cachedUserInfo?.nickName != null && cachedUserInfo!.nickName!.isNotEmpty;
final hasAvatar = cachedUserInfo?.avatarUrl != null && cachedUserInfo!.avatarUrl!.isNotEmpty;
if (!hasNickName || !hasAvatar) {
try {
//
final conversation = await EMClient.getInstance.chatManager.getConversation(
userId,
type: EMConversationType.Chat,
createIfNeed: false,
);
if (conversation != null) {
final messages = await conversation.loadMessages(loadCount: 50);
for (var message in messages) {
Map<String, dynamic>? attributes;
try {
attributes = message.attributes;
} catch (e) {
continue;
}
if (attributes == null || attributes.isEmpty) {
continue;
}
if (message.direction == MessageDirection.RECEIVE) {
final fromUserId = message.from;
if (fromUserId != null && fromUserId == userId) {
final nickName = attributes['sender_nickName'] as String? ?? attributes['nickName'] as String?;
final avatarUrl = attributes['sender_avatarUrl'] as String? ?? attributes['avatarUrl'] as String?;
if ((nickName != null && nickName.isNotEmpty) || (avatarUrl != null && avatarUrl.isNotEmpty)) {
cachedUserInfo = ExtendedUserInfo(
userId: userId,
nickName: nickName ?? cachedUserInfo?.nickName,
avatarUrl: avatarUrl ?? cachedUserInfo?.avatarUrl,
);
// ConversationController
if (Get.isRegistered<ConversationController>()) {
try {
final conversationController = Get.find<ConversationController>();
conversationController.cacheUserInfo(userId, cachedUserInfo);
} catch (e) {
//
}
}
break;
}
}
} else if (message.direction == MessageDirection.SEND) {
final toUserId = message.to;
if (toUserId != null && toUserId == userId) {
final nickName = attributes['receiver_nickName'] as String?;
final avatarUrl = attributes['receiver_avatarUrl'] as String?;
if ((nickName != null && nickName.isNotEmpty) || (avatarUrl != null && avatarUrl.isNotEmpty)) {
cachedUserInfo = ExtendedUserInfo(
userId: userId,
nickName: nickName ?? cachedUserInfo?.nickName,
avatarUrl: avatarUrl ?? cachedUserInfo?.avatarUrl,
);
// ConversationController
if (Get.isRegistered<ConversationController>()) {
try {
final conversationController = Get.find<ConversationController>();
conversationController.cacheUserInfo(userId, cachedUserInfo);
} catch (e) {
//
}
}
break;
}
}
}
}
}
} catch (e) {
// 使
}
}
//
final response = await networkService.userApi.getBaseUserInfo(userId);
@ -73,14 +180,49 @@ class _LiveRoomGuestListDialogState extends State<LiveRoomGuestListDialog> {
int? age;
String? cityName;
int? vipLevel;
String? extAvatarUrl; //
String? extNickName; //
// gender
// gender "1" "2""0"
// genderCode0-, 1-
try {
final emGender = emUserInfo?.gender;
if (emGender != null) {
final emGenderStr = emGender.toString().trim();
// : "1"=, "2"=, "0"= -> : 0=, 1=
if (emGenderStr == '1') {
genderCode = 0; //
} else if (emGenderStr == '2') {
genderCode = 1; //
} else if (emGenderStr != '0' && emGenderStr.isNotEmpty) {
//
final emGenderInt = int.tryParse(emGenderStr);
if (emGenderInt != null) {
if (emGenderInt == 1) {
genderCode = 0; //
} else if (emGenderInt == 2) {
genderCode = 1; //
} else if (emGenderInt != 0) {
//
genderCode = emGenderInt >= 2 ? 1 : (emGenderInt == 1 ? 0 : null);
}
}
}
}
} catch (e) {
//
}
if (emUserInfo?.ext != null) {
//
if (genderCode == null && emUserInfo?.ext != null) {
try {
final extJson = json.decode(emUserInfo!.ext!);
if (extJson is Map<String, dynamic>) {
genderCode = extJson['genderCode'] is int
? extJson['genderCode'] as int
: int.tryParse(extJson['genderCode']?.toString() ?? '0');
: int.tryParse(extJson['genderCode']?.toString() ?? '');
age = extJson['age'] is int
? extJson['age'] as int
: int.tryParse(extJson['age']?.toString() ?? '');
@ -88,26 +230,88 @@ class _LiveRoomGuestListDialogState extends State<LiveRoomGuestListDialog> {
vipLevel = extJson['vipLevel'] is int
? extJson['vipLevel'] as int
: int.tryParse(extJson['vipLevel']?.toString() ?? '');
extAvatarUrl = extJson['avatarUrl']?.toString() ??
extJson['profilePhoto']?.toString();
extNickName = extJson['nickName']?.toString();
}
} catch (e) {
print('解析扩展信息失败: $e');
//
}
}
guestList.add({
// 使使使
// null
String avatarUrl = '';
final emAvatar = emUserInfo?.avatarUrl?.trim();
final cachedAvatar = cachedUserInfo?.avatarUrl?.trim();
final extAvatar = extAvatarUrl?.trim();
if (emAvatar != null && emAvatar.isNotEmpty) {
avatarUrl = emAvatar;
} else if (cachedAvatar != null && cachedAvatar.isNotEmpty) {
avatarUrl = cachedAvatar;
} else if (extAvatar != null && extAvatar.isNotEmpty) {
avatarUrl = extAvatar;
}
// URL
if (avatarUrl.isNotEmpty) {
avatarUrl = avatarUrl.replaceAll('`', '');
}
// 使使使使 API
// null
String name = '';
final emNickName = emUserInfo?.nickName?.trim();
final cachedNickName = cachedUserInfo?.nickName?.trim();
final extNick = extNickName?.trim();
final apiNickName = userBaseData.nickName.trim();
if (emNickName != null && emNickName.isNotEmpty) {
name = emNickName;
} else if (cachedNickName != null && cachedNickName.isNotEmpty) {
name = cachedNickName;
} else if (extNick != null && extNick.isNotEmpty) {
name = extNick;
} else if (apiNickName.isNotEmpty) {
name = apiNickName;
}
// 使 userId
if (name.isEmpty) {
name = userId;
}
// genderCode null gender
// gender=0
int? finalGenderCode = genderCode;
if (finalGenderCode == null) {
//
// 0
if (avatarUrl.isNotEmpty || name.isNotEmpty) {
finalGenderCode = 0; //
} else {
finalGenderCode = -1; //
}
}
final guestData = {
'userId': userId,
'avatar': emUserInfo?.avatarUrl ?? '',
'name': emUserInfo?.nickName ?? userBaseData.nickName,
'avatar': avatarUrl,
'name': name,
'age': age,
'location': cityName ?? '',
'vipLevel': vipLevel,
'genderCode': genderCode ?? 0, // 0-, 1-
'genderCode': finalGenderCode, // 0-, 1-, -1-
'hasMaleGuest': false, // TODO:
'hasFemaleGuest': false, // TODO:
});
};
guestList.add(guestData);
}
} catch (e) {
print('获取用户信息失败: ${conversation.id}, $e');
print('❌ [LiveRoomGuestListDialog] 获取用户信息异常: userId=${conversation.id}, error=$e');
}
}
@ -116,7 +320,7 @@ class _LiveRoomGuestListDialogState extends State<LiveRoomGuestListDialog> {
_isLoading = false;
});
} catch (e) {
print('获取联系人列表失败: $e');
print('❌ [LiveRoomGuestListDialog] 获取联系人列表异常: $e');
setState(() {
_guestList = [];
_isLoading = false;
@ -197,14 +401,16 @@ class _LiveRoomGuestListDialogState extends State<LiveRoomGuestListDialog> {
}
//
// genderCode: 0-, 1-
// genderCode: 0-, 1-, -1-
final filteredList = _guestList.where((guest) {
final genderCode = guest['genderCode'] as int? ?? 0;
final genderCode = guest['genderCode'] as int? ?? -1;
if (_selectedTab == 0) {
// genderCode == 1
// genderCode -1
return genderCode == 1;
} else {
// genderCode == 0
// genderCode -1
return genderCode == 0;
}
}).toList();
@ -228,6 +434,10 @@ class _LiveRoomGuestListDialogState extends State<LiveRoomGuestListDialog> {
}
Widget _buildGuestItem(Map<String, dynamic> guest) {
final userId = guest['userId'] as String? ?? '';
final avatar = guest['avatar'] as String? ?? '';
final name = guest['name'] as String? ?? '';
//
final hasGuest = _selectedTab == 0
? (guest['hasMaleGuest'] as bool? ?? false)
@ -241,29 +451,38 @@ class _LiveRoomGuestListDialogState extends State<LiveRoomGuestListDialog> {
//
ClipRRect(
borderRadius: BorderRadius.circular(20.w),
child: CachedNetworkImage(
imageUrl: guest['avatar'] as String? ?? '',
width: 40.w,
height: 40.w,
fit: BoxFit.cover,
placeholder: (context, url) => Container(
width: 40.w,
height: 40.w,
color: Colors.grey[300],
child: Center(
child: CircularProgressIndicator(
strokeWidth: 2,
color: Colors.grey[600],
child: avatar.isNotEmpty
? CachedNetworkImage(
imageUrl: avatar,
width: 40.w,
height: 40.w,
fit: BoxFit.cover,
placeholder: (context, url) => Container(
width: 40.w,
height: 40.w,
color: Colors.grey[300],
child: Center(
child: CircularProgressIndicator(
strokeWidth: 2,
color: Colors.grey[600],
),
),
),
errorWidget: (context, url, error) {
return Container(
width: 40.w,
height: 40.w,
color: Colors.grey[300],
child: Icon(Icons.person, size: 24.w),
);
},
)
: Container(
width: 40.w,
height: 40.w,
color: Colors.grey[300],
child: Icon(Icons.person, size: 24.w),
),
),
),
errorWidget: (context, url, error) => Container(
width: 40.w,
height: 40.w,
color: Colors.grey[300],
child: Icon(Icons.person, size: 24.w),
),
),
),
SizedBox(width: 12.w),
//
@ -274,7 +493,7 @@ class _LiveRoomGuestListDialogState extends State<LiveRoomGuestListDialog> {
Row(
children: [
Text(
guest['name'] as String? ?? '',
name.isNotEmpty ? name : userId,
style: TextStyle(
fontSize: 15.w,
fontWeight: FontWeight.w500,

Loading…
Cancel
Save