SDKInterface.m 13.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
//
//  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 "YS_Analytic.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]) {
        
        [[YS_Analytic defaultAanlytic] gameTimeWithuserId:[reportInfo openId] userName:[reportInfo playerName] zone:[reportInfo serverId]];
        
        [[YS_Analytic defaultAanlytic] roleLoginSuccessWithUserID:[reportInfo openId] userName:[reportInfo playerName] userZone:[reportInfo serverId]];
        
        [[YS_Analytic defaultAanlytic] accountLoginSuccessWithUserID:[reportInfo openId] accountType:kbAccountRegistered];
        
        [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]){
        
        [[YS_Analytic defaultAanlytic] setLevel:[[reportInfo playerLevel] intValue] ];
        
        TDGAAccount * account = [TDGAAccount setAccount:[reportInfo openId]];
        [account setLevel:[[reportInfo playerLevel] intValue]];
    }
    else if ([reportT isEqualToString:REPORT_CREATEROLE]){
        
        NSLog(@"reportSDK createRole");
        
        [TalkingDataAppCpa onCreateRole:[reportInfo playerName]];
        
        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:queryId//[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:queryId//[self acquireTimestamp]
                                       userInfo:queryId
                                   cpDefineInfo:queryId
                                      urlScheme:identifier
                                 ViewController:_gameViewController];
            
        }
        
    }];
    
    [[YS_Analytic defaultAanlytic] onChargeRequst:queryId
                                            iapId:productId
                                   currencyAmount:[myamount intValue]
                                     currencyType:@"cny"
                            virtualCurrencyAmount:[myamount intValue]*10
                                      paymentType:@"alipay"];
  
    
    int64_t delayInSeconds = 3.0;// 延迟的时间
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        NSLog(@"chage state");
        [[PoolChecker getInstance] setOnlyOneTimePay:false];
    });
}


-(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"];
    NSString* appId = [[PoolConfig getInstance]getValueByKey:@"appId"];
    
//    channelId = @"gamegsolch055";
//    gamegsKey = @"3756116169";
//    platName = @"gwios";
//    gameInfo = @"gamegsol009";
//    markert = @"appstore";
    
    [[sdk shareInstance]initWithChannelId:channelId
                                gamegsKey:gamegsKey
                                 platName:platName
                                 gameInfo:gameInfo
                              application:application
                              WithOptions:launchOptions
                                  markert:markert];
    
    [[YS_Analytic defaultAanlytic] onStart:appId withChannelId:channelId];
    
    
    
    NSString* talkingDataAppId = [[PoolConfig getInstance]getValueByKey:@"talkingDataAppId"];
    NSString* talkingDataChannelId = [[PoolConfig getInstance]getValueByKey:@"talkingDataChannelId"];
    
//    talkingDataAppId = @"B4A2620C7B2D41BDB727408AFD260FEB";
//    talkingDataChannelId = @"appstore";
    [TalkingDataGA onStart:talkingDataAppId withChannelId:talkingDataChannelId];
    
    
    
    NSString* adTrackingAppId = [[PoolConfig getInstance]getValueByKey:@"adTrackingAppId"];
    NSString* adTrackingChannelId = [[PoolConfig getInstance]getValueByKey:@"adTrackingChannelId"];
    
//    adTrackingAppId = @"DD96381D5307468B8FD7285303BBD468";
//    adTrackingChannelId = @"appstore";
    [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:@"userid"];
    [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{
    
    //[[YS_Analytic defaultAanlytic] onChargeSuccess:orderId];
    
    [TalkingDataAppCpa onPay:accountId
                 withOrderId:orderId
                  withAmount:amount*100
            withCurrencyType:currencyType
                 withPayType:payType];
    
//    TDGAVirtualCurrency onChargeRequst:<#(NSString *)#> iapId:<#(NSString *)#> currencyAmount:<#(double)#> currencyType:<#(NSString *)#> virtualCurrencyAmount:<#(double)#> paymentType:<#(NSString *)#>
    [TDGAVirtualCurrency onChargeSuccess:orderId];
}

@end