Sin descripción

KXLittleLogginView.m 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. //
  2. // KXLittleLogginView.m
  3. // CAISHEN
  4. //
  5. // Created by kuxuan on 2017/8/28.
  6. // Copyright © 2017年 kuxuan. All rights reserved.
  7. //
  8. #import "KXLittleLogginView.h"
  9. #define baseTag 444
  10. static int count = 0; // 短信倒计时计数
  11. @implementation KXLittleLogginView
  12. {
  13. UIView *_whiteView;
  14. UIButton *_testButton;
  15. UIButton *_completeButton;
  16. NSTimer *_timer;
  17. UIButton *_soundButton;
  18. }
  19. - (instancetype)initWithFrame:(CGRect)frame
  20. {
  21. if (self = [super initWithFrame:frame]) {
  22. self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];
  23. [self setupUI];
  24. }
  25. return self;
  26. }
  27. - (void)setupUI
  28. {
  29. _whiteView = [[UIView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2.0-150, 180*SCREEN_MUTI, 300, 250)];
  30. _whiteView.layer.cornerRadius = 8;
  31. _whiteView.layer.masksToBounds = YES;
  32. _whiteView.backgroundColor = [UIColor whiteColor];
  33. [self addSubview:_whiteView];
  34. UILabel *titlelabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 25, 300, 20)];
  35. titlelabel.textColor = [UIColor titleColor];
  36. titlelabel.font = FONT_BOLD(16);
  37. titlelabel.textAlignment = NSTextAlignmentCenter;
  38. titlelabel.text = @"验证手机号";
  39. [_whiteView addSubview:titlelabel];
  40. NSArray *placeholderArray=@[@"请输入手机号",@"请输入验证码"];
  41. for (int i=0; i<2; i++) {
  42. CGFloat width = i==0?CGRectGetWidth(_whiteView.frame)-40:Fitsize(160);
  43. UITextField *someTextField=[[UITextField alloc]initWithFrame:CGRectMake(20, 70+48*i,width, 28)];
  44. someTextField.textColor = [UIColor titleColor];
  45. someTextField.tag=baseTag+i;
  46. someTextField.borderStyle=UITextBorderStyleNone;
  47. someTextField.keyboardType=UIKeyboardTypeNumberPad;
  48. if (i==1) {
  49. someTextField.secureTextEntry=YES;
  50. someTextField.keyboardType=UIKeyboardTypeNumbersAndPunctuation;
  51. }
  52. someTextField.attributedPlaceholder=[[NSAttributedString alloc] initWithString:placeholderArray[i] attributes:@{NSForegroundColorAttributeName:[UIColor detailTitleColor]}];
  53. someTextField.font=FONT_SYS(12);
  54. someTextField.leftView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 28)];
  55. someTextField.leftViewMode=UITextFieldViewModeAlways;
  56. someTextField.returnKeyType=UIReturnKeyDone;
  57. someTextField.delegate=self;
  58. [_whiteView addSubview:someTextField];
  59. }
  60. for (int i=0; i<2; i++) {
  61. UILabel *lineLabel=[[UILabel alloc]initWithFrame:CGRectMake(20, 97 + 48*i, 260, 1)];
  62. lineLabel.backgroundColor=[UIColor lineColor];
  63. [_whiteView addSubview:lineLabel];
  64. }
  65. _testButton=[UIButton buttonWithType:UIButtonTypeCustom];
  66. _testButton.frame=CGRectMake(160, 112, 300-162, 30);
  67. _testButton.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
  68. _testButton.titleLabel.font=FONT_SYS(12*SCREEN_MUTI);
  69. [_testButton setTitle:@"点击获取验证码" forState:UIControlStateNormal];
  70. [_testButton setTitleColor:[UIColor titleColor] forState:UIControlStateNormal];
  71. [_testButton addTarget:self action:@selector(testAction:) forControlEvents:UIControlEventTouchUpInside];
  72. [_whiteView addSubview:_testButton];
  73. _completeButton=[UIButton buttonWithType:UIButtonTypeCustom];
  74. _completeButton.frame=CGRectMake(20, 165, 300-40, 45);
  75. _completeButton.layer.cornerRadius=6;
  76. _completeButton.layer.masksToBounds=YES;
  77. _completeButton.backgroundColor=[UIColor KXColorWithHex:0xcccccc];
  78. _completeButton.titleLabel.font=FONT_SYS(16);
  79. [_completeButton setTitle:@"完成" forState:UIControlStateNormal];
  80. [_completeButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  81. [_completeButton addTarget:self action:@selector(logginAction) forControlEvents:UIControlEventTouchUpInside];
  82. [_whiteView addSubview:_completeButton];
  83. UIButton *codeButton=[UIButton buttonWithType:UIButtonTypeSystem];
  84. codeButton.frame=CGRectMake(20, 220, 100, 15);
  85. codeButton.titleLabel.font=FONT_SYS(12);
  86. [codeButton setTitle:@"账户密码验证" forState:UIControlStateNormal];
  87. [codeButton setTitleColor:[UIColor KXColorWithHex:0xe42a2a] forState:UIControlStateNormal];
  88. codeButton.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft;
  89. [codeButton addTarget:self action:@selector(codeAction:) forControlEvents:UIControlEventTouchUpInside];
  90. [_whiteView addSubview:codeButton];
  91. _soundButton=[UIButton buttonWithType:UIButtonTypeCustom];
  92. _soundButton.frame=CGRectMake(100, 220, 180, 15);
  93. _soundButton.titleLabel.font=FONT_SYS(12);
  94. _soundButton.hidden = NO;
  95. NSMutableAttributedString *att = [[NSMutableAttributedString alloc]initWithString:@"收不到短信?请使用语音验证码"];
  96. [att addAttribute:NSForegroundColorAttributeName value:[UIColor detailTitleColor] range:NSMakeRange(0, 9)];
  97. [att addAttribute:NSForegroundColorAttributeName value:[UIColor KXColorWithHex:0xe42a2a] range:NSMakeRange(9, 5)];
  98. [_soundButton setAttributedTitle:att forState:UIControlStateNormal];
  99. _soundButton.contentHorizontalAlignment=UIControlContentHorizontalAlignmentRight;
  100. [_soundButton addTarget:self action:@selector(soundAction:) forControlEvents:UIControlEventTouchUpInside];
  101. [_whiteView addSubview:_soundButton];
  102. UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
  103. [closeButton setImage:[UIImage imageNamed:@"main_little_close"] forState:UIControlStateNormal];
  104. closeButton.frame = CGRectMake(240, 0, 60, 60);
  105. [closeButton addTarget:self action:@selector(closeAction) forControlEvents:UIControlEventTouchUpInside];
  106. [_whiteView addSubview:closeButton];
  107. }
  108. - (void)closeAction
  109. {
  110. [self.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  111. [obj removeFromSuperview];
  112. }];
  113. [self removeFromSuperview];
  114. }
  115. -(void)testAction:(UIButton *)btn
  116. {
  117. _soundButton.hidden = YES;
  118. _testButton.enabled=NO;
  119. UITextField *phoneTF=[_whiteView viewWithTag:baseTag];
  120. NSString *urlString=[NSString stringWithFormat:@"%@/user/register",URL];
  121. [KXHTTP post:urlString params:@{@"phone":phoneTF.text,@"qrcode":@"1",@"sign":[[NSString stringWithFormat:@"%@%@",phoneTF.text,KXREGISTER_SECRET] md5],@"version":@"2"} success:^(id json) {
  122. if ([json[@"success"] isEqual:@(1)]) {
  123. _testButton.enabled = YES;
  124. _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateTime) userInfo:nil repeats:YES];
  125. [[NSRunLoop currentRunLoop]addTimer:_timer forMode:NSRunLoopCommonModes];
  126. [_timer fire];
  127. }
  128. } failure:^(NSError *error) {
  129. _testButton.enabled=YES;
  130. }];
  131. }
  132. - (void)soundAction:(UIButton *)btn
  133. {
  134. NSString *urlString=[NSString stringWithFormat:@"%@/user/register",URL];
  135. UITextField *phoneTF=[self viewWithTag:baseTag];
  136. NSDictionary *params = @{@"phone":phoneTF.text,@"qrcode":@"1",@"smstype":@"2",@"sign":[[NSString stringWithFormat:@"%@%@",phoneTF.text,KXREGISTER_SECRET] md5],@"version":@"2"};
  137. [KXHTTP post:urlString params:params success:^(id json) {
  138. BOOL isSuccess = json[@"success"];
  139. if (isSuccess) {
  140. [self updateTime];
  141. }
  142. } failure:^(NSError *error) {
  143. }];
  144. }
  145. -(void)updateTime {
  146. count ++;
  147. if (count >= 60) {
  148. count = 0;
  149. _testButton.enabled = YES;
  150. _testButton.titleLabel.text = [NSString stringWithFormat:@"点击获取验证码"];
  151. [_timer invalidate];
  152. return;
  153. }
  154. _soundButton.hidden = YES;
  155. _testButton.titleLabel.text = [NSString stringWithFormat:@"重新发送%ds", 60 - count];
  156. }
  157. -(void)codeAction:(UIButton *)btn
  158. {
  159. UITextField *codeTF=[_whiteView viewWithTag:baseTag+1];
  160. if ([codeTF.attributedPlaceholder.string isEqualToString:@"请输入密码"]) {
  161. codeTF.attributedPlaceholder=[[NSAttributedString alloc] initWithString:@"请输入验证码" attributes:@{NSForegroundColorAttributeName:[UIColor detailTitleColor]}];
  162. _testButton.hidden = NO;
  163. if (count >= 60||count == 0) {
  164. _soundButton.hidden = NO;
  165. }else{
  166. _soundButton.hidden = YES;
  167. }
  168. [btn setTitle:@"账号密码登录" forState:UIControlStateNormal];
  169. }else{
  170. codeTF.attributedPlaceholder=[[NSAttributedString alloc] initWithString:@"请输入密码" attributes:@{NSForegroundColorAttributeName:[UIColor detailTitleColor]}];
  171. _testButton.hidden = YES;
  172. _soundButton.hidden = YES;
  173. [btn setTitle:@"手机号验证码登录" forState:UIControlStateNormal];
  174. }
  175. }
  176. -(void)logginAction
  177. {
  178. UITextField *phoneTF=[_whiteView viewWithTag:baseTag];
  179. UITextField *codeTF=[_whiteView viewWithTag:baseTag+1];
  180. if ([codeTF.placeholder isEqualToString:@"请输入密码"]) {
  181. NSString *urlString=[NSString stringWithFormat:@"%@/user/login",URL];
  182. [KXHTTP post:urlString params:@{@"phone":phoneTF.text,@"password":codeTF.text} success:^(id json) {
  183. [[NSUserDefaults standardUserDefaults]setObject:phoneTF.text forKey:USER_NAME];
  184. [[NSUserDefaults standardUserDefaults]setObject:codeTF.text forKey:USER_PASSWORD];
  185. [[NSUserDefaults standardUserDefaults]setObject:@"" forKey:USER_TOKEN];
  186. [[NSUserDefaults standardUserDefaults]setObject:json[@"user"][@"PHPSESSID"] forKey:kloginSessionId];
  187. [[NSUserDefaults standardUserDefaults]synchronize];
  188. [_delegate littleLogginViewSuccess];
  189. [self closeAction];
  190. } failure:^(NSError *error) {
  191. }];
  192. }else{
  193. NSString *urlString=[NSString stringWithFormat:@"%@/user/loginCode",URL];
  194. [KXHTTP post:urlString params:@{@"phone":phoneTF.text,@"code":codeTF.text,@"source":KXCHANNEL_ID} success:^(id json) {
  195. [[NSUserDefaults standardUserDefaults]setObject:phoneTF.text forKey:USER_NAME];
  196. [[NSUserDefaults standardUserDefaults]setObject:@"" forKey:USER_PASSWORD];
  197. NSDictionary *uuser = json[@"user"];
  198. [[NSUserDefaults standardUserDefaults]setObject:uuser[@"user_token"] forKey:USER_TOKEN];
  199. [[NSUserDefaults standardUserDefaults]setObject:json[@"user"][@"PHPSESSID"] forKey:kloginSessionId];
  200. [[NSUserDefaults standardUserDefaults]synchronize];
  201. [_delegate littleLogginViewSuccess];
  202. [self closeAction];
  203. } failure:^(NSError *error) {
  204. }];
  205. }
  206. }
  207. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
  208. {
  209. UITextField *phoneTF=[_whiteView viewWithTag:baseTag];
  210. UITextField *codeTF=[_whiteView viewWithTag:baseTag+1];
  211. if (phoneTF.text.length!=0&&codeTF.text.length!=0) {
  212. _completeButton.backgroundColor = [UIColor baseColor];
  213. }else{
  214. _completeButton.backgroundColor = [UIColor KXColorWithHex:0xcccccc];
  215. }
  216. return YES;
  217. }
  218. @end