diff --git a/lib/util/net_util.dart b/lib/util/net_util.dart index e3e3a5f..0f81941 100644 --- a/lib/util/net_util.dart +++ b/lib/util/net_util.dart @@ -92,29 +92,51 @@ class NetUtil { }; } + static Map headParam; + + /// 同步请求 static Future post(String path, {Map params, Map queryParameters, NetMethod method = NetMethod.POST, bool cache = false, bool showToast = true}) async { - - var paramsData = { - 'postData': params ?? {}, - 'queryParameters': queryParameters ?? {} - }; - + var paramsData = {'postData': params ?? {}, 'queryParameters': queryParameters ?? {}}; // 根据请求参数,获取缓存的Key String cacheKey = getRequestParamsCachedKey(paramsData, path); // 参数签名 TODO 加密? // post请求的参数 Map bodyParams = params; + + ///只拿一次头部数据 + if (NetUtil.headParam == null) { + NetUtil.headParam = await _getMustHeadParams(); + } + + + // token 读取SP缓存中的用户token + String token = await SharedPreferencesUtil.getStringValue(GlobalConfig.SHARED_KEY_TOKEN); + + if (EmptyUtil.isEmpty(token) && !bool.fromEnvironment("dart.vm.product")) { + try { + Map setting = await NativeUtil.getSetting(); + token = setting['token']; + } catch (e, s) { + print(s); + print(e); + } + } + if (!EmptyUtil.isEmpty(token)) { + // params['token'] = token; + NetUtil.headParam['Authorization'] = 'Bearer ' + token; + } + + + if (NetUtil.headParam['token'] == null) {} // 请求头参数 - Map headParam = await _getMustHeadParams(); + Map headParam = NetUtil.headParam; 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, @@ -171,8 +193,8 @@ class NetUtil { if (result[GlobalConfig.HTTP_RESPONSE_KEY_CODE]?.toString() == '401000') { try { Future.delayed(Duration(seconds: 0)).then((onValue) async { - String token= await SharedPreferencesUtil.getStringValue(GlobalConfig.SHARED_KEY_TOKEN); - if(token!=null&&token.length>2){ + String token = await SharedPreferencesUtil.getStringValue(GlobalConfig.SHARED_KEY_TOKEN); + if (token != null && token.length > 2) { BuildContext context = navigatorKey.currentState.overlay.context; Provider.of(context, listen: false).unLogin(); } @@ -213,27 +235,26 @@ 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 { - var paramsData = { - 'postData': params ?? {}, - 'queryParameters': queryParameters ?? {} - }; + static void request(String path, + {NetMethod method = NetMethod.GET, + Map params, + Map queryParameters, + OnSuccess onSuccess, + OnError onError, + OnCache onCache, + bool showToast = true}) async { + var paramsData = {'postData': params ?? {}, 'queryParameters': queryParameters ?? {}}; // 根据请求参数,获取缓存的Key String cacheKey = getRequestParamsCachedKey(paramsData, path); - print("缓存Key"+cacheKey); + print("缓存Key" + cacheKey); // // 读取缓存回调 if (onCache != null) { await _onCallBackCacheData(onCache, cacheKey); } 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); // TODO 解密? if (isSuccess(result)) { if (onSuccess != null) { @@ -243,7 +264,11 @@ class NetUtil { } 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()); @@ -348,8 +373,7 @@ class NetUtil { params["os_version"] = iosInfo?.systemVersion?.toString(); // 设备型号 如:iPhone 11pro - params['device_model'] = - EncodeUtil.encodeBase64(iosInfo?.name?.toString() ?? ''); + params['device_model'] = EncodeUtil.encodeBase64(iosInfo?.name?.toString() ?? ''); // 设备ID params['device_id'] = iosInfo?.identifierForVendor?.toString(); @@ -455,11 +479,8 @@ class NetUtil { } /// 根据请求参数,获取缓存的数据 - static Future getRequestCachedData(String url, {Map params,Map queryParameters}) async { - var paramsData = { - 'postData': params ?? {}, - 'queryParameters': queryParameters ?? {} - }; + static Future getRequestCachedData(String url, {Map params, Map queryParameters}) async { + var paramsData = {'postData': params ?? {}, 'queryParameters': queryParameters ?? {}}; Map cacheMap = await SharedPreferencesUtil.getNetCacheResult(getRequestParamsCachedKey(paramsData, url)); if (!EmptyUtil.isEmpty(cacheMap) && @@ -490,11 +511,11 @@ class NetUtil { static String getRequestParamsCachedKey(Map map, String path) { String key; if (EmptyUtil.isEmpty(map)) { - key= EncodeUtil.generateMd5(path); - }else{ - key= EncodeUtil.generateMd5(path + map.toString()); + key = EncodeUtil.generateMd5(path); + } else { + key = EncodeUtil.generateMd5(path + map.toString()); } - return key; + return key; } // 七牛云文件上传 diff --git a/lib/util/router_util.dart b/lib/util/router_util.dart index b838ac1..bcc1c7e 100644 --- a/lib/util/router_util.dart +++ b/lib/util/router_util.dart @@ -16,11 +16,7 @@ class RouterUtil { * data 额外参数 * */ static Future route(SkipModel skipModel, Map data, BuildContext context) async { - // skipModel.skipIdentifier="pub.flutter.credit_card"; - // if (Application.hasStringMethod(skipModel.skipIdentifier.toString())) { - // Application.doStringMethod(skipModel.skipIdentifier.toString()); - // return null; - // } + if (skipModel.skipIdentifier == null || skipModel.skipIdentifier == '') { print('skipIdentifier 参数不存在,无法跳转页面'); return Future.error('skipIdentifier 参数不存在,无法跳转页面'); @@ -40,6 +36,15 @@ class RouterUtil { } } + if (skipModel?.requiredTaobaoAuth == '1') { + UserInfoModel user = + await Provider.of(context, listen: false).getUserInfoModel(); + if (!user?.isTBAuth ?? false) { + TaobaoAuth.auth(context); + return; + } + } + Widget page = EmptyPage(); if (PageFactory.hasRegisted(skipModel.skipIdentifier)) { page = PageFactory.create(skipModel.skipIdentifier, data); diff --git a/pubspec.yaml b/pubspec.yaml index 21ea213..ff83f53 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -41,10 +41,10 @@ dependencies: # app更新dialogUI(用于IOS,以便统一样式) flutter_update_dialog: 1.0.0 flutter_alibc: - path: ../zhiying_flutter_alibc -# git: -# ref: 0.0.6 -# url: http://192.168.0.138:3000/FnuoOS_ZhiYing/zhiying_flutter_alibc.git +# path: ../zhiying_flutter_alibc + git: + ref: 0.0.7 + url: http://192.168.0.138:3000/FnuoOS_ZhiYing/zhiying_flutter_alibc.git url_launcher: ^5.6.0 #图片预览控件 photo_view: ^0.10.3 @@ -86,7 +86,7 @@ dependencies: save_image: git: url: 'http://192.168.0.138:3000/FnuoOS_ZhiYing/save_image.git' - ref: '0.0.1' + ref: '0.0.2' dio_cookie_manager: ^1.0.0 #支付宝支付flutterSDK