VefiCodeLoginController.m 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. //
  2. // VefiCodeLoginController.m
  3. // KulexiuForTeacher
  4. //
  5. // Created by Kyle on 2022/3/18.
  6. //
  7. #import "VefiCodeLoginController.h"
  8. #import "VefiBodyView.h"
  9. #import "FirstSettingViewController.h"
  10. #import "AppDelegate.h"
  11. #import "UserInfoManager.h"
  12. #import "VeriCheckView.h"
  13. @interface VefiCodeLoginController ()
  14. {
  15. NSTimer *_time;
  16. int _count;
  17. }
  18. @property (nonatomic, strong) VefiBodyView *bodyView;
  19. @end
  20. @implementation VefiCodeLoginController
  21. - (void)viewDidLoad {
  22. [super viewDidLoad];
  23. // Do any additional setup after loading the view.
  24. self.ks_prefersNavigationBarHidden = YES;
  25. [self configUI];
  26. }
  27. - (void)viewDidAppear:(BOOL)animated {
  28. [super viewDidAppear:animated];
  29. if (![NSString isEmptyString:self.phoneNo]) {
  30. [self queryVefiCode];
  31. }
  32. }
  33. - (void)configUI {
  34. CGFloat height = KPortraitHeight;
  35. _bodyView = [VefiBodyView shareInstance];
  36. _bodyView.phoneText.text = self.phoneNo;
  37. [self.scrollView addSubview:_bodyView];
  38. [_bodyView mas_makeConstraints:^(MASConstraintMaker *make) {
  39. make.top.mas_equalTo(self.scrollView.mas_top);
  40. make.right.left.mas_equalTo(self.view);
  41. make.height.mas_equalTo(height);
  42. }];
  43. MJWeakSelf;
  44. [_bodyView vefiLoginCallback:^(VEFIACTION action, NSString * _Nonnull code) {
  45. [weakSelf operationAction:action code:code];
  46. }];
  47. self.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  48. }
  49. - (void)operationAction:(VEFIACTION)action code:(NSString *)code {
  50. switch (action) {
  51. case VEFIACTION_BACK:
  52. {
  53. [self.navigationController popViewControllerAnimated:YES];
  54. }
  55. break;
  56. case VEFIACTION_VEFIACTION: // 发送验证码
  57. {
  58. [self queryVefiCode];
  59. }
  60. break;
  61. case VEFIACTION_LOGIN: // 登录
  62. {
  63. [self loginAction:code];
  64. }
  65. break;
  66. default:
  67. break;
  68. }
  69. }
  70. - (void)queryVefiCode {
  71. NSString *phoneNo = self.phoneNo;
  72. // 图形化验证
  73. VeriCheckView *view = [VeriCheckView shareInstanceShowInView:[NSObject getKeyWindow]];
  74. __weak typeof(view) weakView = view;
  75. [view showViewCallback:^(NSDictionary * _Nonnull parm) {
  76. [self veriImageCodeWithParm:parm checkView:weakView];
  77. }];
  78. [view cancelCheck:^{
  79. [self displayKeyboard];
  80. }];
  81. [view disPlayImageWithPhone:phoneNo];
  82. }
  83. - (void)displayKeyboard {
  84. [self.bodyView showKeyboard];
  85. }
  86. // 发送短信 校验图形验证码
  87. - (void)veriImageCodeWithParm:(NSDictionary *)parm checkView:(VeriCheckView *)checkView {
  88. [LOADING_MANAGER showHUD];
  89. [KSNetworkingManager sendSmsRequest:KS_POST mobile:[parm ks_stringValueForKey:@"phone"] type:@"LOGIN" code:[parm ks_stringValueForKey:@"code"] success:^(NSDictionary * _Nonnull dic) {
  90. [LOADING_MANAGER removeHUD];
  91. if ([dic ks_integerValueForKey:@"code"] == 200 && [dic ks_boolValueForKey:@"status"]) {
  92. [checkView hiddView];
  93. [LOADING_MANAGER MBShowAUTOHidingInWindow:@"验证码已发送"];
  94. [self codeButtonCountDown];
  95. }
  96. else {
  97. [LOADING_MANAGER MBShowAUTOHidingInWindow:MESSAGEKEY];
  98. }
  99. } faliure:^(NSError * _Nonnull error) {
  100. [LOADING_MANAGER removeHUD];
  101. }];
  102. }
  103. - (void)codeButtonCountDown {
  104. _bodyView.vefiButton.userInteractionEnabled = NO;
  105. [_bodyView.vefiButton setBackgroundColor:HexRGB(0xd5d5d5)];
  106. __weak typeof(self) weakSelf = self;
  107. _time = [NSTimer scheduledTimerWithTimeInterval:1 target:weakSelf selector:@selector(buttonChangeAction) userInfo:nil repeats:YES];
  108. [[NSRunLoop mainRunLoop] addTimer:_time forMode:NSRunLoopCommonModes];
  109. _count = 59;
  110. }
  111. - (void)clearButtonState {
  112. [_time invalidate];
  113. _time = nil;
  114. _count = 0;
  115. [_bodyView.vefiButton setTitle:@"重新发送" forState:UIControlStateNormal];
  116. [_bodyView.vefiButton setBackgroundColor:THEMECOLOR];
  117. _bodyView.vefiButton.userInteractionEnabled = YES;
  118. }
  119. #pragma mark ---- 验证码按钮状态
  120. - (void)buttonChangeAction {
  121. _bodyView.vefiButton.userInteractionEnabled = NO;
  122. NSString *title = [NSString stringWithFormat:@"重新发送(%d)",_count];
  123. [_bodyView.vefiButton setTitle:title forState:UIControlStateNormal];
  124. _count--;
  125. if(_count == -1){
  126. [self clearButtonState];
  127. }
  128. }
  129. - (void)dealloc {
  130. [_time invalidate];
  131. _time = nil;
  132. }
  133. - (void)loginAction:(NSString *)code {
  134. [LOADING_MANAGER showHUD];
  135. [KSNetworkingManager smsLoginRequest:KS_POST phone:self.phoneNo smsCode:code success:^(NSDictionary * _Nonnull dic) {
  136. [LOADING_MANAGER removeHUD];
  137. if ([dic ks_integerValueForKey:@"code"] == 200 && [dic ks_boolValueForKey:@"status"]) {
  138. NSDictionary *result = [dic ks_dictionaryValueForKey:@"data"];
  139. UserDefaultSet(self.phoneNo, PHONEKEY);
  140. UserDefaultRemoveObjectForKey(PASSWORDKEY);
  141. NSDictionary *authentication = [result ks_dictionaryValueForKey:@"authentication"];
  142. UserDefaultSet([authentication ks_stringValueForKey:@"access_token"], TokenKey);
  143. UserDefaultSet([authentication ks_stringValueForKey:@"refresh_token"], RefreshToken);
  144. UserDefaultSet([authentication ks_stringValueForKey:@"token_type"], Token_type);
  145. [[NSUserDefaults standardUserDefaults] synchronize];
  146. [KSNetworkingManager configRequestHeader];
  147. BOOL hasPassword = [result ks_boolValueForKey:@"password"];
  148. if (hasPassword) {
  149. [USER_MANAGER queryUserInfoConnectionIMCallback:^(UserInfo * _Nonnull userInfo) {
  150. [self toHomeView];
  151. }];
  152. }
  153. else {
  154. [self settingPasswordWithPhone:self.phoneNo code:code];
  155. }
  156. }
  157. else {
  158. [LOADING_MANAGER MBShowAUTOHidingInWindow:MESSAGEKEY];
  159. }
  160. } faliure:^(NSError * _Nonnull error) {
  161. [LOADING_MANAGER removeHUD];
  162. }];
  163. }
  164. - (void)settingPasswordWithPhone:(NSString *)phone code:(NSString *)code {
  165. NSString *token = UserDefault(TokenKey);
  166. FirstSettingViewController *settingVC = [[FirstSettingViewController alloc] init];
  167. settingVC.phone = phone;
  168. settingVC.code = code;
  169. settingVC.access_token = [token copy];
  170. UserDefaultRemoveObjectForKey(TokenKey);
  171. [self.navigationController pushViewController:settingVC animated:YES];
  172. }
  173. - (void)toHomeView {
  174. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  175. [appDelegate initTableBar];
  176. [appDelegate refreshThemeColorAndConfig];
  177. UITabBarController *tabBarController = appDelegate.tabBarController;
  178. [tabBarController setSelectedIndex:0];
  179. appDelegate.window.rootViewController = tabBarController;
  180. }
  181. /*
  182. #pragma mark - Navigation
  183. // In a storyboard-based application, you will often want to do a little preparation before navigation
  184. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  185. // Get the new view controller using [segue destinationViewController].
  186. // Pass the selected object to the new view controller.
  187. }
  188. */
  189. @end