CNLiveLogin.m
14.6 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
//
// CNLiveLogin.m
// CNLiveUserSystemDemo
//
// Created by iOS on 16/9/13.
// Copyright © 2016年 zhulin. All rights reserved.
//
#import "CNLiveLogin.h"
#import "CNLiveUserNetworkManager.h"
#import "CNLiveUserSystemTools.h"
#import "NSDictionary+Safety.h"
//快捷登录
static NSString *shortcutLoginTestURL = @"http://test.open.cnlive.com/openapi/api2/user/loginInQuickly";
static NSString *shortcutLoginFormalURL = @"https://api.cnlive.com/open/api2/user/loginInQuickly";
//登录
static NSString *loginTestURL = @"http://test.open.cnlive.com/openapi/api2/user/loginIn";
static NSString *loginFormalURL = @"https://api.cnlive.com/open/api2/user/loginIn";
//第三方登录
static NSString *thirdLoginTestURL = @"http://test.open.cnlive.com/openapi/api2/user/loginIn3rd";
static NSString *thirdLoginFormalURL = @"https://api.cnlive.com/open/api2/user/loginIn3rd";
//验证第三方账号是否注册,若注册则登录返回用户信息,若未注册则errorCode
static NSString *thirdCheckLoginTestURL = @"http://test.open.cnlive.com/openapi/api2/user/loginIn3rdHasMobile";
static NSString *thirdCheckLoginFormalURL = @"https://api.cnlive.com/open/api2/user/loginIn3rdHasMobile";
//第三方注册强制绑定手机号: 手机号+密码+验证码+三方账号
static NSString *thirdBindMobileLoginTestURL = @"http://test.open.cnlive.com/openapi/api2/user/loginIn3rdAndBindMobile";
static NSString *thirdBindMobileLoginFormalURL = @"https://api.cnlive.com/open/api2/user/loginIn3rdAndBindMobile";
@implementation CNLiveLogin
- (void)thirdPartyLoginWithAppId:(NSString *)appId appKey:(NSString *)appKey thirdPartyPlatType:(NSString *)plat thirdPartyId:(NSString *)thirdPartyId nickName:(NSString *)nickName gender:(NSString *)gender location:(NSString *)location faceUrl:(NSString *)faceUrl frmId:(NSString *)frmId success:(void (^)(id result))success failure:(void (^)(NSDictionary *))failure
{
[CNLiveUserSystemTools getRealTimeString:^(NSString * _Nullable realTime, NSDate * _Nullable netDate) {
NSString *timeString = realTime;
NSDictionary *params = @{@"appId":[CNLiveUserSystemTools verify:appId],
@"thirdPartyPlat":[CNLiveUserSystemTools verify:plat],
@"thirdPartyId":[CNLiveUserSystemTools verify:thirdPartyId],
@"nickName":[CNLiveUserSystemTools verify:nickName],
@"gender":[CNLiveUserSystemTools verify:gender],
@"location":[CNLiveUserSystemTools verify:location],
@"faceUrl":[CNLiveUserSystemTools verify:faceUrl],
@"uuid":[CNLiveUserSystemTools uidForStat:realTime],
@"frmId":[CNLiveUserSystemTools verify:frmId],
@"clientPlat":@"i",
@"platform_id":[NSBundle mainBundle].bundleIdentifier,
@"timestamp": timeString};
NSMutableDictionary *parameter = [NSMutableDictionary dictionaryWithDictionary:params];
NSString *string = [NSString stringWithFormat:@"%@&key=%@", [CNLiveUserSystemTools signvalue:parameter], [CNLiveUserSystemTools verify:appKey]];
NSString *signString = [[CNLiveUserSystemTools sha1:string] uppercaseString];
[parameter setObject:signString forKey:@"sign"];
NSString *url;
if ([CNLiveUserSystemTools isTestEnvironment]) {
url = thirdLoginTestURL;
} else {
url = thirdLoginFormalURL;
}
[self requestWithURL:url parameter:parameter success:success failure:failure];
}];
}
/**
* 快捷登录(旧)
*/
- (void)shortcutLoginWithAppId:(NSString *)appId appKey:(NSString *)appKey userName:(NSString *)userName verificationCode:(NSString *)verificationCode frmId:(NSString *)frmId success:(void (^)(id result))success failure :(void (^)(NSDictionary *errorDic))failure
{
[CNLiveUserSystemTools getRealTimeString:^(NSString * _Nullable realTime, NSDate * _Nullable netDate) {
NSString *timeString = realTime;
NSDictionary *params = @{@"appId":[CNLiveUserSystemTools verify:appId],
@"userName":[CNLiveUserSystemTools verify:userName],
@"verificationCode":[CNLiveUserSystemTools verify:verificationCode],
@"uuid":[CNLiveUserSystemTools uidForStat:realTime],
@"frmId":[CNLiveUserSystemTools verify:frmId],
@"clientPlat":@"i",
@"platform_id":[NSBundle mainBundle].bundleIdentifier,
@"timestamp": timeString};
NSMutableDictionary *parameter = [NSMutableDictionary dictionaryWithDictionary:params];
NSString *string = [NSString stringWithFormat:@"%@&key=%@", [CNLiveUserSystemTools signvalue:parameter], [CNLiveUserSystemTools verify:appKey]];
NSString *signString = [[CNLiveUserSystemTools sha1:string] uppercaseString];
[parameter setObject:signString forKey:@"sign"];
NSString *url;
if ([CNLiveUserSystemTools isTestEnvironment]) {
url = shortcutLoginTestURL;
} else {
url = shortcutLoginFormalURL;
}
[self requestWithURL:url parameter:parameter success:success failure:failure];
}];
}
/**
* 快捷登录(新)
*/
- (void)shortcutLoginWithAppId:(NSString *)appId appKey:(NSString *)appKey userName:(NSString *)userName countryCode:(NSString *)countryCode verificationCode:(NSString *)verificationCode frmId:(NSString *)frmId success:(void (^)(id result))success failure :(void (^)(NSDictionary *errorDic))failure
{
[CNLiveUserSystemTools getRealTimeString:^(NSString * _Nullable realTime, NSDate * _Nullable netDate) {
NSString *timeString = realTime;
NSDictionary *params = @{@"appId":[CNLiveUserSystemTools verify:appId],
@"userName":[CNLiveUserSystemTools verify:userName],
@"countryCode":countryCode,
@"verificationCode":[CNLiveUserSystemTools verify:verificationCode],
@"uuid":[CNLiveUserSystemTools uidForStat:realTime],
@"frmId":[CNLiveUserSystemTools verify:frmId],
@"clientPlat":@"i",
@"platform_id":[NSBundle mainBundle].bundleIdentifier,
@"timestamp": timeString};
NSMutableDictionary *parameter = [NSMutableDictionary dictionaryWithDictionary:params];
NSString *string = [NSString stringWithFormat:@"%@&key=%@", [CNLiveUserSystemTools signvalue:parameter], [CNLiveUserSystemTools verify:appKey]];
NSString *signString = [[CNLiveUserSystemTools sha1:string] uppercaseString];
[parameter setObject:signString forKey:@"sign"];
NSString *url;
if ([CNLiveUserSystemTools isTestEnvironment]) {
url = shortcutLoginTestURL;
} else {
url = shortcutLoginFormalURL;
}
[self requestWithURL:url parameter:parameter success:success failure:failure];
}];
}
/**
* 登录
*/
- (void)loginWithAppId:(NSString *)appId appKey:(NSString *)appKey userName:(NSString *)userName password:(NSString *)password frmId:(NSString *)frmId success:(void (^)(id result))success failure:(void (^)(NSDictionary *))failure
{
[CNLiveUserSystemTools getRealTimeString:^(NSString * _Nullable realTime, NSDate * _Nullable netDate) {
NSString *timeString = realTime;
NSDictionary *params = @{@"appId":[CNLiveUserSystemTools verify:appId],
@"userName":[CNLiveUserSystemTools verify:userName],
@"pwd":[CNLiveUserSystemTools verify:password],
@"uuid":[CNLiveUserSystemTools uidForStat:realTime],
@"frmId":[CNLiveUserSystemTools verify:frmId],
@"clientPlat":@"i",
@"platform_id":[NSBundle mainBundle].bundleIdentifier,
@"timestamp": timeString};
NSMutableDictionary *parameter = [NSMutableDictionary dictionaryWithDictionary:params];
NSString *string = [NSString stringWithFormat:@"%@&key=%@", [CNLiveUserSystemTools signvalue:parameter], [CNLiveUserSystemTools verify:appKey]];
NSString *signString = [[CNLiveUserSystemTools sha1:string] uppercaseString];
[parameter setObject:signString forKey:@"sign"];
NSString *url;
if ([CNLiveUserSystemTools isTestEnvironment]) {
url = loginTestURL;
} else {
url = loginFormalURL;
}
[self requestWithURL:url parameter:parameter success:success failure:failure];
}];
}
/**
* 验证第三方账号是否注册,若注册直接登录返回用户信息,若未注册返回errorCode
*/
- (void)thirdPartyCheckLoginWithAppId:(NSString *)appId appKey:(NSString *)appKey thirdPartyPlatType:(NSString *)plat thirdPartyId:(NSString *)thirdPartyId frmId:(NSString *)frmId success:(void (^)(id result))success failure :(void (^)(NSDictionary *errorDic))failure
{
[CNLiveUserSystemTools getRealTimeString:^(NSString * _Nullable realTime, NSDate * _Nullable netDate) {
NSDictionary *params = @{@"appId":[CNLiveUserSystemTools verify:appId],
@"thirdPartyPlat":[CNLiveUserSystemTools verify:plat],
@"thirdPartyId":[CNLiveUserSystemTools verify:thirdPartyId],
@"uuid":[CNLiveUserSystemTools uidForStat:realTime],
@"frmId":[CNLiveUserSystemTools verify:frmId],
@"clientPlat":@"i"
};
NSMutableDictionary *parameter = [NSMutableDictionary dictionaryWithDictionary:params];
NSString *string = [NSString stringWithFormat:@"%@&key=%@", [CNLiveUserSystemTools signvalue:parameter], [CNLiveUserSystemTools verify:appKey]];
NSString *signString = [[CNLiveUserSystemTools sha1:string] uppercaseString];
[parameter setObject:signString forKey:@"sign"];
NSString *url;
if ([CNLiveUserSystemTools isTestEnvironment]) {
url = thirdCheckLoginTestURL;
} else {
url = thirdCheckLoginFormalURL;
}
[self requestWithURL:url parameter:parameter success:success failure:failure];
}];
}
/**
* 第三方注册强制绑定手机号: 手机号+密码+验证码+三方账号
*/
- (void)thirdPartyBindMobileLoginWithAppId:(NSString *)appId appKey:(NSString *)appKey thirdPartyPlatType:(NSString *)plat thirdPartyId:(NSString *)thirdPartyId nickName:(NSString *)nickName gender:(NSString *)gender location:(NSString *)location faceUrl:(NSString *)faceUrl mobile:(NSString *)mobile verificationCode:(NSString *)verificationCode pawd:(NSString *)pawd frmId:(NSString *)frmId success:(void (^)(id result))success failure :(void (^)(NSDictionary *errorDic))failure
{
[CNLiveUserSystemTools getRealTimeString:^(NSString * _Nullable realTime, NSDate * _Nullable netDate) {
NSDictionary *params = @{@"appId":[CNLiveUserSystemTools verify:appId],
@"thirdPartyPlat":[CNLiveUserSystemTools verify:plat],
@"thirdPartyId":[CNLiveUserSystemTools verify:thirdPartyId],
@"nickName":[CNLiveUserSystemTools verify:nickName],
@"gender":[CNLiveUserSystemTools verify:gender],
@"location":[CNLiveUserSystemTools verify:location],
@"faceUrl":[CNLiveUserSystemTools verify:faceUrl],
@"uuid":[CNLiveUserSystemTools uidForStat:realTime],
// @"frmId":[CNLiveUserSystemTools verify:frmId],
@"mobile":[CNLiveUserSystemTools verify:mobile],
@"verificationCode":[CNLiveUserSystemTools verify:verificationCode],
@"pawd":[CNLiveUserSystemTools verify:pawd],
@"clientPlat":@"i"
};
NSMutableDictionary *parameter = [NSMutableDictionary dictionaryWithDictionary:params];
NSString *string = [NSString stringWithFormat:@"%@&key=%@", [CNLiveUserSystemTools signvalue:parameter], [CNLiveUserSystemTools verify:appKey]];
NSString *signString = [[CNLiveUserSystemTools sha1:string] uppercaseString];
[parameter setObject:signString forKey:@"sign"];
NSString *url;
if ([CNLiveUserSystemTools isTestEnvironment]) {
url = thirdBindMobileLoginTestURL;
} else {
url = thirdBindMobileLoginFormalURL;
}
[self requestWithURL:url parameter:parameter success:success failure:failure];
}];
}
#pragma mark - request
- (void)requestWithURL:(NSString *)URL parameter:(NSDictionary *)param success:(void(^)(id result))success failure :(void (^)(NSDictionary *errorDic))failure
{
[[CNLiveUserNetworkManager manager] requestPostURL:URL parameters:param success:^(NSURLResponse * _Nullable response, id _Nullable responseObject) {
responseObject = [responseObject validatedDictionary];
if ([[responseObject allKeys] containsObject:@"errorCode"] && [responseObject[@"errorCode"] isEqualToString:@"0"]) {
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:responseObject[@"data"][@"token"] forKey:@"CNLiveAuthToken"];
[userDefaults setObject:responseObject[@"data"][@"uid"] forKey:@"CNLiveUserSid"];
[userDefaults synchronize];
if (success) {
success(responseObject);
}
} else {
if (failure) {
failure(responseObject);
}
[CNLiveUserSystemTools errorStat:responseObject[@"errorMessage"]];
}
} failure:^(NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSDictionary *errorDic = @{@"errorCode": [NSString stringWithFormat:@"%ld", error.code], @"errorMessage": error.localizedDescription};
if (failure) {
failure(errorDic);
}
[CNLiveUserSystemTools errorStat:error.localizedDescription];
}];
}
@end