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

#import "CNLiveUserInfo.h"
#import "CNLiveNetworkManager.h"
#import "CNLiveUserSystemTools.h"
#import "NSDictionary+Safety.h"

//更新手机号
static  NSString *updateMobileURL       = @"https://api.cnlive.com/open/api2/user/updateMobileById";

//查询用户信息
static  NSString *queryUserInfoURL      = @"https://api.cnlive.com/open/api2/user/readUserById";

//修改用户单项信息
static  NSString *updateUserByIdURL     = @"https://api.cnlive.com/open/api2/user/updateUserById";

//更新用户附加信息
static  NSString *updateOtherInfoURL    = @"https://api.cnlive.com/open/api2/user/updateUserExtInfo";

//修改密码
static  NSString *updatePwdURL          = @"https://api.cnlive.com/open/api2/user/updatePwdByMobile";

//绑定第三方账号
static  NSString *BindingThirdURL       = @"https://api.cnlive.com/open/api2/user/bind3rd";


@implementation CNLiveUserInfo

/**
 *  更新手机号
 */
- (void)updateMobileWithAppId:(NSString *)appId appKey:(NSString *)appKey uid:(NSString *)uid mobile:(NSString *)mobile verificationCode:(NSString *)verificationCode token:(NSString *)token 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],
                             @"uid":[CNLiveUserSystemTools verify:uid],
                             @"mobile":[CNLiveUserSystemTools verify:mobile],
                             @"verificationCode":[CNLiveUserSystemTools verify:verificationCode],
                             @"clientPlat":@"i",
                             @"token":[CNLiveUserSystemTools verify:token],
                             @"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:updateMobileURL parameter:parameter success:success failure:failure];

}

/**
 *  绑定第三方账号
 */
- (void)bindingThirdPartyWithAppId:(NSString *)appId appKey:(NSString *)appKey uid:(NSString *)uid thirdPartyPlatType:(NSString *)plat thirdPartyId:(NSString *)thirdPartyId token:(NSString *)token success:(void (^)(id))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],
                             @"uid":[CNLiveUserSystemTools verify:uid],
                             @"thirdPartyPlat":[CNLiveUserSystemTools verify:plat],
                             @"thirdPartyId":[CNLiveUserSystemTools verify:thirdPartyId],
                             @"token":[CNLiveUserSystemTools verify:token],
                             @"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:BindingThirdURL parameter:parameter success:success failure:failure];
}

/**
 *  查询用户信息
 */
- (void)queryUserInfoWithAppId:(NSString *)appId appKey:(NSString *)appKey uid:(NSString *)uid srcUid:(NSString *)srcUid token:(NSString *)token 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],
                             @"uid":[CNLiveUserSystemTools verify:uid],
                             @"srcUid":[CNLiveUserSystemTools verify:srcUid],
                             @"clientPlat":@"i",
                             @"token":[CNLiveUserSystemTools verify:token],
                             @"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:queryUserInfoURL parameter:parameter success:success failure:failure];

}

/**
 *  修改用户单项信息
 */
- (void)modifyUserInfoWithAppId:(NSString *)appId appKey:(NSString *)appKey type:(UserInfoType)type uid:(NSString *)uid modifyInfo:(NSString *)info token:(NSString *)token success:(void (^)(id result))success failure :(void (^)(NSDictionary *error))failure
{
    
    NSDate *date = [NSDate date];
    NSInteger time = [date timeIntervalSince1970];
    NSString *timeString = [NSString stringWithFormat:@"%ld", (long)time];
    
    NSDictionary *params = @{@"appId":[CNLiveUserSystemTools verify:appId],
                             @"uid":[CNLiveUserSystemTools verify:uid],
                             @"type":[self updateType:type],
                             @"value":[CNLiveUserSystemTools verify:info],
                             @"clientPlat":@"i",
                             @"token":[CNLiveUserSystemTools verify:token],
                             @"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:updateUserByIdURL parameter:parameter success:success failure:failure];

}

/**
 *  更新用户附加信息
 */
- (void)updateOtherInfoWithAppId:(NSString *)appId appKey:(NSString *)appKey uid:(NSString *)uid extInfo:(NSString *)extInfo token:(NSString *)token 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],
                             @"token":[CNLiveUserSystemTools verify:token],
                             @"uid":[CNLiveUserSystemTools verify:uid],
                             @"extInfo":[CNLiveUserSystemTools verify:extInfo],
                             @"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:updateOtherInfoURL parameter:parameter success:success failure:failure];
}

/**
 *  修改密码
 */
- (void)changePasswordWithAppId:(NSString *)appId appKey:(NSString *)appKey mobile:(NSString *)mobile verificationCode:(NSString *)verificationCode newPassword:(NSString *)newPassword 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],
                             @"mobile":[CNLiveUserSystemTools verify:mobile],
                             @"verificationCode":[CNLiveUserSystemTools verify:verificationCode],
                             @"newPwd":[CNLiveUserSystemTools verify:newPassword],
                             @"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:updatePwdURL 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
{

    [[CNLiveNetworkManager 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];

    }];
}

#pragma mark - type

- (NSString *)updateType:(UserInfoType)updateType
{
    NSString *type;
    switch (updateType) {
        case UserInfoTypeNickName:
            type = @"nickName";
            break;
        case UserInfoTypeGender:
            type = @"gender";
            break;
        case UserInfoTypeEmail:
            type = @"email";
            break;
        case UserInfoTypeLocation:
            type = @"location";
            break;
        default:
            break;
    }
    return type;
}

@end