From e972d17f1ca94361c72499a76b5910aa9771b3b6 Mon Sep 17 00:00:00 2001 From: guozhigq Date: Sat, 27 Jan 2024 22:37:16 +0800 Subject: [PATCH] =?UTF-8?q?mod:=20=E4=BC=98=E5=8C=96=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/http/init.dart | 20 ++++++++++++++++---- lib/http/interceptor.dart | 40 ++++++++++++++++----------------------- 2 files changed, 32 insertions(+), 28 deletions(-) diff --git a/lib/http/init.dart b/lib/http/init.dart index 6244df04..dcc9f402 100644 --- a/lib/http/init.dart +++ b/lib/http/init.dart @@ -179,8 +179,14 @@ class Request { ); return response; } on DioException catch (e) { - print('get error: $e'); - return Future.error(await ApiInterceptor.dioError(e)); + Response errResponse = Response( + data: { + 'message': await ApiInterceptor.dioError(e) + }, // 将自定义 Map 数据赋值给 Response 的 data 属性 + statusCode: 200, + requestOptions: RequestOptions(), + ); + return errResponse; } } @@ -201,8 +207,14 @@ class Request { // print('post success: ${response.data}'); return response; } on DioException catch (e) { - print('post error: $e'); - return Future.error(await ApiInterceptor.dioError(e)); + Response errResponse = Response( + data: { + 'message': await ApiInterceptor.dioError(e) + }, // 将自定义 Map 数据赋值给 Response 的 data 属性 + statusCode: 200, + requestOptions: RequestOptions(), + ); + return errResponse; } } diff --git a/lib/http/interceptor.dart b/lib/http/interceptor.dart index 48ba7e60..362ff17a 100644 --- a/lib/http/interceptor.dart +++ b/lib/http/interceptor.dart @@ -5,7 +5,6 @@ import 'package:dio/dio.dart'; import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:hive/hive.dart'; import '../utils/storage.dart'; -// import 'package:get/get.dart' hide Response; class ApiInterceptor extends Interceptor { @override @@ -71,35 +70,28 @@ class ApiInterceptor extends Interceptor { return '发送请求超时,请检查网络设置'; case DioExceptionType.unknown: final String res = await checkConnect(); - return '$res \n 网络异常,请稍后重试!'; - // default: - // return 'Dio异常'; + return '$res,网络异常!'; } } static Future checkConnect() async { final ConnectivityResult connectivityResult = await Connectivity().checkConnectivity(); - if (connectivityResult == ConnectivityResult.mobile) { - return 'connected with mobile network'; - } else if (connectivityResult == ConnectivityResult.wifi) { - return 'connected with wifi network'; - } else if (connectivityResult == ConnectivityResult.ethernet) { - // I am connected to a ethernet network. - return ''; - } else if (connectivityResult == ConnectivityResult.vpn) { - // I am connected to a vpn network. - // Note for iOS and macOS: - // There is no separate network interface type for [vpn]. - // It returns [other] on any device (also simulator) - return ''; - } else if (connectivityResult == ConnectivityResult.other) { - // I am connected to a network which is not in the above mentioned networks. - return ''; - } else if (connectivityResult == ConnectivityResult.none) { - return 'not connected to any network'; - } else { - return ''; + switch (connectivityResult) { + case ConnectivityResult.mobile: + return '正在使用移动流量'; + case ConnectivityResult.wifi: + return '正在使用wifi'; + case ConnectivityResult.ethernet: + return '正在使用局域网'; + case ConnectivityResult.vpn: + return '正在使用代理网络'; + case ConnectivityResult.other: + return '正在使用其他网络'; + case ConnectivityResult.none: + return '未连接到任何网络'; + default: + return ''; } } }