feat(mobile-app): 在主页添加随机3-6秒轮询检查维护状态
- 用户登录后在 HomeShellPage 每 3-6 秒(随机)检查一次维护状态 - 随机间隔可避免所有用户同时请求,减少服务器压力 - 后端发起维护后,用户最多 6 秒内会看到维护弹窗 - App 进入后台时暂停检查,恢复前台时立即检查并重启定时器 - 启动时、从后台恢复时也会立即检查一次 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
0781c53101
commit
7e8113805d
|
|
@ -7,6 +7,7 @@ import '../../../../core/theme/app_colors.dart';
|
||||||
import '../../../../core/di/injection_container.dart';
|
import '../../../../core/di/injection_container.dart';
|
||||||
import '../../../../core/services/contract_check_service.dart';
|
import '../../../../core/services/contract_check_service.dart';
|
||||||
import '../../../../core/updater/update_service.dart';
|
import '../../../../core/updater/update_service.dart';
|
||||||
|
import '../../../../core/providers/maintenance_provider.dart';
|
||||||
import '../../../../routes/route_paths.dart';
|
import '../../../../routes/route_paths.dart';
|
||||||
import '../../../../routes/app_router.dart';
|
import '../../../../routes/app_router.dart';
|
||||||
import '../../../../bootstrap.dart';
|
import '../../../../bootstrap.dart';
|
||||||
|
|
@ -33,6 +34,9 @@ class _HomeShellPageState extends ConsumerState<HomeShellPage>
|
||||||
/// 后台合同检查定时器
|
/// 后台合同检查定时器
|
||||||
Timer? _contractCheckTimer;
|
Timer? _contractCheckTimer;
|
||||||
|
|
||||||
|
/// 维护状态检查定时器
|
||||||
|
Timer? _maintenanceCheckTimer;
|
||||||
|
|
||||||
/// 是否正在显示弹窗(防止重复弹窗)
|
/// 是否正在显示弹窗(防止重复弹窗)
|
||||||
bool _isShowingDialog = false;
|
bool _isShowingDialog = false;
|
||||||
|
|
||||||
|
|
@ -40,12 +44,15 @@ class _HomeShellPageState extends ConsumerState<HomeShellPage>
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
WidgetsBinding.instance.addObserver(this);
|
WidgetsBinding.instance.addObserver(this);
|
||||||
// 首次进入时检查更新和未签署合同
|
// 首次进入时检查维护状态、更新和未签署合同
|
||||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
|
_checkMaintenanceStatus();
|
||||||
_checkForUpdateIfNeeded();
|
_checkForUpdateIfNeeded();
|
||||||
_checkContractsAndKyc();
|
_checkContractsAndKyc();
|
||||||
// 启动后台定时检查
|
// 启动后台定时检查
|
||||||
_startBackgroundContractCheck();
|
_startBackgroundContractCheck();
|
||||||
|
// 启动维护状态定时检查
|
||||||
|
_startMaintenanceCheck();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -53,19 +60,55 @@ class _HomeShellPageState extends ConsumerState<HomeShellPage>
|
||||||
void dispose() {
|
void dispose() {
|
||||||
WidgetsBinding.instance.removeObserver(this);
|
WidgetsBinding.instance.removeObserver(this);
|
||||||
_contractCheckTimer?.cancel();
|
_contractCheckTimer?.cancel();
|
||||||
|
_maintenanceCheckTimer?.cancel();
|
||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void didChangeAppLifecycleState(AppLifecycleState state) {
|
void didChangeAppLifecycleState(AppLifecycleState state) {
|
||||||
// 从后台恢复时检查更新
|
// 从后台恢复时检查维护状态和更新
|
||||||
if (state == AppLifecycleState.resumed) {
|
if (state == AppLifecycleState.resumed) {
|
||||||
|
_checkMaintenanceStatus();
|
||||||
_checkForUpdateIfNeeded();
|
_checkForUpdateIfNeeded();
|
||||||
// 从后台恢复时也触发一次检查
|
// 从后台恢复时也触发一次检查
|
||||||
_scheduleNextContractCheck();
|
_scheduleNextContractCheck();
|
||||||
|
// 恢复维护检查定时器
|
||||||
|
_startMaintenanceCheck();
|
||||||
} else if (state == AppLifecycleState.paused) {
|
} else if (state == AppLifecycleState.paused) {
|
||||||
// 进入后台时取消定时器
|
// 进入后台时取消定时器
|
||||||
_contractCheckTimer?.cancel();
|
_contractCheckTimer?.cancel();
|
||||||
|
_maintenanceCheckTimer?.cancel();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 启动维护状态定时检查(随机 3-6 秒检查一次)
|
||||||
|
void _startMaintenanceCheck() {
|
||||||
|
_scheduleNextMaintenanceCheck();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 安排下一次维护检查
|
||||||
|
void _scheduleNextMaintenanceCheck() {
|
||||||
|
_maintenanceCheckTimer?.cancel();
|
||||||
|
// 随机 3-6 秒
|
||||||
|
final randomSeconds = 3 + Random().nextInt(4); // 3 + 0~3 = 3~6
|
||||||
|
_maintenanceCheckTimer = Timer(Duration(seconds: randomSeconds), () async {
|
||||||
|
await _checkMaintenanceStatus();
|
||||||
|
// 检查完成后安排下一次检查
|
||||||
|
if (mounted) {
|
||||||
|
_scheduleNextMaintenanceCheck();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 检查系统维护状态
|
||||||
|
Future<void> _checkMaintenanceStatus() async {
|
||||||
|
if (!mounted) return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
final maintenanceNotifier = ref.read(maintenanceProvider.notifier);
|
||||||
|
await maintenanceNotifier.showMaintenanceDialogIfNeeded(context);
|
||||||
|
} catch (e) {
|
||||||
|
debugPrint('[HomeShellPage] 检查维护状态失败: $e');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue