基础库
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 

179 lignes
6.7 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. await NetUtil.post('/api/v1/tbredirect', params: params, method: NetMethod.POST);
  59. FlutterAlibc.loginOut();
  60. } else {
  61. print("access 失败 $resultMsg");
  62. Fluttertoast.showToast(msg: "access 失败 $resultMsg");
  63. }
  64. }
  65. return false;
  66. }
  67. ///
  68. /// 淘宝授权
  69. /// ⚠️ 弃用: 因业务修改,使用上面一个方法
  70. static Future authOld(BuildContext context) async {
  71. bool isAuth = await TaobaoAuth.isAuth();
  72. if (isAuth) {
  73. Fluttertoast.showToast(msg: '你已经授权过了');
  74. Provider.of<UserInfoNotifier>(context, listen: false).updateUserAuth(true);
  75. return null;
  76. }
  77. bool isConfirm = await showDialog(
  78. context: context,
  79. builder: (BuildContext context) {
  80. return TaobaoAuthAlert();
  81. });
  82. if (isConfirm != null && isConfirm == true) {
  83. Map<String, dynamic> data = Map<String, dynamic>.from(await NetUtil.post('/api/v1/taobao/auth', method: NetMethod.GET));
  84. Logger.debug(data);
  85. if (data['code'] != 1) {
  86. return null;
  87. }
  88. String url = data['data']['redirect_url'];
  89. print("授权链接" + url);
  90. TradeResult result;
  91. if (Platform.isAndroid) {
  92. // result = await FlutterAlibc.openByUrl(url: url, backUrl: "alisdk://", isAuth: true);
  93. print("name: $name" + "\n" + "alibcAppKey: $alibcAppKey");
  94. var resultMsg = await FlutterAlibc.taoKeLoginNew(name, alibcAppKey);
  95. FlutterAlibc.loginOut();
  96. if (!EmptyUtil.isEmpty(resultMsg["accessToken"])) {
  97. print("access 成功 $resultMsg");
  98. Map<String, dynamic> params = {};
  99. params['access_token'] = resultMsg["accessToken"]?.toString();
  100. await NetUtil.post('/api/v1/tbredirect',
  101. params: params, method: NetMethod.POST);
  102. } else {
  103. print("access 失败 $resultMsg");
  104. Fluttertoast.showToast(msg: "access 失败 $resultMsg");
  105. }
  106. // if(result.errorCode=="0"){
  107. // await Navigator.push(context,MaterialPageRoute(builder: (_)=>AuthSuccessPage(authResultType: AuthResultType.success,)) );
  108. // }else{
  109. // await Navigator.push(context,MaterialPageRoute(builder: (_)=>AuthSuccessPage(authResultType: AuthResultType.error,)) );
  110. // }
  111. } else if (Platform.isIOS) {
  112. // result = await FlutterAlibc.openByUrl(url: url);
  113. print("iOSname: $name" + "\n" + "iOSalibcAppKey: $alibcAppKey");
  114. var resultMsg = await FlutterAlibc.taoKeLoginNewIOS(name, alibcAppKey);
  115. if (!EmptyUtil.isEmpty(resultMsg["accessToken"])) {
  116. print("access iOS 成功 $resultMsg");
  117. Map<String, dynamic> params = {};
  118. params['access_token'] = resultMsg["accessToken"]?.toString();
  119. await NetUtil.post('/api/v1/tbredirect',
  120. params: params, method: NetMethod.POST);
  121. } else {
  122. print("access 失败 $resultMsg");
  123. Fluttertoast.showToast(msg: "access 失败 $resultMsg");
  124. }
  125. }
  126. // Logger.log('${result.errorCode} ${result.errorMessage} ');
  127. // if(null != result && result.errorCode == '0'){
  128. // Provider.of<UserInfoNotifier>(context, listen: false).updateUserAuth(true);
  129. // }
  130. await initAuth(context);
  131. }
  132. }
  133. // 返回是否授权
  134. static Future<bool> isAuth() async {
  135. try {
  136. Map<String, dynamic> data = Map<String, dynamic>.from(await NetUtil.post('/api/v1/user/profile', method: NetMethod.GET));
  137. if (data['code'] == 1) {
  138. ProfileModel _profile = ProfileModel.fromJson(Map<String, dynamic>.from(data['data']));
  139. return _profile.isAuth;
  140. }
  141. } catch (e, s) {
  142. Logger.error(e, s);
  143. }
  144. return false;
  145. }
  146. // 返回是否授权
  147. static void initAuth(BuildContext context) async {
  148. try {
  149. Map<String, dynamic> data = Map<String, dynamic>.from(
  150. await NetUtil.post('/api/v1/user/profile', method: NetMethod.GET));
  151. if (data['code'] == 1) {
  152. ProfileModel model =
  153. ProfileModel.fromJson(Map<String, dynamic>.from(data['data']));
  154. if (null != model && model.isAuth) {
  155. Provider.of<UserInfoNotifier>(context, listen: false).updateUserAuth(true);
  156. }
  157. }
  158. } catch (e, s) {
  159. Logger.error(e, s);
  160. }
  161. }
  162. }