TCRegisterViewController.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. //
  2. // TCRegisterViewController.m
  3. // TCLVBIMDemo
  4. //
  5. // Created by dackli on 16/10/1.
  6. // Copyright © 2016年 tencent. All rights reserved.
  7. //
  8. #import "TCRegisterViewController.h"
  9. #import "UIView+CustomAutoLayout.h"
  10. #import "TCLoginModel.h"
  11. #import "TXWechatInfoView.h"
  12. #define L(X) NSLocalizedString((X), nil)
  13. @interface TCRegisterViewController ()
  14. @end
  15. @implementation TCRegisterViewController
  16. {
  17. UITextField *_accountTextField; // 用户名/手机号
  18. UITextField *_pwdTextField; // 密码/验证码
  19. UITextField *_pwdTextField2; // 确认密码(用户名注册)
  20. TXWechatInfoView *_wechatInfoView;
  21. UIButton *_regBtn; // 注册
  22. UIView *_lineView1;
  23. UIView *_lineView2;
  24. UIView *_lineView3;
  25. }
  26. - (void)viewDidLoad {
  27. [super viewDidLoad];
  28. [self initUI];
  29. UITapGestureRecognizer *tag = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickScreen)];
  30. [self.view addGestureRecognizer:tag];
  31. }
  32. - (void)didReceiveMemoryWarning {
  33. [super didReceiveMemoryWarning];
  34. // Dispose of any resources that can be recreated.
  35. }
  36. - (void)viewWillAppear:(BOOL)animated {
  37. [super viewWillAppear:animated];
  38. [self.navigationController setNavigationBarHidden:NO];
  39. [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
  40. [self.navigationController.navigationBar setShadowImage:[UIImage new]];
  41. [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
  42. [self.navigationController.navigationBar setBackgroundColor:[UIColor clearColor]];
  43. }
  44. - (void)viewWillDisappear:(BOOL)animated {
  45. [super viewWillDisappear:animated];
  46. [self.navigationController setNavigationBarHidden:YES];
  47. [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
  48. [self.navigationController.navigationBar setShadowImage:nil];
  49. }
  50. - (void)initUI {
  51. UIImage *image = [UIImage imageNamed:@"loginBG"];
  52. self.view.layer.contents = (id)image.CGImage;
  53. _accountTextField = [[UITextField alloc] init];
  54. _accountTextField.font = [UIFont systemFontOfSize:14];
  55. _accountTextField.textColor = [UIColor colorWithWhite:1 alpha:1];
  56. _accountTextField.returnKeyType = UIReturnKeyNext;
  57. _accountTextField.adjustsFontSizeToFitWidth = YES;
  58. _accountTextField.minimumFontSize = 9;
  59. _accountTextField.delegate = self;
  60. _pwdTextField = [[UITextField alloc] init];
  61. _pwdTextField.font = [UIFont systemFontOfSize:14];
  62. _pwdTextField.textColor = [UIColor colorWithWhite:1 alpha:1];
  63. _pwdTextField.returnKeyType = UIReturnKeyNext;
  64. _pwdTextField.delegate = self;
  65. _pwdTextField2 = [[UITextField alloc] init];
  66. _pwdTextField2.font = [UIFont systemFontOfSize:14];
  67. _pwdTextField2.textColor = [UIColor colorWithWhite:1 alpha:1];
  68. _pwdTextField2.secureTextEntry = YES;
  69. [_pwdTextField2 setPlaceholder:NSLocalizedString(@"TCRegisterView.HintConfirmPassword", nil)];
  70. _pwdTextField2.returnKeyType = UIReturnKeyGo;
  71. _pwdTextField2.delegate = self;
  72. _lineView1 = [[UIView alloc] init];
  73. [_lineView1 setBackgroundColor:[UIColor whiteColor]];
  74. _lineView2 = [[UIView alloc] init];
  75. [_lineView2 setBackgroundColor:[UIColor whiteColor]];
  76. _lineView3 = [[UIView alloc] init];
  77. [_lineView3 setBackgroundColor:[UIColor whiteColor]];
  78. _regBtn = [[UIButton alloc] init];
  79. _regBtn.titleLabel.font = [UIFont systemFontOfSize:16];
  80. [_regBtn setTitle:NSLocalizedString(@"TCRegisterView.DoRegister", nil) forState:UIControlStateNormal];
  81. [_regBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  82. [_regBtn setBackgroundImage:[UIImage imageNamed:@"button"] forState:UIControlStateNormal];
  83. [_regBtn setBackgroundImage:[UIImage imageNamed:@"button_pressed"] forState:UIControlStateSelected];
  84. [_regBtn addTarget:self action:@selector(reg:) forControlEvents:UIControlEventTouchUpInside];
  85. TXWechatInfoView *infoView = [[TXWechatInfoView alloc] initWithFrame:CGRectMake(10, _regBtn.bottom+20, self.view.width - 20, 100)];
  86. _wechatInfoView = infoView;
  87. [self.view addSubview:_accountTextField];
  88. [self.view addSubview:_lineView1];
  89. [self.view addSubview:_pwdTextField];
  90. [self.view addSubview:_lineView2];
  91. [self.view addSubview:_pwdTextField2];
  92. [self.view addSubview:_lineView3];
  93. [self.view addSubview:_regBtn];
  94. [self.view addSubview:infoView];
  95. [self relayout];
  96. }
  97. - (void)relayout {
  98. CGFloat screen_width = self.view.bounds.size.width;
  99. [_accountTextField sizeWith:CGSizeMake(screen_width - 50, 33)];
  100. [_accountTextField alignParentTopWithMargin:97];
  101. [_accountTextField alignParentLeftWithMargin:25];
  102. [_lineView1 sizeWith:CGSizeMake(screen_width - 44, 1)];
  103. [_lineView1 layoutBelow:_accountTextField margin:6];
  104. [_lineView1 alignParentLeftWithMargin:22];
  105. [_pwdTextField sizeWith:CGSizeMake(screen_width - 50, 33)];
  106. [_pwdTextField layoutBelow:_lineView1 margin:6];
  107. [_pwdTextField alignParentLeftWithMargin:25];
  108. [_lineView2 sizeWith:CGSizeMake(screen_width - 44, 1)];
  109. [_lineView2 layoutBelow:_pwdTextField margin:6];
  110. [_lineView2 alignParentLeftWithMargin:22];
  111. [_pwdTextField2 sizeWith:CGSizeMake(screen_width - 50, 33)];
  112. [_pwdTextField2 layoutBelow:_lineView2 margin:6];
  113. [_pwdTextField2 alignParentLeftWithMargin:25];
  114. [_lineView3 sizeWith:CGSizeMake(screen_width - 44, 1)];
  115. [_lineView3 layoutBelow:_pwdTextField2 margin:6];
  116. [_lineView3 alignParentLeftWithMargin:22];
  117. [_regBtn sizeWith:CGSizeMake(screen_width - 44, 35)];
  118. [_regBtn layoutBelow:_lineView3 margin:36];
  119. [_regBtn alignParentLeftWithMargin:22];
  120. _wechatInfoView.top = _regBtn.bottom + 30;
  121. [_accountTextField setPlaceholder:NSLocalizedString(@"TCRegisterView.PlaceholderUserName", nil)];
  122. [_accountTextField setText:@""];
  123. _accountTextField.keyboardType = UIKeyboardTypeDefault;
  124. [_pwdTextField setPlaceholder:NSLocalizedString(@"TCRegisterView.PlaceholderPassword", nil)];
  125. [_pwdTextField setText:@""];
  126. [_pwdTextField2 setText:@""];
  127. _pwdTextField.secureTextEntry = YES;
  128. _pwdTextField2.hidden = NO;
  129. _lineView3.hidden = NO;
  130. _accountTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:_accountTextField.placeholder attributes:@{NSForegroundColorAttributeName: [UIColor colorWithWhite:1 alpha:0.5]}];
  131. _pwdTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:_pwdTextField.placeholder attributes:@{NSForegroundColorAttributeName: [UIColor colorWithWhite:1 alpha:0.5]}];
  132. _pwdTextField2.attributedPlaceholder = [[NSAttributedString alloc] initWithString:_pwdTextField2.placeholder attributes:@{NSForegroundColorAttributeName: [UIColor colorWithWhite:1 alpha:0.5]}];
  133. }
  134. - (void)clickScreen {
  135. [_accountTextField resignFirstResponder];
  136. [_pwdTextField resignFirstResponder];
  137. [_pwdTextField2 resignFirstResponder];
  138. }
  139. - (void)reg:(UIButton *)button {
  140. TCLoginModel *loginModel = [TCLoginModel sharedInstance];
  141. NSString *userName = _accountTextField.text;
  142. NSString *failedReason = nil;
  143. if (![loginModel validateUserName:userName failedReason:&failedReason]) {
  144. [HUDHelper alertTitle:NSLocalizedString(@"TCLoginView.HintUserNameError", nil) message:failedReason cancel:NSLocalizedString(@"Common.OK", nil)];
  145. return;
  146. }
  147. NSString *pwd = _pwdTextField.text;
  148. if (![loginModel validatePassword:pwd failedReason:&failedReason]) {
  149. [HUDHelper alertTitle:NSLocalizedString(@"TCLoginView.ErrorPasswordWrong", nil) message:failedReason cancel:NSLocalizedString(@"Common.OK", nil)];
  150. return;
  151. }
  152. NSString *pwd2 = _pwdTextField2.text;
  153. if ([pwd compare:pwd2] != NSOrderedSame) {
  154. [HUDHelper alertTitle:NSLocalizedString(@"TCLoginView.ErrorPasswordWrong", nil) message:NSLocalizedString(@"TCRegisterView.ErrorPasswordConsistency", nil) cancel:NSLocalizedString(@"Common.OK", nil)];
  155. return;
  156. }
  157. // 用户名密码注册
  158. __weak typeof(self) weakSelf = self;
  159. [[HUDHelper sharedInstance] syncLoading];
  160. [[TCLoginModel sharedInstance] registerWithUsername:userName password:pwd succ:^(NSString *userName, NSString *md5pwd) {
  161. // 注册成功后直接登录
  162. [[TCLoginModel sharedInstance] loginWithUsername:userName password:pwd succ:^(NSString* userName, NSString* md5pwd ,NSString *token,NSString *refreshToken,NSInteger expires) {
  163. [[HUDHelper sharedInstance] syncStopLoading];
  164. [weakSelf.loginListener loginOK:userName hashedPwd:md5pwd token:token refreshToken:refreshToken expires:expires];
  165. } fail:^(NSString *userName, int errCode, NSString *errMsg) {
  166. [[HUDHelper sharedInstance] syncStopLoading];
  167. [weakSelf.loginListener loginFail:userName code:errCode message:errMsg];
  168. NSLog(@"%s %d %@", __func__, errCode, errMsg);
  169. }];
  170. [TCUtil report:xiaoshipin_register userName:userName code:200 msg:@"注册成功"];
  171. } fail:^(int errCode, NSString *errMsg) {
  172. [[HUDHelper sharedInstance] syncStopLoading];
  173. NSMutableDictionary *param = [NSMutableDictionary dictionary];
  174. [param setObject:userName forKey:@"userName"];
  175. [param setObject:@"register" forKey:@"action"];
  176. if (errCode == 612) {
  177. [HUDHelper alertTitle:NSLocalizedString(@"Common.Hint", nil) message:NSLocalizedString(@"TCRegisterView.ErrorUserNameRegistered", nil) cancel:NSLocalizedString(@"Common.OK", nil)];
  178. [TCUtil report:xiaoshipin_register userName:userName code:errCode msg:@"用户ID已经被注册"];
  179. }else{
  180. [HUDHelper alertTitle:NSLocalizedString(@"Common.Hint", nil) message:[NSString stringWithFormat:NSLocalizedString(@"Common.HintErrorCode", nil), errCode] cancel:NSLocalizedString(@"Common.OK", nil)];
  181. [TCUtil report:xiaoshipin_register userName:userName code:errCode msg:errMsg];
  182. }
  183. }];
  184. }
  185. #pragma mark - UITextFieldDelegate
  186. - (BOOL)textFieldShouldReturn:(UITextField *)textField{
  187. NSArray<UITextField *> *chain = @[_accountTextField, _pwdTextField, _pwdTextField2];
  188. NSInteger index = [chain indexOfObject:textField];
  189. if (index != NSNotFound) {
  190. if (index < chain.count - 1) {
  191. [chain[index + 1] becomeFirstResponder];
  192. } else {
  193. [textField resignFirstResponder];
  194. [self reg:nil];
  195. }
  196. }
  197. return YES;
  198. }
  199. @end