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


@implementation SDKInterface : Interface

int mExchange;

/**
 初始化SDK
*/
- (void)SDKinit:(UIViewController *) control
{
    //必须继承
    [super SDKinit:control];
    
    [PoolConfig getInstance];
    
    NSString* appKey = [[PoolConfig getInstance]getValueByKey:@"appKey"];
    NSString* privateKey = [[PoolConfig getInstance]getValueByKey:@"privateKey"];
    NSString* appSecret = [[PoolConfig getInstance]getValueByKey:@"appSecret"];
    [[RSDKPlatform sharedRsdk]initWithAppKey:appKey appSecret:appSecret privateKey:privateKey setDelegate:self Debug:NO];
    
    [[PoolChecker getInstance] sendNotice:PoolSDKNotificationInitSDK notiName:@"InitSuccess" statusCode:POOLSDK_NO_ERROR description:@"initSuccess" extendData:nil];
}

/**
 登录SDK
 */
- (void)SDKloginSDK
{
    NSLog(@"调用了LoginSDK");
    [[RSDKPlatform sharedRsdk]loginWithCustomParams:@"custom"];
}


-(void)userCallBack:(NSMutableDictionary*)userInfo withCode:(kRSDK_UserCallBack_Code)code
{
    switch (code) {
        case RSDK_UserCallBackFail://登录失败
            [[PoolChecker getInstance] sendNotice:PoolSDKNotificationLogin notiName:@"loginFail" statusCode:POOLSDK_ERROR_LOGIN_FAILED description:@"loginFail" extendData:nil];
            break;
        case RSDK_AccountSwitchSuccess:
            NSLog(@"RSDK_AccountSwitchSuccess");
            //[self loginCheck:userInfo type:PoolSDKNotificationSwitchAccount];
            break;
        case RSDK_LoginSuccess://登录成功
            NSLog(@"loginCheck");
            [self loginCheck:userInfo type:0];
            break;
            
        case RSDK_LogoutSuccess://注销成功
            [[PoolChecker getInstance] sendNotice:PoolSDKNotificationLogout notiName:@"logoutSuccess" statusCode:POOLSDK_NO_ERROR description:@"logoutSuccess" extendData:nil];
            break;
        default:
            break;
    }
}

- (void)loginCheck:(NSMutableDictionary*)userInfo type:(int)type
{
    NSString *userId = [userInfo objectForKey:@"userId"];
    PoolLoginInfo* loginInfo = [[PoolLoginInfo alloc]init];
    [loginInfo setOpenId:userId];
    [loginInfo setTimestamp:[self getCurrentTimestamp]];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        if (type == PoolSDKNotificationSwitchAccount) {
            [[PoolChecker getInstance] startCheckDelegate:loginInfo type:PoolSDKNotificationSwitchAccount];
        }else{
            [[PoolChecker getInstance] startCheck:loginInfo];
        }
    });
}


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

-(BOOL)hasUserCenter
{
    return YES;
}

/**
 SDK
 */
- (void)SDKgotoUserCenter
{
    [[RSDKPlatform sharedRsdk] enterPlatform];
}

- (void)SDKreport:(PoolReportInfo *)reportInfo
{
    NSLog(@"reportInfo");
    if ([REPORT_CREATEROLE isEqualToString:[reportInfo reportType]]) {
        
    }else if ([[reportInfo reportType] isEqualToString:REPORT_ENTER]) {
        NSMutableDictionary *dicInfo = [[NSMutableDictionary alloc]init];
        [dicInfo setObject:[reportInfo playerId] forKey:@"gameUserId"];
        [dicInfo setObject:[reportInfo serverId] forKey:@"serverId"];
        [dicInfo setObject:[[NSString alloc]initWithFormat:@"%ld",[reportInfo roleCTime].longValue] forKey:@"registerTime"];
        [[RSDKPlatform sharedRsdk] trackEvent:RSDK_Analytics_SetGameUserInfo eventInfo:dicInfo];
    }
}

-(BOOL)hasLogout
{
    return YES;
}

- (void)SDKlogoutSDK
{
    NSLog(@"切换账号");
    [[RSDKPlatform sharedRsdk]logout];
}

//-(BOOL)hasSwitchAccount
//{
//    return YES;
//}
//
//-(void)switchAccount
//{
//    [[RSDKPlatform sharedRsdk]accountSwitch];
//}


- (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 = [payInfo productName];
    NSString *serverId = [payInfo serverId];
    NSString *roleId = [payInfo playerId];
    NSString *roleName = [payInfo playerName];
    int coinNum = myamount.intValue * payInfo.exchange.intValue;
    NSString *coinNumStr = [[NSString alloc]initWithFormat:@"%d",coinNum];
    NSLog(@"coinNum%@",coinNumStr);
    
    NSMutableDictionary *paymentInfo = [[NSMutableDictionary alloc] init];
    [paymentInfo setObject:productId forKey:@"Product_Id"];
    [paymentInfo setObject:myamount forKey:@"Product_Price"];
    [paymentInfo setObject:productName forKey:@"Product_Name"];
    [paymentInfo setObject:coinNumStr forKey:@"Coin_Num"];
    [paymentInfo setObject:serverId forKey:@"Server_Id"];
    [paymentInfo setObject:roleId forKey:@"Role_Id"];
    [paymentInfo setObject:roleName forKey:@"Role_Name"];
    [paymentInfo setObject:queryId forKey:@"private_data"];
    [[RSDKPlatform sharedRsdk]payWithProduct:paymentInfo];
}

- (void)payCallBack:(NSMutableDictionary *)productInfo withCode:(kRSDK_PayCallBack_Code)code
{
    NSLog(@"pay callback");
}


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

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [[RWrapperManager sharedInstance]application:application didFinishLaunchingWithOptions:launchOptions];
    return YES;
}

-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    [[RWrapperManager sharedInstance] application:application openURL:url
        sourceApplication:sourceApplication annotation:annotation];
    return YES;
}


@end