CNLiveVerificationCode.m
2.94 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
//
// CNLiveVerificationCode.m
// CNLiveUserSystemDemo
//
// Created by iOS on 16/9/13.
// Copyright © 2016年 zhulin. All rights reserved.
//
#import "CNLiveVerificationCode.h"
#import "CNLiveUserSystemTools.h"
#import "CNLiveNetworkManager.h"
#import "NSDictionary+Safety.h"
//快捷登录前发手机验证码
static NSString *shortcutLoginURL = @"http://api.cnlive.com/open/api2/user/sendVerifyCode";
//注册或修改手机号前发手机验证码
static NSString *registerOrModifyURL = @"http://api.cnlive.com/open/api2/user/sendVerifyCodeForUnRegistered";
//找回或修改密码前发手机验证码
static NSString *passwordURL = @"http://api.cnlive.com/open/api2/user/sendVerifyCodeForRegistered";
@implementation CNLiveVerificationCode
- (void)getVerificationCodeWithAppId:(NSString *)appId appKey:(NSString *)appKey type:(VerificationCodeType)type mobile:(NSString *)mobile success:(void (^)(id result))success failure:(void (^)(NSError *))failure
{
NSDate *date = [NSDate date];
NSInteger time = [date timeIntervalSince1970];
NSString *timeString = [NSString stringWithFormat:@"%ld", (long)time];
NSDictionary *params = @{@"appId":[CNLiveUserSystemTools verify:appId],
@"mobile":[CNLiveUserSystemTools verify:mobile],
@"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"];
[self requestWithURL:[self getURL:type] parameter:parameter success:success failure:failure];
}
#pragma mark - request
- (void)requestWithURL:(NSString *)URL parameter:(NSDictionary *)param success:(void(^)(id result))success failure :(void (^)(NSError * error))failure
{
[[CNLiveNetworkManager manager] requestPostURL:URL parameters:param success:^(NSURLResponse * _Nullable response, id _Nullable responseObject) {
responseObject = [responseObject validatedDictionary];
success(responseObject);
} failure:^(NSURLResponse * _Nullable response, NSError * _Nullable error) {
failure(error);
}];
}
- (NSString *)getURL:(VerificationCodeType)type
{
NSString *url;
switch (type) {
case VerificationCodeTypeShortcutLogin:
url = shortcutLoginURL;
break;
case VerificationCodeTypeRegister:
url = registerOrModifyURL;
break;
case VerificationCodeTypeRetrievePassword:
url = passwordURL;
break;
default:
break;
}
return url;
}
@end