YOUPAILCBindPhoneAccountVC.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. //
  2. // YOUPAILCBindPhoneAccountVC.m
  3. // LiveChat
  4. //
  5. // Created by 张灿 on 2018/9/22.
  6. // Copyright © 2018年 caicai. All rights reserved.
  7. //
  8. #import "YOUPAILCBindPhoneAccountVC.h"
  9. @interface YOUPAILCBindPhoneAccountVC ()<UIScrollViewDelegate,UITextViewDelegate>
  10. @property(nonatomic,strong)UITextField* youpaipphoneTextField;//,*pwdTextField;
  11. @property(nonatomic,assign)NSInteger youpaipphoneLength;
  12. @property(nonatomic,strong)UITextField* youpaipcodeTextField;
  13. @property(nonatomic,strong)UIButton* youpaiploginBtn;
  14. @property(nonatomic,strong)UIButton* youpaipverifyBtn;
  15. @property(nonatomic,strong)dispatch_source_t youpaipcodeTimer;
  16. @end
  17. @implementation YOUPAILCBindPhoneAccountVC
  18. - (void)viewDidLoad {
  19. [super viewDidLoad];
  20. self.title = @"绑定手机号";
  21. self.view.backgroundColor = [UIColor whiteColor];
  22. self.youpaipphoneLength = 0;
  23. [self youpaifsetupView];
  24. }
  25. - (void)youpaifsetupView{
  26. UIScrollView* scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, NavBarHeight, KScreenWidth, KScreenHeight-NavBarHeight)];
  27. scrollView.contentSize = CGSizeMake(KScreenWidth, KScreenHeight-NavBarHeight+1);
  28. scrollView.bounces = YES;
  29. scrollView.showsHorizontalScrollIndicator = NO;
  30. scrollView.showsVerticalScrollIndicator = NO;
  31. scrollView.delegate = self;
  32. [self.view addSubview:scrollView];
  33. UITextField* phoneTextField = [[UITextField alloc]initWithFrame:CGRectMake(27, 27, KScreenWidth-54, 56)];
  34. self.youpaipphoneTextField = phoneTextField;
  35. phoneTextField.layer.cornerRadius = 28.0f;
  36. phoneTextField.clipsToBounds = YES;
  37. phoneTextField.backgroundColor = LZF7F8FAColor;
  38. phoneTextField.textColor = LZ273145Color;
  39. phoneTextField.tintColor = LZ7C69FEColor;
  40. UIView *phoneTipV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 28+53+12, 56)];
  41. UILabel* phoneTipLabel = [[UILabel alloc]initWithFrame:CGRectMake(28, 0, 53, 56)];
  42. phoneTipLabel.text = @"手机号";
  43. phoneTipLabel.textColor = LZ273145Color;
  44. phoneTipLabel.font = LCFont17;
  45. phoneTipLabel.textAlignment = NSTextAlignmentLeft;
  46. [phoneTipV addSubview:phoneTipLabel];
  47. phoneTextField.leftView = phoneTipV;
  48. phoneTextField.leftViewMode = UITextFieldViewModeAlways;
  49. phoneTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
  50. [[phoneTextField valueForKey:@"_clearButton"] setImage:[UIImage imageNamed:@"vqu_images_D_nick_clean"] forState:UIControlStateNormal];
  51. phoneTextField.keyboardType = UIKeyboardTypeNumberPad;
  52. [phoneTextField addTarget:self action:@selector(textFieldDidEditing:) forControlEvents:UIControlEventEditingChanged];
  53. NSMutableAttributedString* attrStr = [[NSMutableAttributedString alloc]initWithString:@"请输入手机号码"];
  54. [attrStr addAttribute:NSFontAttributeName value:LCFont17 range:NSMakeRange(0, attrStr.length)];
  55. [attrStr addAttribute:NSForegroundColorAttributeName value:LZD3D1D7Color range:NSMakeRange(0, attrStr.length)];
  56. phoneTextField.attributedPlaceholder = attrStr;
  57. phoneTextField.contentHorizontalAlignment=UIControlContentHorizontalAlignmentCenter;
  58. [scrollView addSubview:phoneTextField];
  59. UITextField* codeTextField = [[UITextField alloc]initWithFrame:CGRectMake(27, CGRectGetMaxY(phoneTextField.frame) + 14.0f, KScreenWidth-54, 56)];
  60. self.youpaipcodeTextField = codeTextField;
  61. codeTextField.layer.cornerRadius = 28.0f;
  62. codeTextField.clipsToBounds = YES;
  63. codeTextField.backgroundColor = LZF7F8FAColor;
  64. codeTextField.textColor = LZ273145Color;
  65. codeTextField.tintColor = LZ7C69FEColor;
  66. UIView *codeTipLV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 28+53+12, 56)];
  67. UILabel* codeTipLabel = [[UILabel alloc]initWithFrame:CGRectMake(28, 0, 53, 56)];
  68. codeTipLabel.text = @"验证码";
  69. codeTipLabel.textColor = LZ273145Color;
  70. codeTipLabel.font = LCFont17;
  71. codeTipLabel.textAlignment = NSTextAlignmentLeft;
  72. [codeTipLV addSubview:codeTipLabel];
  73. codeTextField.leftView = codeTipLV;
  74. codeTextField.leftViewMode = UITextFieldViewModeAlways;
  75. UIView* rightview = [[UIView alloc]initWithFrame:CGRectMake(0, 0,107, 56)];
  76. UIButton* verifyBtn = [[UIButton alloc]initWithFrame:CGRectMake(14.0f, 14.0f, 79.0f, 28.0f)];
  77. self.youpaipverifyBtn = verifyBtn;
  78. verifyBtn.layer.cornerRadius = 14.0f;
  79. verifyBtn.layer.masksToBounds = YES;
  80. verifyBtn.backgroundColor = LZD3D1D7Color;
  81. verifyBtn.titleLabel.font = LCFont12;
  82. [verifyBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
  83. [verifyBtn setTitle:@"获取验证码" forState:(UIControlStateNormal)];
  84. verifyBtn.userInteractionEnabled = NO;
  85. [verifyBtn addTarget:self action:@selector(youpaifverifyBtnClick) forControlEvents:(UIControlEventTouchUpInside)];
  86. [rightview addSubview:verifyBtn];
  87. codeTextField.rightView = rightview;
  88. codeTextField.rightViewMode = UITextFieldViewModeAlways;
  89. codeTextField.keyboardType = UIKeyboardTypeNumberPad;
  90. [codeTextField addTarget:self action:@selector(textFieldDidEditing:) forControlEvents:UIControlEventEditingChanged];
  91. NSMutableAttributedString* attrStrCode = [[NSMutableAttributedString alloc]initWithString:@"短信验证码"];
  92. [attrStrCode addAttribute:NSFontAttributeName value:LCFont17 range:NSMakeRange(0, attrStrCode.length)];
  93. [attrStrCode addAttribute:NSForegroundColorAttributeName value:LZD3D1D7Color range:NSMakeRange(0, attrStrCode.length)];
  94. codeTextField.attributedPlaceholder = attrStrCode;
  95. codeTextField.contentHorizontalAlignment=UIControlContentHorizontalAlignmentCenter;
  96. [scrollView addSubview:codeTextField];
  97. UIButton* loginBtn = [[UIButton alloc]init];
  98. self.youpaiploginBtn = loginBtn;
  99. loginBtn.userInteractionEnabled = NO;
  100. loginBtn.backgroundColor = LZD3D1D7Color;
  101. loginBtn.layer.cornerRadius = 28.0;
  102. loginBtn.layer.masksToBounds = YES;
  103. loginBtn.titleLabel.font = LCFont17;
  104. [loginBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
  105. [loginBtn setTitle:@"绑定手机号" forState:(UIControlStateNormal)];
  106. [loginBtn addTarget:self action:@selector(youpaifloginBtnClick) forControlEvents:(UIControlEventTouchUpInside)];
  107. [scrollView addSubview:loginBtn];
  108. [loginBtn makeConstraints:^(MASConstraintMaker *make) {
  109. make.top.offset(KScreenHeight-100-SafeHeight-56.0f-NavBarHeight);
  110. make.centerX.equalTo(self.view.centerX);
  111. make.size.mas_equalTo(CGSizeMake(KScreenWidth-54.0f, 56.0f));
  112. }];
  113. }
  114. //监听输入的手机号码自动添加空格
  115. -(void)textFieldDidEditing:(UITextField *)textField{
  116. if ([self.youpaipphoneTextField.text isEqualToString:@""] || [self.youpaipcodeTextField.text isEqualToString:@""]){
  117. self.youpaiploginBtn.backgroundColor = LZD3D1D7Color;
  118. [self.youpaiploginBtn setBackgroundImage:nil forState:(UIControlStateNormal)];
  119. self.youpaiploginBtn.userInteractionEnabled = NO;
  120. }else{
  121. [self.youpaiploginBtn setBackgroundImage:[LCTools ColorImage:self.youpaiploginBtn.frame.size FromColors:@[LZBFB6FFColor,LZ7C69FEColor] ByGradientType:(GradientLeftToRight)] forState:(UIControlStateNormal)];
  122. self.youpaiploginBtn.userInteractionEnabled = YES;;
  123. }
  124. if (textField == self.youpaipphoneTextField) {
  125. if (textField.text.length > self.youpaipphoneLength) {
  126. if (textField.text.length == 4 || textField.text.length == 9 ) {//输入
  127. NSMutableString * str = [[NSMutableString alloc ] initWithString:textField.text];
  128. [str insertString:@" " atIndex:(textField.text.length-1)];
  129. textField.text = str;
  130. }if (textField.text.length >= 13 ) {//输入完成
  131. textField.text = [textField.text substringToIndex:13];
  132. self.youpaipverifyBtn.userInteractionEnabled = YES;
  133. [self.youpaipverifyBtn setBackgroundImage:[LCTools ColorImage:self.youpaipverifyBtn.frame.size FromColors:@[LZBFB6FFColor,LZ7C69FEColor] ByGradientType:(GradientLeftToRight)] forState:(UIControlStateNormal)];
  134. }
  135. self.youpaipphoneLength = textField.text.length;
  136. }else if (textField.text.length < self.youpaipphoneLength){//删除
  137. if (textField.text.length == 4 || textField.text.length == 9) {
  138. textField.text = [NSString stringWithFormat:@"%@",textField.text];
  139. textField.text = [textField.text substringToIndex:(textField.text.length-1)];
  140. }
  141. self.youpaipverifyBtn.userInteractionEnabled = NO;
  142. [self.youpaipverifyBtn setBackgroundImage:nil forState:(UIControlStateNormal)];
  143. self.youpaipphoneLength = textField.text.length;
  144. }
  145. }
  146. }
  147. - (void)youpaifverifyBtnClick{
  148. NSString *phoneText = [self.youpaipphoneTextField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
  149. if ([ZCRegularHelper regularPhoneNumber:phoneText]) {//首先手机格式保证正确
  150. [LCHttpHelper requestWithURLString:PhoneCode parameters:@{@"mobile":phoneText,@"type":@"bind"} needToken:NO type:(HttpRequestTypePost) success:^(id responseObject) {
  151. NSDictionary* dict = (NSDictionary*)responseObject;
  152. NSInteger code = [[dict objectForKey:@"code"] integerValue];
  153. if (code==0) {//成功
  154. [ZCHUDHelper showTitle:@"验证码已发送"];
  155. __block int timeout=120;
  156. dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
  157. dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
  158. dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
  159. dispatch_source_set_event_handler(_timer, ^{
  160. if(timeout<=0){
  161. dispatch_source_cancel(_timer);
  162. dispatch_async(dispatch_get_main_queue(), ^{
  163. [self.youpaipverifyBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
  164. self.youpaipverifyBtn.userInteractionEnabled = YES;
  165. });
  166. }else{
  167. NSString *strTime = [NSString stringWithFormat:@"%.2dS",timeout];
  168. dispatch_async(dispatch_get_main_queue(), ^{
  169. //设置界面的按钮显示 根据自己需求设置
  170. [self.youpaipverifyBtn setTitle:strTime forState:UIControlStateNormal];
  171. self.youpaipverifyBtn.userInteractionEnabled = NO;
  172. });
  173. timeout--;
  174. }
  175. });
  176. dispatch_resume(_timer);
  177. }
  178. } failure:^(NSError *error) {
  179. }];
  180. }
  181. }
  182. - (void)youpaifloginBtnClick{
  183. NSString *phoneText = [self.youpaipphoneTextField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
  184. if ([ZCRegularHelper regularPhoneNumber:phoneText]) {
  185. if ([ZCRegularHelper regularVerifyCode4:self.youpaipcodeTextField.text]) {
  186. NSMutableDictionary* params = [NSMutableDictionary dictionary];
  187. [params setObject:phoneText forKey:@"mobile"];
  188. [params setObject:self.youpaipcodeTextField.text forKey:@"phone_code"];
  189. // [params setObject:self.pwdTextField.text forKey:@"password"];
  190. [LCHttpHelper requestWithURLString:BindPhone parameters:params needToken:YES type:(HttpRequestTypePost) success:^(id responseObject) {
  191. NSDictionary* dict = (NSDictionary*)responseObject;
  192. NSInteger code = [[dict objectForKey:@"code"] integerValue];
  193. if (code==0) {//成功
  194. [ZCHUDHelper showTitle:@"绑定成功"];
  195. if(self->_youpaipinLogin)
  196. {
  197. // [LCSaveData saveLoginMark:YES];
  198. [[YOUPAILCIMLoginManager sharedCenter]IMLogin:[LCSaveModel getUserModel].youpaipuserinfo.youpaipuser_id token:[LCSaveModel getUserModel].youpaipuserinfo.youpaipim_token];
  199. // [LCTools changeRootToTabbar];
  200. }
  201. else
  202. {
  203. [self.navigationController popViewControllerAnimated:YES];
  204. }
  205. }
  206. } failure:^(NSError *error) {
  207. }];
  208. }
  209. }
  210. }
  211. //-(void)youpaifshowBtnClick:(UIButton*)btn{
  212. // if (btn.selected) {
  213. // btn.selected = NO;
  214. // [btn setImage:[UIImage imageNamed:@"vqu_images_sign_input3"] forState:(UIControlStateNormal)];
  215. // self.pwdTextField.secureTextEntry = YES;
  216. // }else{
  217. // btn.selected = YES;
  218. // [btn setImage:[UIImage imageNamed:@"vqu_images_sign_input2"] forState:(UIControlStateNormal)];
  219. // self.pwdTextField.secureTextEntry = NO;
  220. // }
  221. //}
  222. - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
  223. [self.view endEditing:YES];
  224. }
  225. - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
  226. [self.view endEditing:YES];
  227. }
  228. @end