- C# 文件命名改为 snake_case(28 个文件重命名) - C# 类转换为主构造函数(8 个类) - 空 catch 添加异常类型(2 处) - 新建 GlobalUsings.cs(Health.Infrastructure、Health.WebApi) - Flutter 移除 go_router,改用 Riverpod 路由栈 - Flutter 移除 flutter_secure_storage,改用 sqflite 持久化 - 修复 Flutter 构建路径(Flutter SDK 迁至 D 盘) - 后端端口改为 0.0.0.0:5000,支持局域网访问
100 lines
3.0 KiB
Dart
100 lines
3.0 KiB
Dart
import 'package:dio/dio.dart';
|
|
import 'local_database.dart';
|
|
|
|
/// API 基础地址
|
|
const String baseUrl = 'http://10.4.185.103:5000';
|
|
|
|
/// Dio HTTP 客户端封装——带 token 注入、401 自动刷新
|
|
class ApiClient {
|
|
final Dio _dio;
|
|
final LocalDatabase _db;
|
|
|
|
ApiClient({required LocalDatabase db})
|
|
: _db = db,
|
|
_dio = Dio(BaseOptions(
|
|
baseUrl: baseUrl,
|
|
connectTimeout: const Duration(seconds: 15),
|
|
receiveTimeout: const Duration(seconds: 60),
|
|
headers: {'Content-Type': 'application/json'},
|
|
)) {
|
|
_dio.interceptors.add(_AuthInterceptor(this));
|
|
_dio.interceptors.add(LogInterceptor(requestBody: true, responseBody: false));
|
|
}
|
|
|
|
Dio get dio => _dio;
|
|
|
|
Future<String?> get accessToken => _db.read('access_token');
|
|
Future<String?> get refreshToken => _db.read('refresh_token');
|
|
|
|
Future<void> saveTokens(String access, String refresh) async {
|
|
await _db.write('access_token', access);
|
|
await _db.write('refresh_token', refresh);
|
|
}
|
|
|
|
Future<void> clearTokens() async {
|
|
await _db.deleteAll();
|
|
}
|
|
|
|
/// 带 token 的 GET 请求
|
|
Future<Response> get(String path, {Map<String, dynamic>? queryParameters}) async {
|
|
return _dio.get(path, queryParameters: queryParameters);
|
|
}
|
|
|
|
/// 带 token 的 POST 请求
|
|
Future<Response> post(String path, {dynamic data}) async {
|
|
return _dio.post(path, data: data);
|
|
}
|
|
|
|
/// 带 token 的 PUT 请求
|
|
Future<Response> put(String path, {dynamic data}) async {
|
|
return _dio.put(path, data: data);
|
|
}
|
|
|
|
/// 带 token 的 DELETE 请求
|
|
Future<Response> delete(String path) async {
|
|
return _dio.delete(path);
|
|
}
|
|
}
|
|
|
|
/// 认证拦截器:自动注入 token + 401 刷新
|
|
class _AuthInterceptor extends Interceptor {
|
|
final ApiClient _client;
|
|
|
|
_AuthInterceptor(this._client);
|
|
|
|
@override
|
|
void onRequest(RequestOptions options, RequestInterceptorHandler handler) async {
|
|
if (!options.path.contains('/auth/')) {
|
|
final token = await _client.accessToken;
|
|
if (token != null) {
|
|
options.headers['Authorization'] = 'Bearer $token';
|
|
}
|
|
}
|
|
handler.next(options);
|
|
}
|
|
|
|
@override
|
|
void onError(DioException err, ErrorInterceptorHandler handler) async {
|
|
if (err.response?.statusCode == 401) {
|
|
final refresh = await _client.refreshToken;
|
|
if (refresh != null) {
|
|
try {
|
|
final response = await Dio(BaseOptions(baseUrl: baseUrl))
|
|
.post('/api/auth/refresh', data: {'refreshToken': refresh});
|
|
final data = response.data['data'];
|
|
if (data != null) {
|
|
await _client.saveTokens(data['accessToken'], data['refreshToken']);
|
|
final opts = err.requestOptions;
|
|
final token = data['accessToken'];
|
|
opts.headers['Authorization'] = 'Bearer $token';
|
|
final retryResponse = await Dio(BaseOptions(baseUrl: baseUrl)).fetch(opts);
|
|
return handler.resolve(retryResponse);
|
|
}
|
|
} catch (_) {}
|
|
}
|
|
await _client.clearTokens();
|
|
}
|
|
handler.next(err);
|
|
}
|
|
}
|