52 lines
1.1 KiB
Dart
52 lines
1.1 KiB
Dart
import 'package:dio/dio.dart';
|
|
|
|
import '../dto/base_dto.dart';
|
|
|
|
|
|
///请求拦截器
|
|
void onRequest(
|
|
RequestOptions options,
|
|
RequestInterceptorHandler handler,
|
|
) {
|
|
return handler.next(options);
|
|
}
|
|
|
|
///响应拦截器
|
|
void onResponse(
|
|
Response<dynamic> response,
|
|
ResponseInterceptorHandler handler,
|
|
) {
|
|
var apiResponse = ApiDto.fromJson(response.data);
|
|
if (apiResponse.code == 1) {
|
|
handler.next(response);
|
|
} else {
|
|
handler.reject(
|
|
DioException(
|
|
requestOptions: response.requestOptions,
|
|
response: response,
|
|
error: {'code': 0, 'message': apiResponse.message},
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
///错误响应
|
|
void onError(
|
|
DioException e,
|
|
ErrorInterceptorHandler handler,
|
|
) {
|
|
if (e.type == DioExceptionType.connectionTimeout) {
|
|
print("请求超时");
|
|
} else if (e.type == DioExceptionType.badResponse) {
|
|
if (e.response?.statusCode == 404) {
|
|
print("接口404不存在");
|
|
} else {
|
|
print("500");
|
|
}
|
|
} else if (e.type == DioExceptionType.connectionError) {
|
|
print("网络连接失败");
|
|
} else {
|
|
print("接口请求异常报错");
|
|
}
|
|
}
|