From a0e66a26754b92696c6ead0ae5817f6a4fff44f0 Mon Sep 17 00:00:00 2001 From: Jolie <412895109@qq.com> Date: Sat, 29 Nov 2025 17:09:20 +0800 Subject: [PATCH] =?UTF-8?q?feat(im):=20=E4=BC=98=E5=8C=96=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E5=A4=84=E7=90=86=E4=B8=8E=E7=95=8C=E9=9D=A2=E5=B1=95?= =?UTF-8?q?=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修改 ExtendedUserInfo 构造函数,移除 userId 默认空字符串赋值 - 新增对自定义消息类型的解析,支持识别直播房间邀请消息 - 移除 sendCustomMessage 方法参数末尾多余逗号 - 在获取会话列表后增加日志输出,便于调试用户信息获取 - 简化 joinChannel 方法异常处理逻辑,提升代码可读性 - 更新 RoomItem 组件点击事件为异步方法,确保频道加入完成后再跳转页面 - 调整消息气泡布局和尺寸限制,优化显示效果 - 将底部信息栏改为绝对定位,并调整头像和昵称样式以适配新布局 --- lib/controller/discover/room_controller.dart | 26 ++-- .../message/conversation_controller.dart | 9 +- lib/im/im_manager.dart | 2 +- .../live/live_room_guest_list_dialog.dart | 3 +- lib/widget/message/room_item.dart | 138 +++++++++--------- 5 files changed, 89 insertions(+), 89 deletions(-) diff --git a/lib/controller/discover/room_controller.dart b/lib/controller/discover/room_controller.dart index 8916bf3..d8b3fb9 100644 --- a/lib/controller/discover/room_controller.dart +++ b/lib/controller/discover/room_controller.dart @@ -149,21 +149,17 @@ class RoomController extends GetxController with WidgetsBindingObserver { } Future joinChannel(String channelName) async { - try { - final response = await _networkService.rtcApi.getSwRtcToken(channelName); - final base = response.data; - if (base.isSuccess && base.data != null) { - rtcChannel.value = base.data; - currentRole = CurrentRole.normalUser; - await _joinRtcChannel( - base.data!.token, - channelName, - base.data!.uid, - ClientRoleType.clientRoleAudience, - ); - } - } catch (e) { - SmartDialog.showToast('加入频道异常:$e'); + final response = await _networkService.rtcApi.getSwRtcToken(channelName); + final base = response.data; + if (base.isSuccess && base.data != null) { + rtcChannel.value = base.data; + currentRole = CurrentRole.normalUser; + await _joinRtcChannel( + base.data!.token, + channelName, + base.data!.uid, + ClientRoleType.clientRoleAudience, + ); } } diff --git a/lib/controller/message/conversation_controller.dart b/lib/controller/message/conversation_controller.dart index bd54bbc..48d6a51 100644 --- a/lib/controller/message/conversation_controller.dart +++ b/lib/controller/message/conversation_controller.dart @@ -18,7 +18,7 @@ class ExtendedUserInfo { // 从 EMUserInfo 创建 factory ExtendedUserInfo.fromEMUserInfo(EMUserInfo emUserInfo) { return ExtendedUserInfo( - userId: emUserInfo.userId ?? '', + userId: emUserInfo.userId, nickName: emUserInfo.nickName, avatarUrl: emUserInfo.avatarUrl, ); @@ -262,6 +262,13 @@ class ConversationController extends GetxController { return '[文件]'; }else if(message?.body.type == MessageType.LOCATION){ return '[位置]'; + }else if(message?.body.type == MessageType.CUSTOM){ + final body = message?.body as EMCustomMessageBody; + // 检查是否是分享房间类型 + if(body.event == 'live_room_invite'){ + return '[分享房间]'; + } + return '[自定义消息]'; } return '暂无消息'; } diff --git a/lib/im/im_manager.dart b/lib/im/im_manager.dart index cab66ad..a448fea 100644 --- a/lib/im/im_manager.dart +++ b/lib/im/im_manager.dart @@ -430,7 +430,7 @@ class IMManager { } /// 发送自定义消息 - Future sendCustomMessage(String targetId, String event, Map? data,) async { + Future sendCustomMessage(String targetId, String event, Map? data) async { final customMsg = EMMessage.createCustomSendMessage( targetId: targetId, // `event` 为需要传递的自定义消息事件,比如礼物消息,可以设置: diff --git a/lib/widget/live/live_room_guest_list_dialog.dart b/lib/widget/live/live_room_guest_list_dialog.dart index ab7b804..88b0c94 100644 --- a/lib/widget/live/live_room_guest_list_dialog.dart +++ b/lib/widget/live/live_room_guest_list_dialog.dart @@ -41,7 +41,6 @@ class _LiveRoomGuestListDialogState extends State { try { // 获取会话列表 final conversations = await IMManager.instance.getConversations(); - if (conversations.isEmpty) { setState(() { _guestList = []; @@ -62,7 +61,7 @@ class _LiveRoomGuestListDialogState extends State { // 获取环信用户信息 final contactsMap = await IMManager.instance.getContacts(userId); final emUserInfo = contactsMap[userId]; - + Get.log('获取会话列表: ${emUserInfo?.gender}'); // 获取用户详细信息(包含性别) final response = await networkService.userApi.getBaseUserInfo(userId); diff --git a/lib/widget/message/room_item.dart b/lib/widget/message/room_item.dart index 3c52ba4..8029576 100644 --- a/lib/widget/message/room_item.dart +++ b/lib/widget/message/room_item.dart @@ -59,7 +59,7 @@ class RoomItem extends StatelessWidget { } /// 处理点击事件 - void _handleTap() { + void _handleTap() async{ final channelId = _getChannelId(); if (channelId.isNotEmpty) { // 获取 RoomController 并加入频道 @@ -68,11 +68,7 @@ class RoomItem extends StatelessWidget { : Get.put(RoomController()); // 加入频道并跳转 - roomController.joinChannel(channelId).then((_) { - Get.to(() => const LiveRoomPage(id: 0)); - }).catchError((e) { - print('❌ 加入直播间失败: $e'); - }); + await roomController.joinChannel(channelId); } } @@ -95,7 +91,7 @@ class RoomItem extends StatelessWidget { child: Row( mainAxisAlignment: isSentByMe ? MainAxisAlignment.end : MainAxisAlignment.start, - crossAxisAlignment: CrossAxisAlignment.center, + crossAxisAlignment: CrossAxisAlignment.start, children: [ if (!isSentByMe) _buildAvatar(), if (!isSentByMe) SizedBox(width: 8.w), @@ -113,8 +109,7 @@ class RoomItem extends StatelessWidget { GestureDetector( onTap: _handleTap, child: Container( - constraints: BoxConstraints(maxWidth: 250.w), - margin: EdgeInsets.only(top: 10.h), + constraints: BoxConstraints(maxWidth: 150.w, maxHeight: 150.w), decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(12.w), @@ -135,8 +130,8 @@ class RoomItem extends StatelessWidget { Stack( children: [ Container( - width: 250.w, - height: 250.w, + width: 150.w, + height: 150.w, color: Colors.grey[200], child: anchorAvatar.isNotEmpty ? CachedNetworkImage( @@ -204,70 +199,73 @@ class RoomItem extends StatelessWidget { ), ), ), - ], - ), - // 底部信息 - Container( - padding: EdgeInsets.all(12.w), - child: Row( - children: [ - // 头像 - ClipRRect( - borderRadius: BorderRadius.circular(12.w), - child: Container( - width: 24.w, - height: 24.w, - color: Colors.grey[300], - child: anchorAvatar.isNotEmpty - ? CachedNetworkImage( - imageUrl: anchorAvatar, - width: 24.w, - height: 24.w, - fit: BoxFit.cover, - placeholder: (context, url) => - Container( - color: Colors.grey[300], - child: Center( - child: SizedBox( - width: 12.w, - height: 12.w, - child: CircularProgressIndicator( - strokeWidth: 2, - color: Colors.grey[600], + Positioned( + left: 8.w, + bottom: 8.w, + child: Row( + children: [ + // 头像 + ClipRRect( + borderRadius: BorderRadius.circular(12.w), + child: Container( + width: 24.w, + height: 24.w, + color: Colors.grey[300], + child: anchorAvatar.isNotEmpty + ? CachedNetworkImage( + imageUrl: anchorAvatar, + width: 24.w, + height: 24.w, + fit: BoxFit.cover, + placeholder: (context, url) => + Container( + color: Colors.grey[300], + child: Center( + child: SizedBox( + width: 12.w, + height: 12.w, + child: CircularProgressIndicator( + strokeWidth: 2, + color: Colors.grey[600], + ), ), ), ), - ), - errorWidget: (context, url, error) => - Icon( - Icons.person, - size: 16.w, - color: Colors.grey[600], - ), - ) - : Icon( - Icons.person, - size: 16.w, - color: Colors.grey[600], - ), - ), - ), - SizedBox(width: 8.w), - // 标题 - Expanded( - child: Text( - anchorName, - style: TextStyle( - fontSize: 13.sp, - color: Colors.black87, - fontWeight: FontWeight.w500, + errorWidget: (context, url, error) => + Icon( + Icons.person, + size: 16.w, + color: Colors.grey[600], + ), + ) + : Icon( + Icons.person, + size: 16.w, + color: Colors.grey[600], + ), + ), ), - maxLines: 1, - overflow: TextOverflow.ellipsis, - ), + SizedBox(width: 5.w), + // 标题 + Container( + constraints: BoxConstraints( + maxWidth: 100.w, + ), + child: Text( + anchorName, + style: TextStyle( + fontSize: 11.sp, + color: Colors.white, + fontWeight: FontWeight.w500, + ), + maxLines: 1, + overflow: TextOverflow.ellipsis, + ), + ), + ], ), - ], - ), + ) + ], ), ], ),