#import "LoginAndRegViewController.h"
#import "UserModel.h"
#import "GlobalModel.h"
#import "AccountTool.h"
#import "NSStringTool.h"
#import "AlertTool.h"
#import "HttpHeader.h"
#import "StringUtil.h"
#import <SSKeychain.h>
#import "UINavigationController+FDFullscreenPopGesture.h"
#import "NSDate+Utilities.h"
#define countDown 300
@interface LoginAndRegViewController ()
{
// 倒計時時間
NSInteger _time;
// 全局模型
GlobalModel *_globalModel;
AccountTool *_accountTool;
}
@property (nonatomic, strong) NSTimer *timer;
// 手機號碼Field
@property (weak, nonatomic) IBOutlet UITextField *phoneNumTF;
// 驗證碼Field
@property (weak, nonatomic) IBOutlet UITextField *checkCodeTF;
// 獲取驗證碼按鈕
@property (weak, nonatomic) IBOutlet UIButton *checkCodeBtn;
// 登錄按鈕
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
@end
@implementation LoginAndRegViewController
#pragma mark - view life circle viewController生命周期方法
- (void)viewDidLoad {
[super viewDidLoad];
[self prepareUI];
self.navigationController.fd_fullscreenPopGestureRecognizer.enabled = NO;
}
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:NO];
if (self.isHiddenBackBtn) {
self.backBtn.hidden = YES;
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark - custom methods 自定義方法
- (void)prepareUI{
self.title = @"登錄";
_globalModel = [GlobalModel sharedGlobalModel];
_accountTool = [[AccountTool alloc] init];
self.loginBtn.enabled = NO;
if ([_accountTool getPhoneNum] != nil) {// 手機號碼不為空
self.phoneNumTF.text = [_accountTool getPhoneNum];
if ([_accountTool getUserToken]) {
[self requestDataWithLogin];
}
}
// 監(jiān)聽TextField.text改變
[self.phoneNumTF addTarget:self action:@selector(textfieldDidChangedText:) forControlEvents:UIControlEventEditingChanged];
[self.checkCodeTF addTarget:self action:@selector(checkCodeTFDidChangedText:) forControlEvents:UIControlEventEditingChanged];
if (_globalModel.codeDate) {
NSInteger countDownSec = [_globalModel.codeDate distanceInSecondsToDate:[NSDate date]];
if (countDownSec < countDown) {
// 驗證碼時間內(nèi)
self.checkCodeBtn.enabled = NO;
_time = countDown - countDownSec;
self.checkCodeBtn.titleLabel.text = [NSString stringWithFormat:@"%zi秒", _time];
[self.checkCodeBtn setTitle:[NSString stringWithFormat:@"%zi秒", _time] forState:UIControlStateNormal];
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeDown) userInfo:nil repeats:YES];
return;
}else{
// 驗證碼時間外
_time = countDown;
}
}
_time = countDown;
}
- (void)timeDown
{
_time --;
if (_time == 0) {
self.checkCodeBtn.titleLabel.text = @"重新獲取";
[self.checkCodeBtn setTitle:@"重新獲取" forState:UIControlStateNormal];
self.checkCodeBtn.enabled = YES;
[_timer invalidate];
_timer = nil;
_time = countDown;
return;
}
self.checkCodeBtn.titleLabel.text = [NSString stringWithFormat:@"%zi秒", _time];
[self.checkCodeBtn setTitle:[NSString stringWithFormat:@"%zi秒", _time] forState:UIControlStateNormal];
}
//
- (void)textfieldDidChangedText:(UITextField *)textfield{
if (textfield.text.length == 11 && [StringUtil isMobile:textfield.text] && self.checkCodeTF.text.length != 0) {
self.loginBtn.enabled = YES;
}else{
self.loginBtn.enabled = NO;
}
}
- (void)checkCodeTFDidChangedText:(UITextField *)textfield{
if (self.checkCodeTF.text.length != 0) {
self.loginBtn.enabled = YES;
}else{
self.loginBtn.enabled = NO;
}
}
#pragma mark 按鈕響應(yīng)
// 登錄按鈕
- (IBAction)loginBtnDidClick:(UIButton *)sender {
// 校驗
if (![StringUtil isMobile:self.phoneNumTF.text]) {
[AlertTool alertWithTipStr:@"手機號碼錯誤,請重新輸入"];
return;
}
// keychain中保存的賬戶數(shù)組
NSArray *accountArr = [SSKeychain accountsForService:KeychainService];
for (NSDictionary *dict in accountArr) {
// 判斷賬戶數(shù)組的賬戶是否與textField中的相匹配
if ([dict[@"acct"] isEqualToString:self.phoneNumTF.text]) {
// keychain中保存過
[self requestDataWithLogin];
}
}
// keychain中沒保存過
[self requestDataWithReg];
}
// 獲取驗證碼按鈕
- (IBAction)checkCodeBtnDidClick:(UIButton *)sender {
// 驗證手機號碼
if (![StringUtil isMobile:self.phoneNumTF.text]) {
[AlertTool alertWithTipStr:@"手機號碼錯誤,請重新輸入"];
return;
}
self.checkCodeBtn.enabled = NO;
self.checkCodeBtn.titleLabel.text = [NSString stringWithFormat:@"%zi秒", _time];
[self.checkCodeBtn setTitle:[NSString stringWithFormat:@"%zi秒", _time] forState:UIControlStateNormal];
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeDown) userInfo:nil repeats:YES];
_globalModel.codeDate = [NSDate date];
// 驗證碼請求
[self requestDataWithValidCode];
}
#pragma mark 網(wǎng)絡(luò)請求
#pragma mark - sources and delegates 代理庶诡、協(xié)議方法
#pragma mark - getters and setters 屬性的設(shè)置和獲取方法
-(void)dealloc{
[self.timer invalidate];
self.timer = nil;
}
@end
NSDateUtils
https://github.com/hychen1024/NSDateUtils