You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

266 lines
12 KiB

//
// WXYZ_BindPhoneViewController.m
// WXReader
//
// Created by Andrew on 2018/7/27.
// Copyright © 2018年 Andrew. All rights reserved.
//
#import "WXYZ_BindPhoneViewController.h"
#import "TFAreaCodeViewController.h"
#import "TFValidationButton.h"
#import "UIView+LayoutCallback.h"
#import "NSObject+Observer.h"
@interface WXYZ_BindPhoneViewController ()
@property (nonatomic, weak) UITextField *accountTextField;
@property (nonatomic, weak) UITextField *codeTextField;
@property (nonatomic, weak) TFValidationButton *sendCodeButton;
@property (nonatomic, weak) UIButton *loginButton;
@end
@implementation WXYZ_BindPhoneViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self initialize];
[self createSubviews];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self setStatusBarDefaultStyle];
}
#pragma mark - 初始化
- (void)initialize {
[self setNavigationBarTitle:TFLocalizedString(@"绑定手机号")];
self.view.backgroundColor = kWhiteColor;
}
#pragma mark - UI
- (void)createSubviews {
UIView *leftView = ({
UIView *leftView = [[UIView alloc] init];
leftView.backgroundColor = kWhiteColor;
UILabel *zoneLabel = [[UILabel alloc] init];
zoneLabel.backgroundColor = kWhiteColor;
zoneLabel.textColor = kBlackColor;
zoneLabel.font = kFont18;
zoneLabel.text = [NSString stringWithFormat:@"%@%@", @"+", TFSystemInfoManager.zoneNumber];
[leftView addSubview:zoneLabel];
[zoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.centerY.equalTo(leftView);
}];
UIImageView *downImageView = [[UIImageView alloc] init];
downImageView.backgroundColor = kWhiteColor;
downImageView.image = [[UIImage imageNamed:@"public_dropDown"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[leftView addSubview:downImageView];
[downImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(zoneLabel.mas_right).offset(kQuarterMargin);
make.centerY.equalTo(zoneLabel);
make.width.height.mas_equalTo(15.0);
}];
WS(weakSelf)
[leftView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithActionBlock:^(id _Nonnull sender) {
TFAreaCodeViewController *vc = [[TFAreaCodeViewController alloc] init];
vc.areaCodeBlock = ^(NSString * _Nonnull areaCode) {
TFSystemInfoManager.zoneNumber = areaCode;
zoneLabel.text = [NSString stringWithFormat:@"%@%@", @"+", TFSystemInfoManager.zoneNumber];
};
[weakSelf.navigationController pushViewController:vc animated:YES];
}]];
leftView;
});
[self.view addSubview:leftView];
leftView.frameBlock = ^(UIView * _Nonnull view) {
[view addBorderLineWithBorderWidth:1.0 borderColor:kGrayLineColor cornerRadius:0.0 borderType:UIBorderSideTypeBottom];
};
CGFloat width = SCREEN_WIDTH - 4 * kMargin;
CGFloat height = kGeometricWidth(width, 61.0, 334.0);
[leftView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view).offset(2 * kMargin);
make.top.equalTo(self.view).offset(PUB_NAVBAR_HEIGHT + kLineX(40.0));
make.size.mas_equalTo(CGSizeMake(90.0, height));
}];
UITextField *accountTextField = ({
UITextField *accountTextField = [[UITextField alloc] init];
self.accountTextField = accountTextField;
accountTextField.backgroundColor = [UIColor whiteColor];
accountTextField.font = kFont16;
accountTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
accountTextField.textColor = kBlackColor;
[accountTextField addTarget:self action:@selector(textDidChange:) forControlEvents:UIControlEventEditingChanged];
accountTextField.keyboardType = UIKeyboardTypeNumberPad;
accountTextField.placeholder = TFLocalizedString(@"手机号");
accountTextField.frameBlock = ^(UIView * _Nonnull view) {
[view addBorderLineWithBorderWidth:kCellLineHeight borderColor:kGrayLineColor cornerRadius:0 borderType:UIBorderSideTypeBottom];
};
accountTextField;
});
[self.view addSubview:accountTextField];
[accountTextField mas_makeConstraints:^(MASConstraintMaker *make) {
if (leftView.hidden) {
make.left.equalTo(leftView);
} else {
make.left.equalTo(leftView.mas_right);
}
make.top.height.equalTo(leftView);
make.right.equalTo(self.view).offset(-2 * kMargin);
}];
NSString *maxText = [NSString stringWithFormat:@"(%@)%@", @"59", TFLocalizedString(@"后重新发送")];
CGFloat maxWidth = [TFViewHelper getDynamicWidthWithLabelFont:kMainFont labelHeight:height labelText:maxText maxWidth:SCREEN_WIDTH / 2.0 - 50.0];
UIView *rightView = ({
UIView *rightView = [[UIView alloc] init];
TFValidationButton *sendCodeButton = [[TFValidationButton alloc] initWithFrame:CGRectZero identify:@"login_timer"];
self.sendCodeButton = sendCodeButton;
sendCodeButton.enabled = NO;
[sendCodeButton.titleLabel setFont:kMainFont];
sendCodeButton.titleLabel.textAlignment = NSTextAlignmentCenter;
[sendCodeButton setTitleColor:kRedColor forState:UIControlStateNormal];
[sendCodeButton setTitleColor:kColorRGB(199, 199, 205) forState:UIControlStateDisabled];
[sendCodeButton addTarget:self action:@selector(sendCodeEvent:) forControlEvents:UIControlEventTouchUpInside];
[rightView addSubview:sendCodeButton];
[sendCodeButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(rightView);
make.height.mas_equalTo(sendCodeButton.intrinsicContentSize.height);
make.width.mas_equalTo(maxWidth);
}];
UIView *lineView = [[UIView alloc] init];
lineView.backgroundColor = kGrayLineColor;
[rightView addSubview:lineView];
[lineView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(rightView);
make.size.mas_equalTo(CGSizeMake(1.0, kMargin));
make.centerY.equalTo(rightView);
}];
rightView;
});
[self.view addSubview:rightView];
rightView.frameBlock = ^(UIView * _Nonnull view) {
[view addBorderLineWithBorderWidth:1.0 borderColor:kGrayLineColor cornerRadius:0.0 borderType:UIBorderSideTypeBottom];
};
[rightView mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.height.equalTo(accountTextField);
make.top.equalTo(accountTextField.mas_bottom).offset(kMargin);
make.width.mas_equalTo(maxWidth + kMargin);
}];
UITextField *codeTextField = ({
UITextField *codeTextField = [[UITextField alloc] init];
self.codeTextField = codeTextField;
codeTextField.backgroundColor = accountTextField.backgroundColor;
codeTextField.keyboardType = UIKeyboardTypeNumberPad;
codeTextField.font = accountTextField.font;
codeTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
codeTextField.textColor = accountTextField.textColor;
codeTextField.placeholder = TFLocalizedString(@"验证码");
[codeTextField addTarget:self action:@selector(textDidChange:) forControlEvents:UIControlEventEditingChanged];
codeTextField.frameBlock = ^(UIView * _Nonnull view) {
[view addBorderLineWithBorderWidth:kCellLineHeight borderColor:kGrayLineColor cornerRadius:0 borderType:UIBorderSideTypeBottom];
};
codeTextField;
});
[self.view addSubview:codeTextField];
[codeTextField mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(leftView);
make.top.equalTo(rightView);
make.height.equalTo(accountTextField);
make.right.equalTo(rightView.mas_left);
}];
UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeSystem];
self.loginButton = loginButton;
loginButton.titleLabel.font = kFont15;
[loginButton setTitleColor:kWhiteColor forState:UIControlStateNormal];
[loginButton setTitleColor:kGrayTextColor forState:UIControlStateDisabled];
[loginButton setTitle:TFLocalizedString(@"绑定") forState:UIControlStateNormal];
[loginButton addObserver:KEY_PATH(loginButton, enabled) complete:^(UIButton * _Nonnull obj, id _Nullable oldVal, id _Nullable newVal) {
obj.backgroundColor = [newVal boolValue] ? kRedColor : kColorRGB(229, 230, 231);
}];
loginButton.enabled = NO;
loginButton.frameBlock = ^(UIButton * _Nonnull button) {
button.layer.cornerRadius = (CGRectGetWidth(button.bounds) + CGRectGetHeight(button.bounds)) * 0.058;
};
[loginButton addTarget:self action:@selector(bindPhone) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:loginButton];
[loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view).offset(2 * kMargin);
make.right.equalTo(self.view).offset(-2 * kMargin);
make.top.equalTo(codeTextField.mas_bottom).offset(is_iPhone4 || is_iPhone5 ? 40 : 60);
make.height.equalTo(loginButton.mas_width).multipliedBy(44.0 / 334.0);
}];
}
#pragma mark - Event
- (void)textDidChange:(UITextField *)textField {
if (self.sendCodeButton.countdownState == NO) {
self.sendCodeButton.enabled = (self.accountTextField.text.length > 0);
}
self.loginButton.enabled = (self.accountTextField.text.length > 0 && self.codeTextField.text.length > 0);
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self.view endEditing:YES];
}
#pragma mark - 网络请求
- (void)sendCodeEvent:(TFValidationButton *)button
{
NSDictionary *params = @{
@"mobile" : [TFSystemInfoManager.zoneNumber stringByAppendingString:self.accountTextField.text]
};
WS(weakSelf)
[TFNetworkTools POST:Send_Verification_Code parameters:params model:nil success:^(BOOL isSuccess, id _Nullable t_model, TFNetworkRequestModel *requestModel) {
if (isSuccess) {
[button startTiming];
[TFPromptManager showPromptViewWithStatus:TFPromptStatusSuccess promptTitle:TFLocalizedString(@"验证码已发送")];
[weakSelf.codeTextField becomeFirstResponder];
} else {
[TFPromptManager showPromptViewWithStatus:TFPromptStatusError promptTitle:requestModel.msg ?: TFLocalizedString(@"发送失败")];
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[TFPromptManager showPromptWithError:error defaultText:TFLocalizedString(@"发送失败")];
}];
}
- (void)bindPhone {
self.view.userInteractionEnabled = NO;
NSDictionary *params = @{
@"mobile" : [TFSystemInfoManager.zoneNumber stringByAppendingString:self.accountTextField.text],
@"code" : self.codeTextField.text
};
WS(weakSelf)
[TFNetworkTools POST:Phone_Binding parameters:params model:TFUserInfoManager.class success:^(BOOL isSuccess, TFUserInfoManager * _Nullable t_model, TFNetworkRequestModel *requestModel) {
if (isSuccess) {
[TFPromptManager showPromptViewWithStatus:TFPromptStatusSuccess promptTitle:TFLocalizedString(@"绑定成功")];
!weakSelf.completionBlock ?: weakSelf.completionBlock();
} else {
[TFPromptManager showPromptViewWithStatus:TFPromptStatusError promptTitle:requestModel.msg];
weakSelf.view.userInteractionEnabled = YES;
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[TFPromptManager showPromptWithError:error defaultText:nil];
weakSelf.view.userInteractionEnabled = YES;
}];
}
@end