diff --git a/lib/core/router/app_router.dart b/lib/core/router/app_router.dart index 8e108c8b..df18c213 100644 --- a/lib/core/router/app_router.dart +++ b/lib/core/router/app_router.dart @@ -45,8 +45,8 @@ GoRouter createRouter(AuthCubit authCubit) { // 注册登录注册 路由 GoRoute( - path: RoutePaths.machineDetails, - builder: (context, state) => const LoginPage(), // 替换为实际页面 + path: RoutePaths.login, + builder: (context, state) => const LoginPage(), ), // 1. 外部路由(无底部导航栏) diff --git a/lib/features/auth/presentation/bloc/auth_cubit.dart b/lib/features/auth/presentation/bloc/auth_cubit.dart index ae2f3fff..6993a526 100644 --- a/lib/features/auth/presentation/bloc/auth_cubit.dart +++ b/lib/features/auth/presentation/bloc/auth_cubit.dart @@ -68,12 +68,25 @@ class AuthCubit extends Cubit { /// 当 HTTP 登录/注册成功后调用 Future loginSuccess(UserEntity user) async { - await storage.saveUser(user); - await tcp.connect(host: TCPConsts.TCP_IP, port: TCPConsts.TCP_PORT); - // await _authTcpDatasource.sendAuthPacket(); //包括发送认证包和获取列表和切换函数 - //tcp.startHeartbeat(interval: const Duration(seconds: 4)); //启动心跳 + // 🔥 关键修复:先立即更新状态,触发 GoRouter redirect appCubit.setAuth(user); emit(AuthAuthenticated(user)); + + // 然后异步执行其他操作(使用 unawaited 避免阻塞) + _performPostLoginTasks(user); + } + + /// 登录后后台任务(不阻塞主流程) + Future _performPostLoginTasks(UserEntity user) async { + try { + await storage.saveUser(user); + await tcp.connect(host: TCPConsts.TCP_IP, port: TCPConsts.TCP_PORT); + // await _authTcpDatasource.sendAuthPacket(); + // tcp.startHeartbeat(interval: const Duration(seconds: 4)); + } catch (e) { + _logger.logWithLevel('[AUTH] 登录后任务失败: $e', level: 'ERROR'); + // 不影响已登录状态,只是记录错误 + } } /// 退出登录 (主动或被动) diff --git a/lib/features/auth/presentation/pages/login_page.dart b/lib/features/auth/presentation/pages/login_page.dart index ab0695c0..9c46a550 100644 --- a/lib/features/auth/presentation/pages/login_page.dart +++ b/lib/features/auth/presentation/pages/login_page.dart @@ -39,7 +39,9 @@ class _LoginPageState extends State { body: BlocListener( listener: (context, state) { if (state is LoginSuccess) { - context.go(RoutePaths.home); + // 登录成功后,AuthCubit 状态已更新 + // GoRouter 的 redirect 会自动检测到已登录状态并跳转到首页 + // 不需要手动调用 context.go } else if (state is LoginFailure) { // 可以在这里弹出简洁的黑白提示框 ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('登录失败,${state.message}')));