CNLiveThirdLoginManager.m
13.4 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
//
// CNLiveThirdLoginManager.m
// CNLiveUserSystemDemo
//
// Created by iOS on 16/9/19.
// Copyright © 2016年 zhulin. All rights reserved.
//
#import "CNLiveThirdLoginManager.h"
#import <TencentOpenAPI/TencentOAuth.h>
#import <TencentOpenAPI/QQApiInterfaceObject.h>
#import <TencentOpenAPI/QQApiInterface.h>
#define verificationCode @"cnlive"
@interface CNLiveThirdLoginManager ()<WXApiDelegate,TencentSessionDelegate,TencentLoginDelegate,WBHttpRequestDelegate,WeiboSDKDelegate>
{
TencentOAuth *tencentOAuth;
BOOL _isBindingThird;
}
@property (nonatomic, copy)NSString *appCode;
@property (nonatomic, copy)NSString *SId;
@property (nonatomic, copy)NSString *SSecret;
@property (nonatomic, copy)NSString *accessToken;
@end
@implementation CNLiveThirdLoginManager
+ (CNLiveThirdLoginManager *)manager
{
static CNLiveThirdLoginManager *share = nil;
static dispatch_once_t predicate;
dispatch_once(&predicate, ^{
share = [[self alloc] init];
});
return share;
}
- (BOOL)handleOpenURL:(NSURL *)url
{
if ([url.scheme isEqualToString:WeChatAppKey]) {
return [WXApi handleOpenURL:url delegate:self];
} else if ([url.scheme isEqualToString:QQAppKey]) {
return [TencentOAuth HandleOpenURL:url];
} else if ([url.scheme isEqualToString:[NSString stringWithFormat:@"wb%@", SinaAppKey]]) {
return [WeiboSDK handleOpenURL:url delegate:self];
} else if ([url.scheme isEqualToString:[NSString stringWithFormat:@"tencent%@", QQAppKey]]) {
[TencentOAuth HandleOpenURL:url];
return [QQApiInterface handleOpenURL:url delegate:self];
}
else {
return YES;
}
}
-(void)qqLoginIsBindingThirdParty:(BOOL)Binding
{
if (Binding) {
_isBindingThird = YES;
} else {
_isBindingThird = NO;
}
tencentOAuth = [[TencentOAuth alloc] initWithAppId:QQAppKey andDelegate:self];
NSArray *permissions = [NSArray arrayWithObjects:kOPEN_PERMISSION_GET_VIP_INFO, kOPEN_PERMISSION_GET_USER_INFO, kOPEN_PERMISSION_GET_SIMPLE_USER_INFO, nil];
[tencentOAuth authorize:permissions inSafari:NO];
}
- (void)weChatLoginIsBindingThirdParty:(BOOL)Binding
{
if (Binding) {
_isBindingThird = YES;
} else {
_isBindingThird = NO;
}
if ([WXApi isWXAppInstalled]) {
SendAuthReq* req = [[SendAuthReq alloc] init];
req.scope = @"snsapi_userinfo";
req.state = verificationCode;
[WXApi sendReq:req];
}
else {
NSLog(@"没安装微信");
}
}
- (void)sinaLoginIsBindingThirdParty:(BOOL)Binding
{
if (Binding) {
_isBindingThird = YES;
} else {
_isBindingThird = NO;
}
WBAuthorizeRequest *request = [WBAuthorizeRequest request];
request.redirectURI = kSinaRedirectURI;
request.scope = @"all";
request.userInfo = @{@"SSO_From": @"SendMessageToWeiboViewController",
@"Other_Info_1": [NSNumber numberWithInt:123],
@"Other_Info_2": @[@"obj1", @"obj2"],
@"Other_Info_3": @{@"key1": @"obj1", @"key2": @"obj2"}};
[WeiboSDK sendRequest:request];
}
#pragma mark -- TencentSessionDelegate
//登陆完成调用
- (void)tencentDidLogin
{
[tencentOAuth getUserInfo];
}
- (void)getUserInfoResponse:(APIResponse *)response
{
NSLog(@"%@",response.jsonResponse);
NSDictionary *result = response.jsonResponse;
if (_isBindingThird) {
UserInfoModel *model = [UserInfoModel modelWithKeyValues:[Tools getLocalUserInfo]];
[CNLiveUserSystemCenter bindingThirdPartyWithUid:model.uid thirdPartyPlatType:@"2" thirdPartyId:[tencentOAuth getUserOpenID] success:^(id result) {
NSLog(@"绑定成功----%@",result);
} failure:^(NSDictionary *errorDic) {
NSLog(@"绑定失败----%@",errorDic);
}];
} else {
NSString *gender;
if ([[result objectForKey:@"gender"] isEqualToString:@"女"]) {
gender = @"f";
} else if ([[result objectForKey:@"gender"] isEqualToString:@"男"]) {
gender = @"m";
} else {
gender = @"n";
}
[CNLiveUserSystemCenter thirdPartyLoginWithThirdPartyPlatType:@"2" thirdPartyId:[tencentOAuth getUserOpenID] nickName:[result objectForKey:@"nickname"] gender:gender location:[result objectForKey:@"city"] faceUrl:[result objectForKey:@"figureurl"] frmId:nil success:^(id result) {
NSLog(@"qq登录------%@",result);
[Tools setUserLogin:YES];
[SVProgressHUD showSuccessWithStatus:@"登录成功"];
[UserInfoSava saveUserinfoDic:result[@"data"]];
[[NSNotificationCenter defaultCenter] postNotificationName:ThirdLoginSuccess object:nil userInfo:nil];
} failure:^(NSDictionary *errorDic) {
[[NSNotificationCenter defaultCenter] postNotificationName:ThirdLoginFailure object:nil userInfo:nil];
}];
}
}
//非网络错误导致登录失败:
-(void)tencentDidNotLogin:(BOOL)cancelled
{
NSLog(@"tencentDidNotLogin");
if (cancelled){
}else{
}
}
// 网络错误导致登录失败:
-(void)tencentDidNotNetWork
{
NSLog(@"tencentDidNotNetWork");
}
#pragma mark - WXApiDelegate
- (void)onResp:(BaseResp *)resp
{
if([resp isKindOfClass:[SendAuthResp class]]) {
SendAuthResp *temp = (SendAuthResp*)resp;
if (temp.errCode == 0) {
// 用户已经授权
SendAuthResp *authResp = (SendAuthResp *)resp;
self.appCode = temp.code;
[self getWeChatUserInfo:authResp];
// }
}
else if (temp.errCode == -4) {
// 用户拒绝授权
NSLog(@"用户拒绝授权");
}
else if (temp.errCode == -2) {
// 用户取消
NSLog(@"用户取消");
}
}
}
- (void)getWeChatUserInfo:(SendAuthResp *)response
{
NSString *url =[NSString stringWithFormat:@"https://api.weixin.qq.com/sns/oauth2/access_token?appid=%@&secret=%@&code=%@&grant_type=authorization_code",WeChatAppKey,WeChatAppSecret,response.code];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSURL *zoneUrl = [NSURL URLWithString:url];
NSString *zoneStr = [NSString stringWithContentsOfURL:zoneUrl encoding:NSUTF8StringEncoding error:nil];
NSData *data = [zoneStr dataUsingEncoding:NSUTF8StringEncoding];
dispatch_async(dispatch_get_main_queue(), ^{
if (data) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
if ([[dic allKeys] containsObject:@"errcode"]) {
[[NSNotificationCenter defaultCenter] postNotificationName:ThirdLoginFailure object:nil userInfo:nil];
}
else {
[self getUserInfoWithAccessTocken:[dic objectForKey:@"access_token"] OpenID:[dic objectForKey:@"openid"]];
}
}
});
});
}
- (void)getUserInfoWithAccessTocken:(NSString *)accessTocken OpenID:(NSString *)openID
{
NSString *getUserInfoUrl =[NSString stringWithFormat:@"https://api.weixin.qq.com/sns/userinfo?access_token=%@&openid=%@", accessTocken, openID];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSURL *zoneUrl = [NSURL URLWithString:getUserInfoUrl];
NSString *zoneStr = [NSString stringWithContentsOfURL:zoneUrl encoding:NSUTF8StringEncoding error:nil];
NSData *data = [zoneStr dataUsingEncoding:NSUTF8StringEncoding];
dispatch_async(dispatch_get_main_queue(), ^{
if (data) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
if (_isBindingThird) {
UserInfoModel *model = [UserInfoModel modelWithKeyValues:[Tools getLocalUserInfo]];
[CNLiveUserSystemCenter bindingThirdPartyWithUid:model.uid thirdPartyPlatType:@"3" thirdPartyId:dic[@"openid"] success:^(id result) {
NSLog(@"绑定成功----%@",result);
} failure:^(NSDictionary *errorDic) {
NSLog(@"绑定失败----%@",errorDic);
}];
} else {
if ([[dic allKeys] containsObject:@"errcode"]) {
[[NSNotificationCenter defaultCenter] postNotificationName:ThirdLoginFailure object:nil userInfo:nil];
} else {
NSString *getGender = [dic objectForKey:@"sex"];
NSString *gender;
switch ([getGender intValue]) {
case 0:
{
gender = @"n";
}
break;
case 1:
{
gender = @"m";
}
break;
case 2:
{
gender = @"f";
}
break;
default:
break;
}
[CNLiveUserSystemCenter thirdPartyLoginWithThirdPartyPlatType:@"3" thirdPartyId:[dic objectForKey:@"openid"] nickName:[dic objectForKey:@"nickname"] gender:gender location:[dic objectForKey:@"country"] faceUrl:[dic objectForKey:@"headimgurl"] frmId:nil success:^(id result) {
NSLog(@"微信登录------%@",result);
[Tools setUserLogin:YES];
[SVProgressHUD showSuccessWithStatus:@"登录成功"];
[UserInfoSava saveUserinfoDic:result[@"data"]];
[[NSNotificationCenter defaultCenter] postNotificationName:ThirdLoginSuccess object:nil userInfo:nil];
} failure:^(NSDictionary *errorDic) {
[[NSNotificationCenter defaultCenter] postNotificationName:ThirdLoginFailure object:nil userInfo:nil];
}];
}
}
}
});
});
}
#pragma mark - sinaDelegate
- (void)didReceiveWeiboRequest:(WBBaseRequest *)request
{
}
- (void)didReceiveWeiboResponse:(WBBaseResponse *)response
{
if ([response isKindOfClass:WBAuthorizeResponse.class])
{
if ([response isKindOfClass:WBAuthorizeResponse.class])
{
self.accessToken = [response.userInfo objectForKey:@"access_token"];
[self getUserInfoWithUId:[response.userInfo objectForKey:@"uid"] accessToken:[response.userInfo objectForKey:@"access_token"]];
}
}
}
- (void)getUserInfoWithUId:(NSString *)uid accessToken:(NSString *)token
{
NSString *urlStr = [NSString stringWithFormat:@"https://api.weibo.com/2/users/show.json?uid=%@&access_token=%@&source=%@", uid, token, SinaAppKey];
[WBHttpRequest requestWithURL:urlStr httpMethod:@"GET" params:nil delegate:self withTag:@"userinfo"];
}
#pragma mark - WBHttpRequestDelegate
- (void)request:(WBHttpRequest *)request didFinishLoadingWithDataResult:(NSData *)data
{
if ([request.tag isEqualToString:@"userinfo"]) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
if (_isBindingThird) {
UserInfoModel *model = [UserInfoModel modelWithKeyValues:[Tools getLocalUserInfo]];
[CNLiveUserSystemCenter bindingThirdPartyWithUid:model.uid thirdPartyPlatType:@"1" thirdPartyId:[dic objectForKey:@"id"] success:^(id result) {
NSLog(@"绑定成功----%@",result);
} failure:^(NSDictionary *errorDic) {
NSLog(@"绑定失败----%@",errorDic);
}];
} else {
NSString *gender;
if ([dic objectForKey:@"gender"]) {
gender = [dic objectForKey:@"gender"];
} else {
gender = @"n";
}
[CNLiveUserSystemCenter thirdPartyLoginWithThirdPartyPlatType:@"1" thirdPartyId:[dic objectForKey:@"id"] nickName:[dic objectForKey:@"name"] gender:gender location:[dic objectForKey:@"location"] faceUrl:[dic objectForKey:@"avatar_hd"] frmId:nil success:^(id result) {
NSLog(@"微博登录------%@",result);
[Tools setUserLogin:YES];
[SVProgressHUD showSuccessWithStatus:@"登录成功"];
[UserInfoSava saveUserinfoDic:result[@"data"]];
[[NSNotificationCenter defaultCenter] postNotificationName:ThirdLoginSuccess object:nil userInfo:nil];
} failure:^(NSDictionary *errorDic) {
[[NSNotificationCenter defaultCenter] postNotificationName:ThirdLoginFailure object:nil userInfo:nil];
}];
}
}
}
@end