ModifyUserInfoViewController.m 7.27 KB
//
//  ModifyUserInfoViewController.m
//  CNLiveUserSystemDemo
//
//  Created by 朱林 on 16/9/17.
//  Copyright © 2016年 zhulin. All rights reserved.
//

#import "ModifyUserInfoViewController.h"
#import "RegistBasicInformationView.h"

@interface ModifyUserInfoViewController ()

@property (nonatomic, strong)UITextField *textField1;
@property (nonatomic, strong)UIButton *saveBtn;
@property (nonatomic, strong)RegistBasicInformationView *registBasicInformationView;
@property (nonatomic, strong)UIButton *verificationBtn;

@end

@implementation ModifyUserInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    if ([self.title isEqualToString:@"更新手机号"]) {
        [self.view addSubview:self.registBasicInformationView];
        [self.view addSubview:self.verificationBtn];
    } else {
        [self.view addSubview:self.textField1];
    }
    
    [self.view addSubview:self.saveBtn];
}

#pragma mark - action

- (void)saveBtnAction
{
    [SVProgressHUD show];
    
    UserInfoModel *model = [UserInfoModel modelWithKeyValues:[Tools getLocalUserInfo]];

    if ([self.title isEqualToString:@"更新手机号"]) {
        [CNLiveUserSystemCenter updateMobileWithUid:model.uid mobile:_registBasicInformationView.phoneTF.text verificationCode:_registBasicInformationView.verificationTF.text success:^(id result) {
            
            NSLog(@"-----------%@",result);
            if ([[result allKeys] containsObject:@"errorCode"] && [result[@"errorCode"] isEqualToString:@"0"]) {
                [SVProgressHUD showSuccessWithStatus:@"手机号修改成功"];
                [self.navigationController popViewControllerAnimated:YES];
                
            } else {
                [Tools toast:result[@"errorMessage"]];
                [SVProgressHUD dismiss];
            }
            
        } failure:^(NSError *error) {
            [Tools toast:error.localizedDescription];
            [SVProgressHUD dismiss];
        }];
    } else {
        [CNLiveUserSystemCenter modifyUserInfoType:_modifyType uid:model.uid modifyInfo:_textField1.text success:^(id result) {
            
            NSLog(@"-----------%@",result);
            if ([[result allKeys] containsObject:@"errorCode"] && [result[@"errorCode"] isEqualToString:@"0"]) {
                [SVProgressHUD showSuccessWithStatus:@"修改成功"];
                [self.navigationController popViewControllerAnimated:YES];
                
            } else {
                [Tools toast:result[@"errorMessage"]];
                [SVProgressHUD dismiss];
            }

        } failure:^(NSError *error) {
            [Tools toast:error.localizedDescription];
            [SVProgressHUD dismiss];

        }];
    }
    
}

- (void)verificationBtnAction
{
    [self startTimer];
    
    [CNLiveUserSystemCenter verificationCodeType:VerificationCodeTypeRegister mobile:_registBasicInformationView.phoneTF.text success:^(id result) {
        
        [Tools toast:result[@"errorMessage"]];

        
    } failure:^(NSError *error) {
        [Tools toast:error.localizedDescription];
    }];
}

- (void)startTimer
{
    __block int timeout = 60; //倒计时时间
    
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    
    dispatch_source_set_event_handler(_timer, ^{
        
        if(timeout<=0){ //倒计时结束,关闭
            
            dispatch_source_cancel(_timer);
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //设置界面的按钮显示 根据自己需求设置
                
                [self.verificationBtn setTitle:@"发送验证码" forState:UIControlStateNormal];
                
                self.verificationBtn.userInteractionEnabled = YES;
                
            });
            
        }else{
            
            //            int minutes = timeout / 60;
            
            int seconds = timeout % 60;
            
            NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //设置界面的按钮显示 根据自己需求设置
                
                [self.verificationBtn setTitle:[NSString stringWithFormat:@"%@秒",strTime] forState:UIControlStateNormal];
                self.verificationBtn.userInteractionEnabled = NO;
                
            });
            
            timeout--;
        }
        
    });
    
    dispatch_resume(_timer);
}


#pragma mark - getter

- (UITextField *)textField1
{
    if (!_textField1) {
        _textField1 = [[UITextField alloc] initWithFrame:CGRectMake(20, 70, KScreenWidth-40, 50)];
        _textField1.autocorrectionType = UITextAutocorrectionTypeNo;
        _textField1.autocapitalizationType = UITextAutocapitalizationTypeNone;
        _textField1.placeholder = @"请输入更改信息";
        _textField1.textColor = [UIColor blackColor];
        _textField1.font = [UIFont systemFontOfSize:17];
        _textField1.clearButtonMode = UITextFieldViewModeAlways;
    }
    return _textField1;
}

- (UIButton *)saveBtn
{
    if (!_saveBtn) {
        _saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _saveBtn.backgroundColor = [UIColor orangeColor];
        [_saveBtn setFrame:CGRectMake(10, 250, KScreenWidth - 20, 40)];
        _saveBtn.layer.cornerRadius = 20;
        _saveBtn.layer.masksToBounds = YES;
        [_saveBtn setTitle:@"保  存" forState:UIControlStateNormal];
        [_saveBtn addTarget:self action:@selector(saveBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _saveBtn;
}

- (RegistBasicInformationView *)registBasicInformationView
{
    if (!_registBasicInformationView) {
            _registBasicInformationView = [[RegistBasicInformationView alloc] initWithFrame:CGRectMake(0, 70, KScreenWidth, 171) hasPwd:NO isPhone:YES];
    }
    return _registBasicInformationView;
}

- (UIButton *)verificationBtn
{
    if (!_verificationBtn) {
        _verificationBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_verificationBtn setFrame:CGRectMake(KScreenWidth - 105, 80, 95, 37)];
        _verificationBtn.backgroundColor = [UIColor orangeColor];
        _verificationBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [_verificationBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        [_verificationBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_verificationBtn addTarget:self action:@selector(verificationBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _verificationBtn;
}

- (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.
}
*/

@end