CNLiveVerificationCode.m 5.86 KB
//
//  CNLiveVerificationCode.m
//  CNLiveUserSystemDemo
//
//  Created by iOS on 16/9/13.
//  Copyright © 2016年 zhulin. All rights reserved.
//

#import "CNLiveVerificationCode.h"
#import "CNLiveUserSystemTools.h"
#import "CNLiveUserNetworkManager.h"
#import "NSDictionary+Safety.h"

//快捷登录前发手机验证码
static  NSString *shortcutLoginTestURL = @"http://test.open.cnlive.com/openapi/api2/user/sendVerifyCode";
static  NSString *shortcutLoginFormalURL = @"https://api.cnlive.com/open/api2/user/sendVerifyCode";

//注册或修改手机号前发手机验证码
static  NSString *registerOrModifyTestURL = @"http://test.open.cnlive.com/openapi/api2/user/sendVerifyCodeForUnRegistered";
static  NSString *registerOrModifyFormalURL = @"https://api.cnlive.com/open/api2/user/sendVerifyCodeForUnRegistered";

//找回或修改密码前发手机验证码
static  NSString *passwordTestURL = @"http://test.open.cnlive.com/openapi/api2/user/sendVerifyCodeForRegistered";
static  NSString *passwordFormalURL = @"https://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 (^)(NSDictionary *))failure
{
    [CNLiveUserSystemTools getRealTimeString:^(NSString * _Nullable realTime, NSDate * _Nullable netDate) {
        NSString *timeString = realTime;
        
        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];
    }];
}

- (void)getVerificationCodeWithAppId:(NSString *)appId appKey:(NSString *)appKey type:(VerificationCodeType)type mobile:(NSString *)mobile countryCode:(NSString *)countryCode 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],
                                 @"mobile":[CNLiveUserSystemTools verify:mobile],
                                @"countryCode":countryCode, @"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 (^)(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"]) {
            
            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];

    }];
}

- (NSString *)getURL:(VerificationCodeType)type
{
    NSString *url;
    switch (type) {
        case VerificationCodeTypeShortcutLogin:
        {
            if ([CNLiveUserSystemTools isTestEnvironment]) {
                url = shortcutLoginTestURL;
            } else {
                url = shortcutLoginFormalURL;
            }
        }
            break;
        case VerificationCodeTypeRegister:
        {
            if ([CNLiveUserSystemTools isTestEnvironment]) {
                url = registerOrModifyTestURL;
            } else {
                url = registerOrModifyFormalURL;
            }
        }
            break;
        case VerificationCodeTypeRetrievePassword:
        {
            if ([CNLiveUserSystemTools isTestEnvironment]) {
                url = passwordTestURL;
            } else {
                url = passwordFormalURL;
            }
        }
            break;
            
        default:
            break;
    }
    return url;
}

@end