TCLoginViewController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. //
  2. // TCLoginViewController.m
  3. // TCLVBIMDemo
  4. //
  5. // Created by dackli on 16/8/1.
  6. // Copyright © 2016年 tencent. All rights reserved.
  7. //
  8. #import "TCLoginViewController.h"
  9. #import "TCLoginModel.h"
  10. #import "TCUtil.h"
  11. #import "TCLoginParam.h"
  12. #import "TCRegisterViewController.h"
  13. #import "UIView+CustomAutoLayout.h"
  14. #import "HUDHelper.h"
  15. #import "TCRegisterViewController.h"
  16. #import "TCUserInfoModel.h"
  17. #import "SmallButton.h"
  18. #import "TXWechatInfoView.h"
  19. @interface TCLoginViewController ()<UITextFieldDelegate,TCLoginListener, UIGestureRecognizerDelegate>
  20. {
  21. TCLoginParam *_loginParam;
  22. UITextField *_accountTextField; // 用户名/手机号
  23. UITextField *_pwdTextField; // 密码/验证码
  24. UIButton *_loginBtn; // 登录
  25. UIButton *_regBtn; // 注册
  26. TXWechatInfoView *_wechatInfoView; // 公众号信息
  27. UIView *_lineView1;
  28. UIView *_lineView2;
  29. BOOL _isSMSLoginType; // YES 表示手机号登录,NO 表示用户名登录
  30. }
  31. @end
  32. @implementation TCLoginViewController
  33. - (void)viewDidLoad {
  34. [super viewDidLoad];
  35. _loginParam = [TCLoginParam shareInstance];
  36. BOOL isAutoLogin = [TCLoginModel isAutoLogin];
  37. [self pullLoginUI];
  38. if (isAutoLogin && [_loginParam isValid]) {
  39. [self autoLogin];
  40. }else {
  41. }
  42. }
  43. - (void)viewWillAppear:(BOOL)animated {
  44. [super viewWillAppear:animated];
  45. [self.navigationController setNavigationBarHidden:YES];
  46. }
  47. - (void)didReceiveMemoryWarning {
  48. [super didReceiveMemoryWarning];
  49. // Dispose of any resources that can be recreated.
  50. }
  51. #pragma mark - login
  52. - (void)autoLogin {
  53. if ([_loginParam isExpired]) {
  54. // 刷新票据
  55. __weak typeof(self) weakSelf = self;
  56. [[HUDHelper sharedInstance] syncLoading:NSLocalizedString(@"TCLoginView.AutoLoggingIn", nil)];
  57. [[TCLoginModel sharedInstance] login:_loginParam.identifier hashPwd:_loginParam.hashedPwd succ:^(NSString* userName, NSString* md5pwd ,NSString *token,NSString *refreshToken,NSInteger expires) {
  58. [weakSelf loginOK:userName hashedPwd:md5pwd token:token refreshToken:refreshToken expires:expires];
  59. [[HUDHelper sharedInstance] syncStopLoading];
  60. [self dismissViewControllerAnimated:YES completion:nil];
  61. // [[AppDelegate sharedAppDelegate] enterMainUI];
  62. } fail:^(NSString *userName, int errCode, NSString *errMsg) {
  63. [[HUDHelper sharedInstance] syncStopLoading];
  64. [weakSelf loginFail:userName code:errCode message:errMsg];
  65. [[HUDHelper sharedInstance] syncLoading:NSLocalizedString(@"TCLoginView.AutoLoggingIn", nil)];
  66. [self pullLoginUI];
  67. }];
  68. }
  69. else {
  70. [self pullLoginUI];
  71. }
  72. }
  73. - (void)pullLoginUI {
  74. if (_accountTextField == nil) {
  75. [self setupUI];
  76. } else {
  77. [self relayout];
  78. }
  79. }
  80. - (void)setupUI {
  81. _isSMSLoginType = NO;
  82. [self initUI];
  83. UITapGestureRecognizer *tag = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
  84. [self.view addGestureRecognizer:tag];
  85. }
  86. - (void)initUI {
  87. UIImage *image = [UIImage imageNamed:@"loginBG"];
  88. self.view.layer.contents = (id)image.CGImage;
  89. UIButton *backButton = [SmallButton buttonWithType:UIButtonTypeCustom];
  90. [backButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
  91. [backButton addTarget:self action:@selector(onGoBack:) forControlEvents:UIControlEventTouchUpInside];
  92. CGFloat top = [UIApplication sharedApplication].statusBarFrame.size.height;
  93. backButton.frame = CGRectMake(15 * kScaleX, MAX(top+5, 30 * kScaleY), 14 , 23);
  94. [self.view addSubview:backButton];
  95. _accountTextField = [[UITextField alloc] init];
  96. _accountTextField.font = [UIFont systemFontOfSize:14];
  97. _accountTextField.textColor = [UIColor colorWithWhite:1 alpha:1];
  98. _accountTextField.returnKeyType = UIReturnKeyNext;
  99. _accountTextField.delegate = self;
  100. _pwdTextField = [[UITextField alloc] init];
  101. _pwdTextField.font = [UIFont systemFontOfSize:14];
  102. _pwdTextField.textColor = [UIColor colorWithWhite:1 alpha:1];
  103. _pwdTextField.returnKeyType = UIReturnKeyGo;
  104. _pwdTextField.delegate = self;
  105. _lineView1 = [[UIView alloc] init];
  106. [_lineView1 setBackgroundColor:[UIColor whiteColor]];
  107. _lineView2 = [[UIView alloc] init];
  108. [_lineView2 setBackgroundColor:[UIColor whiteColor]];
  109. _loginBtn = [[UIButton alloc] init];
  110. _loginBtn.titleLabel.font = [UIFont systemFontOfSize:16];
  111. [_loginBtn setTitle:NSLocalizedString(@"TCLoginView.Login", nil) forState:UIControlStateNormal];
  112. [_loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  113. [_loginBtn setBackgroundImage:[UIImage imageNamed:@"button"] forState:UIControlStateNormal];
  114. [_loginBtn setBackgroundImage:[UIImage imageNamed:@"button_pressed"] forState:UIControlStateSelected];
  115. [_loginBtn addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside];
  116. _regBtn = [[UIButton alloc] init];
  117. _regBtn.titleLabel.font = [UIFont systemFontOfSize:14];
  118. [_regBtn setTitleColor:[UIColor colorWithWhite:1 alpha:0.5] forState:UIControlStateNormal];
  119. [_regBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
  120. [_regBtn setTitle:NSLocalizedString(@"TCLoginView.Register", nil) forState:UIControlStateNormal];
  121. [_regBtn addTarget:self action:@selector(reg:) forControlEvents:UIControlEventTouchUpInside];
  122. TXWechatInfoView *infoView = [[TXWechatInfoView alloc] initWithFrame:CGRectMake(10, _regBtn.bottom+20, self.view.width - 20, 100)];
  123. _wechatInfoView = infoView;
  124. [self.view addSubview:_accountTextField];
  125. [self.view addSubview:_lineView1];
  126. [self.view addSubview:_pwdTextField];
  127. [self.view addSubview:_lineView2];
  128. [self.view addSubview:_loginBtn];
  129. [self.view addSubview:_regBtn];
  130. [self.view addSubview:infoView];
  131. UISwipeGestureRecognizer *gesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(onGoBack:)];
  132. gesture.delegate = self;
  133. gesture.direction = UISwipeGestureRecognizerDirectionRight;
  134. [self.view addGestureRecognizer:gesture];
  135. [self relayout];
  136. }
  137. - (void)relayout {
  138. CGFloat screen_width = self.view.bounds.size.width;
  139. [_accountTextField sizeWith:CGSizeMake(screen_width - 50, 33)];
  140. [_accountTextField alignParentTopWithMargin:97];
  141. [_accountTextField alignParentLeftWithMargin:25];
  142. [_lineView1 sizeWith:CGSizeMake(screen_width - 44, 1)];
  143. [_lineView1 layoutBelow:_accountTextField margin:6];
  144. [_lineView1 alignParentLeftWithMargin:22];
  145. if (_isSMSLoginType) {
  146. [_pwdTextField sizeWith:CGSizeMake(150, 33)];
  147. } else {
  148. [_pwdTextField sizeWith:CGSizeMake(screen_width - 50, 33)];
  149. }
  150. [_pwdTextField layoutBelow:_lineView1 margin:6];
  151. [_pwdTextField alignParentLeftWithMargin:25];
  152. [_lineView2 sizeWith:CGSizeMake(screen_width - 44, 1)];
  153. [_lineView2 layoutBelow:_pwdTextField margin:6];
  154. [_lineView2 alignParentLeftWithMargin:22];
  155. [_loginBtn sizeWith:CGSizeMake(screen_width - 44, 35)];
  156. [_loginBtn layoutBelow:_lineView2 margin:36];
  157. [_loginBtn alignParentLeftWithMargin:22];
  158. [_regBtn sizeToFit];
  159. [_regBtn sizeWith:CGSizeMake(_regBtn.frame.size.width, 15)];
  160. [_regBtn layoutBelow:_loginBtn margin:25];
  161. [_regBtn alignParentRightWithMargin:25];
  162. _wechatInfoView.top = _regBtn.bottom + 30;
  163. [_accountTextField setPlaceholder:NSLocalizedString(@"TCLoginView.PlaceholderEnterUserName", nil)];
  164. [_accountTextField setText:@""];
  165. _accountTextField.keyboardType = UIKeyboardTypeDefault;
  166. [_pwdTextField setPlaceholder:NSLocalizedString(@"TCLoginView.PlaceholderEnterPassword", nil)];
  167. [_pwdTextField setText:@""];
  168. _pwdTextField.secureTextEntry = YES;
  169. _accountTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:_accountTextField.placeholder attributes:@{NSForegroundColorAttributeName: [UIColor colorWithWhite:1 alpha:0.5]}];
  170. _pwdTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:_pwdTextField.placeholder attributes:@{NSForegroundColorAttributeName: [UIColor colorWithWhite:1 alpha:0.5]}];
  171. }
  172. - (void)onGoBack:(id)sender {
  173. [self dismissViewControllerAnimated:YES completion:nil];
  174. // [[AppDelegate sharedAppDelegate] enterMainUI];
  175. }
  176. - (void)hideKeyboard {
  177. [self.view endEditing:YES];
  178. }
  179. - (void)reg:(UIButton *)button {
  180. TCRegisterViewController *regViewController = [[TCRegisterViewController alloc] init];
  181. regViewController.loginListener = self;
  182. [self.navigationController pushViewController:regViewController animated:YES];
  183. }
  184. - (void)switchLoginWay:(UIButton *)button {
  185. _isSMSLoginType = !_isSMSLoginType;
  186. [self hideKeyboard];
  187. [self relayout];
  188. }
  189. - (void)login:(UIButton *)button {
  190. NSString *userName = _accountTextField.text;
  191. NSString *failedReason = nil;
  192. if (![[TCLoginModel sharedInstance] validateUserName:userName failedReason:&failedReason]) {
  193. [HUDHelper alertTitle:NSLocalizedString(@"TCLoginView.HintUserNameError", nil)
  194. message:failedReason cancel:NSLocalizedString(@"Common.OK", nil)];
  195. return;
  196. }
  197. NSString *pwd = _pwdTextField.text;
  198. if (![[TCLoginModel sharedInstance] validatePassword:pwd failedReason:&failedReason]) {
  199. [HUDHelper alertTitle:NSLocalizedString(@"TCLoginView.HintPasswordError", nil) message:failedReason cancel:NSLocalizedString(@"Common.OK", nil)];
  200. return;
  201. }
  202. // 用户名密码登录
  203. [self hideKeyboard];
  204. [[HUDHelper sharedInstance] syncLoading];
  205. __weak __typeof(self) weakSelf = self;
  206. [[TCLoginModel sharedInstance] loginWithUsername:userName password:pwd succ:^(NSString* userName, NSString* md5pwd ,NSString *token,NSString *refreshToken,NSInteger expires) {
  207. [[HUDHelper sharedInstance] syncStopLoading];
  208. [weakSelf loginOK:userName hashedPwd:md5pwd token:token refreshToken:refreshToken expires:expires];
  209. } fail:^(NSString *userName, int errCode, NSString *errMsg) {
  210. [[HUDHelper sharedInstance] syncStopLoading];
  211. [weakSelf loginFail:userName code:errCode message:errMsg];
  212. NSLog(@"%s %d %@", __func__, errCode, errMsg);
  213. }];
  214. }
  215. - (void)loginFail:(NSString*)userName code:(int)errCode message:(NSString *)errMsg{
  216. NSMutableDictionary *param = [NSMutableDictionary dictionary];
  217. [param setObject:userName forKey:@"userName"];
  218. [param setObject:@"login" forKey:@"action"];
  219. NSString *loginFailedTitle = NSLocalizedString(@"TCLoginView.HintLoginFailed", nil);
  220. if(errCode == 620){
  221. [HUDHelper alertTitle:loginFailedTitle
  222. message:NSLocalizedString(@"TCLoginView.ErrorAccountNotExists", nil)
  223. cancel:NSLocalizedString(@"Common.OK", nil)];
  224. [TCUtil report:xiaoshipin_login userName:userName code:errCode msg:@"账号未注册"];
  225. }
  226. else if(errCode == 621){
  227. [HUDHelper alertTitle:loginFailedTitle
  228. message:NSLocalizedString(@"TCLoginView.ErrorPasswordWrong", nil)
  229. cancel:NSLocalizedString(@"Common.OK", nil)];
  230. [TCUtil report:xiaoshipin_login userName:userName code:errCode msg:@"密码错误"];
  231. }
  232. else{
  233. [HUDHelper alertTitle:loginFailedTitle message:[NSString stringWithFormat:NSLocalizedString(@"Common.HintErrorCode", nil), errCode] cancel:NSLocalizedString(@"Common.OK", nil)];
  234. [TCUtil report:xiaoshipin_login userName:userName code:errCode msg:errMsg];
  235. }
  236. }
  237. - (void)loginOK:(NSString*)userName hashedPwd:(NSString*)hashedPwd token:(NSString *)token refreshToken:(NSString *)refreshToken expires:(NSInteger)expires
  238. {
  239. // 进入主界面
  240. _loginParam.identifier = userName;
  241. _loginParam.hashedPwd = hashedPwd;
  242. _loginParam.token = token;
  243. _loginParam.tokenTime = [[NSDate date] timeIntervalSince1970];
  244. _loginParam.refreshToken = refreshToken;
  245. _loginParam.expires = expires;
  246. [_loginParam saveToLocal];
  247. [[TCLoginModel sharedInstance] scheduleRefreshLoginForExpireDate:_loginParam.expireDate];
  248. //[[AppDelegate sharedAppDelegate] enterMainUI];
  249. [TCUtil report:xiaoshipin_login userName:userName code:200 msg:@"登录成功"];
  250. [self dismissViewControllerAnimated:YES completion:nil];
  251. }
  252. #pragma mark - UITextFieldDelegate
  253. - (BOOL)textFieldShouldReturn:(UITextField *)textField{
  254. NSArray<UITextField *> *chain = @[_accountTextField, _pwdTextField];
  255. NSInteger index = [chain indexOfObject:textField];
  256. if (index != NSNotFound) {
  257. if (index < chain.count - 1) {
  258. [chain[index + 1] becomeFirstResponder];
  259. } else {
  260. [textField resignFirstResponder];
  261. [self login:nil];
  262. }
  263. }
  264. return YES;
  265. }
  266. #pragma mark - UIGestureRecognizerDelegate
  267. - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
  268. {
  269. CGPoint location = [gestureRecognizer locationInView:self.view];
  270. UIView *view = [self.view hitTest:location withEvent:nil];
  271. return ![view isKindOfClass:[UIControl class]] && location.x <= 20;
  272. }
  273. @end