From d003e762d3ce72cafcc8ff34d63498bc57ea36b4 Mon Sep 17 00:00:00 2001 From: PH2 <1293456824@qq.com> Date: Mon, 7 Sep 2020 16:59:10 +0800 Subject: [PATCH 1/2] =?UTF-8?q?1=E3=80=81=E7=BC=93=E5=AD=98=E5=9B=9E?= =?UTF-8?q?=E8=B0=83=E7=9A=84=E5=88=A4=E6=96=AD=E3=80=82=E5=A6=82=E6=9E=9C?= =?UTF-8?q?=E4=B8=BA=E7=A9=BA=E5=B0=B1=E4=B8=8D=E8=A7=A6=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/util/net_util.dart | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/util/net_util.dart b/lib/util/net_util.dart index 303aa2f..9e659aa 100644 --- a/lib/util/net_util.dart +++ b/lib/util/net_util.dart @@ -64,7 +64,7 @@ class NetUtil { headers: {'device': 'wx_applet', 'Platform': 'wx_applet'}, )); _dio.interceptors.add(_NetInterceptors()); - _dio.interceptors.add(LogInterceptor()); +// _dio.interceptors.add(LogInterceptor()); const bool inProduction = const bool.fromEnvironment("dart.vm.product"); if (proxyUrl != null && proxyUrl != '' && !inProduction) { @@ -287,8 +287,7 @@ class NetUtil { (cacheMap[GlobalConfig.HTTP_RESPONSE_KEY_CODE] == GlobalConfig.RESPONSE_SUCCESS_CODE || cacheMap[GlobalConfig.HTTP_RESPONSE_KEY_CODE] == - '${GlobalConfig.RESPONSE_SUCCESS_CODE}') && - null != cacheMap[GlobalConfig.HTTP_RESPONSE_KEY_DATA]) { + '${GlobalConfig.RESPONSE_SUCCESS_CODE}') && !EmptyUtil.isEmpty(cacheMap[GlobalConfig.HTTP_RESPONSE_KEY_DATA])) { onCache(cacheMap[GlobalConfig.HTTP_RESPONSE_KEY_DATA]); return; } From 8b39bcba3d3edddcb3af05ecd672776c7226eb59 Mon Sep 17 00:00:00 2001 From: PH2 <1293456824@qq.com> Date: Tue, 8 Sep 2020 14:51:32 +0800 Subject: [PATCH 2/2] =?UTF-8?q?1=E3=80=81=E6=B7=BB=E5=8A=A0=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E5=90=8C=E6=AD=A5=E8=AF=B7=E6=B1=82=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/util/net_util.dart | 87 +++++++++++++++++++++++++----------------- 1 file changed, 52 insertions(+), 35 deletions(-) diff --git a/lib/util/net_util.dart b/lib/util/net_util.dart index 9e659aa..38cfaa0 100644 --- a/lib/util/net_util.dart +++ b/lib/util/net_util.dart @@ -46,7 +46,7 @@ class NetUtil { if (_dio == null) { var setting = await NativeUtil.getSetting(); String domain = setting['domain']; //'http://www.hairuyi.com/'; - _config(domain, proxyUrl: '192.168.0.112:8888'); +// _config(domain, proxyUrl: '192.168.0.66:8866'); } return _dio; } @@ -86,49 +86,39 @@ class NetUtil { /// 同步请求 static Future post(String path, - {Map params}) async { + {Map params, NetMethod method = NetMethod.POST}) async { if (params == null) { params = {}; } - + // 根据请求参数,获取缓存的Key + String cacheKey = _getRequestParamsCachedKey(params, path); + // 参数签名 TODO 加密? Map sign = await signParams(params); + Response response; try { Dio dio = await NetUtil.getInstance().dio; - response = await dio.post(path, data: sign); + response = await dio.request(path, data: sign, options: Options(method: enumToString(method))); } on DioError catch (e) { _formatError(e); -// Logger.error(e); - } - if (response == null) { - return null; } - if (response.statusCode != 200) { - // 请求错误 - const bool inProduction = const bool.fromEnvironment("dart.vm.product"); - if (!inProduction) { - Fluttertoast.showToast(msg: response.statusMessage); + try { + var result = response.data is Map ? response.data : jsonDecode(response.data); + // TODO 解密? + if (isSuccess(result)) { + // 缓存成功的数据 + _setCallBackCacheData(cacheKey, response.data is Map ? jsonEncode(response.data) : response.data); + return result; + // 缓存返回的数据 } - // log.e(response.statusMessage); - return null; - } - - var result = response.data; - - if (result[GlobalConfig.HTTP_RESPONSE_KEY_CODE] == - GlobalConfig.RESPONSE_SUCCESS_CODE || - result[GlobalConfig.HTTP_RESPONSE_KEY_CODE] == - '${GlobalConfig.RESPONSE_SUCCESS_CODE}') { - Logger.error(result[GlobalConfig.HTTP_RESPONSE_KEY_MSG]); - Fluttertoast.showToast( - msg: result[GlobalConfig.HTTP_RESPONSE_KEY_MSG], - toastLength: Toast.LENGTH_SHORT, - gravity: ToastGravity.BOTTOM, - ); + Logger.error('error: ' + result[GlobalConfig.HTTP_RESPONSE_KEY_MSG]); + Fluttertoast.showToast(msg: result[GlobalConfig.HTTP_RESPONSE_KEY_MSG], toastLength: Toast.LENGTH_SHORT, gravity: ToastGravity.BOTTOM,); + return result; + } catch (e) { + return null; } - return result; } /// 异步请求 @@ -166,10 +156,7 @@ class NetUtil { var result = response.data is Map ? response.data : jsonDecode(response.data); // TODO 解密? - if (result[GlobalConfig.HTTP_RESPONSE_KEY_CODE] == - GlobalConfig.RESPONSE_SUCCESS_CODE || - result[GlobalConfig.HTTP_RESPONSE_KEY_CODE] == - '${GlobalConfig.RESPONSE_SUCCESS_CODE}') { + if (isSuccess(result)) { if (onSuccess != null) { onSuccess(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA]); // 缓存返回的数据 @@ -186,7 +173,7 @@ class NetUtil { gravity: ToastGravity.BOTTOM, ); if (onError != null) { - onError(result[GlobalConfig.HTTP_RESPONSE_KEY_MSG] ?? '未知错误'); + onError( !EmptyUtil.isEmpty(result) ? !EmptyUtil.isEmpty(result[GlobalConfig.HTTP_RESPONSE_KEY_MSG]) ? result[GlobalConfig.HTTP_RESPONSE_KEY_MSG] : '未知错误' : '未知错误'); } } catch (e) { if (onError != null) { @@ -299,6 +286,36 @@ class NetUtil { SharedPreferencesUtil.setNetCacheResult(cacheKey, value); } + /// 根据请求参数,获取缓存的数据 + static Future getRequestCachedData(String url, {Map params}) async{ + Map cacheMap = await SharedPreferencesUtil.getNetCacheResult(_getRequestParamsCachedKey(params, url)); + if (!EmptyUtil.isEmpty(cacheMap) && + cacheMap.containsKey(GlobalConfig.HTTP_RESPONSE_KEY_CODE) && + (cacheMap[GlobalConfig.HTTP_RESPONSE_KEY_CODE] == + GlobalConfig.RESPONSE_SUCCESS_CODE || + cacheMap[GlobalConfig.HTTP_RESPONSE_KEY_CODE] == + '${GlobalConfig.RESPONSE_SUCCESS_CODE}') && !EmptyUtil.isEmpty(cacheMap[GlobalConfig.HTTP_RESPONSE_KEY_DATA])) { + return cacheMap[GlobalConfig.HTTP_RESPONSE_KEY_DATA]; + } + return null; + } + + /// 判断后台返回是否成功 + static bool isSuccess(Map data){ + try { + if (!EmptyUtil.isEmpty(data) && + (data[GlobalConfig.HTTP_RESPONSE_KEY_CODE] == + GlobalConfig.RESPONSE_SUCCESS_CODE || + data[GlobalConfig.HTTP_RESPONSE_KEY_CODE] == + '${GlobalConfig.RESPONSE_SUCCESS_CODE}')) { + return true; + } + }catch(e){ + return false; + } + return false; + } + /// 根据请求参数,获取缓存的Key static String _getRequestParamsCachedKey( Map map, String path) {