fix(mobile-app): 修复切换账号时 token 过期导致自动退出登录

根因:switchToAccount() 流程中,定时器在 _clearCurrentAccountData()
之后才停止。clear 阶段会删除 token,但此时定时器仍在运行,
in-flight 的 API 请求收到 401 → 触发 _handleTokenExpired()
→ 调用 logoutCurrentAccount() 把正在恢复的新账号数据全部清掉
→ 用户被自动踢到登录页面。

修复:将 onBeforeRestore 回调(停止定时器)移到 _clearCurrentAccountData()
之前执行,确保所有 API 请求停止后再清除 token。

修改前: save → clear(删token) → 停定时器 → restore
修改后: save → 停定时器 → clear(删token) → restore

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
hailin 2026-02-26 12:21:57 -08:00
parent cfc03fe523
commit a5cc3fdc5b
1 changed files with 11 additions and 9 deletions

View File

@ -215,16 +215,16 @@ class MultiAccountService {
/// ///
/// true /// true
/// ///
/// [onBeforeRestore] /// [onBeforeRestore]
/// Provider authProviderwalletStatusProvider /// Provider token in-flight API
/// /// 401 tokenExpired 退
/// ///
/// ///
/// 1. /// 1.
/// 2. /// 2.
/// 3. /// 3.
/// 3.5 onBeforeRestore clear 401 退
/// 4. /// 4.
/// 4.5 onBeforeRestore
/// 5. /// 5.
/// 6. /// 6.
/// 7. /// 7.
@ -254,15 +254,17 @@ class MultiAccountService {
debugPrint('$_tag switchToAccount() - 已保存当前账号数据: $currentId'); debugPrint('$_tag switchToAccount() - 已保存当前账号数据: $currentId');
} }
// 4. // 3.5 clear
await _clearCurrentAccountData(); // clear tokenin-flight API 401
// _handleTokenExpired logoutCurrentAccount()
// 4.5 Provider
if (onBeforeRestore != null) { if (onBeforeRestore != null) {
await onBeforeRestore(); await onBeforeRestore();
debugPrint('$_tag switchToAccount() - 已重置内存状态'); debugPrint('$_tag switchToAccount() - 已停止定时器和重置内存状态');
} }
// 4.
await _clearCurrentAccountData();
// 5. // 5.
await _restoreAccountData(userSerialNum); await _restoreAccountData(userSerialNum);