From 356636c4b4a9ee884275a05d603cfdc78ad88982 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E9=94=8B?= <2535831261@qq.com> Date: Mon, 27 Jul 2026 16:57:05 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E8=A7=A3=E6=9E=90=20TOKEN=20?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E6=8A=A5=E9=94=99=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/features/auth/model/model_jwt.dart | 41 +++++++++++-------- .../auth/view_model_auth/view_model_auth.dart | 18 ++++---- .../scan_qrcode/pages/page_scan_qrcode.dart | 17 ++++---- 3 files changed, 41 insertions(+), 35 deletions(-) diff --git a/lib/features/auth/model/model_jwt.dart b/lib/features/auth/model/model_jwt.dart index adefb0d..af3d24c 100644 --- a/lib/features/auth/model/model_jwt.dart +++ b/lib/features/auth/model/model_jwt.dart @@ -31,26 +31,33 @@ class JwtDecodedData { }); factory JwtDecodedData.fromJson(Map json) => JwtDecodedData( - authType: json["authType"], - deviceCode: json["deviceCode"], - deviceId: json["deviceId"], - deviceRole: json["deviceRole"], - eventName: json["eventName"], - oId: json["oId"]?.toString(), - oIds: json["oIds"] == null + authType: json['authType']?.toString(), + deviceCode: json['deviceCode']?.toString(), + deviceId: _readInt(json['deviceId']), + deviceRole: json['deviceRole']?.toString(), + eventName: json['eventName']?.toString(), + oId: json['oId']?.toString(), + oIds: json['oIds'] == null ? [] - : List.from(json["oIds"]!.map((x) => x?.toString())), - organizerId: json["organizerId"]?.toString(), + : List.from(json['oIds']!.map((x) => x?.toString())), + organizerId: json['organizerId']?.toString(), ); Map toJson() => { - "authType": authType, - "deviceCode": deviceCode, - "deviceId": deviceId, - "deviceRole": deviceRole, - "eventName": eventName, - "oId": oId, - "oIds": oIds == null ? [] : List.from(oIds!.map((x) => x)), - "organizerId": organizerId, + 'authType': authType, + 'deviceCode': deviceCode, + 'deviceId': deviceId, + 'deviceRole': deviceRole, + 'eventName': eventName, + 'oId': oId, + 'oIds': oIds == null ? [] : List.from(oIds!.map((x) => x)), + 'organizerId': organizerId, }; } + +int? _readInt(dynamic value) { + if (value == null) return null; + if (value is int) return value; + if (value is num) return value.toInt(); + return int.tryParse(value.toString()); +} diff --git a/lib/features/auth/view_model_auth/view_model_auth.dart b/lib/features/auth/view_model_auth/view_model_auth.dart index a11077a..75c05a6 100644 --- a/lib/features/auth/view_model_auth/view_model_auth.dart +++ b/lib/features/auth/view_model_auth/view_model_auth.dart @@ -1,3 +1,4 @@ +import 'package:flutter/foundation.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_riverpod/legacy.dart'; import 'package:jwt_decoder/jwt_decoder.dart'; @@ -49,17 +50,16 @@ class AuthViewModel extends StateNotifier { /// 解析 TOKEN,并更新状态 Future parseTokenSetState(String token) async { final decoded = JwtDecoder.decode(token); - if (decoded['data'] != null && decoded['data'] is Map) { - try { - final data = JwtDecodedData.fromJson(decoded['data']); - state = state.copyWith(jwtDecodedData: data); - } catch (error) { - state = const AuthState(errorMessage: '认证失败,请重试'); - return false; - } + final rawData = decoded['data']; + if (rawData is! Map) return false; + try { + final data = JwtDecodedData.fromJson(Map.from(rawData)); + state = state.copyWith(jwtDecodedData: data); return true; + } catch (error) { + debugPrint('认证失败,请重试: $error'); + return false; } - return false; } /// 获取赛事列表 diff --git a/lib/features/scan_qrcode/pages/page_scan_qrcode.dart b/lib/features/scan_qrcode/pages/page_scan_qrcode.dart index 8a5e1c9..6296cc6 100644 --- a/lib/features/scan_qrcode/pages/page_scan_qrcode.dart +++ b/lib/features/scan_qrcode/pages/page_scan_qrcode.dart @@ -6,7 +6,6 @@ import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:recording_tool/app/router/app_navigator.dart'; import 'package:recording_tool/core/cache/app_storage.dart'; import 'package:recording_tool/core/cache/storage_keys.dart'; -import 'package:recording_tool/features/auth/pages/page_auth.dart'; import 'package:recording_tool/features/auth/view_model_auth/view_model_auth.dart'; import 'package:recording_tool/features/competition_teams/pages/page_competition_team_list.dart'; import 'package:recording_tool/features/events/model/model_event_info.dart'; @@ -43,14 +42,14 @@ class _ScanQrCodePageState extends ConsumerState { WidgetsBinding.instance.addPostFrameCallback((_) async { final token = AppStorage.getString(StorageKeys.authToken); if (token?.isNotEmpty ?? false) { - final success = await ref - .read(authProvider.notifier) - .parseTokenSetState(token!); - if (!success) { - AppToast.show('请重新鉴权'); - AppNavigator.pushAndRemoveUntil(const AuthPageWidget()); - return; - } + // final success = await ref + // .read(authProvider.notifier) + // .parseTokenSetState(token!); + // if (!success) { + // AppToast.show('请重新鉴权'); + // AppNavigator.pushAndRemoveUntil(const AuthPageWidget()); + // return; + // } } }); }