CNLiveUserInfo.m 9.05 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";

@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 (^)(NSError *))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)queryUserInfoWithAppId:(NSString *)appId appKey:(NSString *)appKey uid:(NSString *)uid srcUid:(NSString *)srcUid token:(NSString *)token 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],
                             @"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 (^)(NSError *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 (^)(NSError *))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 (^)(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],
                             @"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 (^)(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);
        
    }];
}

#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