BindMobileController.m 8.9 KB
//
//  BindMobileController.m
//  DreamSleep
//
//  Created by peter on 2022/9/24.
//

#import "BindMobileController.h"

@interface BindMobileController ()
@property (weak, nonatomic) IBOutlet UILabel *bindMobileLab;
@property (weak, nonatomic) IBOutlet UILabel *mobileLab;
@property (weak, nonatomic) IBOutlet UIImageView *mobileIcon;
@property (weak, nonatomic) IBOutlet UILabel *countryLab;
@property (weak, nonatomic) IBOutlet UIImageView *selectIcon;
@property (weak, nonatomic) IBOutlet UITextField *mobileTF;
@property (weak, nonatomic) IBOutlet UIView *mobileLine;
@property (weak, nonatomic) IBOutlet UILabel *verifyLab;
@property (weak, nonatomic) IBOutlet UITextField *verifyTF;
@property (weak, nonatomic) IBOutlet UIButton *getCodeBtn;
@property (weak, nonatomic) IBOutlet UIView *codeLine;
@property (weak, nonatomic) IBOutlet UIButton *bindBtn;
@property (weak, nonatomic) IBOutlet UILabel *tipsLab;
@property (weak, nonatomic) IBOutlet UILabel *timeLab;
@property (nonatomic, strong) NSTimer *countTimer;
@property (nonatomic, assign) int totalSecond;
@end

@implementation BindMobileController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initUI];
}

- (void)initUI {
    self.totalSecond = 60;
    self.naviBarAlpha = 1.0;
    self.naviBgColor = DSClearColor;
    self.view.dk_backgroundColorPicker = DKColorPickerWithKey(VCViewBG);
    [self.backBtn dk_setImage:DKImagePickerWithNames(@"cus_back_icon", @"sys_back_icon", @"sys_back_icon") forState:UIControlStateNormal];
    [self.dsNaviBar addSubview:self.backBtn];
    
    self.bindMobileLab.dk_textColorPicker = DKColorPickerWithKey(Dk_TITLE);
    self.mobileLab.dk_textColorPicker = DKColorPickerWithKey(Dk_TITLE);
    self.countryLab.dk_textColorPicker = DKColorPickerWithKey(Dk_TITLE);
    self.mobileTF.dk_textColorPicker = DKColorPickerWithKey(Dk_TITLE);
    UIColor *placeholderColor = [self.dk_manager.themeVersion isEqualToString:DKThemeVersionNormal] ? ColorFromHexA(0x333333, .5) : ColorFromHexA(0xE8E9E9, .5);
    NSDictionary *attributes = @{NSForegroundColorAttributeName:placeholderColor, NSFontAttributeName:SysFont(15)};
    self.mobileTF.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入手机号" attributes:attributes];
    self.verifyLab.dk_textColorPicker = DKColorPickerWithKey(Dk_TITLE);
    self.verifyLab.dk_textColorPicker = DKColorPickerWithKey(Dk_TITLE);
    self.verifyTF.dk_textColorPicker = DKColorPickerWithKey(Dk_TITLE);
    self.verifyTF.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入验证码" attributes:attributes];
    self.tipsLab.dk_textColorPicker = DKColorPickerWithColors(SmallTextColor, ColorFromHexA(0xFFFFFF, .3), DSWhite);
    self.timeLab.dk_textColorPicker = DKColorPickerWithColors(SubTitleColor, ColorFromHexA(0xFFFFFF, .5), DSWhite);
    
    self.mobileLine.dk_backgroundColorPicker = DKColorPickerWithColors(ColorFromHex(0xE6E6E6), AlertDarkColor, DSWhite);
    self.codeLine.dk_backgroundColorPicker = DKColorPickerWithColors(ColorFromHex(0xE6E6E6), AlertDarkColor, DSWhite);
    
    self.mobileIcon.dk_imagePicker = DKImagePickerWithNames(@"ic_bangding_shouji", @"dk_ic_bangding_shouji", @"ic_bangding_shouji");
    self.selectIcon.dk_imagePicker = DKImagePickerWithNames(@"ic_bangding_xiala", @"dk_ic_bangding_xiala", @"ic_bangding_xiala");
    
    self.bindBtn.dk_backgroundColorPicker = DKColorPickerWithKey(BrandBG);
    [self.bindBtn dk_setTitleColorPicker:DKColorPickerWithKey(Sub_Navi_TITLE) forState:UIControlStateNormal];
    
    [self.bindMobileLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(30);
        make.top.equalTo(self.view).offset(118);
    }];
    [self.mobileLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bindMobileLab);
        make.top.equalTo(self.bindMobileLab.mas_bottom).offset(40);
    }];
    [self.mobileIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mobileLab);
        make.top.equalTo(self.mobileLab.mas_bottom).offset(24);
    }];
    [self.countryLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mobileIcon.mas_right).offset(8);
        make.centerY.equalTo(self.mobileIcon);
    }];
    [self.selectIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.countryLab.mas_right).offset(4);
        make.centerY.equalTo(self.countryLab);
    }];
    [self.mobileTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.selectIcon.mas_right).offset(12);
        make.centerY.equalTo(self.selectIcon);
        make.right.equalTo(self.view).offset(-60);
    }];
    [self.mobileLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mobileIcon);
        make.right.equalTo(self.view).offset(-30);
        make.top.equalTo(self.mobileIcon.mas_bottom).offset(9);
        make.height.equalTo(@1);
    }];
    [self.verifyLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mobileLine);
        make.top.equalTo(self.mobileLine.mas_bottom).offset(24);
    }];
    [self.verifyTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.verifyLab);
        make.top.equalTo(self.verifyLab.mas_bottom).offset(24);
    }];
    [self.getCodeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view).offset(-30);
        make.center.equalTo(self.verifyTF);
    }];
    [self.timeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view).offset(-30);
        make.center.equalTo(self.verifyTF);
    }];
    [self.codeLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.verifyTF);
        make.right.equalTo(self.view).offset(-30);
        make.top.equalTo(self.verifyTF.mas_bottom).offset(8);
        make.height.equalTo(@1);
    }];
    [self.bindBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.codeLine.mas_bottom).offset(64);
        make.size.mas_equalTo(CGSizeMake(206, 50));
    }];
    [self.tipsLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(15);
        make.right.equalTo(self.view).offset(-15);
        make.top.equalTo(self.bindBtn.mas_bottom).offset(48);
    }];
}

- (void)dealloc {
    [_countTimer invalidate];
    _countTimer = nil;
}

#pragma mark - Action
- (IBAction)getMobileCode:(UIButton *)sender {
    if (self.mobileTF.text.length == 0) {
        [DSProgressHUD showToast:@"请输入手机号"];
        return;
    }
    [DSProgressHUD showProgressHUDWithInfo:@""];
    [UserRequestModel getMobileCode:self.mobileTF.text completion:^(UserRequestModel * _Nonnull requestModel) {
        [DSProgressHUD dissmissProgressHUD];
        if (requestModel.resCode == DSResCodeSuccess) {
            [self.verifyTF becomeFirstResponder];
            self.timeLab.hidden = NO;
            self.getCodeBtn.hidden = YES;
            [self.countTimer setFireDate:[NSDate date]];
            [self.getCodeBtn setTitle:@"重新获取" forState:UIControlStateNormal];
        } else {
            [DSProgressHUD showToast:requestModel.errMessage];
        }
    }];
}

- (IBAction)bindMobile:(UIButton *)sender {
    if (self.mobileTF.text.length == 0) {
        [DSProgressHUD showToast:@"请输入手机号"];
        return;
    }
    if (self.verifyTF.text.length == 0) {
        [DSProgressHUD showToast:@"请输入验证码"];
        return;
    }
    if (self.mobileTF.text.length && self.verifyTF.text.length) {
        [DSProgressHUD showProgressHUDWithInfo:@""];
        [UserRequestModel verifyBindMobile:self.mobileTF.text code:self.verifyTF.text completion:^(UserRequestModel * _Nonnull requestModel) {
            [DSProgressHUD dissmissProgressHUD];
            if (requestModel.resCode == DSResCodeSuccess) {
                // 保存绑定手机号码标识
                [LoginUtils updateUserInfo:requestModel.mobileDic];
                
                [self.navigationController popViewControllerAnimated:YES];
            } else {
                [DSProgressHUD showToast:requestModel.errMessage];
            }
        }];
    }
}

#pragma mark - 关闭侧滑
- (BOOL)enableInteractivePopGestureRecognizer {
    return NO;
}

#pragma mark - lazy
- (NSTimer *)countTimer {
    if (!_countTimer) {
        WS(weakSelf);
        _countTimer = [NSTimer timerWithTimeInterval:1.0 repeats:YES block:^(NSTimer * _Nonnull timer) {
            if (weakSelf.totalSecond == 0) {
                weakSelf.getCodeBtn.hidden = NO;
                weakSelf.timeLab.hidden = YES;
                [weakSelf.countTimer setFireDate:[NSDate distantFuture]];
                return;
            }
            weakSelf.totalSecond--;
            weakSelf.timeLab.text = [NSString stringWithFormat:@"%ds后重发", weakSelf.totalSecond];
        }];
        [[NSRunLoop currentRunLoop] addTimer:_countTimer forMode:NSRunLoopCommonModes];
    }
    return _countTimer;
}

@end