|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- import 'dart:io';
-
- import 'package:flutter/material.dart';
- import 'package:flutter_alibc/alibc_const_key.dart';
- import 'package:flutter_alibc/alibc_model.dart';
- import 'package:flutter_alibc/flutter_alibc.dart';
- import 'package:fluttertoast/fluttertoast.dart';
- import 'package:package_info/package_info.dart';
- import 'package:zhiying_comm/models/profile/profile_model.dart';
- import 'package:zhiying_comm/pages/auth_page/auth_success_page/auth_success_page.dart';
- import 'package:zhiying_comm/util/dialog/loading/loading.dart';
- import 'package:zhiying_comm/util/taobao/taobao_auth_alert.dart';
- import 'package:zhiying_comm/zhiying_comm.dart';
- import 'package:provider/provider.dart';
-
- class TaobaoAuth {
- // static ProfileModel _profile;
-
- static String name = "";
- static String alibcAppKey = "";
-
- static setParams({@required String name, @required String alibcAppKey}) {
- TaobaoAuth.name = name;
- TaobaoAuth.alibcAppKey = alibcAppKey;
- }
-
- /// 因业务逻辑改变,每次是淘宝都必须检查是否授权
- ///
- /// true:有授权,调用此方法可以继续往下走。 不需要打开授权窗口
- /// false = 无授权,并且打开了授权窗口。 需要打开授权窗口
- ///
- static Future<bool> auth(BuildContext context) async {
-
- /// 1、请求网络,获取授权状态
- Loading.show(context);
- bool isAuth = await TaobaoAuth.isAuth();
- Loading.dismiss();
-
- /// 2、有授权,直接返回true,往下执行
- if (isAuth) {
- Provider.of<UserInfoNotifier>(context, listen: false).updateUserAuth(true);
- return true;
- }
-
- /// 3、打开淘宝授权弹窗,进行授权
- bool isConfirm = await showDialog( context: context, builder: (_) => TaobaoAuthAlert());
- if (isConfirm != null && isConfirm == true) {
- Map<String, dynamic> data = Map<String, dynamic>.from(await NetUtil.post('/api/v1/taobao/auth', method: NetMethod.GET));
- Logger.debug(data);
- if (data['code'] != 1) {
- return false;
- }
- String url = data['data']['redirect_url'];
- print("授权链接" + url);
-
- var resultMsg;
- if (Platform.isAndroid) {
- resultMsg = await FlutterAlibc.taoKeLoginNew(name, alibcAppKey);
- } else if (Platform.isIOS) {
- resultMsg = await FlutterAlibc.taoKeLoginNewIOS(name, alibcAppKey);
- }
-
- if (!EmptyUtil.isEmpty(resultMsg["accessToken"])) {
- // Fluttertoast.showToast(msg: '授权成功~');
- print("access 成功 $resultMsg");
- Map<String, dynamic> params = {};
- params['access_token'] = resultMsg["accessToken"]?.toString();
- var result = await NetUtil.post('/api/v1/tbredirect', params: params, method: NetMethod.POST);
- if (NetUtil.isSuccess(result)) {
- Fluttertoast.showToast(msg: '授权成功~');
- }
- } else {
- print("access 失败 $resultMsg");
- Fluttertoast.showToast(msg: "access 失败 $resultMsg");
- }
- FlutterAlibc.loginOut();
- }
- return false;
- }
-
- ///
- /// 淘宝授权
- /// ⚠️ 弃用: 因业务修改,使用上面一个方法
- static Future authOld(BuildContext context) async {
- bool isAuth = await TaobaoAuth.isAuth();
- if (isAuth) {
- Fluttertoast.showToast(msg: '你已经授权过了');
- Provider.of<UserInfoNotifier>(context, listen: false).updateUserAuth(true);
- return null;
- }
-
- bool isConfirm = await showDialog(
- context: context,
- builder: (BuildContext context) {
- return TaobaoAuthAlert();
- });
- if (isConfirm != null && isConfirm == true) {
- Map<String, dynamic> data = Map<String, dynamic>.from(await NetUtil.post('/api/v1/taobao/auth', method: NetMethod.GET));
- Logger.debug(data);
- if (data['code'] != 1) {
- return null;
- }
- String url = data['data']['redirect_url'];
- print("授权链接" + url);
- TradeResult result;
- if (Platform.isAndroid) {
- // result = await FlutterAlibc.openByUrl(url: url, backUrl: "alisdk://", isAuth: true);
- print("name: $name" + "\n" + "alibcAppKey: $alibcAppKey");
- var resultMsg = await FlutterAlibc.taoKeLoginNew(name, alibcAppKey);
- FlutterAlibc.loginOut();
- if (!EmptyUtil.isEmpty(resultMsg["accessToken"])) {
- print("access 成功 $resultMsg");
- Map<String, dynamic> params = {};
- params['access_token'] = resultMsg["accessToken"]?.toString();
- await NetUtil.post('/api/v1/tbredirect',
- params: params, method: NetMethod.POST);
-
- } else {
- print("access 失败 $resultMsg");
- Fluttertoast.showToast(msg: "access 失败 $resultMsg");
- }
- // if(result.errorCode=="0"){
- // await Navigator.push(context,MaterialPageRoute(builder: (_)=>AuthSuccessPage(authResultType: AuthResultType.success,)) );
- // }else{
- // await Navigator.push(context,MaterialPageRoute(builder: (_)=>AuthSuccessPage(authResultType: AuthResultType.error,)) );
- // }
- } else if (Platform.isIOS) {
- // result = await FlutterAlibc.openByUrl(url: url);
- print("iOSname: $name" + "\n" + "iOSalibcAppKey: $alibcAppKey");
- var resultMsg = await FlutterAlibc.taoKeLoginNewIOS(name, alibcAppKey);
- if (!EmptyUtil.isEmpty(resultMsg["accessToken"])) {
- print("access iOS 成功 $resultMsg");
- Map<String, dynamic> params = {};
- params['access_token'] = resultMsg["accessToken"]?.toString();
- await NetUtil.post('/api/v1/tbredirect',
- params: params, method: NetMethod.POST);
- } else {
- print("access 失败 $resultMsg");
- Fluttertoast.showToast(msg: "access 失败 $resultMsg");
- }
-
- }
- // Logger.log('${result.errorCode} ${result.errorMessage} ');
- // if(null != result && result.errorCode == '0'){
- // Provider.of<UserInfoNotifier>(context, listen: false).updateUserAuth(true);
- // }
- await initAuth(context);
- }
- }
-
- // 返回是否授权
- static Future<bool> isAuth() async {
- try {
- Map<String, dynamic> data = Map<String, dynamic>.from(await NetUtil.post('/api/v1/user/profile', method: NetMethod.GET));
- if (data['code'] == 1) {
- ProfileModel _profile = ProfileModel.fromJson(Map<String, dynamic>.from(data['data']));
- return _profile.isAuth;
- }
- } catch (e, s) {
- Logger.error(e, s);
- }
- return false;
- }
-
- // 返回是否授权
- static void initAuth(BuildContext context) async {
- try {
- Map<String, dynamic> data = Map<String, dynamic>.from(
- await NetUtil.post('/api/v1/user/profile', method: NetMethod.GET));
- if (data['code'] == 1) {
- ProfileModel model =
- ProfileModel.fromJson(Map<String, dynamic>.from(data['data']));
- if (null != model && model.isAuth) {
- Provider.of<UserInfoNotifier>(context, listen: false).updateUserAuth(true);
- }
- }
- } catch (e, s) {
- Logger.error(e, s);
- }
- }
- }
|