1
This commit is contained in:
@@ -1,13 +1,16 @@
|
||||
import 'package:app/providers/user_store.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter_easyloading/flutter_easyloading.dart';
|
||||
|
||||
import '../dto/base_dto.dart';
|
||||
|
||||
|
||||
///请求拦截器
|
||||
void onRequest(
|
||||
RequestOptions options,
|
||||
RequestInterceptorHandler handler,
|
||||
) {
|
||||
) async {
|
||||
String token = await UserStore.getToken();
|
||||
options.headers['Authorization'] = 'Bearer $token';
|
||||
return handler.next(options);
|
||||
}
|
||||
|
||||
@@ -18,6 +21,7 @@ void onResponse(
|
||||
) {
|
||||
var apiResponse = ApiDto.fromJson(response.data);
|
||||
if (apiResponse.code == 1) {
|
||||
response.data = apiResponse.data;
|
||||
handler.next(response);
|
||||
} else {
|
||||
handler.reject(
|
||||
@@ -35,17 +39,25 @@ void onError(
|
||||
DioException e,
|
||||
ErrorInterceptorHandler handler,
|
||||
) {
|
||||
var title = "";
|
||||
if (e.type == DioExceptionType.connectionTimeout) {
|
||||
print("请求超时");
|
||||
title = "请求超时";
|
||||
} else if (e.type == DioExceptionType.badResponse) {
|
||||
if (e.response?.statusCode == 404) {
|
||||
print("接口404不存在");
|
||||
title = "接口404不存在";
|
||||
} else {
|
||||
print("500");
|
||||
title = "500";
|
||||
}
|
||||
} else if (e.type == DioExceptionType.connectionError) {
|
||||
print("网络连接失败");
|
||||
title = "网络连接失败";
|
||||
} else {
|
||||
print("接口请求异常报错");
|
||||
title = "异常其他错误";
|
||||
}
|
||||
showError(title);
|
||||
handler.next(e);
|
||||
}
|
||||
|
||||
///显示错误信息
|
||||
void showError(String message) {
|
||||
EasyLoading.showError(message);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user