123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- //
- // VefiCodeLoginController.m
- // KulexiuForTeacher
- //
- // Created by Kyle on 2022/3/18.
- //
- #import "VefiCodeLoginController.h"
- #import "VefiBodyView.h"
- #import "FirstSettingViewController.h"
- #import "AppDelegate.h"
- #import "UserInfoManager.h"
- @interface VefiCodeLoginController ()
- {
- NSTimer *_time;
- int _count;
- }
- @property (nonatomic, strong) VefiBodyView *bodyView;
- @end
- @implementation VefiCodeLoginController
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
- self.ks_prefersNavigationBarHidden = YES;
- [self configUI];
-
- }
- - (void)viewDidAppear:(BOOL)animated {
- [super viewDidAppear:animated];
- if (![NSString isEmptyString:self.phoneNo]) {
- [self queryVefiCode];
- }
- [_bodyView showKeyboard];
- }
- - (void)configUI {
- CGFloat height = KPortraitHeight;
- _bodyView = [VefiBodyView shareInstance];
- _bodyView.phoneText.text = self.phoneNo;
- [self.scrollView addSubview:_bodyView];
- [_bodyView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.mas_equalTo(self.scrollView.mas_top);
- make.right.left.mas_equalTo(self.view);
- make.height.mas_equalTo(height);
- }];
- MJWeakSelf;
- [_bodyView vefiLoginCallback:^(VEFIACTION action, NSString * _Nonnull code) {
- [weakSelf operationAction:action code:code];
- }];
- if (@available(iOS 11.0, *)) {
- self.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
- } else {
- // Fallback on earlier versions
- self.automaticallyAdjustsScrollViewInsets = NO;
- }
-
- }
- - (void)operationAction:(VEFIACTION)action code:(NSString *)code {
- switch (action) {
- case VEFIACTION_BACK:
- {
- [self.navigationController popViewControllerAnimated:YES];
- }
- break;
- case VEFIACTION_VEFIACTION: // 发送验证码
- {
- [self queryVefiCode];
- [self.bodyView showKeyboard];
- }
- break;
- case VEFIACTION_LOGIN: // 登录
- {
- [self loginAction:code];
- }
- break;
- default:
- break;
- }
- }
- - (void)queryVefiCode {
- [self showhud];
- [KSNetworkingManager sendSmsRequest:KS_POST mobile:self.phoneNo type:@"LOGIN" success:^(NSDictionary * _Nonnull dic) {
- [self removehub];
- if ([dic ks_integerValueForKey:@"code"] == 200 && [dic ks_boolValueForKey:@"status"]) {
- [self MBPShow:@"验证码已发送"];
- [self codeButtonCountDown];
- }
- else {
- [self MBPShow:MESSAGEKEY];
- }
- } faliure:^(NSError * _Nonnull error) {
- [self removehub];
- }];
- }
- - (void)codeButtonCountDown {
- _bodyView.vefiButton.userInteractionEnabled = NO;
- [_bodyView.vefiButton setBackgroundColor:HexRGB(0xd5d5d5)];
- __weak typeof(self) weakSelf = self;
- _time = [NSTimer scheduledTimerWithTimeInterval:1 target:weakSelf selector:@selector(buttonChangeAction) userInfo:nil repeats:YES];
- [[NSRunLoop mainRunLoop] addTimer:_time forMode:NSRunLoopCommonModes];
- _count = 89;
- }
- - (void)clearButtonState {
- [_time invalidate];
- _time = nil;
- _count = 0;
- [_bodyView.vefiButton setTitle:@"重新发送" forState:UIControlStateNormal];
- [_bodyView.vefiButton setBackgroundColor:THEMECOLOR];
- _bodyView.vefiButton.userInteractionEnabled = YES;
- }
- #pragma mark ---- 验证码按钮状态
- - (void)buttonChangeAction {
- _bodyView.vefiButton.userInteractionEnabled = NO;
- NSString *title = [NSString stringWithFormat:@"重新发送(%d)",_count];
- [_bodyView.vefiButton setTitle:title forState:UIControlStateNormal];
- _count--;
- if(_count == -1){
- [self clearButtonState];
- }
- }
- - (void)dealloc {
- [_time invalidate];
- _time = nil;
- }
- - (void)loginAction:(NSString *)code {
-
- [self showhud];
- [KSNetworkingManager smsLoginRequest:KS_POST phone:self.phoneNo smsCode:code success:^(NSDictionary * _Nonnull dic) {
- [self removehub];
- if ([dic ks_integerValueForKey:@"code"] == 200 && [dic ks_boolValueForKey:@"status"]) {
- NSDictionary *result = [dic ks_dictionaryValueForKey:@"data"];
- // 保存用户类型
- // UserDefaultSet([result ks_stringValueForKey:@"companyId"], OrganizationIDKey);
- UserDefaultSet(self.phoneNo, PHONEKEY);
- UserDefaultRemoveObjectForKey(PASSWORDKEY);
- NSDictionary *authentication = [result ks_dictionaryValueForKey:@"authentication"];
- UserDefaultSet([authentication ks_stringValueForKey:@"access_token"], TokenKey);
- UserDefaultSet([authentication ks_stringValueForKey:@"refresh_token"], RefreshToken);
- UserDefaultSet([authentication ks_stringValueForKey:@"token_type"], Token_type);
- [[NSUserDefaults standardUserDefaults] synchronize];
- [KSNetworkingManager configRequestHeader];
-
- BOOL hasPassword = [result ks_boolValueForKey:@"password"];
- if (hasPassword) {
- [USER_MANAGER queryUserInfoConnectionIMCallback:^(UserInfo * _Nonnull userInfo) {
- [self toHomeView];
- }];
- }
- else {
- [self settingPasswordWithPhone:self.phoneNo code:code];
- }
- }
- else {
- [self MBPShow:MESSAGEKEY];
- }
- } faliure:^(NSError * _Nonnull error) {
- [self removehub];
- }];
- }
- - (void)settingPasswordWithPhone:(NSString *)phone code:(NSString *)code {
- NSString *token = UserDefault(TokenKey);
- FirstSettingViewController *settingVC = [[FirstSettingViewController alloc] init];
- settingVC.phone = phone;
- settingVC.code = code;
- settingVC.access_token = [token copy];
- UserDefaultRemoveObjectForKey(TokenKey);
- [self.navigationController pushViewController:settingVC animated:YES];
-
- }
- - (void)toHomeView {
- AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
- [appDelegate initTableBar];
- UITabBarController *tabBarController = appDelegate.tabBarController;
- [tabBarController setSelectedIndex:0];
- appDelegate.window.rootViewController = tabBarController;
- }
- /*
- #pragma mark - Navigation
- // In a storyboard-based application, you will often want to do a little preparation before navigation
- - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
- // Get the new view controller using [segue destinationViewController].
- // Pass the selected object to the new view controller.
- }
- */
- @end
|