merge new features
This commit is contained in:
@@ -10,7 +10,7 @@ import '../core/utils/app_logger.dart';
|
||||
class ApiResponse<T> {
|
||||
final int code;
|
||||
final String message;
|
||||
final T? data;
|
||||
final dynamic data;
|
||||
final String? error;
|
||||
final String? correlationId;
|
||||
|
||||
@@ -26,7 +26,7 @@ class ApiResponse<T> {
|
||||
return ApiResponse<T>(
|
||||
code: json['code'] as int? ?? 0,
|
||||
message: json['msg'] as String? ?? '',
|
||||
data: json['data'] as T?,
|
||||
data: json['data'],
|
||||
error: json['error'] as String?,
|
||||
correlationId: json['correlation_id'] as String?,
|
||||
);
|
||||
@@ -198,6 +198,12 @@ class ApiService {
|
||||
AppLogger.d('API Error: ${error.requestOptions.uri} - ${error.message}');
|
||||
|
||||
// 检查是否是 401 错误(HTTP 401 或 JSON code: 401)
|
||||
final silent404 =
|
||||
error.requestOptions.extra['silent404'] as bool? ?? false;
|
||||
if (silent404 && error.response?.statusCode == 404) {
|
||||
return handler.next(error);
|
||||
}
|
||||
|
||||
bool is401Error = error.response?.statusCode == 401;
|
||||
if (!is401Error && error.response?.data is Map<String, dynamic>) {
|
||||
final data = error.response!.data as Map<String, dynamic>;
|
||||
@@ -320,12 +326,16 @@ class ApiService {
|
||||
Map<String, dynamic>? queryParameters,
|
||||
bool noAuth = false,
|
||||
bool isNoData = false,
|
||||
bool silent404 = false,
|
||||
Map<String, dynamic>? headers,
|
||||
}) async {
|
||||
final response = await _dio.get<T>(
|
||||
path,
|
||||
queryParameters: queryParameters,
|
||||
options: Options(extra: {'noAuth': noAuth}, headers: headers),
|
||||
options: Options(
|
||||
extra: {'noAuth': noAuth, 'silent404': silent404},
|
||||
headers: headers,
|
||||
),
|
||||
);
|
||||
// 如果是分享请求, 则不进入 _parseResponse
|
||||
if (isNoData) {
|
||||
@@ -356,9 +366,12 @@ class ApiService {
|
||||
AppLogger.d('Response Data: ${response.data}');
|
||||
|
||||
var isActivEmail = 0;
|
||||
if (response.statusCode == 200) {
|
||||
Map<String, dynamic>? tmp = response.data as Map<String, dynamic>?;
|
||||
isActivEmail = tmp?['code'] as int;
|
||||
if (response.statusCode == 200 && response.data is Map) {
|
||||
final tmp = Map<String, dynamic>.from(response.data as Map);
|
||||
final code = tmp['code'];
|
||||
if (code is int) {
|
||||
isActivEmail = code;
|
||||
}
|
||||
}
|
||||
|
||||
if (isNoData || isActivEmail == 203) {
|
||||
@@ -447,11 +460,18 @@ class ApiService {
|
||||
T _parseResponse<T>(Response response) {
|
||||
final data = response.data;
|
||||
if (data is Map<String, dynamic>) {
|
||||
final apiResponse = ApiResponse<T>.fromJson(data);
|
||||
final apiResponse = ApiResponse<dynamic>.fromJson(data);
|
||||
if (!apiResponse.isSuccess && !apiResponse.isContinue) {
|
||||
throw ServerException(apiResponse.message, code: apiResponse.code);
|
||||
}
|
||||
return apiResponse.data as T;
|
||||
final payload = apiResponse.data;
|
||||
if (payload is T) {
|
||||
return payload;
|
||||
}
|
||||
if (data is T) {
|
||||
return data as T;
|
||||
}
|
||||
return payload as T;
|
||||
}
|
||||
return data as T;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user