基础库
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.

taobao_auth.dart 2.8 KiB

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