// // LDPhoneFieldView.m // YouHuiProject // // Created by 小花 on 2018/5/21. // Copyright © 2018年 kuxuan. All rights reserved. // #import "LDPhoneFieldView.h" #import "WLCaptcheButton.h" #import "LDSendCodeRequest.h" @interface LDPhoneFieldView (){ NSString *_union_id; } @property (nonatomic, strong) UIImageView *phoneIcon; @property (nonatomic, strong) UIImageView *codeIcon; @property (nonatomic, strong) UITextField *phoneField; @property (nonatomic, strong) UITextField *codeField; @property (nonatomic, strong) WLCaptcheButton *codeButton; @property (nonatomic, strong) UIView *line1; @property (nonatomic, strong) UIView *line2; @end @implementation LDPhoneFieldView - (instancetype)initWithFrame:(CGRect)frame wx_union_id:(NSString *)union_id{ self = [super initWithFrame:frame]; _union_id = union_id; if (self) { [self initSubView]; [self addObserveForButtons]; } return self; } - (void)initSubView { [self addSubview:self.phoneIcon]; [self addSubview:self.codeIcon]; [self addSubview:self.phoneField]; [self addSubview:self.codeField]; [self addSubview:self.codeButton]; [self addSubview:self.line1]; [self addSubview:self.line2]; [self.phoneIcon mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(35); make.top.mas_equalTo(5); make.width.mas_equalTo(18); make.height.mas_equalTo(18); }]; [self.phoneField mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.phoneIcon.mas_right).mas_offset(13); make.centerY.mas_equalTo(self.phoneIcon); make.right.mas_equalTo(-37); make.height.mas_equalTo(25); }]; [self.line1 mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(38); make.right.mas_equalTo(-38); make.height.mas_equalTo(1); make.top.mas_equalTo(self.phoneField.mas_bottom).mas_offset(5); }]; [self.codeIcon mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.phoneIcon); make.width.height.mas_equalTo(18); make.top.mas_equalTo(self.line1.mas_bottom).mas_offset(30); }]; [self.codeField mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.codeIcon.mas_right).mas_offset(13); make.centerY.mas_equalTo(self.codeIcon); make.right.mas_equalTo(-130); make.height.mas_equalTo(25); }]; [self.codeButton mas_makeConstraints:^(MASConstraintMaker *make) { make.right.mas_equalTo(-33); make.width.mas_equalTo(76); make.height.mas_equalTo(22); make.centerY.mas_equalTo(self.codeField); }]; [self.line2 mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(38); make.right.mas_equalTo(-38); make.height.mas_equalTo(1); make.top.mas_equalTo(self.codeField.mas_bottom).mas_offset(5); }]; } - (void)addObserveForButtons { RACSignal *textSingal = [self.phoneField.rac_textSignal map:^id(NSString * value) { return @(value.length >= 11); }]; [textSingal subscribeNext:^(NSNumber *textActionSignal) { self.codeButton.backgroundColor = [textActionSignal boolValue] ? [UIColor whiteColor]:[UIColor YHColorWithHex:0xd8d8d8]; self.codeButton.layer.borderColor = [textActionSignal boolValue] ? [UIColor homeRedColor].CGColor:[UIColor YHColorWithHex:0xd8d8d8].CGColor; UIColor *color = [textActionSignal boolValue] ? [UIColor homeRedColor]:[UIColor whiteColor]; [self.codeButton setTitleColor:color forState:UIControlStateNormal]; self.codeButton.enabled = [textActionSignal boolValue]; }]; RACSignal *textSingal2 = [self.codeField.rac_textSignal map:^id(NSString * value) { return @(value.length > 0); }]; RACSignal *calculActiveSignal = [RACSignal combineLatest:@[textSingal, textSingal2] reduce:^id(NSNumber *textValid, NSNumber *textValid2) { return @([textValid boolValue] && [textValid2 boolValue] ); }]; [calculActiveSignal subscribeNext:^(NSNumber *calculEnble) { if (self.nextEnable) { self.nextEnable([calculEnble boolValue]); } }]; } /** 发送验证码 */ - (void)sendCodeAction:(WLCaptcheButton *)sender { [self.codeField becomeFirstResponder]; NSString *ttl = [PublicFunction getNowTimeTimestamp]; NSString *sign = [NSString stringWithFormat:@"phone=%@&ttl=%@%@",self.phoneField.text,ttl,SignCode]; NSString *md5Str = [PublicFunction md5:sign]; NSDictionary *para = @{@"phone":self.phoneField.text, @"ttl":ttl, @"sign":md5Str, @"union_id":_union_id }; [LDSendCodeRequest post:Send_Code params:para success:^(id json) { if (json[@"success"]) { [sender fire]; } } failure:^(NSError *error) { }]; } - (NSString *)getPhoneText { return self.phoneField.text; } - (NSString *)getCodeText { return self.codeField.text; } - (UIImageView *)phoneIcon { if (!_phoneIcon) { _phoneIcon = [[UIImageView alloc] init]; _phoneIcon.image = [UIImage imageNamed:@"phone_icon"]; } return _phoneIcon; } - (UIImageView *)codeIcon { if (!_codeIcon) { _codeIcon = [[UIImageView alloc] init]; _codeIcon.image = [UIImage imageNamed:@"code_icon"]; } return _codeIcon; } - (UITextField *)phoneField { if (!_phoneField) { _phoneField = [[UITextField alloc] init]; _phoneField.placeholder = @"输入手机号"; _phoneField.keyboardType = UIKeyboardTypeNumberPad; _phoneField.font = [UIFont systemFontOfSize:14]; } return _phoneField; } - (UITextField *)codeField { if (!_codeField) { _codeField = [[UITextField alloc] init]; _codeField.placeholder = @"输入验证码"; _codeField.keyboardType = UIKeyboardTypeNumberPad; _codeField.font = [UIFont systemFontOfSize:14]; } return _codeField; } - (WLCaptcheButton *)codeButton { if (!_codeButton) { _codeButton = [[WLCaptcheButton alloc] init]; _codeButton.identifyKey = @"linkButton"; _codeButton.layer.borderWidth = 1; _codeButton.layer.cornerRadius = 12; [_codeButton setTitle:@"获取验证码" forState:UIControlStateNormal]; _codeButton.titleLabel.font = [UIFont systemFontOfSize:12]; [_codeButton addTarget:self action:@selector(sendCodeAction:) forControlEvents:UIControlEventTouchUpInside]; // _codeButton.enabled = NO; _codeButton.enabled = YES; } return _codeButton; } - (UIView *)line1 { if (!_line1) { _line1 = [[UIView alloc] init]; _line1.backgroundColor = [UIColor YHColorWithHex:0xD8D8D8]; } return _line1; } - (UIView *)line2 { if (!_line2) { _line2 = [[UIView alloc] init]; _line2.backgroundColor = [UIColor YHColorWithHex:0xD8D8D8]; } return _line2; } @end