基础库
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 

131 rinda
4.8 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/taobao/taobao_auth_alert.dart';
  11. import 'package:zhiying_comm/zhiying_comm.dart';
  12. import 'package:provider/provider.dart';
  13. class TaobaoAuth {
  14. // static ProfileModel _profile;
  15. static String name = "";
  16. static String alibcAppKey = "";
  17. static setParams({@required String name, @required String alibcAppKey}) {
  18. TaobaoAuth.name = name;
  19. TaobaoAuth.alibcAppKey = alibcAppKey;
  20. }
  21. // 淘宝授权
  22. static Future auth(BuildContext context) async {
  23. bool isAuth = await TaobaoAuth.isAuth();
  24. if (isAuth) {
  25. Fluttertoast.showToast(msg: '你已经授权过了');
  26. Provider.of<UserInfoNotifier>(context, listen: false).updateUserAuth(true);
  27. return null;
  28. }
  29. bool isConfirm = await showDialog(
  30. context: context,
  31. builder: (BuildContext context) {
  32. return TaobaoAuthAlert();
  33. });
  34. if (isConfirm != null && isConfirm == true) {
  35. Map<String, dynamic> data = Map<String, dynamic>.from(
  36. await NetUtil.post('/api/v1/taobao/auth', method: NetMethod.GET));
  37. Logger.debug(data);
  38. if (data['code'] != 1) {
  39. return null;
  40. }
  41. String url = data['data']['redirect_url'];
  42. print("授权链接" + url);
  43. TradeResult result;
  44. if (Platform.isAndroid) {
  45. // result = await FlutterAlibc.openByUrl(url: url, backUrl: "alisdk://", isAuth: true);
  46. print("name: $name" + "\n" + "alibcAppKey: $alibcAppKey");
  47. var resultMsg = await FlutterAlibc.taoKeLoginNew(name, alibcAppKey);
  48. FlutterAlibc.loginOut();
  49. if (!EmptyUtil.isEmpty(resultMsg["accessToken"])) {
  50. print("access 成功 $resultMsg");
  51. Map<String, dynamic> params = {};
  52. params['access_token'] = resultMsg["accessToken"]?.toString();
  53. await NetUtil.post('/api/v1/tbredirect',
  54. params: params, method: NetMethod.POST);
  55. } else {
  56. print("access 失败 $resultMsg");
  57. Fluttertoast.showToast(msg: "access 失败 $resultMsg");
  58. }
  59. // if(result.errorCode=="0"){
  60. // await Navigator.push(context,MaterialPageRoute(builder: (_)=>AuthSuccessPage(authResultType: AuthResultType.success,)) );
  61. // }else{
  62. // await Navigator.push(context,MaterialPageRoute(builder: (_)=>AuthSuccessPage(authResultType: AuthResultType.error,)) );
  63. // }
  64. } else if (Platform.isIOS) {
  65. // result = await FlutterAlibc.openByUrl(url: url);
  66. print("iOSname: $name" + "\n" + "iOSalibcAppKey: $alibcAppKey");
  67. var resultMsg = await FlutterAlibc.taoKeLoginNewIOS(name, alibcAppKey);
  68. if (!EmptyUtil.isEmpty(resultMsg["accessToken"])) {
  69. print("access iOS 成功 $resultMsg");
  70. Map<String, dynamic> params = {};
  71. params['access_token'] = resultMsg["accessToken"]?.toString();
  72. await NetUtil.post('/api/v1/tbredirect',
  73. params: params, method: NetMethod.POST);
  74. } else {
  75. print("access 失败 $resultMsg");
  76. Fluttertoast.showToast(msg: "access 失败 $resultMsg");
  77. }
  78. }
  79. // Logger.log('${result.errorCode} ${result.errorMessage} ');
  80. // if(null != result && result.errorCode == '0'){
  81. // Provider.of<UserInfoNotifier>(context, listen: false).updateUserAuth(true);
  82. // }
  83. await initAuth(context);
  84. }
  85. }
  86. // 返回是否授权
  87. static Future<bool> isAuth() async {
  88. // if (_profile != null) {
  89. // return _profile.isAuth;
  90. // }
  91. try {
  92. Map<String, dynamic> data = Map<String, dynamic>.from(
  93. await NetUtil.post('/api/v1/user/profile', method: NetMethod.GET));
  94. if (data['code'] == 1) {
  95. ProfileModel _profile =
  96. ProfileModel.fromJson(Map<String, dynamic>.from(data['data']));
  97. return _profile.isAuth;
  98. }
  99. } catch (e, s) {
  100. Logger.error(e, s);
  101. }
  102. return false;
  103. }
  104. // 返回是否授权
  105. static void initAuth(BuildContext context) async {
  106. try {
  107. Map<String, dynamic> data = Map<String, dynamic>.from(
  108. await NetUtil.post('/api/v1/user/profile', method: NetMethod.GET));
  109. if (data['code'] == 1) {
  110. ProfileModel model =
  111. ProfileModel.fromJson(Map<String, dynamic>.from(data['data']));
  112. if (null != model && model.isAuth) {
  113. Provider.of<UserInfoNotifier>(context, listen: false).updateUserAuth(true);
  114. }
  115. }
  116. } catch (e, s) {
  117. Logger.error(e, s);
  118. }
  119. }
  120. }