解决解析 TOKEN 类型报错问题
This commit is contained in:
@@ -31,26 +31,33 @@ class JwtDecodedData {
|
||||
});
|
||||
|
||||
factory JwtDecodedData.fromJson(Map<String, dynamic> 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<String>.from(json["oIds"]!.map((x) => x?.toString())),
|
||||
organizerId: json["organizerId"]?.toString(),
|
||||
: List<String>.from(json['oIds']!.map((x) => x?.toString())),
|
||||
organizerId: json['organizerId']?.toString(),
|
||||
);
|
||||
|
||||
Map<String, dynamic> toJson() => {
|
||||
"authType": authType,
|
||||
"deviceCode": deviceCode,
|
||||
"deviceId": deviceId,
|
||||
"deviceRole": deviceRole,
|
||||
"eventName": eventName,
|
||||
"oId": oId,
|
||||
"oIds": oIds == null ? [] : List<dynamic>.from(oIds!.map((x) => x)),
|
||||
"organizerId": organizerId,
|
||||
'authType': authType,
|
||||
'deviceCode': deviceCode,
|
||||
'deviceId': deviceId,
|
||||
'deviceRole': deviceRole,
|
||||
'eventName': eventName,
|
||||
'oId': oId,
|
||||
'oIds': oIds == null ? [] : List<dynamic>.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());
|
||||
}
|
||||
|
||||
@@ -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<AuthState> {
|
||||
/// 解析 TOKEN,并更新状态
|
||||
Future<bool> parseTokenSetState(String token) async {
|
||||
final decoded = JwtDecoder.decode(token);
|
||||
if (decoded['data'] != null && decoded['data'] is Map<String, dynamic>) {
|
||||
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<String, dynamic>.from(rawData));
|
||||
state = state.copyWith(jwtDecodedData: data);
|
||||
return true;
|
||||
} catch (error) {
|
||||
debugPrint('认证失败,请重试: $error');
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// 获取赛事列表
|
||||
|
||||
@@ -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<ScanQrCodePage> {
|
||||
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;
|
||||
// }
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user