SDKInterface.m 7.52 KB
#include "SDKInterface.h"
#import <UIKit/UIKit.h>

#import "AthenaSDK/AthenaAllManager.h"
#import "AthenaSDK/AthenaHuiTiaoNotification.h"
#import "AthenaSDK/AthenaMessage.h"

#import "TrackingIO.h"

@implementation SDKInterface : Interface

int mExchange;
NSString *AthenaSDKAppId;
NSString *AthenaSDKAppKey;

- (void)SDKinit:(UIViewController *) control
{

    [super SDKinit:control];
    
    [PoolConfig getInstance];
    [[AthenaAllManager AthenaInstance]AthenaChuShiHua];
    [[AthenaAllManager AthenaInstance]AthenaYangShi:@"sdk"];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveLoginMessage:) name:AthenaCKAssetsLoginNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(HCKAssetsToolCallBack:) name:@"AthenaCKAssetsToolCallBack" object:nil];
    
    [[PoolChecker getInstance] sendNotice:PoolSDKNotificationInitSDK notiName:@"InitSuccess" statusCode:POOLSDK_NO_ERROR description:@"initSuccess" extendData:nil];
    
    NSString* channelId = [[PoolConfig getInstance] getValueByKey:@"gamechannelid"];
    if(channelId == nil || [channelId length] <= 0){
        channelId = @"unknown";
    }
    [TrackingIO initWithappKey:[[PoolConfig getInstance] getValueByKey:@"renyunAppKey"] withChannelId:channelId];
}

/**
 登录SDK
 */
- (void)SDKloginSDK
{
    //APPID APPKEY
    AthenaSDKAppId = [[PoolConfig getInstance] getValueByKey:@"APP_ID"];
    AthenaSDKAppKey =[[PoolConfig getInstance] getValueByKey:@"APP_KEY"];
    [[AthenaAllManager AthenaInstance]AthenaDengLu:AthenaSDKAppId :AthenaSDKAppKey :self.controller];
    
}

/**
 loginCheck
 */
- (void)receiveLoginMessage:(NSNotification *)notification
{
    NSLog(@"loginchek");
    PoolLoginInfo *loginInfo = [PoolLoginInfo alloc];
    loginInfo.openId = [[AthenaMessage getInstance]openId];
    loginInfo.sign = [[AthenaMessage getInstance] token];
    loginInfo.timestamp = [[AthenaMessage getInstance] timestamp];
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [[PoolChecker getInstance] startCheck:loginInfo];
    });
    
}


-(NSString*)DataTojsonString:(id)object
{
    NSString *jsonString = nil;
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:object
                                                       options:NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string
                                                         error:&error];
    jsonString = [jsonData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
    return jsonString;
}

/**
 苹果支付结果回调
 */

- (void)HCKAssetsToolCallBack:(NSNotification *)notification
{
    NSDictionary* payResultDic = notification.object;
    
    NSString* receiptStr = [payResultDic objectForKey:@"AthenaCKAssetsToolResult"];
    NSString* queryId = [payResultDic objectForKey:@"query_id"];
    
    NSDictionary* receiptDic = [NSDictionary dictionaryWithObjectsAndKeys:receiptStr,@"receipt",queryId,@"query_id", nil];
    NSString* result = [[NetCenter getInstance] httpPostSyn:[PoolUtils createGzyazfUrl] :receiptDic];
    
    
    if([@"success" isEqualToString:result]){//确认订单成功
    }else{//失败
        NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
        NSMutableArray* queryIdArray = [userDefaults objectForKey:@"queryIds"];
        
        NSMutableArray* tempQueryIdArray = [[NSMutableArray alloc] initWithCapacity:10];
        
        if (queryIdArray) {
        }else{
            queryIdArray = [[NSMutableArray alloc]initWithCapacity:10];
        }
        
        for (NSString* arrayQueryId in queryIdArray) {
            [tempQueryIdArray addObject:arrayQueryId];
        }
        
        [tempQueryIdArray addObject:queryId];
        [userDefaults setObject:tempQueryIdArray forKey:@"queryIds"];
        [userDefaults setObject:receiptStr forKey:queryId];
        [userDefaults synchronize];
    }
}

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

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

- (void)SDKreport:(PoolReportInfo *)reportInfo
{
    NSLog(@"reportInfo");
    NSString* reportType = [reportInfo reportType];
    if (reportInfo) {
        if ([reportType isEqualToString:REPORT_ENTER]) {
            [TrackingIO setLoginWithAccountID:[reportInfo playerId]];
            
            dispatch_async(dispatch_get_global_queue(0, 0), ^{
                [self handlerLeakOrder];
            });
        }else if ([reportType isEqualToString:REPORT_CREATEROLE]){
            [TrackingIO setRegisterWithAccountID:[reportInfo playerId]];
        }
    }
}

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

- (void)SDKlogoutSDK
{
    NSLog(@"切换账号");
}

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

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

//补单处理
- (void) handlerLeakOrder{
    NSUserDefaults* userDefaults2 = [NSUserDefaults standardUserDefaults];
    NSMutableArray* queryIdArray = [userDefaults2 objectForKey:@"queryIds"];
    
    NSLog(@"orderDic%@",queryIdArray);
    NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
    NSMutableArray* failQueryIdArray = [[NSMutableArray alloc]initWithCapacity:10];
    
    if(queryIdArray){
        for (NSString* queryId in queryIdArray) {
            NSString* receiptStr = [userDefaults valueForKey:queryId];
            NSDictionary* receiptDic = [NSDictionary dictionaryWithObjectsAndKeys:receiptStr,@"receipt",queryId,@"query_id", nil];
            NSString* result = [[NetCenter getInstance] httpPostSyn:[PoolUtils createGzyazfUrl] :receiptDic];
            if([@"success" isEqualToString:result]){//成功
                [userDefaults removeObjectForKey:queryId];
            }else{//补单失败
                [failQueryIdArray addObject:queryId];
            }
        }
        
        if([failQueryIdArray count] > 0){//含有补单失败项
            [userDefaults setObject:failQueryIdArray forKey:@"queryIds"];
        }else{//全部补单成功
            [userDefaults removeObjectForKey:@"queryIds"];
        }
        [userDefaults synchronize];
    }
}


- (void)receiveCreateOrder:(NSNotification *)notification
{
    NSLog(@"创建订单成功");
    PoolGzyazfInfo *payInfo = [notification object];
    
    NSNumber *amount = @([[payInfo postAmount] integerValue] * mExchange);
    NSString *serverId = [payInfo serverId];
    NSString *roleName = [payInfo playerName];
    NSString *queryId = [payInfo queryId];
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [self handlerLeakOrder];
    });
    
    NSString *other = [payInfo other];
    
    if (other && [other length] > 0) {
        [[AthenaAllManager AthenaInstance] AthenaCKQuery:serverId :roleName :amount :[payInfo queryId] :self.controller :[payInfo serverName]];
    }else{
        NSString* products = [payInfo products];
        [[AthenaAllManager AthenaInstance] AthenaCKQueryOperation:self.controller AthenaHuoWuID:products AthenaJiaGe:[payInfo postAmount] AthenaHuoWuMing:[payInfo productName] AthenaDingDanHao:queryId];
    }
}

-(void)paymentSuccess:(NSString *)accountId orderId:(NSString *)orderId currencyAmount:(double)amount currencyType:(NSString *)currencyType payType:(NSString *)payType custom:(NSArray *)customArray
{
    [TrackingIO setPayment:orderId paymentType:payType currentType:currencyType currencyAmount:amount];
}

@end