fix: token过期自动跳转登录页
- dio_client.dart: 401错误时触发onUnauthorized回调 - main.dart: 连接回调到AuthProvider.forceLogout - 完整链路: 401 -> 清除token -> 强制登出 -> 显示LoginPage
This commit is contained in:
@@ -17,6 +17,9 @@ class NetworkConfig {
|
||||
class DioClient {
|
||||
late final Dio _dio;
|
||||
|
||||
/// 未授权回调(token 过期时触发)
|
||||
VoidCallback? onUnauthorized;
|
||||
|
||||
DioClient() {
|
||||
_dio = _createDio();
|
||||
_setupInterceptors();
|
||||
@@ -92,7 +95,13 @@ class DioClient {
|
||||
) {
|
||||
final data = response.data;
|
||||
if (data is Map<String, dynamic>) {
|
||||
return ApiResponse.fromJson(data, fromJson);
|
||||
final apiResponse = ApiResponse.fromJson(data, fromJson);
|
||||
// 检测业务层未授权(后端返回 HTTP 200 + code "0002")
|
||||
if (apiResponse.isUnauthorized) {
|
||||
LocalStorage.clearUserData();
|
||||
onUnauthorized?.call();
|
||||
}
|
||||
return apiResponse;
|
||||
}
|
||||
return ApiResponse.fail('响应数据格式错误');
|
||||
}
|
||||
@@ -109,6 +118,7 @@ class DioClient {
|
||||
|
||||
if (_isUnauthorized(e)) {
|
||||
_clearUserData();
|
||||
onUnauthorized?.call();
|
||||
return ApiResponse.unauthorized('登录已过期,请重新登录');
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user