|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515 |
- //
- // FlutterAlibcHandle.m
- // flutter_alibc
- //
- // Created by 吴兴 on 2019/8/28.
- //
-
- #import "FlutterAlibcHandle.h"
- #import "FlutterAlibcTools.h"
- #import "FlutterAlibcConstKey.h"
- #import <AlibcTradeSDK/AlibcTradeSDK.h>
- //#import <AlibabaAuthSDK/albbsdk.h>
- #import "ALiTradeWebViewController.h"
- #import "FlutterWxViewCtrlViewController.h"
- #import <AlibabaAuthEntrance/ALBBSDK.h>
- #import <AlibabaAuthEntrance/ALBBCompatibleSession.h>
- @implementation FlutterAlibcHandle
- FlutterMethodChannel *_flutterAlibcChannel = nil;
-
-
- - (instancetype)initWithRegistrar:(NSObject <FlutterPluginRegistrar> *)registrar methodChannel:(FlutterMethodChannel *)flutterMethodChannel {
- self = [super init];
- if (self) {
- _flutterAlibcChannel = flutterMethodChannel;
- }
- return self;
- }
-
- #pragma mark- 对flutter暴露的方法
- #pragma mark -- 初始化阿里百川
- - (void)initAlibc:(FlutterMethodCall *)call result:(FlutterResult)result {
- // 百川平台基础SDK初始化,加载并初始化各个业务能力插件
- NSString *version = call.arguments[@"version"];
- NSString *appName = call.arguments[@"appName"];
- // 判断是否为空
- if(![FlutterAlibcTools isNil:version]){
- [[AlibcTradeSDK sharedInstance] setIsvVersion:version]; //设置三方App版本,可用于标识App版本
- }
- if(![FlutterAlibcTools isNil:appName]){
- [[AlibcTradeSDK sharedInstance] setIsvAppName:appName]; //设置三方App名称,可用于标识App
- }
- [[AlibcTradeSDK sharedInstance] setDebugLogOpen:YES];//开发阶段打开日志开关,方便排查错误信息
- [[AlibcTradeSDK sharedInstance] setEnv:AlibcEnvironmentRelease];//线上环境
-
-
- [[AlibcTradeSDK sharedInstance] asyncInitWithSuccess:^{
- // 告诉flutter,初始化完成
- result(@{FlutterAlibcConstKey_ErrorCode:@"0",FlutterAlibcConstKey_ErrorMessage:@"success"});
- } failure:^(NSError *error) {
- NSLog(@"Init failed: %@", error.description);
- result(@{FlutterAlibcConstKey_ErrorCode:[NSString stringWithFormat: @"%ld", (long)error.code],FlutterAlibcConstKey_ErrorMessage:error.description});
- }];
- }
- #pragma mark --淘宝登录
- - (void)loginTaoBao:(FlutterMethodCall *)call result:(FlutterResult)result{
- if(![[ALBBCompatibleSession sharedInstance] isLogin]) {
- UIViewController *rootViewController =
- [UIApplication sharedApplication].delegate.window.rootViewController;
- [[ALBBSDK sharedInstance] setH5Only:NO];
- [[ALBBSDK sharedInstance] auth:rootViewController successCallback:^{
- ALBBUser *userInfo =[[ALBBCompatibleSession sharedInstance] getUser];
- // 登录成功
- result(@{
- FlutterAlibcConstKey_ErrorCode:@"0",
- FlutterAlibcConstKey_ErrorMessage:@"success",
- FlutterAlibcConstKey_Data:@{
- // 昵称
- @"nick":userInfo.nick,
- // 头像地址
- @"avatarUrl":userInfo.avatarUrl,
- @"openId":userInfo.openId,
- @"openSid":userInfo.openSid,
- @"topAccessToken":userInfo.topAccessToken,
- @"topAuthCode":userInfo.topAuthCode,
- }
- });
- } failureCallback:^(NSError *error) {
- // 登录失败
- result(@{
- FlutterAlibcConstKey_ErrorCode:[NSString stringWithFormat: @"%ld", (long)error.code],
- FlutterAlibcConstKey_ErrorMessage:error.localizedDescription,
- FlutterAlibcConstKey_Data:@{}
- });
- }];
-
- } else {
- ALBBUser *userInfo =[[ALBBCompatibleSession sharedInstance] getUser];
- // 登录成功
- result(@{
- FlutterAlibcConstKey_ErrorCode:@"0",
- FlutterAlibcConstKey_ErrorMessage:@"success",
- FlutterAlibcConstKey_Data:@{
- // 昵称
- @"nick":userInfo.nick,
- // 头像地址
- @"avatarUrl":userInfo.avatarUrl,
- @"openId":userInfo.openId,
- @"openSid":userInfo.openSid,
- @"topAccessToken":userInfo.topAccessToken,
- @"topAuthCode":userInfo.topAuthCode,
- }
- });
- }
- }
- //
- //else if([@"useAlipayNative" isEqualToString:call.method]){
- // [_handler useAlipayNative:call result:result];
- //}
- #pragma mark --淘客登录
- -(void)taoKeLogin:(FlutterMethodCall *)call result:(FlutterResult)result{
- // 需要获取的数据
- NSNumber *type1 = call.arguments[@"openType"];
- AlibcOpenType openType = [self openType:[type1 intValue]];
- BOOL isNeedCustomNativeFailMode = [call.arguments[@"isNeedCustomNativeFailMode"] boolValue];
- // 不用push了,没有nav,默认都present
- // BOOL isNeedPush = [call.arguments[@"isNeedPush"] boolValue];
- BOOL isNeedPush = YES;
- NSNumber *failMode = call.arguments[@"nativeFailMode"];
- AlibcNativeFailMode nativeFailMode = [self NativeFailMode:[failMode intValue]];
- NSNumber *schemeType = call.arguments[@"schemeType"];
- NSString *linkKey = [self schemeType:[schemeType intValue]];
- NSString *url = call.arguments[@"url"];
- AlibcTradeTaokeParams *taokeParam = [self getTaokeParams:call];
- NSDictionary *trackParam = call.arguments[@"trackParam"];
- NSString *backUrl = [FlutterAlibcTools changeType:call.arguments[@"backUrl"]];
- // NSString *backUrl = [FlutterAlibcTools nullToNil:call.arguments[@"backUrl"]];
-
- UIViewController *rootViewController = [UIApplication sharedApplication].delegate.window.rootViewController;
-
-
- AlibcTradeShowParams* showParam = [[AlibcTradeShowParams alloc] init];
- showParam.openType = openType;
- showParam.isNeedCustomNativeFailMode = isNeedCustomNativeFailMode;
- showParam.isNeedPush=isNeedPush;
- showParam.nativeFailMode=nativeFailMode;
- showParam.linkKey=linkKey;
- showParam.backUrl= backUrl;
-
- ALiTradeWebViewController* webviewVC = [[ALiTradeWebViewController alloc] init];
-
- NSInteger res = [[AlibcTradeSDK sharedInstance].tradeService
- openByUrl:url
- identity:@"trade"
- webView:webviewVC.webView
- parentController:rootViewController
- showParams:showParam
- taoKeParams:taokeParam
- trackParam:trackParam tradeProcessSuccessCallback:^(AlibcTradeResult * _Nullable alibcTradeResult) {
- // // 交易成功,判断是付款成功还是加入购物车
- // if(alibcTradeResult.result == AlibcTradeResultTypePaySuccess){
- // // 付款成功
- // result(@{
- // FlutterAlibcConstKey_ErrorCode:@"0",
- // FlutterAlibcConstKey_ErrorMessage:@"付款成功",
- // FlutterAlibcConstKey_Data:@{
- // @"type":@0,
- // @"paySuccessOrders":[alibcTradeResult payResult].paySuccessOrders,
- // @"payFailedOrders":[alibcTradeResult payResult].payFailedOrders,
- // }
- // });
- // }else if(alibcTradeResult.result== AlibcTradeResultTypeAddCard){
- // // 加入购物车
- // result(@{
- // FlutterAlibcConstKey_ErrorCode:@"0",
- // FlutterAlibcConstKey_ErrorMessage:@"加入购物车成功",
- // FlutterAlibcConstKey_Data:@{
- // @"type":@1,
- // }
- // });
- // }
- } tradeProcessFailedCallback:^(NSError * _Nullable error) {
- // result(@{
- // FlutterAlibcConstKey_ErrorCode:[NSString stringWithFormat: @"%ld", (long)error.code],
- // FlutterAlibcConstKey_ErrorMessage:[error localizedDescription],
- // });
- }];
-
- if (res == 1) {
- // 新建一个view
- FlutterWxViewCtrlViewController *WxVC = [[FlutterWxViewCtrlViewController alloc] init];
- WxVC.vc = webviewVC;
- WxVC.accessBlock = ^(NSString * accessToken){
- NSLog(@"accessToken = %@",accessToken);
- if (accessToken) {
- result(@{
- @"accessToken":accessToken
- });
- }else{
- result(@{
- @"accessToken":@""
- });
- }
-
- };
- UINavigationController *root = [[UINavigationController alloc] initWithRootViewController:WxVC];
- [rootViewController presentViewController:root animated:NO completion:^{
-
- }];
- }
- }
- #pragma mark --退出登录
- - (void)loginOut{
- [[ALBBSDK sharedInstance] logout];
- }
- #pragma mark --通过url打开,包括h5,唤起手淘等
- - (void)openByUrl:(FlutterMethodCall *)call result:(FlutterResult)result{
- // 需要获取的数据
- NSNumber *type1 = call.arguments[@"openType"];
- AlibcOpenType openType = [self openType:[type1 intValue]];
- BOOL isNeedCustomNativeFailMode = [call.arguments[@"isNeedCustomNativeFailMode"] boolValue];
- // 不用push了,没有nav,默认都present
- // BOOL isNeedPush = [call.arguments[@"isNeedPush"] boolValue];
- BOOL isNeedPush = NO;
- NSNumber *failMode = call.arguments[@"nativeFailMode"];
- AlibcNativeFailMode nativeFailMode = [self NativeFailMode:[failMode intValue]];
- NSNumber *schemeType = call.arguments[@"schemeType"];
- NSString *linkKey = [self schemeType:[schemeType intValue]];
- NSString *url = call.arguments[@"url"];
- AlibcTradeTaokeParams *taokeParam = [self getTaokeParams:call];
- NSDictionary *trackParam = call.arguments[@"trackParam"];
- NSString *backUrl = [FlutterAlibcTools changeType:call.arguments[@"backUrl"]];
- // NSString *backUrl = [FlutterAlibcTools nullToNil:call.arguments[@"backUrl"]];
-
- UIViewController *rootViewController = [UIApplication sharedApplication].delegate.window.rootViewController;
-
-
- AlibcTradeShowParams* showParam = [[AlibcTradeShowParams alloc] init];
- showParam.openType = openType;
- showParam.isNeedCustomNativeFailMode = isNeedCustomNativeFailMode;
- showParam.isNeedPush=isNeedPush;
- showParam.nativeFailMode=nativeFailMode;
- showParam.linkKey=linkKey;
- showParam.backUrl= backUrl;
- [[AlibcTradeSDK sharedInstance].tradeService openByUrl:url identity:@"trade" webView:nil parentController:rootViewController showParams:showParam taoKeParams:taokeParam trackParam:trackParam tradeProcessSuccessCallback:^(AlibcTradeResult * _Nullable alibcTradeResult) {
- // 交易成功,判断是付款成功还是加入购物车
- if(alibcTradeResult.result == AlibcTradeResultTypePaySuccess){
- // 付款成功
- result(@{
- FlutterAlibcConstKey_ErrorCode:@"0",
- FlutterAlibcConstKey_ErrorMessage:@"付款成功",
- FlutterAlibcConstKey_Data:@{
- @"type":@0,
- @"paySuccessOrders":[alibcTradeResult payResult].paySuccessOrders,
- @"payFailedOrders":[alibcTradeResult payResult].payFailedOrders,
- }
- });
- }else if(alibcTradeResult.result== AlibcTradeResultTypeAddCard){
- // 加入购物车
- result(@{
- FlutterAlibcConstKey_ErrorCode:@"0",
- FlutterAlibcConstKey_ErrorMessage:@"加入购物车成功",
- FlutterAlibcConstKey_Data:@{
- @"type":@1,
- }
- });
- }
- } tradeProcessFailedCallback:^(NSError * _Nullable error) {
- result(@{
- FlutterAlibcConstKey_ErrorCode:[NSString stringWithFormat: @"%ld", (long)error.code],
- FlutterAlibcConstKey_ErrorMessage:[error localizedDescription],
- // android没有,直接去掉
- // FlutterAlibcConstKey_Data:@{
- // @"orderIdList":[[error userInfo] objectForKey:@"orderIdList"],
- // }
- });
- }];
- }
- #pragma mark --打开商品详情
- - (void)openItemDetail:(FlutterMethodCall *)call result:(FlutterResult)result{
- NSString *itemID = call.arguments[@"itemID"];
- id<AlibcTradePage> page = [AlibcTradePageFactory itemDetailPage:itemID];
- [self OpenPageByNewWay:page BizCode:@"detail" Call:call callback:result];
- }
- #pragma mark --打开店铺
- - (void)openShop:(FlutterMethodCall *)call result:(FlutterResult)result{
- NSString *shopId = call.arguments[@"shopId"];
- id<AlibcTradePage> page = [AlibcTradePageFactory shopPage:shopId];
- [self OpenPageByNewWay:page BizCode:@"shop" Call:call callback:result];
- }
- #pragma mark --打开我的购物车
- - (void)openCart:(FlutterMethodCall *)call result:(FlutterResult)result{
- id<AlibcTradePage> page = [AlibcTradePageFactory myCartsPage];
- [self OpenPageByNewWay:page BizCode:@"cart" Call:call callback:result];
- }
-
- #pragma mark --淘客打点
- - (void)syncForTaoke:(FlutterMethodCall *)call result:(FlutterResult)result{
- BOOL isSync = [call.arguments[@"isSync"] boolValue];
- [[AlibcTradeSDK sharedInstance] setIsSyncForTaoke:isSync];
- }
- #pragma mark --设置是否需要 Native AliPay 接口
- - (void)useAlipayNative:(FlutterMethodCall *)call result:(FlutterResult)result{
- BOOL isNeed = [call.arguments[@"isNeed"] boolValue];
- [[AlibcTradeSDK sharedInstance] setShouldUseAlizfNative:isNeed];
- }
- #pragma mark - 不对flutter暴露
- #pragma mark --打开page
- - (void)OpenPageByNewWay:(id<AlibcTradePage>)page BizCode:(NSString *)bizCode Call:(FlutterMethodCall *)call callback:(FlutterResult)callback{
- AlibcTradeShowParams* showParam = [[AlibcTradeShowParams alloc] init];
- NSNumber *type1 = call.arguments[@"openType"];
- AlibcOpenType openType = [self openType:[type1 intValue]];
- BOOL isNeedCustomNativeFailMode = [call.arguments[@"isNeedCustomNativeFailMode"] boolValue];
- // 不用push了,没有nav,默认都present
- // BOOL isNeedPush = [call.arguments[@"isNeedPush"] boolValue];
- BOOL isNeedPush = NO;
- // 不用绑定了,默认为没有,有的话flutter太难搞了
- // BOOL isBindWebview = [call.arguments[@"isBindWebview"] boolValue];
- NSNumber *failMode = call.arguments[@"nativeFailMode"];
- AlibcNativeFailMode nativeFailMode = [self NativeFailMode:[failMode intValue]];
- NSNumber *schemeType = call.arguments[@"schemeType"];
- NSString *linkKey = [self schemeType:[schemeType intValue]];
- AlibcTradeTaokeParams *taokeParam = [self getTaokeParams:call];
- NSDictionary *trackParam = [FlutterAlibcTools nullToNil:call.arguments[@"trackParam"]];
-
- NSString *backUrl = [FlutterAlibcTools changeType:call.arguments[@"backUrl"]];
- // 判断
-
- UIViewController *rootViewController = [UIApplication sharedApplication].delegate.window.rootViewController;
-
- showParam.openType = openType;
- showParam.isNeedPush = isNeedPush;
- showParam.nativeFailMode = nativeFailMode;
- showParam.isNeedCustomNativeFailMode = isNeedCustomNativeFailMode;
- showParam.linkKey = linkKey;
- showParam.backUrl = backUrl;
-
-
- // if (isBindWebview) {
- // ALiTradeWebViewController *viewCtrl = [[ALiTradeWebViewController alloc]init];
- // NSInteger res = [[AlibcTradeSDK sharedInstance].tradeService openByBizCode:bizCode page:page webView:view.webView parentController:view showParams:showParam taoKeParams:[self taokeParam] trackParam:[self customParam] tradeProcessSuccessCallback:self.onTradeSuccess tradeProcessFaelseiledCallback:self.onTradeFailure];
- // }else{
- // if (isNeedPush) {
- // [[AlibcTradeSDK sharedInstance].tradeService openByBizCode:bizCode page:page webView:nil parentController:rootViewController showParams:showParam taoKeParams:taokeParam trackParam:trackParam tradeProcessSuccessCallback:^(AlibcTradeResult * _Nullable result) {
- //// 交易成功,判断是付款成功还是加入购物车
- // if(result.result == AlibcTradeResultTypePaySuccess){
- //// 付款成功
- // callback(@{
- // FlutterAlibcConstKey_ErrorCode:@"0",
- // FlutterAlibcConstKey_ErrorMessage:@"付款成功",
- // FlutterAlibcConstKey_Data:@{
- // @"type":@0,
- // @"paySuccessOrders":[result payResult].paySuccessOrders,
- // @"payFailedOrders":[result payResult].payFailedOrders,
- // }
- // });
- // }else if(result.result== AlibcTradeResultTypeAddCard){
- //// 加入购物车
- // callback(@{
- // FlutterAlibcConstKey_ErrorCode:@"0",
- // FlutterAlibcConstKey_ErrorMessage:@"付款成功",
- // FlutterAlibcConstKey_Data:@{
- // @"type":@1,
- // }
- // });
- // }
- // } tradeProcessFailedCallback:^(NSError * _Nullable error) {
- //// 退出交易流程
- // callback(@{
- // FlutterAlibcConstKey_ErrorCode:[NSString stringWithFormat: @"%ld", (long)error.code],
- // FlutterAlibcConstKey_ErrorMessage:[error localizedDescription],
- // FlutterAlibcConstKey_Data:@{
- // @"orderIdList":[[error userInfo] objectForKey:@"orderIdList"],
- // }
- // });
- // }];
- // }else{
- [[AlibcTradeSDK sharedInstance].tradeService openByBizCode:bizCode page:page webView:nil parentController:rootViewController showParams:showParam taoKeParams:taokeParam trackParam:trackParam tradeProcessSuccessCallback:^(AlibcTradeResult * _Nullable result) {
- // 交易成功,判断是付款成功还是加入购物车
- if(result.result == AlibcTradeResultTypePaySuccess){
- // 付款成功
- callback(@{
- FlutterAlibcConstKey_ErrorCode:@"0",
- FlutterAlibcConstKey_ErrorMessage:@"付款成功",
- FlutterAlibcConstKey_Data:@{
- @"type":@0,
- @"paySuccessOrders":[result payResult].paySuccessOrders,
- @"payFailedOrders":[result payResult].payFailedOrders,
- }
- });
- }else if(result.result== AlibcTradeResultTypeAddCard){
- // 加入购物车
- callback(@{
- FlutterAlibcConstKey_ErrorCode:@"0",
- FlutterAlibcConstKey_ErrorMessage:@"加入购物车成功",
- FlutterAlibcConstKey_Data:@{
- @"type":@1,
- }
- });
- }
- } tradeProcessFailedCallback:^(NSError * _Nullable error) {
- // 退出交易流程
- callback(@{
- FlutterAlibcConstKey_ErrorCode:[NSString stringWithFormat: @"%ld", (long)error.code],
- FlutterAlibcConstKey_ErrorMessage:[error localizedDescription],
- // Android没有,所以去掉
- // FlutterAlibcConstKey_Data:@{
- // @"orderIdList":[[error userInfo] objectForKey:@"orderIdList"],
- // }
- });
- }];
- // }
- //}
- }
- #pragma mark --设置淘客参数
- - (AlibcTradeTaokeParams *)getTaokeParams:(FlutterMethodCall *)call{
- AlibcTradeTaokeParams *taoke = [[AlibcTradeTaokeParams alloc] init];
- if (call.arguments[@"taoKeParams"] == nil || [call.arguments[@"taoKeParams"] isKindOfClass:[NSNull class]]) {
- return nil;
- }
- NSDictionary *taoKeParams = call.arguments[@"taoKeParams"];
- taoke.adzoneId = (taoKeParams[@"adzoneId"] == (id) [NSNull null]) ? nil : taoKeParams[@"adzoneId"];
- taoke.pid = (taoKeParams[@"pid"] == (id) [NSNull null]) ? nil : taoKeParams[@"pid"];
- //有adzoneId则pid失效
- taoke.unionId = (taoKeParams[@"unionId"] == (id) [NSNull null]) ? nil : taoKeParams[@"unionId"];
- taoke.subPid = (taoKeParams[@"subPid"] == (id) [NSNull null]) ? nil : taoKeParams[@"subPid"];
- // 必须是dic
- taoke.extParams = (taoKeParams[@"extParams"] == (id) [NSNull null]) ? nil : taoKeParams[@"extParams"];
-
- // 判断这玩意是什么格式,dic就直接赋值,jsoÏn需要转dic
- // if ([extParams isKindOfClass:[NSDictionary class]]) {
- // taoke.extParams = extParams;
- // }else{
- // 解析字符串
- // taoke.extParams = [FlutterAlibcTools dictionaryWithJsonString:extParams];
- // }
-
- return taoke;
- }
- #pragma mark - 转换
- #pragma mark --唤起端失败的策略转换
- -(AlibcNativeFailMode )NativeFailMode:(int)mode{
- AlibcNativeFailMode openType=AlibcNativeFailModeJumpH5;
- switch (mode) {
- case 0:
- openType=AlibcNativeFailModeJumpH5;
- break;
- case 1:
- openType=AlibcNativeFailModeJumpDownloadPage;
- break;
- case 2:
- openType=AlibcNativeFailModeNone;
- break;
- default:
- break;
- }
- return openType;
-
- }
- //唤起类型
- - (AlibcOpenType)openType:(int)mode{
-
- AlibcOpenType openType=AlibcOpenTypeAuto;
- switch (mode) {
- case 0:
- openType=AlibcOpenTypeAuto;
- break;
- case 1:
- openType=AlibcOpenTypeNative;
- break;
- default:
- break;
- }
- return openType;
- }
-
- //打开类型
- -(NSString*)schemeType:(int)mode{
- NSString *linkKey=@"tmall";
- switch (mode) {
- case 0:
- linkKey=@"tmall";
- break;
- case 1:
- linkKey=@"taobao";
- break;
- default:
- break;
- }
- return linkKey;
- }
-
-
-
-
-
-
-
-
-
-
-
-
- /**
- 可设置的参数
- 1.是否同步淘客打点
- 2.是否使用Native支付宝
- 3.是否使用淘客参数(是,需要设置如下参数)
- adzoneId
- pid
- //有adzoneId则pid失效
- unionId
- subPid
- extParams{
- sellerId
- taokeAppkey
- }
- 4.页面打开方式
- 是否唤端 Auto/Native
- 唤起目标应用 淘宝/天猫
- 是否以push的方式打开页面
- 是否绑定webview
- 是否自定义唤端失败策略(若是:H5,DownLoad,None)
- 5.跟踪参数 customParams自定义
- */
- @end
|