口袋优选

KBPhoneFieldView.m 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. //
  2. // KBPhoneFieldView.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/5/21.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "KBPhoneFieldView.h"
  9. #import "WLCaptcheButton.h"
  10. #import "KBSendCodeRequest.h"
  11. @interface KBPhoneFieldView (){
  12. NSString *_union_id;
  13. }
  14. @property (nonatomic, strong) UIImageView *phoneIcon;
  15. @property (nonatomic, strong) UIImageView *codeIcon;
  16. @property (nonatomic, strong) UITextField *phoneField;
  17. @property (nonatomic, strong) UITextField *codeField;
  18. @property (nonatomic, strong) WLCaptcheButton *codeButton;
  19. @property (nonatomic, strong) UIView *line1;
  20. @property (nonatomic, strong) UIView *line2;
  21. @end
  22. @implementation KBPhoneFieldView
  23. - (instancetype)initWithFrame:(CGRect)frame wx_union_id:(NSString *)union_id{
  24. self = [super initWithFrame:frame];
  25. _union_id = union_id;
  26. if (self) {
  27. [self initSubView];
  28. [self addObserveForButtons];
  29. }
  30. return self;
  31. }
  32. - (void)initSubView {
  33. [self addSubview:self.phoneIcon];
  34. [self addSubview:self.codeIcon];
  35. [self addSubview:self.phoneField];
  36. [self addSubview:self.codeField];
  37. [self addSubview:self.codeButton];
  38. [self addSubview:self.line1];
  39. [self addSubview:self.line2];
  40. [self.phoneIcon mas_makeConstraints:^(MASConstraintMaker *make) {
  41. make.left.mas_equalTo(35);
  42. make.top.mas_equalTo(5);
  43. make.width.mas_equalTo(18);
  44. make.height.mas_equalTo(18);
  45. }];
  46. [self.phoneField mas_makeConstraints:^(MASConstraintMaker *make) {
  47. make.left.mas_equalTo(self.phoneIcon.mas_right).mas_offset(13);
  48. make.centerY.mas_equalTo(self.phoneIcon);
  49. make.right.mas_equalTo(-37);
  50. make.height.mas_equalTo(25);
  51. }];
  52. [self.line1 mas_makeConstraints:^(MASConstraintMaker *make) {
  53. make.left.mas_equalTo(38);
  54. make.right.mas_equalTo(-38);
  55. make.height.mas_equalTo(1);
  56. make.top.mas_equalTo(self.phoneField.mas_bottom).mas_offset(5);
  57. }];
  58. [self.codeIcon mas_makeConstraints:^(MASConstraintMaker *make) {
  59. make.left.mas_equalTo(self.phoneIcon);
  60. make.width.height.mas_equalTo(18);
  61. make.top.mas_equalTo(self.line1.mas_bottom).mas_offset(30);
  62. }];
  63. [self.codeField mas_makeConstraints:^(MASConstraintMaker *make) {
  64. make.left.mas_equalTo(self.codeIcon.mas_right).mas_offset(13);
  65. make.centerY.mas_equalTo(self.codeIcon);
  66. make.right.mas_equalTo(-130);
  67. make.height.mas_equalTo(25);
  68. }];
  69. [self.codeButton mas_makeConstraints:^(MASConstraintMaker *make) {
  70. make.right.mas_equalTo(-33);
  71. make.width.mas_equalTo(76);
  72. make.height.mas_equalTo(22);
  73. make.centerY.mas_equalTo(self.codeField);
  74. }];
  75. [self.line2 mas_makeConstraints:^(MASConstraintMaker *make) {
  76. make.left.mas_equalTo(38);
  77. make.right.mas_equalTo(-38);
  78. make.height.mas_equalTo(1);
  79. make.top.mas_equalTo(self.codeField.mas_bottom).mas_offset(5);
  80. }];
  81. }
  82. - (void)addObserveForButtons {
  83. RACSignal *textSingal = [self.phoneField.rac_textSignal map:^id(NSString * value) {
  84. return @(value.length >= 11);
  85. }];
  86. [textSingal subscribeNext:^(NSNumber *textActionSignal) {
  87. self.codeButton.backgroundColor = [textActionSignal boolValue] ? [UIColor whiteColor]:[UIColor YHColorWithHex:0xd8d8d8];
  88. self.codeButton.layer.borderColor = [textActionSignal boolValue] ? [UIColor homeRedColor].CGColor:[UIColor YHColorWithHex:0xd8d8d8].CGColor;
  89. UIColor *color = [textActionSignal boolValue] ? [UIColor homeRedColor]:[UIColor whiteColor];
  90. [self.codeButton setTitleColor:color forState:UIControlStateNormal];
  91. self.codeButton.enabled = [textActionSignal boolValue];
  92. }];
  93. RACSignal *textSingal2 = [self.codeField.rac_textSignal map:^id(NSString * value) {
  94. return @(value.length > 0);
  95. }];
  96. RACSignal *calculActiveSignal = [RACSignal
  97. combineLatest:@[textSingal,
  98. textSingal2]
  99. reduce:^id(NSNumber *textValid, NSNumber *textValid2) {
  100. return @([textValid boolValue] && [textValid2 boolValue] );
  101. }];
  102. [calculActiveSignal subscribeNext:^(NSNumber *calculEnble) {
  103. if (self.nextEnable) {
  104. self.nextEnable([calculEnble boolValue]);
  105. }
  106. }];
  107. }
  108. /**
  109. 发送验证码
  110. */
  111. - (void)sendCodeAction:(WLCaptcheButton *)sender {
  112. [self.codeField becomeFirstResponder];
  113. NSString *ttl = [PublicFunction getNowTimeTimestamp];
  114. NSString *sign = [NSString stringWithFormat:@"phone=%@&ttl=%@%@",self.phoneField.text,ttl,SignCode];
  115. NSString *md5Str = [PublicFunction md5:sign];
  116. NSDictionary *para = @{@"phone":self.phoneField.text,
  117. @"ttl":ttl,
  118. @"sign":md5Str,
  119. @"union_id":_union_id
  120. };
  121. [KBSendCodeRequest post:Send_Code params:para success:^(id json) {
  122. if (json[@"success"]) {
  123. [sender fire];
  124. }
  125. } failure:^(NSError *error) {
  126. }];
  127. }
  128. - (NSString *)getPhoneText {
  129. return self.phoneField.text;
  130. }
  131. - (NSString *)getCodeText {
  132. return self.codeField.text;
  133. }
  134. - (UIImageView *)phoneIcon {
  135. if (!_phoneIcon) {
  136. _phoneIcon = [[UIImageView alloc] init];
  137. _phoneIcon.image = [UIImage imageNamed:@"phone_icon"];
  138. }
  139. return _phoneIcon;
  140. }
  141. - (UIImageView *)codeIcon {
  142. if (!_codeIcon) {
  143. _codeIcon = [[UIImageView alloc] init];
  144. _codeIcon.image = [UIImage imageNamed:@"code_icon"];
  145. }
  146. return _codeIcon;
  147. }
  148. - (UITextField *)phoneField {
  149. if (!_phoneField) {
  150. _phoneField = [[UITextField alloc] init];
  151. _phoneField.placeholder = @"输入手机号";
  152. _phoneField.keyboardType = UIKeyboardTypeNumberPad;
  153. _phoneField.font = [UIFont systemFontOfSize:14];
  154. }
  155. return _phoneField;
  156. }
  157. - (UITextField *)codeField {
  158. if (!_codeField) {
  159. _codeField = [[UITextField alloc] init];
  160. _codeField.placeholder = @"输入验证码";
  161. _codeField.keyboardType = UIKeyboardTypeNumberPad;
  162. _codeField.font = [UIFont systemFontOfSize:14];
  163. }
  164. return _codeField;
  165. }
  166. - (WLCaptcheButton *)codeButton {
  167. if (!_codeButton) {
  168. _codeButton = [[WLCaptcheButton alloc] init];
  169. _codeButton.identifyKey = @"linkButton";
  170. _codeButton.layer.borderWidth = 1;
  171. _codeButton.layer.cornerRadius = 12;
  172. [_codeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
  173. _codeButton.titleLabel.font = [UIFont systemFontOfSize:12];
  174. [_codeButton addTarget:self action:@selector(sendCodeAction:) forControlEvents:UIControlEventTouchUpInside];
  175. // _codeButton.enabled = NO;
  176. _codeButton.enabled = YES;
  177. }
  178. return _codeButton;
  179. }
  180. - (UIView *)line1 {
  181. if (!_line1) {
  182. _line1 = [[UIView alloc] init];
  183. _line1.backgroundColor = [UIColor YHColorWithHex:0xD8D8D8];
  184. }
  185. return _line1;
  186. }
  187. - (UIView *)line2 {
  188. if (!_line2) {
  189. _line2 = [[UIView alloc] init];
  190. _line2.backgroundColor = [UIColor YHColorWithHex:0xD8D8D8];
  191. }
  192. return _line2;
  193. }
  194. @end