From ba4519125e52f741db0ef3f89c97a900953db36e Mon Sep 17 00:00:00 2001 From: Songzex <2402265378@qq.com> Date: Wed, 22 Apr 2026 15:53:18 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E4=BA=86=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E4=B8=8D=E5=90=8C=E8=B4=A6=E6=88=B7=E9=80=80=E5=87=BA=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E5=90=8E=E4=B8=80=E4=BA=9B=E6=95=B0=E6=8D=AE=E6=BB=9E?= =?UTF-8?q?=E7=95=99=E5=88=B0=E6=9C=80=E6=96=B0=E8=B4=A6=E6=88=B7=E4=B8=8B?= =?UTF-8?q?=E7=9A=84=E7=BC=93=E5=AD=98=E9=97=AE=E9=A2=98=EF=BC=8C=E4=BF=9D?= =?UTF-8?q?=E8=AF=81=E4=BA=86=E4=BD=BF=E7=94=A8=E7=9A=84=E6=AD=A3=E7=A1=AE?= =?UTF-8?q?=E6=80=A7=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../auth/presentation/bloc/auth_cubit.dart | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/lib/features/auth/presentation/bloc/auth_cubit.dart b/lib/features/auth/presentation/bloc/auth_cubit.dart index 020ea1c1..ae2f3fff 100644 --- a/lib/features/auth/presentation/bloc/auth_cubit.dart +++ b/lib/features/auth/presentation/bloc/auth_cubit.dart @@ -16,6 +16,9 @@ import '../../../../core/storage/user_storage.dart'; import '../../../devices/domain/usecases/get_user_device_usecase.dart'; import '../../../devices/domain/usecases/switch_device_usecase.dart'; import '../../../devices/presentation/bloc/devices_cubit.dart'; +import '../../../devices/presentation/bloc/device_status_bloc.dart'; +import '../../../devices/presentation/bloc/device_status_event.dart'; +import '../../../devices/presentation/bloc/devices_state.dart'; import '../../data/datasources/auth_tcp_datasource.dart'; import '../../data/datasources/impl/auth_tcp_datasource_impl.dart'; import 'auth_state.dart'; @@ -76,12 +79,40 @@ class AuthCubit extends Cubit { /// 退出登录 (主动或被动) Future logout() async { //print("退出登录"); + + // 🔥 关键修复:在断开TCP前,先清空所有业务状态,避免重连触发旧数据 + _clearAllBusinessState(); + await storage.deleteUser(); tcp.disconnect(); appCubit.clearAuth(); emit(AuthUnauthenticated()); } + /// 🔥 新增:清空所有业务状态,防止数据泄露到新账户 + void _clearAllBusinessState() { + try { + final devicesCubit = GetIt.I(); + final deviceStatusBloc = GetIt.I(); + + // 1. 清空设备列表和选中设备 + devicesCubit.emit(const DevicesState()); + debugPrint('✅ [AUTH] 已清空 DevicesCubit 状态'); + _logger.logWithLevel('✅ [AUTH] 已清空 DevicesCubit 状态'); + + // 2. 清空设备实时状态 + deviceStatusBloc.add(DeviceStatusReset()); + debugPrint('✅ [AUTH] 已重置 DeviceStatusBloc 状态'); + _logger.logWithLevel('✅ [AUTH] 已重置 DeviceStatusBloc 状态'); + + debugPrint('✅ [AUTH] 所有业务状态已清空'); + _logger.logWithLevel('✅ [AUTH] 所有业务状态已清空'); + } catch (e) { + debugPrint('❌ [AUTH] 清空业务状态失败: $e'); + _logger.logWithLevel('❌ [AUTH] 清空业务状态失败: $e', level: 'ERROR'); + } + } + /// TCP 指令监听 // void _listenToAuthResponse() { // print('>>> [AUTH] begin指令监听: ');