diff --git a/lib/util/taobao/taobao_auth.dart b/lib/util/taobao/taobao_auth.dart index 7093969..2397d2d 100644 --- a/lib/util/taobao/taobao_auth.dart +++ b/lib/util/taobao/taobao_auth.dart @@ -8,6 +8,7 @@ import 'package:fluttertoast/fluttertoast.dart'; import 'package:package_info/package_info.dart'; import 'package:zhiying_comm/models/profile/profile_model.dart'; import 'package:zhiying_comm/pages/auth_page/auth_success_page/auth_success_page.dart'; +import 'package:zhiying_comm/util/dialog/loading/loading.dart'; import 'package:zhiying_comm/util/taobao/taobao_auth_alert.dart'; import 'package:zhiying_comm/zhiying_comm.dart'; import 'package:provider/provider.dart'; @@ -23,8 +24,61 @@ class TaobaoAuth { TaobaoAuth.alibcAppKey = alibcAppKey; } - // 淘宝授权 - static Future auth(BuildContext context) async { + /// 因业务逻辑改变,每次是淘宝都必须检查是否授权 + /// + /// true:有授权,调用此方法可以继续往下走。 不需要打开授权窗口 + /// false = 无授权,并且打开了授权窗口。 需要打开授权窗口 + /// + static Future auth(BuildContext context) async { + + /// 1、请求网络,获取授权状态 + Loading.show(context); + bool isAuth = await TaobaoAuth.isAuth(); + Loading.dismiss(); + + /// 2、有授权,直接返回true,往下执行 + if (isAuth) { + Provider.of(context, listen: false).updateUserAuth(true); + return true; + } + + /// 3、打开淘宝授权弹窗,进行授权 + bool isConfirm = await showDialog( context: context, builder: (_) => TaobaoAuthAlert()); + if (isConfirm != null && isConfirm == true) { + Map data = Map.from(await NetUtil.post('/api/v1/taobao/auth', method: NetMethod.GET)); + Logger.debug(data); + if (data['code'] != 1) { + return false; + } + String url = data['data']['redirect_url']; + print("授权链接" + url); + + var resultMsg; + if (Platform.isAndroid) { + resultMsg = await FlutterAlibc.taoKeLoginNew(name, alibcAppKey); + } else if (Platform.isIOS) { + resultMsg = await FlutterAlibc.taoKeLoginNewIOS(name, alibcAppKey); + } + + if (!EmptyUtil.isEmpty(resultMsg["accessToken"])) { + Fluttertoast.showToast(msg: '授权成功~'); + print("access 成功 $resultMsg"); + Map params = {}; + params['access_token'] = resultMsg["accessToken"]?.toString(); + await NetUtil.post('/api/v1/tbredirect', params: params, method: NetMethod.POST); + FlutterAlibc.loginOut(); + } else { + print("access 失败 $resultMsg"); + Fluttertoast.showToast(msg: "access 失败 $resultMsg"); + } + } + return false; + } + + /// + /// 淘宝授权 + /// ⚠️ 弃用: 因业务修改,使用上面一个方法 + static Future authOld(BuildContext context) async { bool isAuth = await TaobaoAuth.isAuth(); if (isAuth) { Fluttertoast.showToast(msg: '你已经授权过了'); @@ -38,8 +92,7 @@ class TaobaoAuth { return TaobaoAuthAlert(); }); if (isConfirm != null && isConfirm == true) { - Map data = Map.from( - await NetUtil.post('/api/v1/taobao/auth', method: NetMethod.GET)); + Map data = Map.from(await NetUtil.post('/api/v1/taobao/auth', method: NetMethod.GET)); Logger.debug(data); if (data['code'] != 1) { return null; @@ -94,15 +147,10 @@ class TaobaoAuth { // 返回是否授权 static Future isAuth() async { - // if (_profile != null) { - // return _profile.isAuth; - // } try { - Map data = Map.from( - await NetUtil.post('/api/v1/user/profile', method: NetMethod.GET)); + Map data = Map.from(await NetUtil.post('/api/v1/user/profile', method: NetMethod.GET)); if (data['code'] == 1) { - ProfileModel _profile = - ProfileModel.fromJson(Map.from(data['data'])); + ProfileModel _profile = ProfileModel.fromJson(Map.from(data['data'])); return _profile.isAuth; } } catch (e, s) { diff --git a/lib/util/turn_chain/turn_chain_util.dart b/lib/util/turn_chain/turn_chain_util.dart index fff9a74..67dbfb7 100644 --- a/lib/util/turn_chain/turn_chain_util.dart +++ b/lib/util/turn_chain/turn_chain_util.dart @@ -67,9 +67,15 @@ class TurnChainUtil { } /// 2、如果是淘宝或者天猫,判断是否授权 - if ((provider == GlobalConfig.PROVIDER_TB || provider == GlobalConfig.PROVIDER_TM )&& !userInfoModel.isTBAuth) { - TaobaoAuth.auth(context); - return; + /// + /// ⚠️ 因业务修改,淘宝或者天猫每次都必须请求网络请求状态 + // if ((provider == GlobalConfig.PROVIDER_TB || provider == GlobalConfig.PROVIDER_TM ) && !userInfoModel.isTBAuth) { + // TaobaoAuth.auth(context); + // return; + // } + if (provider == GlobalConfig.PROVIDER_TB || provider == GlobalConfig.PROVIDER_TM ) { + bool isAuth = await TaobaoAuth.auth(context); + if (!isAuth) return; } /// 3、获取转链,进行跳转 @@ -332,9 +338,14 @@ class TurnChainUtil { } /// 2、如果是淘宝,判断是否授权 - if ((provider == GlobalConfig.PROVIDER_TB || provider == GlobalConfig.PROVIDER_TM) && !userInfoModel.isTBAuth) { - TaobaoAuth.auth(context); - return null; + // if ((provider == GlobalConfig.PROVIDER_TB || provider == GlobalConfig.PROVIDER_TM) && !userInfoModel.isTBAuth) { + // TaobaoAuth.auth(context); + // return null; + // } + /// ⚠️ 因业务修改,淘宝或者天猫每次都必须请求网络请求状态 + if (provider == GlobalConfig.PROVIDER_TB || provider == GlobalConfig.PROVIDER_TM ) { + bool isAuth = await TaobaoAuth.auth(context); + if (!isAuth) return null; } /// 2.5 如果是拼多多,判断是否需要授权