init
This commit is contained in:
109
client/lib/shared/services/notification_service.dart
Normal file
109
client/lib/shared/services/notification_service.dart
Normal file
@@ -0,0 +1,109 @@
|
||||
import '../../../core/models/api_response.dart';
|
||||
import '../../../core/services/enhanced_api_service.dart';
|
||||
import '../models/notification_model.dart';
|
||||
|
||||
/// 通知服务
|
||||
class NotificationService {
|
||||
static final NotificationService _instance = NotificationService._internal();
|
||||
factory NotificationService() => _instance;
|
||||
NotificationService._internal();
|
||||
|
||||
final EnhancedApiService _enhancedApiService = EnhancedApiService();
|
||||
|
||||
// 缓存时长配置
|
||||
static const Duration _shortCacheDuration = Duration(seconds: 30);
|
||||
|
||||
/// 获取通知列表
|
||||
Future<ApiResponse<Map<String, dynamic>>> getNotifications({
|
||||
int page = 1,
|
||||
int limit = 10,
|
||||
bool onlyUnread = false,
|
||||
}) async {
|
||||
try {
|
||||
final response = await _enhancedApiService.get<Map<String, dynamic>>(
|
||||
'/notifications',
|
||||
queryParameters: {
|
||||
'page': page,
|
||||
'limit': limit,
|
||||
'only_unread': onlyUnread,
|
||||
},
|
||||
cacheDuration: _shortCacheDuration,
|
||||
fromJson: (data) {
|
||||
final notifications = (data['notifications'] as List?)
|
||||
?.map((json) => NotificationModel.fromJson(json))
|
||||
.toList() ??
|
||||
[];
|
||||
return {
|
||||
'notifications': notifications,
|
||||
'total': data['total'] ?? 0,
|
||||
'page': data['page'] ?? 1,
|
||||
'limit': data['limit'] ?? 10,
|
||||
};
|
||||
},
|
||||
);
|
||||
|
||||
return response;
|
||||
} catch (e) {
|
||||
print('获取通知列表异常: $e');
|
||||
return ApiResponse.error(message: '获取通知列表失败: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// 获取未读通知数量
|
||||
Future<ApiResponse<int>> getUnreadCount() async {
|
||||
try {
|
||||
final response = await _enhancedApiService.get<int>(
|
||||
'/notifications/unread-count',
|
||||
cacheDuration: _shortCacheDuration,
|
||||
fromJson: (data) => data['count'] ?? 0,
|
||||
);
|
||||
|
||||
return response;
|
||||
} catch (e) {
|
||||
print('获取未读通知数量异常: $e');
|
||||
return ApiResponse.error(message: '获取未读通知数量失败: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// 标记通知为已读
|
||||
Future<ApiResponse<void>> markAsRead(int notificationId) async {
|
||||
try {
|
||||
final response = await _enhancedApiService.put<void>(
|
||||
'/notifications/$notificationId/read',
|
||||
);
|
||||
|
||||
return response;
|
||||
} catch (e) {
|
||||
print('标记通知已读异常: $e');
|
||||
return ApiResponse.error(message: '标记通知已读失败: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// 标记所有通知为已读
|
||||
Future<ApiResponse<void>> markAllAsRead() async {
|
||||
try {
|
||||
final response = await _enhancedApiService.put<void>(
|
||||
'/notifications/read-all',
|
||||
);
|
||||
|
||||
return response;
|
||||
} catch (e) {
|
||||
print('标记所有通知已读异常: $e');
|
||||
return ApiResponse.error(message: '标记所有通知已读失败: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// 删除通知
|
||||
Future<ApiResponse<void>> deleteNotification(int notificationId) async {
|
||||
try {
|
||||
final response = await _enhancedApiService.delete<void>(
|
||||
'/notifications/$notificationId',
|
||||
);
|
||||
|
||||
return response;
|
||||
} catch (e) {
|
||||
print('删除通知异常: $e');
|
||||
return ApiResponse.error(message: '删除通知失败: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user