VefiCodeLoginController.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. @interface VefiCodeLoginController ()
  13. {
  14. NSTimer *_time;
  15. int _count;
  16. }
  17. @property (nonatomic, strong) VefiBodyView *bodyView;
  18. @end
  19. @implementation VefiCodeLoginController
  20. - (void)viewDidLoad {
  21. [super viewDidLoad];
  22. // Do any additional setup after loading the view.
  23. self.ks_prefersNavigationBarHidden = YES;
  24. [self configUI];
  25. }
  26. - (void)viewDidAppear:(BOOL)animated {
  27. [super viewDidAppear:animated];
  28. if (![NSString isEmptyString:self.phoneNo]) {
  29. [self queryVefiCode];
  30. }
  31. [_bodyView showKeyboard];
  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. if (@available(iOS 11.0, *)) {
  48. self.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  49. } else {
  50. // Fallback on earlier versions
  51. self.automaticallyAdjustsScrollViewInsets = NO;
  52. }
  53. }
  54. - (void)operationAction:(VEFIACTION)action code:(NSString *)code {
  55. switch (action) {
  56. case VEFIACTION_BACK:
  57. {
  58. [self.navigationController popViewControllerAnimated:YES];
  59. }
  60. break;
  61. case VEFIACTION_VEFIACTION: // 发送验证码
  62. {
  63. [self queryVefiCode];
  64. [self.bodyView showKeyboard];
  65. }
  66. break;
  67. case VEFIACTION_LOGIN: // 登录
  68. {
  69. [self loginAction:code];
  70. }
  71. break;
  72. default:
  73. break;
  74. }
  75. }
  76. - (void)queryVefiCode {
  77. [self showhud];
  78. [KSNetworkingManager sendSmsRequest:KS_POST mobile:self.phoneNo type:@"LOGIN" success:^(NSDictionary * _Nonnull dic) {
  79. [self removehub];
  80. if ([dic ks_integerValueForKey:@"code"] == 200 && [dic ks_boolValueForKey:@"status"]) {
  81. [self MBPShow:@"验证码已发送"];
  82. [self codeButtonCountDown];
  83. }
  84. else {
  85. [self MBPShow:MESSAGEKEY];
  86. }
  87. } faliure:^(NSError * _Nonnull error) {
  88. [self removehub];
  89. }];
  90. }
  91. - (void)codeButtonCountDown {
  92. _bodyView.vefiButton.userInteractionEnabled = NO;
  93. [_bodyView.vefiButton setBackgroundColor:HexRGB(0xd5d5d5)];
  94. __weak typeof(self) weakSelf = self;
  95. _time = [NSTimer scheduledTimerWithTimeInterval:1 target:weakSelf selector:@selector(buttonChangeAction) userInfo:nil repeats:YES];
  96. [[NSRunLoop mainRunLoop] addTimer:_time forMode:NSRunLoopCommonModes];
  97. _count = 89;
  98. }
  99. - (void)clearButtonState {
  100. [_time invalidate];
  101. _time = nil;
  102. _count = 0;
  103. [_bodyView.vefiButton setTitle:@"重新发送" forState:UIControlStateNormal];
  104. [_bodyView.vefiButton setBackgroundColor:THEMECOLOR];
  105. _bodyView.vefiButton.userInteractionEnabled = YES;
  106. }
  107. #pragma mark ---- 验证码按钮状态
  108. - (void)buttonChangeAction {
  109. _bodyView.vefiButton.userInteractionEnabled = NO;
  110. NSString *title = [NSString stringWithFormat:@"重新发送(%d)",_count];
  111. [_bodyView.vefiButton setTitle:title forState:UIControlStateNormal];
  112. _count--;
  113. if(_count == -1){
  114. [self clearButtonState];
  115. }
  116. }
  117. - (void)dealloc {
  118. [_time invalidate];
  119. _time = nil;
  120. }
  121. - (void)loginAction:(NSString *)code {
  122. [self showhud];
  123. [KSNetworkingManager smsLoginRequest:KS_POST phone:self.phoneNo smsCode:code success:^(NSDictionary * _Nonnull dic) {
  124. [self removehub];
  125. if ([dic ks_integerValueForKey:@"code"] == 200 && [dic ks_boolValueForKey:@"status"]) {
  126. NSDictionary *result = [dic ks_dictionaryValueForKey:@"data"];
  127. // 保存用户类型
  128. // UserDefaultSet([result ks_stringValueForKey:@"companyId"], OrganizationIDKey);
  129. UserDefaultSet(self.phoneNo, PHONEKEY);
  130. UserDefaultRemoveObjectForKey(PASSWORDKEY);
  131. NSDictionary *authentication = [result ks_dictionaryValueForKey:@"authentication"];
  132. UserDefaultSet([authentication ks_stringValueForKey:@"access_token"], TokenKey);
  133. UserDefaultSet([authentication ks_stringValueForKey:@"refresh_token"], RefreshToken);
  134. UserDefaultSet([authentication ks_stringValueForKey:@"token_type"], Token_type);
  135. [[NSUserDefaults standardUserDefaults] synchronize];
  136. [KSNetworkingManager configRequestHeader];
  137. BOOL hasPassword = [result ks_boolValueForKey:@"password"];
  138. if (hasPassword) {
  139. [USER_MANAGER queryUserInfoConnectionIMCallback:^(UserInfo * _Nonnull userInfo) {
  140. [self toHomeView];
  141. }];
  142. }
  143. else {
  144. [self settingPasswordWithPhone:self.phoneNo code:code];
  145. }
  146. }
  147. else {
  148. [self MBPShow:MESSAGEKEY];
  149. }
  150. } faliure:^(NSError * _Nonnull error) {
  151. [self removehub];
  152. }];
  153. }
  154. - (void)settingPasswordWithPhone:(NSString *)phone code:(NSString *)code {
  155. NSString *token = UserDefault(TokenKey);
  156. FirstSettingViewController *settingVC = [[FirstSettingViewController alloc] init];
  157. settingVC.phone = phone;
  158. settingVC.code = code;
  159. settingVC.access_token = [token copy];
  160. UserDefaultRemoveObjectForKey(TokenKey);
  161. [self.navigationController pushViewController:settingVC animated:YES];
  162. }
  163. - (void)toHomeView {
  164. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  165. [appDelegate initTableBar];
  166. UITabBarController *tabBarController = appDelegate.tabBarController;
  167. [tabBarController setSelectedIndex:0];
  168. appDelegate.window.rootViewController = tabBarController;
  169. }
  170. /*
  171. #pragma mark - Navigation
  172. // In a storyboard-based application, you will often want to do a little preparation before navigation
  173. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  174. // Get the new view controller using [segue destinationViewController].
  175. // Pass the selected object to the new view controller.
  176. }
  177. */
  178. @end