fix(client): 切换账号显示上一账号数据——登录/登出按会话纪元重建 ProviderScope

张三登录后看到汤宇飞的订单列表(含已删行):业务 provider 的 notifier 实例
与 _cache 兜底缓存跨账号存活,拉取失败时把上一账号数据端给新账号(10 个
列表 provider 同款模式)。根治:会话纪元 sessionEpoch(Riverpod 体系外
ValueNotifier)作 ProviderScope 的 key,login/logout 后自增重建整棵状态树。
logout 的撤销会话 POST 改为不阻塞,确保清盘先于 scope 重建,防止新 scope
restore 读到旧 token 把上一账号登回来。

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
wangjia
2026-07-15 00:39:18 +08:00
parent 858f24f74b
commit b8567b0f4e
3 changed files with 42 additions and 11 deletions
+11 -1
View File
@@ -6,6 +6,7 @@ import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_web_plugins/url_strategy.dart';
import 'core/auth/auth_state.dart';
import 'core/auth/session_epoch.dart';
import 'core/config/app_info.dart';
import 'core/errors/error_reporter.dart';
import 'core/platform/single_instance.dart' as single_instance;
@@ -40,7 +41,16 @@ void main() {
}
await AppInfo.load(); // 从配置文件加载「关于我们」品牌信息
runApp(const ProviderScope(child: JiuApp()));
// 会话纪元变化(登录/登出)→ 换 key 重建整个 ProviderScope
// 所有业务 provider 状态(分页/筛选/notifier 内缓存)随账号切换彻底清零,
// 杜绝跨账号内存残留。auth 会话由新 scope 的 restore() 从持久化恢复。
runApp(ValueListenableBuilder<int>(
valueListenable: sessionEpoch,
builder: (_, epoch, __) => ProviderScope(
key: ValueKey(epoch),
child: const JiuApp(),
),
));
},
(error, stack) {
debugPrint('═══ Zone Error ═══════════════════════════════');