From 670183a3b9b42558a3259c069ef8cba116c399ec Mon Sep 17 00:00:00 2001 From: Weller <1812208341@qq.com> Date: Wed, 16 Sep 2020 20:42:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0token=EF=BC=8C=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E8=B7=B3=E8=BD=AC=E9=94=99=E8=AF=AF=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/workspace.xml | 64 ++++++++------------------ example/pubspec.lock | 21 +++++++++ lib/util/net_util.dart | 94 ++++++++++++++++++++++++++------------- lib/util/router_util.dart | 6 +++ lib/zhiying_comm.dart | 2 + pubspec.lock | 21 +++++++++ zhiying_comm.iml | 4 ++ 7 files changed, 135 insertions(+), 77 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index d9bd792..c6f3cb4 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,21 +2,9 @@ - - - - - - - - - - - - @@ -50,7 +38,7 @@ - + @@ -58,15 +46,6 @@ - - - - - - - - - @@ -94,8 +73,8 @@ - - + + @@ -106,8 +85,8 @@ - - + + @@ -115,8 +94,8 @@ - - + + @@ -268,9 +247,8 @@ - - + @@ -280,13 +258,13 @@ - + - + @@ -359,8 +337,8 @@ - - + + @@ -373,15 +351,15 @@ - - + + - + @@ -398,17 +376,11 @@ - - - - - - - + - - + + diff --git a/example/pubspec.lock b/example/pubspec.lock index d312e2e..7644981 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -36,6 +36,13 @@ packages: url: "https://pub.flutter-io.cn" source: hosted version: "2.4.0" + bloc: + dependency: transitive + description: + name: bloc + url: "https://pub.flutter-io.cn" + source: hosted + version: "4.0.0" boolean_selector: dependency: transitive description: @@ -230,6 +237,13 @@ packages: description: flutter source: sdk version: "0.0.0" + flutter_bloc: + dependency: transitive + description: + name: flutter_bloc + url: "https://pub.flutter-io.cn" + source: hosted + version: "4.0.1" flutter_cache_manager: dependency: transitive description: @@ -576,6 +590,13 @@ packages: url: "https://pub.flutter-io.cn" source: hosted version: "0.2.3" + shimmer: + dependency: transitive + description: + name: shimmer + url: "https://pub.flutter-io.cn" + source: hosted + version: "1.1.1" sky_engine: dependency: transitive description: flutter diff --git a/lib/util/net_util.dart b/lib/util/net_util.dart index 8d2d6ee..0e97a16 100644 --- a/lib/util/net_util.dart +++ b/lib/util/net_util.dart @@ -47,7 +47,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.66:8866'); + _config(domain, proxyUrl: '192.168.0.112:8888'); } return _dio; } @@ -87,7 +87,9 @@ class NetUtil { /// 同步请求 static Future post(String path, - {Map params, NetMethod method = NetMethod.POST, bool cache = false}) async { + {Map params, + NetMethod method = NetMethod.POST, + bool cache = false}) async { if (params == null) { params = {}; } @@ -99,25 +101,32 @@ class NetUtil { Response response; try { Dio dio = await NetUtil.getInstance().dio; - response = await dio.request(path, data: sign, options: Options(method: enumToString(method))); + response = await dio.request(path, + data: sign, options: Options(method: enumToString(method))); } on DioError catch (e) { _formatError(e); } try { - var result = response.data is Map ? response.data : jsonDecode(response.data); + var result = + response.data is Map ? response.data : jsonDecode(response.data); // TODO 解密? if (isSuccess(result)) { - // 缓存成功的数据 - if(cache) { - _setCallBackCacheData(cacheKey, response.data is Map ? jsonEncode(response.data) : response.data); - } - return result; - // 缓存返回的数据 + // 缓存成功的数据 + if (cache) { + _setCallBackCacheData(cacheKey, + response.data is Map ? jsonEncode(response.data) : response.data); + } + return result; + // 缓存返回的数据 } 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,); + Fluttertoast.showToast( + msg: result[GlobalConfig.HTTP_RESPONSE_KEY_MSG], + toastLength: Toast.LENGTH_SHORT, + gravity: ToastGravity.BOTTOM, + ); return result; } catch (e) { return null; @@ -148,9 +157,20 @@ class NetUtil { Response response; try { + // token 读取SP缓存中的用户token + String token = await SharedPreferencesUtil.getStringValue( + GlobalConfig.SHARED_KEY_TOKEN) ?? + ''; + + const bool inProduction = const bool.fromEnvironment("dart.vm.product"); + if (!inProduction && token != '') { + token = '7f7bdb785dcb02a4536bea2ffe299eda352e127f'; + } Dio dio = await NetUtil.getInstance().dio; response = await dio.request(path, - data: sign, options: Options(method: enumToString(method))); + data: sign, + options: + Options(method: enumToString(method), headers: {'token': token})); } on DioError catch (e) { _formatError(e); } @@ -176,7 +196,11 @@ class NetUtil { gravity: ToastGravity.BOTTOM, ); if (onError != null) { - onError( !EmptyUtil.isEmpty(result) ? !EmptyUtil.isEmpty(result[GlobalConfig.HTTP_RESPONSE_KEY_MSG]) ? 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) { Logger.error('error: ' + e.toString()); @@ -229,7 +253,9 @@ class NetUtil { // 站长ID String masterId = setting['master_id']; - if (null != masterId && masterId != '' && (!params.containsKey('master_id') || params['master_id'] == '')) { + if (null != masterId && + masterId != '' && + (!params.containsKey('master_id') || params['master_id'] == '')) { params['master_id'] = masterId; } @@ -237,8 +263,9 @@ class NetUtil { params['secret_key'] = setting['secret_key']; // token 读取SP缓存中的用户token - String token = await SharedPreferencesUtil.getStringValue(GlobalConfig.SHARED_KEY_TOKEN); - if(!EmptyUtil.isEmpty(token)){ + String token = await SharedPreferencesUtil.getStringValue( + GlobalConfig.SHARED_KEY_TOKEN); + if (!EmptyUtil.isEmpty(token)) { params['token'] = token; } @@ -274,7 +301,8 @@ class NetUtil { (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])) { + '${GlobalConfig.RESPONSE_SUCCESS_CODE}') && + !EmptyUtil.isEmpty(cacheMap[GlobalConfig.HTTP_RESPONSE_KEY_DATA])) { onCache(cacheMap[GlobalConfig.HTTP_RESPONSE_KEY_DATA]); return; } @@ -287,30 +315,34 @@ class NetUtil { } /// 根据请求参数,获取缓存的数据 - 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 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){ + static bool isSuccess(Map data) { try { if (!EmptyUtil.isEmpty(data) && (data[GlobalConfig.HTTP_RESPONSE_KEY_CODE] == - GlobalConfig.RESPONSE_SUCCESS_CODE || + GlobalConfig.RESPONSE_SUCCESS_CODE || data[GlobalConfig.HTTP_RESPONSE_KEY_CODE] == '${GlobalConfig.RESPONSE_SUCCESS_CODE}')) { return true; } - }catch(e){ + } catch (e) { return false; } return false; diff --git a/lib/util/router_util.dart b/lib/util/router_util.dart index 4442d09..705ec7d 100644 --- a/lib/util/router_util.dart +++ b/lib/util/router_util.dart @@ -12,6 +12,12 @@ class RouterUtil { // 需要淘宝授权 String requiredTaobaoAuth = model['required_taobao_auth'].toString(); + if (skipIdentifier == null || skipIdentifier == '') { + print('skipIdentifier 参数不存在,无法跳转页面'); + return Future.error('skipIdentifier 参数不存在,无法跳转页面'); + } + print('skipIdentifier: ${skipIdentifier}'); + if (requiredLogin == '1') { UserInfoModel user = await Provider.of(context, listen: false).getUserInfoModel(); print(user.toString()); diff --git a/lib/zhiying_comm.dart b/lib/zhiying_comm.dart index 8f6ba8b..190df4c 100644 --- a/lib/zhiying_comm.dart +++ b/lib/zhiying_comm.dart @@ -34,3 +34,5 @@ export 'native/native_router.dart'; // model export 'models/base/widget_model.dart'; export 'models/base/skip_model.dart'; +export 'models/user/user_info_model.dart'; +export 'models/user/user_info_model_notifier.dart'; diff --git a/pubspec.lock b/pubspec.lock index 3f68c7a..a1ec41c 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -36,6 +36,13 @@ packages: url: "https://pub.flutter-io.cn" source: hosted version: "2.4.0" + bloc: + dependency: transitive + description: + name: bloc + url: "https://pub.flutter-io.cn" + source: hosted + version: "4.0.0" boolean_selector: dependency: transitive description: @@ -223,6 +230,13 @@ packages: description: flutter source: sdk version: "0.0.0" + flutter_bloc: + dependency: "direct main" + description: + name: flutter_bloc + url: "https://pub.flutter-io.cn" + source: hosted + version: "4.0.1" flutter_cache_manager: dependency: transitive description: @@ -569,6 +583,13 @@ packages: url: "https://pub.flutter-io.cn" source: hosted version: "0.2.3" + shimmer: + dependency: "direct main" + description: + name: shimmer + url: "https://pub.flutter-io.cn" + source: hosted + version: "1.1.1" sky_engine: dependency: transitive description: flutter diff --git a/zhiying_comm.iml b/zhiying_comm.iml index fd966eb..d1743e7 100644 --- a/zhiying_comm.iml +++ b/zhiying_comm.iml @@ -11,12 +11,16 @@ + + + +