From eaf3ae36c70b8da6dbda087c418d3bbbd9c98f00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cyanghuaxuan=E2=80=9D?= <“646903573@qq.com”> Date: Thu, 22 Apr 2021 15:43:42 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E6=B7=BB=E5=8A=A0=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E8=B6=85=E6=97=B6=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/util/net_util.dart | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/lib/util/net_util.dart b/lib/util/net_util.dart index cfa526a..57b1715 100644 --- a/lib/util/net_util.dart +++ b/lib/util/net_util.dart @@ -96,7 +96,8 @@ class NetUtil { /// 同步请求 static Future post(String path, - {Map params, Map queryParameters, NetMethod method = NetMethod.POST, bool cache = false, bool showToast = true}) async { + {Map params, Map queryParameters, NetMethod method = NetMethod + .POST, bool cache = false, bool showToast = true, int timeOut = 15000}) async { var paramsData = {'postData': params ?? {}, 'queryParameters': queryParameters ?? {}}; // 根据请求参数,获取缓存的Key @@ -109,11 +110,13 @@ class NetUtil { Response response; try { - Dio dio = await NetUtil.getInstance().dio; + Dio dio = await NetUtil + .getInstance() + .dio; response = await dio.request( path, data: !EmptyUtil.isEmpty(bodyParams) ? bodyParams : null, - options: Options(method: enumToString(method), headers: headParam), + options: Options(method: enumToString(method), headers: headParam, receiveTimeout: timeOut), queryParameters: !EmptyUtil.isEmpty(queryParameters) ? queryParameters : null, ); } on DioError catch (e) { @@ -210,12 +213,12 @@ class NetUtil { /// 异步请求 static void request(String path, {NetMethod method = NetMethod.GET, - Map params, - Map queryParameters, - OnSuccess onSuccess, - OnError onError, - OnCache onCache, - bool showToast = true}) async { + Map params, + Map queryParameters, + OnSuccess onSuccess, + OnError onError, + OnCache onCache, + bool showToast = true,int timeOut=15000}) async { var paramsData = {'postData': params ?? {}, 'queryParameters': queryParameters ?? {}}; // 根据请求参数,获取缓存的Key @@ -227,7 +230,11 @@ class NetUtil { } try { - Map result = await NetUtil.post(path, method: method, params: params, queryParameters: queryParameters, showToast: showToast, cache: onCache != null); + Map result = await NetUtil.post(path, method: method, + params: params, + queryParameters: queryParameters, + showToast: showToast, + cache: onCache != null,timeOut: timeOut); // TODO 解密? if (isSuccess(result)) { if (onSuccess != null) { @@ -239,8 +246,8 @@ class NetUtil { if (onError != null) { onError(!EmptyUtil.isEmpty(result) ? !EmptyUtil.isEmpty(result[GlobalConfig.HTTP_RESPONSE_KEY_MSG]) - ? result[GlobalConfig.HTTP_RESPONSE_KEY_MSG] - : '未知错误' + ? result[GlobalConfig.HTTP_RESPONSE_KEY_MSG] + : '未知错误' : '未知错误'); } } catch (e) {