SDKInterface.m 11.6 KB
//
//  UnionInterface.m
//  PoolSdk
//
//  Created by winFan on 11/23/15.
//  Copyright (c) 2015 winFan. All rights reserved.
//
#include "SDKInterface.h"
#import "TalkingDataGA.h"
#import "TalkingDataAppCpa.h"
#import <UIKit/UIKit.h>

@interface SDKInterface()
{
    UIViewController* _gameViewController;
}
@end

@implementation SDKInterface : Interface


int mExchange;


/**
 初始化SDK
*/
- (void)SDKinit:(UIViewController *) control
{
    //必须继承
    [super SDKinit:control];
    
    _gameViewController = control;
    
    [PoolConfig getInstance];
    NSLog(@"sdk chushihua");
    
    [[PoolChecker getInstance] sendNotice:PoolSDKNotificationInitSDK notiName:@"InitSuccess" statusCode:POOLSDK_NO_ERROR description:@"initSuccess" extendData:nil];
    
    //设置代理
    [sdk shareInstance].myDelegate = self;
    //上报信息
    [[sdk shareInstance]sendUserInfo];
}

/**
 登录SDK
 */
- (void)SDKloginSDK
{
    NSLog(@"调用了LoginSDK");
    [[sdk shareInstance]login:_gameViewController];
}

/**
 支付 创建订单
 */
- (void)SDKpayWithPaymentInfo:(PoolCreateOrderInfo *)payInfo
{
    mExchange = [payInfo exchange].intValue;
    [super SDKpayWithPaymentInfo:payInfo];
}

/**
 SDK
 */
- (void)SDKgotoUserCenter
{
}

- (void)SDKreport:(PoolReportInfo *)reportInfo
{
    NSString *reportT = [reportInfo reportType];
    reportInfo.openId = [[PoolChecker getInstance] userId];
    if ([reportT isEqualToString:REPORT_ENTER]) {
        
        [TalkingDataAppCpa onLogin:[reportInfo openId]];
        
        TDGAAccount * account = [TDGAAccount setAccount:[reportInfo openId]];
        [account setAccountName:[reportInfo playerName]];
        [account setLevel:[[reportInfo playerLevel] intValue]];
        [account setGameServer:[reportInfo serverId]];
        
        NSLog(@"reportSDK enter");
        
    }
    else if ([reportT isEqualToString:REPORT_ROLEUPGRADE]){
        TDGAAccount * account = [TDGAAccount setAccount:[reportInfo openId]];
        [account setLevel:[[reportInfo playerLevel] intValue]];
    }
    else if ([reportT isEqualToString:REPORT_CREATEROLE]){
        
        NSLog(@"reportSDK createRole");
        
        TDGAAccount * account = [TDGAAccount setAccount:[reportInfo openId]];
        [account setAccountName:[reportInfo playerName]];
        [account setLevel:[[reportInfo playerLevel] intValue]];
        [account setGameServer:[reportInfo serverId]];
        
    }
    
    
    //TalkingDataAppCpa onPay:<#(NSString *)#> withOrderId:<#(NSString *)#> withAmount:<#(int)#> withCurrencyType:<#(NSString *)#> withPayType:<#(NSString *)#>
    
}


/**
 SDK
 */
- (void)SDKexitSDK
{
    
}

- (void)SDKlogoutSDK
{
    NSLog(@"切换账号");
    [[sdk shareInstance]logout:_gameViewController];
}

- (void)handleOpenurl:(NSURL *)url
{
}

- (void)receivePayMessage : (NSNotification *)notification;
{
    NSString *order_id = [notification object];
    NSLog(@"订单ID:%@", order_id);
}

/**
 @return 商品名称
 */
- (NSString*)getProductNameByAmount:(NSString*)products amount:(NSString*)amount
{
    NSData* jsonData = [NSJSONSerialization dataWithJSONObject:products options:NSJSONWritingPrettyPrinted error:nil];
    if(jsonData){
        NSDictionary* productList = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:nil];
        if(productList){
            NSEnumerator* keysList = [productList keyEnumerator];
            id keyValue;
            while (keyValue = [keysList nextObject])
            {
                NSString* appleAmount = [[productList objectForKey:keyValue] objectForKey:@"amount"];
                if(appleAmount.intValue == amount.intValue){
                     NSString* productName = [[productList objectForKey:keyValue] objectForKey:@"name"];
                    return productName;
                }
            }
        }
    }
    NSLog(@"get product name error amount:%@",amount);
    return @"元宝";
}

/**
 @return 商品ID
 */
- (NSString*)getProductIdByAmount:(NSString*)products amount:(NSString*)amount
{
    NSData* jsonData = [NSJSONSerialization dataWithJSONObject:products options:NSJSONWritingPrettyPrinted error:nil];
    if(jsonData){
        NSDictionary* productList = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:nil];
        if(productList){
            NSEnumerator* keysList = [productList keyEnumerator];
            id keyValue;
            while (keyValue = [keysList nextObject])
            {
                NSLog(@"i found %@",keyValue);
                NSString* appleAmount = [[productList objectForKey:keyValue] objectForKey:@"amount"];
                NSLog(@"appleAmount:%@ amount:%@",appleAmount,amount);
                if(appleAmount.intValue == amount.intValue){
                    return keyValue;
                }
            }
        }
    }
    return @"unknow";
}

- (void)receiveCreateOrder:(NSNotification *)notification
{
    NSLog(@"创建订单成功");
    PoolPayInfo *payInfo = [notification object];
    
    NSString *myamount = [payInfo postAmount];
    NSString *queryId = [payInfo queryId];
    NSString *productId = [self getProductIdByAmount:[payInfo products] amount:myamount];
    NSString *productName = [self getProductNameByAmount:[payInfo products] amount:myamount];
    
    //点击支付时调用
    [[sdk shareInstance]pay:^(NSString *wayStr) {
        
        //如果有且仅有一个支付方式 并且为苹果内购
        if ([wayStr isEqualToString:@"iap"]) {
            
            //调用苹果内购支付方式
            [[sdk shareInstance]jxIapWithProductId:productId
                                             price:myamount
                                       gameOrderId:[self acquireTimestamp]
                                    userInfoString:queryId
                                            cpInfo:queryId
                                          payOrder:^(NSString *payOrder) {
                
                NSLog(@"仅有内购时的订单号---%@",payOrder);
                
            } viewController:[UIApplication sharedApplication].keyWindow.rootViewController];
            
            [[sdk shareInstance]jxUpdatePayResult:^(NSString *result) {
                
                NSLog(@"支付结果回调");
            }];
            
            
        }else{
            
            NSString *identifier = [[NSBundle mainBundle] bundleIdentifier];
            [[sdk shareInstance]payViewGameName:@"青云诀"
                                     goodsTitle:productName
                                      priceText:myamount
                                         number:@"1"
                             productDescription:productName
                                    gameOrderId:[self acquireTimestamp]
                                       userInfo:queryId
                                   cpDefineInfo:queryId
                                      urlScheme:identifier
                                 ViewController:_gameViewController];
            
        }
        
    }];
}


-(NSString*)getCurrentTimestamp{
    // 时间戳转时间
    NSTimeInterval interval = [[NSDate date] timeIntervalSince1970] * 1000;
    NSString* timeStr = [[NSString alloc]initWithFormat:@"%.f",interval];
    return timeStr;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    NSString* channelId = [[PoolConfig getInstance]getValueByKey:@"channelId"];
    NSString* gamegsKey = [[PoolConfig getInstance]getValueByKey:@"gamegsKey"];
    NSString* platName = [[PoolConfig getInstance]getValueByKey:@"platName"];
    NSString* gameInfo = [[PoolConfig getInstance]getValueByKey:@"gameInfo"];
    NSString* markert = [[PoolConfig getInstance]getValueByKey:@"markert"];
    
    [[sdk shareInstance]initWithChannelId:channelId
                                gamegsKey:gamegsKey
                                 platName:platName
                                 gameInfo:gameInfo
                              application:application
                              WithOptions:launchOptions
                                  markert:markert];
    
    NSString* talkingDataAppId = [[PoolConfig getInstance]getValueByKey:@"talkingDataAppId"];
    NSString* talkingDataChannelId = [[PoolConfig getInstance]getValueByKey:@"talkingDataChannelId"];
    [TalkingDataGA onStart:talkingDataAppId withChannelId:talkingDataChannelId];
    NSString* adTrackingAppId = [[PoolConfig getInstance]getValueByKey:@"adTrackingAppId"];
    NSString* adTrackingChannelId = [[PoolConfig getInstance]getValueByKey:@"adTrackingChannelId"];
    [TalkingDataAppCpa init:adTrackingAppId withChannelId:adTrackingChannelId];
    
    return YES;
}

//登录
- (void)addObserver:(id)info loginResult:(resultType)loginResult
{
    NSLog(@"登录信息++++++++info:%@",info);
    NSLog(@"登录结果++++++++++type:%d",loginResult);
    
    NSString *sgkey = [info objectForKey:@"user_sgkey"];
    NSString *userid = [info objectForKey:@"userid"];
    
    PoolLoginInfo* loginInfo = [PoolLoginInfo alloc];
    [loginInfo setTimestamp:[self getCurrentTimestamp]];
    [loginInfo setOpenId:userid];
    [loginInfo setSign:sgkey];
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [[PoolChecker getInstance] startCheck:loginInfo];
    });
}
//注册
- (void)addObserver:(id)info registerResult:(resultType)registerResult
{
    NSString *account = [info objectForKey:@""];
    [TalkingDataAppCpa onRegister:account];
}
//转化正式账号
- (void)addObserver:(id)info conversionResult:(resultType)conversionResult
{
    
}
//游客是否为第一次登陆
- (void)visitorIsFirstLogin:(BOOL)firstLogin
{
    
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    [[sdk shareInstance] jXapplicationWillEnterForeground:application];
}

-(NSString *)acquireTimestamp
{
    NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval a=[dat timeIntervalSince1970];
    NSString *timeString = [NSString stringWithFormat:@"%.0f", a];//转为字符型
    return timeString;
}


- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(nullable NSString *)sourceApplication annotation:(id)annotation {
    [TalkingDataAppCpa onReceiveDeepLink:url];
    return YES;
}
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)options {
    [TalkingDataAppCpa onReceiveDeepLink:url];
    return YES;
}
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void(^)(NSArray * __nullable restorableObjects))restorationHandler {
    [TalkingDataAppCpa onReceiveDeepLink:userActivity.webpageURL];
    return YES;
}

/**
 *充值统计参数
 *accountId	账号ID,必填项, 和登陆、注册使用的accountId保持一致即可。
 *orderId	订单Id,必填项,请保证该值的全局唯一,系统会使用订单Id进行付费订单排重。
 *currencyAmount	货币金额,单位为元,支持小数
 *currencyType	货币类型
 *payType	支付类型(渠道)
 *custom    自定义扩展参数
 */
-(void)paymentSuccess:(NSString*)accountId orderId:(NSString*)orderId currencyAmount:(double)amount currencyType:(NSString*)currencyType payType:(NSString*)payType custom:(NSArray*)customArray{
    
    [TalkingDataAppCpa onPay:accountId withOrderId:orderId withAmount:amount withCurrencyType:currencyType withPayType:payType];
    
//    TDGAVirtualCurrency onChargeRequst:<#(NSString *)#> iapId:<#(NSString *)#> currencyAmount:<#(double)#> currencyType:<#(NSString *)#> virtualCurrencyAmount:<#(double)#> paymentType:<#(NSString *)#>
    [TDGAVirtualCurrency onChargeSuccess:orderId];
}

@end