|
|
@@ -4,6 +4,7 @@ import 'package:sino_med_cloud/core/network/interceptors/auth_interceptor.dart';
|
|
|
import 'package:sino_med_cloud/core/network/interceptors/error_interceptor.dart';
|
|
|
import 'package:sino_med_cloud/core/network/interceptors/log_interceptor.dart';
|
|
|
import 'package:sino_med_cloud/core/utils/logger.dart';
|
|
|
+import 'package:sino_med_cloud/base/common_response.dart';
|
|
|
|
|
|
/// Dio 客户端 - 统一网络请求管理
|
|
|
class DioClient {
|
|
|
@@ -53,26 +54,32 @@ class DioClient {
|
|
|
/// GET 请求
|
|
|
///
|
|
|
/// [path] 请求路径(相对于 baseUrl)
|
|
|
+ /// [fromJsonT] JSON 转换为 T 类型的函数
|
|
|
/// [queryParameters] 查询参数
|
|
|
/// [options] 请求选项
|
|
|
///
|
|
|
- /// 返回 [Response] 对象
|
|
|
- static Future<Response<T>> get<T>(
|
|
|
+ /// 返回 [BaseCommonResponse] 对象
|
|
|
+ static Future<BaseCommonResponse<T>> get<T>(
|
|
|
String path, {
|
|
|
+ required T Function(Object? json) fromJsonT,
|
|
|
Map<String, dynamic>? queryParameters,
|
|
|
Options? options,
|
|
|
CancelToken? cancelToken,
|
|
|
ProgressCallback? onReceiveProgress,
|
|
|
}) async {
|
|
|
try {
|
|
|
- final response = await dio.get<T>(
|
|
|
+ final response = await dio.get(
|
|
|
path,
|
|
|
queryParameters: queryParameters,
|
|
|
options: options,
|
|
|
cancelToken: cancelToken,
|
|
|
onReceiveProgress: onReceiveProgress,
|
|
|
);
|
|
|
- return response;
|
|
|
+
|
|
|
+ return BaseCommonResponse<T>.fromJson(
|
|
|
+ response.data as Map<String, dynamic>,
|
|
|
+ fromJsonT,
|
|
|
+ );
|
|
|
} on DioException catch (e) {
|
|
|
// 错误已由 ErrorInterceptor 处理,直接抛出
|
|
|
rethrow;
|
|
|
@@ -84,13 +91,15 @@ class DioClient {
|
|
|
/// POST 请求
|
|
|
///
|
|
|
/// [path] 请求路径(相对于 baseUrl)
|
|
|
+ /// [fromJsonT] JSON 转换为 T 类型的函数
|
|
|
/// [data] 请求体数据
|
|
|
/// [queryParameters] 查询参数
|
|
|
/// [options] 请求选项
|
|
|
///
|
|
|
- /// 返回 [Response] 对象
|
|
|
- static Future<Response<T>> post<T>(
|
|
|
+ /// 返回 [BaseCommonResponse] 对象
|
|
|
+ static Future<BaseCommonResponse<T>> post<T>(
|
|
|
String path, {
|
|
|
+ required T Function(Object? json) fromJsonT,
|
|
|
dynamic data,
|
|
|
Map<String, dynamic>? queryParameters,
|
|
|
Options? options,
|
|
|
@@ -99,7 +108,7 @@ class DioClient {
|
|
|
ProgressCallback? onReceiveProgress,
|
|
|
}) async {
|
|
|
try {
|
|
|
- final response = await dio.post<T>(
|
|
|
+ final response = await dio.post(
|
|
|
path,
|
|
|
data: data,
|
|
|
queryParameters: queryParameters,
|
|
|
@@ -108,7 +117,11 @@ class DioClient {
|
|
|
onSendProgress: onSendProgress,
|
|
|
onReceiveProgress: onReceiveProgress,
|
|
|
);
|
|
|
- return response;
|
|
|
+
|
|
|
+ return BaseCommonResponse<T>.fromJson(
|
|
|
+ response.data as Map<String, dynamic>,
|
|
|
+ fromJsonT,
|
|
|
+ );
|
|
|
} on DioException catch (e) {
|
|
|
// 错误已由 ErrorInterceptor 处理,直接抛出
|
|
|
rethrow;
|
|
|
@@ -120,45 +133,53 @@ class DioClient {
|
|
|
/// DELETE 请求
|
|
|
///
|
|
|
/// [path] 请求路径(相对于 baseUrl)
|
|
|
+ /// [fromJsonT] JSON 转换为 T 类型的函数
|
|
|
/// [data] 请求体数据(可选)
|
|
|
/// [queryParameters] 查询参数
|
|
|
/// [options] 请求选项
|
|
|
///
|
|
|
- /// 返回 [Response] 对象
|
|
|
- static Future<Response<T>> delete<T>(
|
|
|
+ /// 返回 [BaseCommonResponse] 对象
|
|
|
+ static Future<BaseCommonResponse<T>> delete<T>(
|
|
|
String path, {
|
|
|
+ required T Function(Object? json) fromJsonT,
|
|
|
dynamic data,
|
|
|
Map<String, dynamic>? queryParameters,
|
|
|
Options? options,
|
|
|
CancelToken? cancelToken,
|
|
|
}) async {
|
|
|
try {
|
|
|
- final response = await dio.delete<T>(
|
|
|
+ final response = await dio.delete(
|
|
|
path,
|
|
|
data: data,
|
|
|
queryParameters: queryParameters,
|
|
|
options: options,
|
|
|
cancelToken: cancelToken,
|
|
|
);
|
|
|
- return response;
|
|
|
+
|
|
|
+ return BaseCommonResponse<T>.fromJson(
|
|
|
+ response.data as Map<String, dynamic>,
|
|
|
+ fromJsonT,
|
|
|
+ );
|
|
|
} on DioException catch (e) {
|
|
|
// 错误已由 ErrorInterceptor 处理,直接抛出
|
|
|
rethrow;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // ==================== PUT 请求====================
|
|
|
+ // ==================== PUT 请求 ====================
|
|
|
|
|
|
/// PUT 请求
|
|
|
///
|
|
|
/// [path] 请求路径(相对于 baseUrl)
|
|
|
+ /// [fromJsonT] JSON 转换为 T 类型的函数
|
|
|
/// [data] 请求体数据
|
|
|
/// [queryParameters] 查询参数
|
|
|
/// [options] 请求选项
|
|
|
///
|
|
|
- /// 返回 [Response] 对象
|
|
|
- static Future<Response<T>> put<T>(
|
|
|
+ /// 返回 [BaseCommonResponse] 对象
|
|
|
+ static Future<BaseCommonResponse<T>> put<T>(
|
|
|
String path, {
|
|
|
+ required T Function(Object? json) fromJsonT,
|
|
|
dynamic data,
|
|
|
Map<String, dynamic>? queryParameters,
|
|
|
Options? options,
|
|
|
@@ -167,7 +188,7 @@ class DioClient {
|
|
|
ProgressCallback? onReceiveProgress,
|
|
|
}) async {
|
|
|
try {
|
|
|
- final response = await dio.put<T>(
|
|
|
+ final response = await dio.put(
|
|
|
path,
|
|
|
data: data,
|
|
|
queryParameters: queryParameters,
|
|
|
@@ -176,7 +197,11 @@ class DioClient {
|
|
|
onSendProgress: onSendProgress,
|
|
|
onReceiveProgress: onReceiveProgress,
|
|
|
);
|
|
|
- return response;
|
|
|
+
|
|
|
+ return BaseCommonResponse<T>.fromJson(
|
|
|
+ response.data as Map<String, dynamic>,
|
|
|
+ fromJsonT,
|
|
|
+ );
|
|
|
} on DioException catch (e) {
|
|
|
// 错误已由 ErrorInterceptor 处理,直接抛出
|
|
|
rethrow;
|
|
|
@@ -188,13 +213,15 @@ class DioClient {
|
|
|
/// PATCH 请求
|
|
|
///
|
|
|
/// [path] 请求路径(相对于 baseUrl)
|
|
|
+ /// [fromJsonT] JSON 转换为 T 类型的函数
|
|
|
/// [data] 请求体数据
|
|
|
/// [queryParameters] 查询参数
|
|
|
/// [options] 请求选项
|
|
|
///
|
|
|
- /// 返回 [Response] 对象
|
|
|
- static Future<Response<T>> patch<T>(
|
|
|
+ /// 返回 [BaseCommonResponse] 对象
|
|
|
+ static Future<BaseCommonResponse<T>> patch<T>(
|
|
|
String path, {
|
|
|
+ required T Function(Object? json) fromJsonT,
|
|
|
dynamic data,
|
|
|
Map<String, dynamic>? queryParameters,
|
|
|
Options? options,
|
|
|
@@ -203,7 +230,7 @@ class DioClient {
|
|
|
ProgressCallback? onReceiveProgress,
|
|
|
}) async {
|
|
|
try {
|
|
|
- final response = await dio.patch<T>(
|
|
|
+ final response = await dio.patch(
|
|
|
path,
|
|
|
data: data,
|
|
|
queryParameters: queryParameters,
|
|
|
@@ -212,7 +239,11 @@ class DioClient {
|
|
|
onSendProgress: onSendProgress,
|
|
|
onReceiveProgress: onReceiveProgress,
|
|
|
);
|
|
|
- return response;
|
|
|
+
|
|
|
+ return BaseCommonResponse<T>.fromJson(
|
|
|
+ response.data as Map<String, dynamic>,
|
|
|
+ fromJsonT,
|
|
|
+ );
|
|
|
} on DioException catch (e) {
|
|
|
// 错误已由 ErrorInterceptor 处理,直接抛出
|
|
|
rethrow;
|