基础库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

116 lines
4.1 KiB

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