fix: token过期自动跳转登录页

- dio_client.dart: 401错误时触发onUnauthorized回调
- main.dart: 连接回调到AuthProvider.forceLogout
- 完整链路: 401 -> 清除token -> 强制登出 -> 显示LoginPage
This commit is contained in:
2026-04-01 11:26:07 +08:00
parent ed98db66cf
commit 3f4d2d8b9a
5 changed files with 8407 additions and 8374 deletions

View File

@@ -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('登录已过期,请重新登录');
}