SDKInterface.m 5.84 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>
#import <KCFramework/KCManager.h>

@implementation SDKInterface : Interface


int mExchange;


/**
 初始化SDK
*/
- (void)SDKinit:(UIViewController *) control
{
    //必须继承
    [super SDKinit:control];
    
    [PoolConfig getInstance];
    NSLog(@"sdk chushihua");
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveLoginMessage:) name:@"KCLoginCallBack" object:nil];
    
    [[PoolChecker getInstance] sendNotice:PoolSDKNotificationInitSDK notiName:@"InitSuccess" statusCode:POOLSDK_NO_ERROR description:@"initSuccess" extendData:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(clickLoginoutCallBack:) name:@"clickLoginoutCallBack" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(KCLoginoutCallBack:) name:@"KCLoginoutCallBack" object:nil];
    
    KCManager *kc = [KCManager sharedInstance];
    [kc KCInit];
}

/**
 登录SDK
 */
- (void)SDKloginSDK
{
    NSLog(@"调用了LoginSDK");
    KCManager *kc = [KCManager sharedInstance];
    [kc kCLogin];
}


/**
 loginCheck
 gametoken = f380786eadd923dae20786f69cec037e;
 sessid = m81jhoh3lcrahurdikbllnq3i5;
 time = 1496837007;
 uid = qy257599;
 */
- (void)receiveLoginMessage:(NSNotification *)notification
{
    NSLog(@"loginchek");
    NSDictionary* userInfoDic = notification.userInfo;
    PoolLoginInfo *loginInfo = [PoolLoginInfo alloc];
    loginInfo.openId = [userInfoDic objectForKey:@"uid"];
    loginInfo.sign = [userInfoDic objectForKey:@"gametoken"];
    loginInfo.timestamp = [userInfoDic objectForKey:@"time"];
    loginInfo.other = [userInfoDic objectForKey:@"sessid"];
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [[PoolChecker getInstance] startCheck:loginInfo];
    });
}


- (void)KCLoginoutCallBack:(NSNotification *)notification
{
    NSLog(@"logout callback");
    [[PoolChecker getInstance] sendNotice:PoolSDKNotificationLogout notiName:@"logoutSuccess" statusCode:POOLSDK_NO_ERROR description:@"logoutSuccess" extendData:nil];
    
}

- (void)clickLoginoutCallBack:(NSNotification *)notification
{
    NSLog(@"click logout button");
    [self SDKlogoutSDK];
}

-(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)SDKpayWithPaymentInfo:(PoolCreateOrderInfo *)payInfo
{
    mExchange = [payInfo exchange].intValue;
    [super SDKpayWithPaymentInfo:payInfo];
}

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

- (void)SDKreport:(PoolReportInfo *)reportInfo
{
    NSLog(@"reportInfo");
    if ([REPORT_ENTER isEqualToString:[reportInfo reportType]]) {
        KCManager *kc = [KCManager sharedInstance];
        [kc KCRolemeberWithServerid:[reportInfo serverId] andServername:[reportInfo serverName] andRoleid:[reportInfo playerId] andRolename:[reportInfo playerName]
                       andRolelevel:[reportInfo playerLevel]];

    }
}

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

- (void)SDKlogoutSDK
{
    NSLog(@"切换账号");
    [[KCManager sharedInstance] KCLoginout];
}

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

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


/**
 @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 *serverId = [payInfo serverId];
    NSString *roleName = [payInfo playerName];
    NSString *queryId = [payInfo queryId];
    
    NSLog(@"myamount%d%@",[myamount intValue],@"111");
    
    
    NSString* amount = [[NSString alloc]initWithFormat:@"%d",[myamount intValue]];
    NSString* productId = [self getProductIdByAmount:[payInfo products] amount:amount];
    
    KCManager *kc = [KCManager sharedInstance];
    
    [kc KCPayWithBill:queryId andProductid:productId andAmount:amount andServerid:serverId andRolename:roleName andRolelevel:@"10" andRoleid:[payInfo playerId] andSubject:@"12" andExt:queryId];
    
    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];
    });
}
@end