diff --git a/lib/controller/discover/room_controller.dart b/lib/controller/discover/room_controller.dart index 004f691..88e26f9 100644 --- a/lib/controller/discover/room_controller.dart +++ b/lib/controller/discover/room_controller.dart @@ -264,7 +264,8 @@ class RoomController extends GetxController with WidgetsBindingObserver { } } - Future _fetchRtcChannelDetail(String channelName) async { + /// 获取 RTC 频道详情(公有方法,供外部调用) + Future fetchRtcChannelDetail(String channelName) async { try { final response = await _networkService.rtcApi.getRtcChannelDetail( channelName, @@ -278,6 +279,10 @@ class RoomController extends GetxController with WidgetsBindingObserver { } } + Future _fetchRtcChannelDetail(String channelName) async { + await fetchRtcChannelDetail(channelName); + } + /// 发送公屏消息 Future sendChatMessage(String content) async { final channelName = RTCManager.instance.currentChannelId; @@ -453,88 +458,6 @@ class RoomController extends GetxController with WidgetsBindingObserver { } catch (e) { print('❌ 处理礼物消息失败: $e'); } - } else if (message['type'] == 'join_chat') { - final response = await _networkService.rtcApi - .getDatingRtcChannelUserDetail( - rtcChannel.value!.channelId, - message['uid'], - ); - if (!response.data.isSuccess) { - return; - } - final currentDetail = rtcChannelDetail.value; - if (currentDetail == null) { - return; - } - final userData = response.data.data; - if (userData == null) { - return; - } - - if (message['role'] == 'male_audience') { - final maleInfo = RtcSeatUserInfo( - miId: userData.miId, - userId: userData.userId, - nickName: userData.nickName, - profilePhoto: userData.profilePhoto, - genderCode: userData.genderCode, - seatNumber: userData.seatNumber, - isFriend: userData.isFriend, - isMicrophoneOn: userData.isMicrophoneOn, - isVideoOn: userData.isVideoOn, - uid: message['uid'] is int - ? message['uid'] as int - : int.tryParse(message['uid']?.toString() ?? ''), - ); - final newDetail = RtcChannelDetail( - channelId: currentDetail.channelId, - anchorInfo: currentDetail.anchorInfo, - maleInfo: maleInfo, - femaleInfo: currentDetail.femaleInfo, - ); - rtcChannelDetail.value = newDetail; - } else if (message['role'] == 'female_audience') { - final femaleInfo = RtcSeatUserInfo( - miId: userData.miId, - userId: userData.userId, - nickName: userData.nickName, - profilePhoto: userData.profilePhoto, - genderCode: userData.genderCode, - seatNumber: userData.seatNumber, - isFriend: userData.isFriend, - isMicrophoneOn: userData.isMicrophoneOn, - isVideoOn: userData.isVideoOn, - uid: message['uid'] is int - ? message['uid'] as int - : int.tryParse(message['uid']?.toString() ?? ''), - ); - final newDetail = RtcChannelDetail( - channelId: currentDetail.channelId, - anchorInfo: currentDetail.anchorInfo, - maleInfo: currentDetail.maleInfo, - femaleInfo: femaleInfo, - ); - rtcChannelDetail.value = newDetail; - } - // } - } else if (message['type'] == 'leave_chat') { - if (message['role'] == 'male_audience') { - final newDetail = RtcChannelDetail( - channelId: rtcChannelDetail.value!.channelId, - anchorInfo: rtcChannelDetail.value!.anchorInfo, - maleInfo: null, - femaleInfo: rtcChannelDetail.value!.femaleInfo, - ); - rtcChannelDetail.value = newDetail; - } else if (message['role'] == 'female_audience') { - final newDetail = RtcChannelDetail( - channelId: rtcChannelDetail.value!.channelId, - anchorInfo: rtcChannelDetail.value!.anchorInfo, - maleInfo: rtcChannelDetail.value!.maleInfo, - femaleInfo: null, - ); - rtcChannelDetail.value = newDetail; - } } } diff --git a/lib/rtc/rtc_manager.dart b/lib/rtc/rtc_manager.dart index e7eca2e..fc954bc 100644 --- a/lib/rtc/rtc_manager.dart +++ b/lib/rtc/rtc_manager.dart @@ -6,7 +6,6 @@ import 'package:flutter/foundation.dart'; import 'package:get/get.dart'; import '../controller/discover/room_controller.dart'; -import '../network/network_service.dart'; import '../pages/discover/live_room_page.dart'; /// RTC 管理器,负责管理声网音视频通话功能 @@ -119,6 +118,16 @@ class RTCManager { channelJoinedNotifier.value = true; _currentChannelId = connection.channelId; print('加入频道成功,频道名:${connection.channelId},耗时:${elapsed}ms'); + + // 调用 RoomController 的 fetchRtcChannelDetail 方法 + final channelId = connection.channelId; + if (Get.isRegistered() && + channelId != null && + channelId.isNotEmpty) { + final roomController = Get.find(); + await roomController.fetchRtcChannelDetail(channelId); + } + if (connection.localUid == _currentUid) { await RTMManager.instance.subscribe(_currentChannelId ?? ''); await RTMManager.instance.publishChannelMessage( @@ -131,21 +140,42 @@ class RTCManager { onJoinChannelSuccess!(connection, elapsed); } }, - onUserJoined: (RtcConnection connection, int remoteUid, int elapsed) async{ - print('用户加入,UID:$remoteUid'); - _handleRemoteUserJoined(remoteUid); - if (onUserJoined != null) { - onUserJoined!(connection, remoteUid, elapsed); - } - }, + onUserJoined: + (RtcConnection connection, int remoteUid, int elapsed) async { + print('用户加入,UID:$remoteUid'); + _handleRemoteUserJoined(remoteUid); + + // 调用 RoomController 的 fetchRtcChannelDetail 方法 + final channelId = connection.channelId; + if (Get.isRegistered() && + channelId != null && + channelId.isNotEmpty) { + final roomController = Get.find(); + await roomController.fetchRtcChannelDetail(channelId); + } + + if (onUserJoined != null) { + onUserJoined!(connection, remoteUid, elapsed); + } + }, onUserOffline: ( RtcConnection connection, int remoteUid, UserOfflineReasonType reason, - ) { + ) async { print('用户离开,UID:$remoteUid,原因:$reason'); _handleRemoteUserOffline(remoteUid); + + // 调用 RoomController 的 fetchRtcChannelDetail 方法 + final channelId = connection.channelId; + if (Get.isRegistered() && + channelId != null && + channelId.isNotEmpty) { + final roomController = Get.find(); + await roomController.fetchRtcChannelDetail(channelId); + } + if (onUserOffline != null) { onUserOffline!(connection, remoteUid, reason); } @@ -521,8 +551,13 @@ class RTCManager { await _engine?.muteLocalVideoStream(true); await RTMManager.instance.publishChannelMessage( channelName: _currentChannelId ?? '', - message: json.encode({'type': 'leave_chat', 'uid': _currentUid, 'role': role == CurrentRole.maleAudience - ? 'male_audience' : 'female_audience',}), + message: json.encode({ + 'type': 'leave_chat', + 'uid': _currentUid, + 'role': role == CurrentRole.maleAudience + ? 'male_audience' + : 'female_audience', + }), ); } }