|
|
|
@ -58,7 +58,16 @@ class CallSession { |
|
|
|
class CallController extends GetxController { |
|
|
|
static CallController? _instance; |
|
|
|
static CallController get instance { |
|
|
|
_instance ??= Get.put(CallController()); |
|
|
|
if (_instance != null) { |
|
|
|
return _instance!; |
|
|
|
} |
|
|
|
// 如果 GetX 中已注册,使用 Get.find 获取 |
|
|
|
if (Get.isRegistered<CallController>()) { |
|
|
|
_instance = Get.find<CallController>(); |
|
|
|
return _instance!; |
|
|
|
} |
|
|
|
// 否则创建新实例并注册 |
|
|
|
_instance = Get.put(CallController(), permanent: true); |
|
|
|
return _instance!; |
|
|
|
} |
|
|
|
|
|
|
|
@ -178,6 +187,21 @@ class CallController extends GetxController { |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
// 检查是否在直播间 |
|
|
|
try { |
|
|
|
if (Get.isRegistered<RoomController>()) { |
|
|
|
final roomController = Get.find<RoomController>(); |
|
|
|
if (roomController.isLive.value) { |
|
|
|
SmartDialog.showToast('请先退出直播间'); |
|
|
|
print('⚠️ [CallController] 当前在直播间,无法发起通话'); |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (e) { |
|
|
|
// 忽略错误,继续处理发起通话逻辑 |
|
|
|
print('⚠️ [CallController] 检查直播间状态失败: $e'); |
|
|
|
} |
|
|
|
|
|
|
|
// 清空之前的远端用户UID和通话信息 |
|
|
|
remoteUid.value = null; |
|
|
|
|
|
|
|
@ -202,7 +226,7 @@ class CallController extends GetxController { |
|
|
|
final session = CallSession( |
|
|
|
targetUserId: targetUserId, |
|
|
|
callType: callType, |
|
|
|
status: CallStatus.calling, |
|
|
|
status: CallStatus.waitCalling, |
|
|
|
isInitiator: true, |
|
|
|
startTime: DateTime.now(), |
|
|
|
); |
|
|
|
@ -255,6 +279,21 @@ class CallController extends GetxController { |
|
|
|
required EMMessage message, |
|
|
|
ChatController? chatController, |
|
|
|
}) async { |
|
|
|
// 检查是否在直播间 |
|
|
|
try { |
|
|
|
if (Get.isRegistered<RoomController>()) { |
|
|
|
final roomController = Get.find<RoomController>(); |
|
|
|
if (roomController.isLive.value) { |
|
|
|
SmartDialog.showToast('请先退出直播间再接听'); |
|
|
|
print('⚠️ [CallController] 当前在直播间,无法接听通话'); |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (e) { |
|
|
|
// 忽略错误,继续处理接听逻辑 |
|
|
|
print('⚠️ [CallController] 检查直播间状态失败: $e'); |
|
|
|
} |
|
|
|
|
|
|
|
final callInfo = _parseCallInfo(message); |
|
|
|
if (callInfo == null) { |
|
|
|
return false; |
|
|
|
@ -620,31 +659,43 @@ class CallController extends GetxController { |
|
|
|
callDurationSeconds.value == 0 && |
|
|
|
_callChannelId != null && |
|
|
|
_callChannelId!.isNotEmpty) { |
|
|
|
final response = await _networkService.rtcApi.cancelOneOnOneRtcChannel({ |
|
|
|
'channelId': _callChannelId!, |
|
|
|
}); |
|
|
|
if (!response.data.isSuccess) { |
|
|
|
SmartDialog.showToast(response.data.message); |
|
|
|
return; |
|
|
|
try { |
|
|
|
final response = await _networkService.rtcApi.cancelOneOnOneRtcChannel({ |
|
|
|
'channelId': _callChannelId!, |
|
|
|
}); |
|
|
|
if (response.data.isSuccess) { |
|
|
|
print('✅ [CallController] 已调用取消一对一RTC频道接口,channelId: $_callChannelId'); |
|
|
|
} else { |
|
|
|
print('⚠️ [CallController] 取消一对一RTC频道接口失败: ${response.data.message}'); |
|
|
|
} |
|
|
|
} catch (e) { |
|
|
|
print('⚠️ [CallController] 调用取消接口异常: $e'); |
|
|
|
} |
|
|
|
print('✅ [CallController] 已调用取消一对一RTC频道接口,channelId: $_callChannelId'); |
|
|
|
} else if (callSession != null && |
|
|
|
callDurationSeconds.value > 0 && |
|
|
|
_callChannelId != null && |
|
|
|
_callChannelId!.isNotEmpty) { |
|
|
|
// 如果通话已接通(无论是发起方还是接收方),调用终止接口 |
|
|
|
final response = await _networkService.rtcApi.terminateOneOnOneRtcChannel( |
|
|
|
{'channelId': _callChannelId!}, |
|
|
|
); |
|
|
|
if (!response.data.isSuccess) { |
|
|
|
SmartDialog.showToast(response.data.message); |
|
|
|
return; |
|
|
|
try { |
|
|
|
final response = await _networkService.rtcApi.terminateOneOnOneRtcChannel( |
|
|
|
{'channelId': _callChannelId!}, |
|
|
|
); |
|
|
|
if (response.data.isSuccess) { |
|
|
|
print('✅ [CallController] 已调用终止一对一RTC频道接口,channelId: $_callChannelId'); |
|
|
|
} else { |
|
|
|
print('⚠️ [CallController] 终止一对一RTC频道接口失败: ${response.data.message}'); |
|
|
|
} |
|
|
|
} catch (e) { |
|
|
|
print('⚠️ [CallController] 调用终止接口异常: $e'); |
|
|
|
} |
|
|
|
print('✅ [CallController] 已调用终止一对一RTC频道接口,channelId: $_callChannelId'); |
|
|
|
} |
|
|
|
|
|
|
|
// 离开RTC频道 |
|
|
|
await RTCManager.instance.leaveChannel(); |
|
|
|
try { |
|
|
|
await RTCManager.instance.leaveChannel(); |
|
|
|
} catch (e) { |
|
|
|
print('⚠️ [CallController] 离开RTC频道异常: $e'); |
|
|
|
} |
|
|
|
|
|
|
|
// 服务端会自动修改消息callStatus为'cancelled'或'terminated',客户端通过onMessageContentChanged收到通知 |
|
|
|
|
|
|
|
|