基础库
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

taobao_auth.dart 3.2 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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:zhiying_comm/models/profile/profile_model.dart';
  7. import 'package:zhiying_comm/pages/auth_page/auth_success_page/auth_success_page.dart';
  8. import 'package:zhiying_comm/util/taobao/taobao_auth_alert.dart';
  9. import 'package:zhiying_comm/zhiying_comm.dart';
  10. import 'package:provider/provider.dart';
  11. class TaobaoAuth {
  12. // static ProfileModel _profile;
  13. // 淘宝授权
  14. static Future auth(BuildContext context) async {
  15. bool isAuth = await TaobaoAuth.isAuth();
  16. if (isAuth) {
  17. Fluttertoast.showToast(msg: '你已经授权过了');
  18. Provider.of<UserInfoNotifier>(context, listen: false).updateUserAuth(true);
  19. return null;
  20. }
  21. bool isConfirm = await showDialog(
  22. context: context,
  23. builder: (BuildContext context) {
  24. return TaobaoAuthAlert();
  25. });
  26. if (isConfirm != null && isConfirm == true) {
  27. Map<String, dynamic> data = Map<String, dynamic>.from(await NetUtil.post('/api/v1/taobao/auth', method: NetMethod.GET));
  28. Logger.debug(data);
  29. if (data['code'] != 1) {
  30. return null;
  31. }
  32. String url = data['data']['redirect_url'];
  33. print("授权链接" + url);
  34. TradeResult result;
  35. if (Platform.isAndroid) {
  36. result = await FlutterAlibc.openByUrl(url: url, backUrl: "alisdk://", isAuth: true);
  37. // if(result.errorCode=="0"){
  38. // await Navigator.push(context,MaterialPageRoute(builder: (_)=>AuthSuccessPage(authResultType: AuthResultType.success,)) );
  39. // }else{
  40. // await Navigator.push(context,MaterialPageRoute(builder: (_)=>AuthSuccessPage(authResultType: AuthResultType.error,)) );
  41. // }
  42. } else if (Platform.isIOS) {
  43. result = await FlutterAlibc.openByUrl(url: url);
  44. }
  45. Logger.log('${result.errorCode} ${result.errorMessage} ');
  46. // if(null != result && result.errorCode == '0'){
  47. // Provider.of<UserInfoNotifier>(context, listen: false).updateUserAuth(true);
  48. // }
  49. await initAuth(context);
  50. }
  51. }
  52. // 返回是否授权
  53. static Future<bool> isAuth() async {
  54. // if (_profile != null) {
  55. // return _profile.isAuth;
  56. // }
  57. try {
  58. Map<String, dynamic> data = Map<String, dynamic>.from(await NetUtil.post('/api/v1/user/profile', method: NetMethod.GET));
  59. if (data['code'] == 1) {
  60. ProfileModel _profile = ProfileModel.fromJson(Map<String, dynamic>.from(data['data']));
  61. return _profile.isAuth;
  62. }
  63. }catch(e, s){
  64. Logger.error(e, s);
  65. }
  66. return false;
  67. }
  68. // 返回是否授权
  69. static void initAuth(BuildContext context) async {
  70. try {
  71. Map<String, dynamic> data = Map<String, dynamic>.from(await NetUtil.post('/api/v1/user/profile', method: NetMethod.GET));
  72. if (data['code'] == 1) {
  73. ProfileModel model = ProfileModel.fromJson(Map<String, dynamic>.from(data['data']));
  74. if(null != model && model.isAuth){
  75. Provider.of<UserInfoNotifier>(context, listen: false).updateUserAuth(true);
  76. }
  77. }
  78. }catch(e, s){
  79. Logger.error(e, s);
  80. }
  81. }
  82. }