VefiCodeLoginController.m 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. 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. }
  65. break;
  66. case VEFIACTION_LOGIN: // 登录
  67. {
  68. [self loginAction:code];
  69. }
  70. break;
  71. default:
  72. break;
  73. }
  74. }
  75. - (void)queryVefiCode {
  76. NSString *phoneNo = self.phoneNo;
  77. // 图形化验证
  78. VeriCheckView *view = [VeriCheckView shareInstanceShowInView:[NSObject getKeyWindow]];
  79. __weak typeof(view) weakView = view;
  80. [view showViewCallback:^(NSDictionary * _Nonnull parm) {
  81. [self veriImageCodeWithParm:parm checkView:weakView];
  82. }];
  83. [view disPlayImageWithPhone:phoneNo];
  84. }
  85. // 发送短信 校验图形验证码
  86. - (void)veriImageCodeWithParm:(NSDictionary *)parm checkView:(VeriCheckView *)checkView {
  87. [LOADING_MANAGER showHUD];
  88. [KSNetworkingManager sendSmsRequest:KS_POST mobile:[parm ks_stringValueForKey:@"phone"] type:@"LOGIN" code:[parm ks_stringValueForKey:@"code"] success:^(NSDictionary * _Nonnull dic) {
  89. [LOADING_MANAGER removeHUD];
  90. if ([dic ks_integerValueForKey:@"code"] == 200 && [dic ks_boolValueForKey:@"status"]) {
  91. [checkView hiddView];
  92. [LOADING_MANAGER MBShowAUTOHidingInWindow:@"验证码已发送"];
  93. [self codeButtonCountDown];
  94. }
  95. else {
  96. [LOADING_MANAGER MBShowAUTOHidingInWindow:MESSAGEKEY];
  97. }
  98. } faliure:^(NSError * _Nonnull error) {
  99. [LOADING_MANAGER removeHUD];
  100. }];
  101. }
  102. - (void)codeButtonCountDown {
  103. _bodyView.vefiButton.userInteractionEnabled = NO;
  104. [_bodyView.vefiButton setBackgroundColor:HexRGB(0xd5d5d5)];
  105. __weak typeof(self) weakSelf = self;
  106. _time = [NSTimer scheduledTimerWithTimeInterval:1 target:weakSelf selector:@selector(buttonChangeAction) userInfo:nil repeats:YES];
  107. [[NSRunLoop mainRunLoop] addTimer:_time forMode:NSRunLoopCommonModes];
  108. _count = 59;
  109. }
  110. - (void)clearButtonState {
  111. [_time invalidate];
  112. _time = nil;
  113. _count = 0;
  114. [_bodyView.vefiButton setTitle:@"重新发送" forState:UIControlStateNormal];
  115. [_bodyView.vefiButton setBackgroundColor:THEMECOLOR];
  116. _bodyView.vefiButton.userInteractionEnabled = YES;
  117. }
  118. #pragma mark ---- 验证码按钮状态
  119. - (void)buttonChangeAction {
  120. _bodyView.vefiButton.userInteractionEnabled = NO;
  121. NSString *title = [NSString stringWithFormat:@"重新发送(%d)",_count];
  122. [_bodyView.vefiButton setTitle:title forState:UIControlStateNormal];
  123. _count--;
  124. if(_count == -1){
  125. [self clearButtonState];
  126. }
  127. }
  128. - (void)dealloc {
  129. [_time invalidate];
  130. _time = nil;
  131. }
  132. - (void)loginAction:(NSString *)code {
  133. [LOADING_MANAGER showHUD];
  134. [KSNetworkingManager smsLoginRequest:KS_POST phone:self.phoneNo smsCode:code success:^(NSDictionary * _Nonnull dic) {
  135. [LOADING_MANAGER removeHUD];
  136. if ([dic ks_integerValueForKey:@"code"] == 200 && [dic ks_boolValueForKey:@"status"]) {
  137. NSDictionary *result = [dic ks_dictionaryValueForKey:@"data"];
  138. UserDefaultSet(self.phoneNo, PHONEKEY);
  139. UserDefaultRemoveObjectForKey(PASSWORDKEY);
  140. NSDictionary *authentication = [result ks_dictionaryValueForKey:@"authentication"];
  141. UserDefaultSet([authentication ks_stringValueForKey:@"access_token"], TokenKey);
  142. UserDefaultSet([authentication ks_stringValueForKey:@"refresh_token"], RefreshToken);
  143. UserDefaultSet([authentication ks_stringValueForKey:@"token_type"], Token_type);
  144. [[NSUserDefaults standardUserDefaults] synchronize];
  145. [KSNetworkingManager configRequestHeader];
  146. BOOL hasPassword = [result ks_boolValueForKey:@"password"];
  147. if (hasPassword) {
  148. [USER_MANAGER queryUserInfoConnectionIMCallback:^(UserInfo * _Nonnull userInfo) {
  149. [self toHomeView];
  150. }];
  151. }
  152. else {
  153. [self settingPasswordWithPhone:self.phoneNo code:code];
  154. }
  155. }
  156. else {
  157. [LOADING_MANAGER MBShowAUTOHidingInWindow:MESSAGEKEY];
  158. }
  159. } faliure:^(NSError * _Nonnull error) {
  160. [LOADING_MANAGER removeHUD];
  161. }];
  162. }
  163. - (void)settingPasswordWithPhone:(NSString *)phone code:(NSString *)code {
  164. NSString *token = UserDefault(TokenKey);
  165. FirstSettingViewController *settingVC = [[FirstSettingViewController alloc] init];
  166. settingVC.phone = phone;
  167. settingVC.code = code;
  168. settingVC.access_token = [token copy];
  169. UserDefaultRemoveObjectForKey(TokenKey);
  170. [self.navigationController pushViewController:settingVC animated:YES];
  171. }
  172. - (void)toHomeView {
  173. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  174. [appDelegate initTableBar];
  175. [appDelegate refreshThemeColorAndConfig];
  176. UITabBarController *tabBarController = appDelegate.tabBarController;
  177. [tabBarController setSelectedIndex:0];
  178. appDelegate.window.rootViewController = tabBarController;
  179. }
  180. /*
  181. #pragma mark - Navigation
  182. // In a storyboard-based application, you will often want to do a little preparation before navigation
  183. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  184. // Get the new view controller using [segue destinationViewController].
  185. // Pass the selected object to the new view controller.
  186. }
  187. */
  188. @end