import '../../../core/models/api_response.dart'; import '../../../core/services/enhanced_api_service.dart'; import '../models/learning_progress_model.dart'; /// 学习进度服务 class LearningProgressService { static final LearningProgressService _instance = LearningProgressService._internal(); factory LearningProgressService() => _instance; LearningProgressService._internal(); final EnhancedApiService _enhancedApiService = EnhancedApiService(); // 缓存时长配置 static const Duration _shortCacheDuration = Duration(minutes: 5); /// 获取用户学习进度列表 Future>> getUserLearningProgress({ int page = 1, int limit = 20, }) async { try { final response = await _enhancedApiService.get>( '/user/learning-progress', queryParameters: { 'page': page, 'limit': limit, }, cacheDuration: _shortCacheDuration, fromJson: (data) { final progressList = data['progress'] as List?; print('=== 学习进度数据解析 ==='); print('progress字段: $progressList'); print('数据条数: ${progressList?.length ?? 0}'); if (progressList == null || progressList.isEmpty) { // 返回空列表,不使用默认数据 print('后端返回空数据,显示空状态'); return []; } final result = progressList.map((json) => LearningProgress.fromJson(json)).toList(); print('解析后的数据条数: ${result.length}'); return result; }, ); if (response.success && response.data != null) { return ApiResponse.success(message: '获取成功', data: response.data!); } else { // 如果API失败,返回空列表 return ApiResponse.success( message: '获取失败', data: [], ); } } catch (e) { // 出错时返回空列表 print('获取学习进度异常: $e'); return ApiResponse.success( message: '获取失败', data: [], ); } } /// 获取用户学习统计 Future>> getUserStats({ String timeRange = 'all', }) async { try { final response = await _enhancedApiService.get>( '/user/stats', queryParameters: { 'time_range': timeRange, }, cacheDuration: _shortCacheDuration, fromJson: (data) => data, ); if (response.success && response.data != null) { return ApiResponse.success(message: '获取成功', data: response.data!); } else { return ApiResponse.error(message: response.message); } } catch (e) { return ApiResponse.error(message: '获取统计数据失败: $e'); } } }