基础库
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 

182 行
6.9 KiB

  1. import 'dart:io';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_alibc/alibc_const_key.dart';
  4. import 'package:flutter_alibc/alibc_model.dart';
  5. import 'package:flutter_alibc/flutter_alibc.dart';
  6. import 'package:fluttertoast/fluttertoast.dart';
  7. import 'package:package_info/package_info.dart';
  8. import 'package:zhiying_comm/models/profile/profile_model.dart';
  9. import 'package:zhiying_comm/pages/auth_page/auth_success_page/auth_success_page.dart';
  10. import 'package:zhiying_comm/util/dialog/loading/loading.dart';
  11. import 'package:zhiying_comm/util/taobao/taobao_auth_alert.dart';
  12. import 'package:zhiying_comm/zhiying_comm.dart';
  13. import 'package:provider/provider.dart';
  14. class TaobaoAuth {
  15. // static ProfileModel _profile;
  16. static String name = "";
  17. static String alibcAppKey = "";
  18. static setParams({@required String name, @required String alibcAppKey}) {
  19. TaobaoAuth.name = name;
  20. TaobaoAuth.alibcAppKey = alibcAppKey;
  21. }
  22. /// 因业务逻辑改变,每次是淘宝都必须检查是否授权
  23. ///
  24. /// true:有授权,调用此方法可以继续往下走。 不需要打开授权窗口
  25. /// false = 无授权,并且打开了授权窗口。 需要打开授权窗口
  26. ///
  27. static Future<bool> auth(BuildContext context) async {
  28. /// 1、请求网络,获取授权状态
  29. Loading.show(context);
  30. bool isAuth = await TaobaoAuth.isAuth();
  31. Loading.dismiss();
  32. /// 2、有授权,直接返回true,往下执行
  33. if (isAuth) {
  34. Provider.of<UserInfoNotifier>(context, listen: false).updateUserAuth(true);
  35. return true;
  36. }
  37. /// 3、打开淘宝授权弹窗,进行授权
  38. bool isConfirm = await showDialog( context: context, builder: (_) => TaobaoAuthAlert());
  39. if (isConfirm != null && isConfirm == true) {
  40. Map<String, dynamic> data = Map<String, dynamic>.from(await NetUtil.post('/api/v1/taobao/auth', method: NetMethod.GET));
  41. Logger.debug(data);
  42. if (data['code'] != 1) {
  43. return false;
  44. }
  45. String url = data['data']['redirect_url'];
  46. print("授权链接" + url);
  47. var resultMsg;
  48. if (Platform.isAndroid) {
  49. resultMsg = await FlutterAlibc.taoKeLoginNew(name, alibcAppKey);
  50. } else if (Platform.isIOS) {
  51. resultMsg = await FlutterAlibc.taoKeLoginNewIOS(name, alibcAppKey);
  52. }
  53. if (!EmptyUtil.isEmpty(resultMsg["accessToken"])) {
  54. // Fluttertoast.showToast(msg: '授权成功~');
  55. print("access 成功 $resultMsg");
  56. Map<String, dynamic> params = {};
  57. params['access_token'] = resultMsg["accessToken"]?.toString();
  58. var result = await NetUtil.post('/api/v1/tbredirect', params: params, method: NetMethod.POST);
  59. if (NetUtil.isSuccess(result)) {
  60. Fluttertoast.showToast(msg: '授权成功~');
  61. }
  62. } else {
  63. print("access 失败 $resultMsg");
  64. Fluttertoast.showToast(msg: "access 失败 $resultMsg");
  65. }
  66. FlutterAlibc.loginOut();
  67. }
  68. return false;
  69. }
  70. ///
  71. /// 淘宝授权
  72. /// ⚠️ 弃用: 因业务修改,使用上面一个方法
  73. static Future authOld(BuildContext context) async {
  74. bool isAuth = await TaobaoAuth.isAuth();
  75. if (isAuth) {
  76. Fluttertoast.showToast(msg: '你已经授权过了');
  77. Provider.of<UserInfoNotifier>(context, listen: false).updateUserAuth(true);
  78. return null;
  79. }
  80. bool isConfirm = await showDialog(
  81. context: context,
  82. builder: (BuildContext context) {
  83. return TaobaoAuthAlert();
  84. });
  85. if (isConfirm != null && isConfirm == true) {
  86. Map<String, dynamic> data = Map<String, dynamic>.from(await NetUtil.post('/api/v1/taobao/auth', method: NetMethod.GET));
  87. Logger.debug(data);
  88. if (data['code'] != 1) {
  89. return null;
  90. }
  91. String url = data['data']['redirect_url'];
  92. print("授权链接" + url);
  93. TradeResult result;
  94. if (Platform.isAndroid) {
  95. // result = await FlutterAlibc.openByUrl(url: url, backUrl: "alisdk://", isAuth: true);
  96. print("name: $name" + "\n" + "alibcAppKey: $alibcAppKey");
  97. var resultMsg = await FlutterAlibc.taoKeLoginNew(name, alibcAppKey);
  98. FlutterAlibc.loginOut();
  99. if (!EmptyUtil.isEmpty(resultMsg["accessToken"])) {
  100. print("access 成功 $resultMsg");
  101. Map<String, dynamic> params = {};
  102. params['access_token'] = resultMsg["accessToken"]?.toString();
  103. await NetUtil.post('/api/v1/tbredirect',
  104. params: params, method: NetMethod.POST);
  105. } else {
  106. print("access 失败 $resultMsg");
  107. Fluttertoast.showToast(msg: "access 失败 $resultMsg");
  108. }
  109. // if(result.errorCode=="0"){
  110. // await Navigator.push(context,MaterialPageRoute(builder: (_)=>AuthSuccessPage(authResultType: AuthResultType.success,)) );
  111. // }else{
  112. // await Navigator.push(context,MaterialPageRoute(builder: (_)=>AuthSuccessPage(authResultType: AuthResultType.error,)) );
  113. // }
  114. } else if (Platform.isIOS) {
  115. // result = await FlutterAlibc.openByUrl(url: url);
  116. print("iOSname: $name" + "\n" + "iOSalibcAppKey: $alibcAppKey");
  117. var resultMsg = await FlutterAlibc.taoKeLoginNewIOS(name, alibcAppKey);
  118. if (!EmptyUtil.isEmpty(resultMsg["accessToken"])) {
  119. print("access iOS 成功 $resultMsg");
  120. Map<String, dynamic> params = {};
  121. params['access_token'] = resultMsg["accessToken"]?.toString();
  122. await NetUtil.post('/api/v1/tbredirect',
  123. params: params, method: NetMethod.POST);
  124. } else {
  125. print("access 失败 $resultMsg");
  126. Fluttertoast.showToast(msg: "access 失败 $resultMsg");
  127. }
  128. }
  129. // Logger.log('${result.errorCode} ${result.errorMessage} ');
  130. // if(null != result && result.errorCode == '0'){
  131. // Provider.of<UserInfoNotifier>(context, listen: false).updateUserAuth(true);
  132. // }
  133. await initAuth(context);
  134. }
  135. }
  136. // 返回是否授权
  137. static Future<bool> isAuth() async {
  138. try {
  139. Map<String, dynamic> data = Map<String, dynamic>.from(await NetUtil.post('/api/v1/user/profile', method: NetMethod.GET));
  140. if (data['code'] == 1) {
  141. ProfileModel _profile = ProfileModel.fromJson(Map<String, dynamic>.from(data['data']));
  142. return _profile.isAuth;
  143. }
  144. } catch (e, s) {
  145. Logger.error(e, s);
  146. }
  147. return false;
  148. }
  149. // 返回是否授权
  150. static void initAuth(BuildContext context) async {
  151. try {
  152. Map<String, dynamic> data = Map<String, dynamic>.from(
  153. await NetUtil.post('/api/v1/user/profile', method: NetMethod.GET));
  154. if (data['code'] == 1) {
  155. ProfileModel model =
  156. ProfileModel.fromJson(Map<String, dynamic>.from(data['data']));
  157. if (null != model && model.isAuth) {
  158. Provider.of<UserInfoNotifier>(context, listen: false).updateUserAuth(true);
  159. }
  160. }
  161. } catch (e, s) {
  162. Logger.error(e, s);
  163. }
  164. }
  165. }