api实现
This commit is contained in:
56
lib/src/controllers/user/online_controller.dart
Normal file
56
lib/src/controllers/user/online_controller.dart
Normal file
@@ -0,0 +1,56 @@
|
||||
// 2026-04-09 | OnlineController | 在线统计控制器 | 管理心跳和在线数据
|
||||
import 'dart:async';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:mom_kitchen/src/controllers/base/base_controller.dart';
|
||||
import 'package:mom_kitchen/src/repositories/online_repository.dart' as repo;
|
||||
|
||||
class OnlineController extends BaseController {
|
||||
final repo.OnlineRepository _onlineRepository = repo.OnlineRepository();
|
||||
|
||||
final RxMap<String, dynamic> stats = <String, dynamic>{}.obs;
|
||||
final RxMap<String, dynamic> timeline = <String, dynamic>{}.obs;
|
||||
Timer? _heartbeatTimer;
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
super.onInit();
|
||||
loadStats();
|
||||
startHeartbeat();
|
||||
}
|
||||
|
||||
@override
|
||||
void onClose() {
|
||||
_heartbeatTimer?.cancel();
|
||||
super.onClose();
|
||||
}
|
||||
|
||||
Future<void> loadStats() async {
|
||||
try {
|
||||
stats.value = await _onlineRepository.fetchStats();
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
Future<void> loadTimeline() async {
|
||||
try {
|
||||
timeline.value = await _onlineRepository.fetchTimeline();
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
void startHeartbeat() {
|
||||
_heartbeatTimer?.cancel();
|
||||
_heartbeatTimer = Timer.periodic(
|
||||
const Duration(seconds: 30),
|
||||
(_) => _sendHeartbeat(),
|
||||
);
|
||||
_sendHeartbeat();
|
||||
}
|
||||
|
||||
Future<void> _sendHeartbeat() async {
|
||||
try {
|
||||
await _onlineRepository.sendHeartbeat(platform: 'flutter');
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
int get onlineCount => stats['online_count'] as int? ?? 0;
|
||||
int get todayCount => stats['today_count'] as int? ?? 0;
|
||||
}
|
||||
Reference in New Issue
Block a user