CNLiveLogin.m 10.8 KB
//
//  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 *shortcutLoginURL = @"https://api.cnlive.com/open/api2/user/loginInQuickly";

//登录
static  NSString *loginURL = @"https://api.cnlive.com/open/api2/user/loginIn";

//第三方登录
static  NSString *thirdLoginURL = @"https://api.cnlive.com/open/api2/user/loginIn3rd";

//验证第三方账号是否注册,若注册则登录返回用户信息,若未注册则errorCode
static  NSString *thirdCheckLoginURL = @"https://api.cnlive.com/open/api2/user/loginIn3rdHasMobile4server";

//第三方注册强制绑定手机号: 手机号+密码+验证码+三方账号
static  NSString *thirdBindMobileLoginURL = @"https://api.cnlive.com/open/api2/user/loginIn3rdAndBindMobile4server";


@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
{
    NSDate *date = [NSDate date];
    NSInteger time = [date timeIntervalSince1970];
    NSString *timeString = [NSString stringWithFormat:@"%ld", (long)time];
    
    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:[NSDate date]],
                             @"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"];
    [self requestWithURL:thirdLoginURL 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
{
    NSDate *date = [NSDate date];
    NSInteger time = [date timeIntervalSince1970];
    NSString *timeString = [NSString stringWithFormat:@"%ld", (long)time];
        
    NSDictionary *params = @{@"appId":[CNLiveUserSystemTools verify:appId],
                             @"userName":[CNLiveUserSystemTools verify:userName],
                             @"verificationCode":[CNLiveUserSystemTools verify:verificationCode],
                             @"uuid":[CNLiveUserSystemTools uidForStat:[NSDate date]],
                             @"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"];
    [self requestWithURL:shortcutLoginURL 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
{
    NSDate *date = [NSDate date];
    NSInteger time = [date timeIntervalSince1970];
    NSString *timeString = [NSString stringWithFormat:@"%ld", (long)time];
    
    NSDictionary *params = @{@"appId":[CNLiveUserSystemTools verify:appId],
                             @"userName":[CNLiveUserSystemTools verify:userName],
                             @"pwd":[CNLiveUserSystemTools verify:password],
                             @"uuid":[CNLiveUserSystemTools uidForStat:[NSDate date]],
                             @"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"];
    [self requestWithURL:loginURL 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
{
    NSDictionary *params = @{@"appId":[CNLiveUserSystemTools verify:appId],
                             @"thirdPartyPlat":[CNLiveUserSystemTools verify:plat],
                             @"thirdPartyId":[CNLiveUserSystemTools verify:thirdPartyId],
                             @"uuid":[CNLiveUserSystemTools uidForStat:[NSDate date]],
                             @"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"];
    [self requestWithURL:thirdCheckLoginURL 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
{
    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:[NSDate date]],
//                             @"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"];
    [self requestWithURL:thirdBindMobileLoginURL 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