CNLiveUserUpdateViewController.m 16.5 KB
//
//  CNLiveUserUpdateViewController.m
//  CNLiveScioLive
//
//  Created by CNLive-zxw on 2018/9/25.
//  Copyright © 2018年 CNLive. All rights reserved.
//

#import "CNLiveUserUpdateViewController.h"
#import "CNLiveRegisterViewController.h"
#import "CNLiveForgotPasswordViewController.h"

#import "CNLiveLoginInputView.h"
#import "CNLiveThirdLoginView.h"
#import "ZXWUserModel.h"

@interface CNLiveUserUpdateViewController (){
    BOOL _hasGetVerification;

}
@property (nonatomic, strong) CNLiveLoginInputView *account;
@property (nonatomic, strong) CNLiveLoginInputView *pwd;
@end

@implementation CNLiveUserUpdateViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    _hasGetVerification = NO;
    [self setUpNavigationBar];
    [self setUpSubControllers];
    
    // Do any additional setup after loading the view.
}
#pragma mark - UI
- (void)setUpNavigationBar{
    
    self.navigationController.navigationBar.barTintColor = Color_Blue;
    UIButton *headerButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 20, 44)];
    headerButton.layer.cornerRadius = 2.0;
    headerButton.layer.masksToBounds = true;
    [headerButton setImage:[UIImage imageNamed:@"cnlive_white_back"] forState:UIControlStateNormal];
    [headerButton addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:headerButton];
    self.navigationItem.title = self.navTitle;
    
}
- (void)setUpSubControllers{
    UIButton *sureButton = [[UIButton alloc] init];
    sureButton.tag = 10000;
    sureButton.layer.cornerRadius = 5.0;
    sureButton.layer.masksToBounds = true;
    [sureButton addTarget:self action:@selector(clicekdButton:) forControlEvents:UIControlEventTouchUpInside];
    [sureButton setTitle:CustomStr(@"Sure") forState:UIControlStateNormal];
    [sureButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    sureButton.titleLabel.font = [UIFont systemFontOfSize:17];
    sureButton.backgroundColor = Color_Blue;
    [self.view addSubview:sureButton];
    if([self.navTitle isEqualToString:CustomStr(@"UpdateNickname")]){
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
         NSString *name = [userDefaults valueForKey:@"user_name"];
        _account = [[CNLiveLoginInputView alloc] initWithPic:@"cnlive_phone_email" placeholder:name&&![name isEqualToString:@""]?name:CustomStr(@"Nickname") type:CNLiveButtonTypeNormal];
        _account.textField.text = name&&![name isEqualToString:@""]?name:CustomStr(@"Nickname");
        [self.view addSubview:_account];
        [_account mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view.mas_top).with.offset(15);
            make.left.equalTo(self.view.mas_left).with.offset(0);
            make.right.equalTo(self.view.mas_right).with.offset(0);
            make.height.offset(40);
            
        }];
        [sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self->_account.mas_bottom).with.offset(20);
            make.left.equalTo(self.view.mas_left).with.offset(10);
            make.right.equalTo(self.view.mas_right).with.offset(-10);
            make.height.offset(40);
            
        }];
    }
    else if([self.navTitle isEqualToString:CustomStr(@"UpdateLoginPassword")]){
        _account = [[CNLiveLoginInputView alloc] initWithPic:@"cnlive_password" placeholder:CustomStr(@"PleaseEnterYourOldLoginPassword") type:CNLiveButtonTypeNormal];
        [self.view addSubview:_account];
        [_account mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view.mas_top).with.offset(15);
            make.left.equalTo(self.view.mas_left).with.offset(0);
            make.right.equalTo(self.view.mas_right).with.offset(0);
            make.height.offset(40);
            
        }];
        
        _pwd = [[CNLiveLoginInputView alloc] initWithPic:@"cnlive_password" placeholder:CustomStr(@"PleaseEnterYourNewLoginPassword") type:CNLiveButtonTypeTextEntry];
        [self.view addSubview:_pwd];
        [_pwd mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self->_account.mas_bottom).with.offset(15);
            make.left.equalTo(self.view.mas_left).with.offset(0);
            make.right.equalTo(self.view.mas_right).with.offset(0);
            make.height.offset(40);
            
        }];
        [sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self->_pwd.mas_bottom).with.offset(20);
            make.left.equalTo(self.view.mas_left).with.offset(10);
            make.right.equalTo(self.view.mas_right).with.offset(-10);
            make.height.offset(40);
            
        }];
    }
    else if([self.navTitle isEqualToString:CustomStr(@"BindingPhoneNumber")]){
        _account = [[CNLiveLoginInputView alloc] initWithPic:@"cnlive_phone_email" placeholder:CustomStr(@"PhoneNumber") type:CNLiveButtonTypeNormal];
        [self.view addSubview:_account];
        [_account mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view.mas_top).with.offset(15);
            make.left.equalTo(self.view.mas_left).with.offset(0);
            make.right.equalTo(self.view.mas_right).with.offset(0);
            make.height.offset(40);
            
        }];
        if([User_Defaults objectForKey:@"user_phone"] && [User_Defaults objectForKey:@"user_phone"] != nil && ![[User_Defaults objectForKey:@"user_phone"] isEqualToString:@""]){
            _account.textField.text = [User_Defaults objectForKey:@"user_phone"];
        }
        
        _pwd = [[CNLiveLoginInputView alloc] initWithPic:@"cnlive_phone_message" placeholder:CustomStr(@"Code") type:CNLiveButtonTypeCode];
        __weak typeof(self) weakSelf = self;
        _pwd.block = ^{
            [weakSelf getVerification];
        };
        [self.view addSubview:_pwd];
        [_pwd mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self->_account.mas_bottom).with.offset(15);
            make.left.equalTo(self.view.mas_left).with.offset(0);
            make.right.equalTo(self.view.mas_right).with.offset(0);
            make.height.offset(40);
            
        }];
        [sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self->_pwd.mas_bottom).with.offset(20);
            make.left.equalTo(self.view.mas_left).with.offset(10);
            make.right.equalTo(self.view.mas_right).with.offset(-10);
            make.height.offset(40);
            
        }];
    }
    else if([self.navTitle isEqualToString:CustomStr(@"BindingEmail")]){
        _account = [[CNLiveLoginInputView alloc] initWithPic:@"cnlive_email" placeholder:CustomStr(@"Email") type:CNLiveButtonTypeNormal];
        [self.view addSubview:_account];
        [_account mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view.mas_top).with.offset(15);
            make.left.equalTo(self.view.mas_left).with.offset(0);
            make.right.equalTo(self.view.mas_right).with.offset(0);
            make.height.offset(40);
            
        }];
        if([User_Defaults objectForKey:@"user_email"] && [User_Defaults objectForKey:@"user_email"] != nil && ![[User_Defaults objectForKey:@"user_email"] isEqualToString:@""]){
            _account.textField.text = [User_Defaults objectForKey:@"user_email"];
        }
        [sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self->_account.mas_bottom).with.offset(20);
            make.left.equalTo(self.view.mas_left).with.offset(10);
            make.right.equalTo(self.view.mas_right).with.offset(-10);
            make.height.offset(40);
            
        }];
    }
    
    
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */
#pragma mark - Action
- (void)allResignFirstResponder{
    [_account.textField resignFirstResponder];
    [_pwd.textField resignFirstResponder];
    
}
- (void)clicekdButton:(UIButton *)btn{
    btn.userInteractionEnabled = NO;
    [self updateRequest:btn];
}

- (void)updateRequest:(UIButton *)btn{
    [self allResignFirstResponder];
    if (![[Reachability reachabilityForInternetConnection] currentReachabilityStatus]) {
        [AlertViewTool showHUDViewText:CustomStr(@"NetworkiInterruption")];
        btn.userInteractionEnabled = YES;
        return;
    }
    ZXWUserInformation *share = [ZXWUserInformation shareZXWUserInformation];
    NSString *uid = share.model.uid;
    
    if([self.navTitle isEqualToString:CustomStr(@"UpdateNickname")]){
#pragma mark - CNLiveUserSystemSDK 修改昵称
        NSString *unickname = share.model.nickname;
        if([unickname isEqualToString:_account.textField.text]){
            btn.userInteractionEnabled = YES;
            [self goBack];
            return;
        }
        [AlertViewTool showHUDView];
        [CNLiveUserSystemCenter modifyUserInfoType:UserInfoTypeNickName uid:uid modifyInfo:_account.textField.text success:^(NSDictionary *result) {
            [AlertViewTool hideHUDView];
            if ([result[@"errorCode"] integerValue] == 0) {
                share.model.nickname = self->_account.textField.text;
                [share saveUser_name:self->_account.textField.text];
                [[NSNotificationCenter defaultCenter] postNotificationName:LoginSuccessful object:nil];
                
                [AlertViewTool showHUDViewText:CustomStr(@"UpdateNicknameSuccess") block:^{
                    btn.userInteractionEnabled = YES;
                    [self goBack];
                }];
                
            } else {
                [AlertViewTool showHUDViewText:CustomStr(@"UpdateNicknameFailure") block:^{
                    btn.userInteractionEnabled = YES;
                    [self goBack];
                }];
            }
        } failure:^(NSDictionary *errorDic) {
            btn.userInteractionEnabled = YES;
            [AlertViewTool hideHUDView];
            [AlertViewTool showHUDViewText:errorDic[@"errorMessage"]];
            
        }];
    }
    else if([self.navTitle isEqualToString:CustomStr(@"UpdateLoginPassword")]){
#pragma mark - CNLiveUserSystemSDK 修改密码
        NSString *password = [[NSUserDefaults standardUserDefaults] objectForKey:@"user_pwd"];
        if (![_account.textField.text isEqualToString:password]) {
            [AlertViewTool showHUDViewText:CustomStr(@"OldPasswordError")];
            btn.userInteractionEnabled = YES;
            return;
        }
        
        if(![_pwd.textField.text isValidPassWordLegal]){
            [AlertViewTool showHUDViewText:CustomStr(@"PasswordError")];
            btn.userInteractionEnabled = YES;
            return;
        }
       
        [AlertViewTool showHUDView];
        [CNLiveUserSystemCenter oldPwdChangePasswordWithUid:uid pwd:password newPwd:_pwd.textField.text success:^(id result) {
            [AlertViewTool hideHUDView];
            if ([result[@"errorCode"] integerValue] == 0) {
                [[ZXWUserInformation shareZXWUserInformation] loginOut];

                [AlertViewTool showHUDViewText:CustomStr(@"UpdatePasswordSuccess") block:^{
                    btn.userInteractionEnabled = YES;
                    [self.navigationController popToViewController:self.navigationController.childViewControllers[0] animated:YES];
                }];
            } else {
                [AlertViewTool showHUDViewText:CustomStr(@"UpdatePasswordFailure") block:^{
                    btn.userInteractionEnabled = YES;
                }];
            }
        } failure:^(NSDictionary *errorDic) {
            btn.userInteractionEnabled = YES;
            [AlertViewTool hideHUDView];
            [AlertViewTool showHUDViewText:errorDic[@"errorMessage"]];
            
        }];
    }
    else if([self.navTitle isEqualToString:CustomStr(@"BindingPhoneNumber")]){
#pragma mark - CNLiveUserSystemSDK 绑定手机号
        if(![_account.textField.text isValidMobile]){
            [AlertViewTool showHUDViewText:CustomStr(@"PhoneNumberError")];
            btn.userInteractionEnabled = YES;
            return;
        }
        if (!_hasGetVerification) {
            [AlertViewTool showHUDViewText:CustomStr(@"YouHaveNotGottenTheCaptchaYet")];
            btn.userInteractionEnabled = YES;
            return;
        }
        if(![_pwd.textField.text isValidCode]){
            [AlertViewTool showHUDViewText:CustomStr(@"CodeError")];
            btn.userInteractionEnabled = YES;
            return;
        }
        [AlertViewTool showHUDView];
        [CNLiveUserSystemCenter updateMobileWithUid:uid mobile:_account.textField.text verificationCode:_pwd.textField.text success:^(id result) {
            [AlertViewTool hideHUDView];
            if ([result[@"errorCode"] integerValue] == 0) {
                share.model.mobile = self->_account.textField.text;
                [share saveUser_phone:self->_account.textField.text];
                [AlertViewTool showHUDViewText:CustomStr(@"BindPhoneNumberSuccess") block:^{
                    btn.userInteractionEnabled = YES;
                    [self goBack];
                }];

            } else {
                [AlertViewTool showHUDViewText:CustomStr(@"BindPhoneNumberFailure") block:^{
                    btn.userInteractionEnabled = YES;
                    [self goBack];
                }];
                
            }
        } failure:^(NSDictionary *errorDic) {
            btn.userInteractionEnabled = YES;
            [AlertViewTool hideHUDView];
            [AlertViewTool showHUDViewText:errorDic[@"errorMessage"]];
            
        }];
        
    }
    else if([self.navTitle isEqualToString:CustomStr(@"BindingEmail")]){
#pragma mark - CNLiveUserSystemSDK 绑定邮箱
        if(![_account.textField.text isValidEmail]){
            [AlertViewTool showHUDViewText:CustomStr(@"EmailError")];
            btn.userInteractionEnabled = YES;
            return;
            
        }
        [AlertViewTool showHUDView];
        [CNLiveUserSystemCenter modifyUserInfoType:UserInfoTypeEmail uid:uid modifyInfo:_account.textField.text success:^(NSDictionary *result) {
            [AlertViewTool hideHUDView];
            if ([result[@"errorCode"] integerValue] == 0) {
                [share saveUser_email:self->_account.textField.text];
                [AlertViewTool showHUDViewText:CustomStr(@"BindEmailSuccess") block:^{
                    btn.userInteractionEnabled = YES;
                    [self goBack];
                }];
            } else {
                [AlertViewTool showHUDViewText:CustomStr(@"BindEmailFailure") block:^{
                    btn.userInteractionEnabled = YES;
                    [self goBack];
                }];
            }
        } failure:^(NSDictionary *errorDic) {
            btn.userInteractionEnabled = YES;
            [AlertViewTool hideHUDView];
            [AlertViewTool showHUDViewText:errorDic[@"errorMessage"]];
            
        }];
    }
    
    
}
#pragma mark - CNLiveUserSystemSDK 获取验证码
- (void)getVerification{
    [self allResignFirstResponder];
    if(![_account.textField.text isValidMobile]){
        [AlertViewTool showHUDViewText:CustomStr(@"PhoneNumberError")];
        return;
    }
    
    if (![[Reachability reachabilityForInternetConnection] currentReachabilityStatus]) {
        [AlertViewTool showHUDViewText:CustomStr(@"NetworkiInterruption")];
        return;
    }
    
    [AlertViewTool showHUDView];
    [CNLiveUserSystemCenter verificationCodeType:VerificationCodeTypeRegister mobile:_account.textField.text success:^(NSDictionary *result) {
        [AlertViewTool hideHUDView];
        if ([result[@"errorCode"] integerValue] == 0) {
            self->_hasGetVerification = YES;
            [[NSNotificationCenter defaultCenter] postNotificationName:GetCodeSuccessful object:nil];
            [AlertViewTool showHUDViewText:CustomStr(@"CodeSendingSuccessful")];
            
        } else {
            [AlertViewTool showHUDViewText:CustomStr(@"CodeSendingFailed")];

        }
    } failure:^(NSDictionary *errorDic) {
        [AlertViewTool hideHUDView];
        [AlertViewTool showHUDViewText:errorDic[@"errorMessage"]];

    }];

}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

@end