diff --git a/frontend/mining-app/lib/core/network/api_client.dart b/frontend/mining-app/lib/core/network/api_client.dart index 3508734b..8d3bcee0 100644 --- a/frontend/mining-app/lib/core/network/api_client.dart +++ b/frontend/mining-app/lib/core/network/api_client.dart @@ -5,7 +5,6 @@ import '../error/exceptions.dart'; class ApiClient { final Dio dio; - String? _accessToken; ApiClient({required this.dio}) { dio.options = BaseOptions( @@ -21,27 +20,17 @@ class ApiClient { // 添加请求拦截器,自动注入 token dio.interceptors.add(InterceptorsWrapper( onRequest: (options, handler) async { - if (_accessToken == null) { - // 从存储中加载 token - final prefs = await SharedPreferences.getInstance(); - _accessToken = prefs.getString('access_token'); - } - if (_accessToken != null) { - options.headers['Authorization'] = 'Bearer $_accessToken'; + // 每次请求都从存储中读取最新的 token,确保登录后立即生效 + final prefs = await SharedPreferences.getInstance(); + final token = prefs.getString('access_token'); + if (token != null && token.isNotEmpty) { + options.headers['Authorization'] = 'Bearer $token'; } handler.next(options); }, )); } - void setAccessToken(String? token) { - _accessToken = token; - } - - void clearAccessToken() { - _accessToken = null; - } - Future get( String path, { Map? queryParameters,