SDKInterface.m
12.7 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
//
// 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 <SXSDK/SXSDK.h>
@interface SDKInterface()
{
UIViewController* _gameViewController;
UIInterfaceOrientationMask _interfaceOrientationMask;
int userOpType ;//0为正常登录 1为切换账号
NSString *loginToken;
}
@end
@implementation SDKInterface : Interface
/**
初始化SDK
*/
- (void)SDKinit:(UIViewController *) control
{
//必须继承
[super SDKinit:control];
[PoolConfig getInstance];
_gameViewController = control;
NSString *ori = [[PoolConfig getInstance]getValueByKey:@"screenOrientation"];
if ([@"1" isEqualToString:ori]) {//1为竖屏 2为横屏
_interfaceOrientationMask = UIInterfaceOrientationMaskPortrait;
}else{
_interfaceOrientationMask = UIInterfaceOrientationMaskLandscape;
}
NSString * appID = [[PoolConfig getInstance]getValueByKey:@"sdkAppID"];
NSString * key = [[PoolConfig getInstance]getValueByKey:@"sdkKey"];
NSString * gameVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
NSString * gameID = [[PoolConfig getInstance]getValueByKey:@"sdkAppID"];
NSString * trackingKey = [[PoolConfig getInstance]getValueByKey:@"sdkTrackingKey"];
[[SXSDK shareInstance] initWithApplication:appID key:key version:gameVersion game:gameID trackingKey:trackingKey];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeOrientations:) name:@"shouldChangeOrientations" object:nil];
[[PoolChecker getInstance]setPoolSdkDelegate:self];
[[PoolChecker getInstance] sendNotice:PoolSDKNotificationInitSDK notiName:@"InitSuccess" statusCode:POOLSDK_NO_ERROR description:@"initSuccess" extendData:nil];
[[SXSDK shareInstance] setBlock:^{
userOpType = 0;
[[PoolChecker getInstance] sendNotice:PoolSDKNotificationLogout notiName:@"logoutSuccess" statusCode:POOLSDK_NO_ERROR description:@"logoutSuccess" extendData:nil];
} failCallback:^{
NSLog(@"callback blcok fail");
}];
}
-(void)changeOrientations:(NSNotification*)noti{
NSDictionary *dict = [NSDictionary dictionary];
dict = noti.userInfo;
_interfaceOrientationMask = [dict[@"interfaceOrientationMask"] integerValue];
}
- (NSUInteger) application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
return _interfaceOrientationMask;
}
- (NSString *)encodeParameter:(NSString *)originalPara {
CFStringRef encodeParaCf = CFURLCreateStringByAddingPercentEscapes(NULL, (__bridge CFStringRef)originalPara, NULL, CFSTR("!*'();:@&=+$,/?%#[]"), kCFStringEncodingUTF8);
NSString *encodePara = (__bridge NSString *)(encodeParaCf);
CFRelease(encodeParaCf);
return encodePara;
}
/**
*
**/
- (NSString*)urlEncode:(NSString*)data
{
NSCharacterSet *encodeUrlSet = [NSCharacterSet URLQueryAllowedCharacterSet];
NSString *encodeUrl = [data stringByAddingPercentEncodingWithAllowedCharacters:encodeUrlSet];
NSLog(@"encodeURL:%@",encodeUrl);
return encodeUrl;
}
/**
登录SDK
*/
- (void)SDKloginSDK
{
NSLog(@"调用了LoginSDK");
userOpType = 0;
[[SXSDK shareInstance] loginWithController:_gameViewController succCallback:^(NSDictionary *result) {
NSLog(@"[ 登录成功 ] %@", result);
//NSString *gameid = [result objectForKey:@"gameID"];
loginToken = [result objectForKey:@"data"];
//[self encodeParameter:data];
PoolLoginInfo* loginInfo = [[PoolLoginInfo alloc] init];
loginInfo.timestamp = [self getCurrentTimestamp];
[loginInfo setOpenId:[self encodeParameter:loginToken]];
[loginInfo setSign:[self encodeParameter:loginToken]];
dispatch_async(dispatch_get_global_queue(0, 0), ^{
if (userOpType == 0) {
[[PoolChecker getInstance] startCheck:loginInfo];
}else{
[[PoolChecker getInstance ]startCheckDelegate:loginInfo type:PoolSDKNotificationSwitchAccount];
}
});
} failCallback:^(NSString *message) {
NSLog(@"[ 登录失败 ] %@", message);
} exitCallback:^{
NSLog(@"[ 登录退出 ]");
}];
}
- (void)SDKreport:(PoolReportInfo *)reportInfo
{
NSLog(@"reportInfo");
if ([REPORT_CREATEROLE isEqualToString:[reportInfo reportType]]) {
//0
NSDictionary *data = [NSDictionary dictionaryWithObjectsAndKeys:
[reportInfo openId], @"uid",
[reportInfo playerId], @"roleID",
[reportInfo playerName], @"roleName",
[[PoolConfig getInstance]getValueByKey:@"sdkGameID"], @"gameID",
[reportInfo serverId], @"serverID",
[reportInfo serverName], @"serverName",
[reportInfo playerLevel], @"roleLevel",
[self getSDKTimestamp], @"timestamp",
@"0", @"dataType",
nil];
[[SXSDK shareInstance] achievement:data succCallback:^(NSDictionary *result) {
NSLog(@"[提交信息成功] %@", result);
} failCallback:^(NSDictionary *message) {
NSLog(@"[提交信息失败] %@", [message objectForKey:@"msg"]);
}];
}else if ([[reportInfo reportType] isEqualToString:REPORT_ENTER]) {
//1
NSDictionary *data = [[NSDictionary alloc]initWithObjectsAndKeys:
[reportInfo openId], @"uid",
[reportInfo playerId], @"roleID",
[reportInfo playerName], @"roleName",
[[PoolConfig getInstance]getValueByKey:@"sdkGameID"], @"gameID",
[reportInfo serverId], @"serverID",
[reportInfo serverName], @"serverName",
[reportInfo playerLevel], @"roleLevel",
[self getSDKTimestamp], @"timestamp",
@"1", @"dataType",
nil];
[[SXSDK shareInstance] achievement:data succCallback:^(NSDictionary *result) {
NSLog(@"[提交信息成功] %@", result);
} failCallback:^(NSDictionary *message) {
NSLog(@"[提交信息失败] %@", [message objectForKey:@"msg"]);
}];
}else if([[reportInfo reportType] isEqualToString:REPORT_ROLEUPGRADE]) {
//2
NSDictionary *data = [NSDictionary dictionaryWithObjectsAndKeys:
[reportInfo openId], @"uid",
[reportInfo playerId], @"roleID",
[reportInfo playerName], @"roleName",
[[PoolConfig getInstance]getValueByKey:@"sdkGameID"], @"gameID",
[reportInfo serverId], @"serverID",
[reportInfo serverName], @"serverName",
[reportInfo playerLevel], @"roleLevel",
[self getSDKTimestamp], @"timestamp",
@"2", @"dataType",
nil];
[[SXSDK shareInstance] achievement:data succCallback:^(NSDictionary *result) {
NSLog(@"[提交信息成功] %@", result);
} failCallback:^(NSDictionary *message) {
NSLog(@"[提交信息失败] %@", [message objectForKey:@"msg"]);
}];
}
}
-(BOOL)hasSwitchAccount
{
return YES;
}
- (void)switchAccount
{
NSLog(@"切换账号");
//退出,切换?
[[SXSDK shareInstance] switchAccount:^{
NSLog(@"switch account callback");
userOpType = 1;
}];
}
- (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)SDKpayWithPaymentInfo:(PoolCreateOrderInfo *)payInfo
{
NSString *notifyUrl = [PoolUtils createPayUrl];
NSLog(@"loginToken:%@",loginToken);
NSDictionary *payParameDic = [[NSDictionary alloc]initWithObjectsAndKeys:[self encodeParameter:loginToken],@"accessToken",notifyUrl,@"noticeUrl",[payInfo serverName],@"serverName",[payInfo playerName],@"roleName",[[PoolChecker getInstance]userId],@"openUID",[payInfo serverId],@"serverID", nil];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:payParameDic options:0 error:NULL];
NSString * otherInfo = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
otherInfo = [otherInfo stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
[payInfo setOtherInfo:otherInfo];
[super SDKpayWithPaymentInfo:payInfo];
}
- (void)receiveCreateOrder:(NSNotification *)notification
{
NSLog(@"创建订单成功");
PoolPayInfo *payInfo = [notification object];
[[SXSDK shareInstance] pay:[payInfo other]
succCallback:^(NSDictionary *result) {
//NSLog(@"[ 支付成功 ] %@", result);
}
failCallback:^(NSDictionary *message) {
//NSLog(@"[ 购买异常 ] %@", message);
}];
}
-(NSString*)getCurrentTimestamp{
// 时间戳转时间
NSTimeInterval interval = [[NSDate date] timeIntervalSince1970] * 1000;
NSString* timeStr = [[NSString alloc]initWithFormat:@"%.f",interval];
return timeStr;
}
-(NSString *)getSDKTimestamp {
NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
NSTimeInterval a=[dat timeIntervalSince1970];
NSInteger t = a;
NSString *timeString = [NSString stringWithFormat:@"%ld", (long)t];
return timeString;
}
-(void)PoolSdkLoginSuccess:(PoolLoginInfo*)loginInfo{
[[PoolChecker getInstance] sendNotice:PoolSDKNotificationLogin notiName:@"LoginSuccess" statusCode:POOLSDK_NO_ERROR description:@"LoginSucecess" extendData:loginInfo];
}
-(void)PoolSdkLoginFail:(NSString*)msg
{
[[PoolChecker getInstance] sendNotice:PoolSDKNotificationLogin notiName:@"LoginFail" statusCode:POOLSDK_ERROR_LOGIN_FAILED description:@"LoginFail" extendData:msg];
}
-(void)PoolSdkSwitchAccountSuccess:(PoolLoginInfo*)loginInfo
{
NSLog(@"switch success");
[[PoolChecker getInstance] sendNotice:PoolSDKNotificationSwitchAccount notiName:@"SwitchSuccess" statusCode:POOLSDK_NO_ERROR description:@"SwitchSucecess" extendData:loginInfo];
}
-(void)PoolSdkSwitchAccountFail:(NSString*)msg
{
[[PoolChecker getInstance] sendNotice:PoolSDKNotificationSwitchAccount notiName:@"SwitchFail" statusCode:POOLSDK_ERROR_SWICHT_ACCOUNT_FAILED description:@"SwitchFail" extendData:msg];
}
@end