import 'package:flutter/foundation.dart'; import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'api_service.dart'; import 'home_api.dart'; import 'network_config.dart'; import 'user_api.dart'; import 'rtc_api.dart'; /// 网络请求服务管理器 class NetworkService { static final NetworkService _instance = NetworkService._internal(); // API服务实例 late final ApiService _apiService; // 主API服务 late final UserApi _userApi; late final HomeApi _homeApi; late final RtcApi _rtcApi; /// 获取单例实例 factory NetworkService() { return _instance; } NetworkService._internal() { // 初始化API服务 - 使用主API final dio = NetworkConfig.createDio(); _apiService = ApiService(dio); _userApi = UserApi(dio); _homeApi = HomeApi(dio); _rtcApi = RtcApi(dio); } /// 通用GET请求 Future get( String path, {Map? queryParameters, bool showLoading = false, bool showError = true, ApiType apiType = ApiType.main}) async { try { if (showLoading) { _showLoading(); } // 如果有queryParameters,将其附加到path String finalPath = path; if (queryParameters != null && queryParameters.isNotEmpty) { final queryString = queryParameters.entries .map((e) => '${e.key}=${e.value}') .join('&'); finalPath = '$path?$queryString'; } // 根据API类型选择对应的服务 final apiService = _getApiServiceByType(apiType); final response = await apiService.get(finalPath); return response.data; } catch (e) { if (showError) { _handleError(e); } return null; } finally { if (showLoading) { _hideLoading(); } } } /// 通用POST请求 Future post( String path, dynamic data, {bool showLoading = false, bool showError = true, ApiType apiType = ApiType.main}) async { try { if (showLoading) { _showLoading(); } // 根据API类型选择对应的服务 final apiService = _getApiServiceByType(apiType); final response = await apiService.post( path, data, ); return response.data; } catch (e) { if (showError) { _handleError(e); } return null; } finally { if (showLoading) { _hideLoading(); } } } /// 通用PUT请求(通过POST模拟) Future put( String path, dynamic data, {bool showLoading = false, bool showError = true, ApiType apiType = ApiType.main}) async { // 注意:这是一个简化实现,实际项目中应该使用真实的PUT方法 try { if (showLoading) { _showLoading(); } // 在实际项目中,应该使用支持PUT方法的API服务 // 这里使用POST作为替代 final apiService = _getApiServiceByType(apiType); final response = await apiService.post( path, data, ); return response.data; } catch (e) { if (showError) { _handleError(e); } return null; } finally { if (showLoading) { _hideLoading(); } } } /// 通用DELETE请求(通过GET模拟) Future delete( String path, {bool showLoading = false, bool showError = true, ApiType apiType = ApiType.main}) async { // 注意:这是一个简化实现,实际项目中应该使用真实的DELETE方法 return get(path, showLoading: showLoading, showError: showError, apiType: apiType); } /// 通用PATCH请求(通过POST模拟) Future patch( String path, dynamic data, {bool showLoading = false, bool showError = true, ApiType apiType = ApiType.main}) async { // 注意:这是一个简化实现,实际项目中应该使用真实的PATCH方法 return post(path, data, showLoading: showLoading, showError: showError, apiType: apiType); } /// 获取API服务实例 ApiService _getApiServiceByType(ApiType type) { // 无论类型是什么,都返回主API服务 return _apiService; } /// 用户相关API UserApi get userApi => _userApi; /// 首页相关API HomeApi get homeApi => _homeApi; /// RTC相关API RtcApi get rtcApi => _rtcApi; void _showLoading() { // 使用FlutterSmartDialog显示加载指示器 SmartDialog.showLoading(); } void _hideLoading() { // 隐藏加载指示器 SmartDialog.dismiss(); } void _handleError(dynamic error) { // 统一错误处理逻辑 debugPrint('网络错误: $error'); // 这里可以根据不同的错误类型进行处理 } }