注册 页面常用的判断知识

//
//  RegistViewController.m
//  O2O
//
//  Created by Apple on 15/9/9.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "RegistViewController.h"
#import "UserInfo.h"
#import "UserInfoDB.h"
#import "NSString+Null.h"
#import "TKAlertCenter.h"
#import "NSString+MD5.h"
#import "CompleteRegistController.h"
#import "UIViewController+KeyBoard.h"
#import "CDPMonitorKeyboard.h"
#import "SVProgressHUD.h"
#import "Config.h"
#import "PrivacyViewController.h"

@interface RegistViewController ()<ProtolDelegate, UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *teleTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField2;
@property (weak, nonatomic) IBOutlet UITextField *idCodeTextfield;
@property (weak, nonatomic) IBOutlet UIButton *agreeBtn;
@property (weak, nonatomic) IBOutlet UIButton *getIdentifyingCodeBtn;
@property (weak, nonatomic) IBOutlet UIButton *codeButton;
@property (weak, nonatomic) IBOutlet UITextField *codeTextField;

@property (assign) BOOL isAgreeUserProtocol;
@property (copy) NSString *sendIdCodeMobilePhone;

@property (nonatomic,strong) BaseHTTPRequestOperationManager * requestManager                          ;
@property (nonatomic) int second;
@property (nonatomic, strong) NSTimer *mytimer;

@property (weak, nonatomic) IBOutlet UIButton *titleButton;

@property(nonatomic, copy) NSString *pathPrivacy;

@end

@implementation RegistViewController

#pragma mark 初始化方法
- (void)viewDidLoad
{
    [super viewDidLoad];
        self.title = @"注册";
    self.agreeBtn.selected = YES;
    self.isAgreeUserProtocol = self.agreeBtn.selected;
    self.teleTextField.delegate = self;
    
    _codeButton.layer.borderColor = MAIN_GREEN_COLOR_NORMAL.CGColor;
    _codeButton.layer.borderWidth = 0.5;
    
    DLog(@"%@",self.view);
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:(UIBarButtonItemStylePlain) target:self action:@selector(validatieInput:)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor colorWithWholeRed:162 green:229 blue:65 alpha:1.0];
    
    [self config];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    self.textFildsArray = [NSMutableArray arrayWithObjects:_passwordTextField,_passwordTextField2,_idCodeTextfield,_teleTextField,_codeTextField, nil];
    
    [self addKeyBoardObsever];
    DLog(@"%@,%@,%@,%@,%@",self.view,_passwordTextField,_passwordTextField2,_idCodeTextfield,_teleTextField);


}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self removeKeyboardObserer];
    DLog(@"%@",self.view);
}

#pragma mark 点击事件

#pragma mark --验证输入
- (IBAction)getIdentifyingCodeClick:(UIButton *)sender
{
    if ([_teleTextField.text isEqualToString:@""]||_teleTextField.text==nil) {
        [self aletrViewWithMessage:@"请输入手机号"];
        return;
    }
    if ([sender.titleLabel.text isEqualToString:@"获取验证码"]) {
        if (self.mytimer) {
            [self.mytimer invalidate];
            self.mytimer = nil;
        }
        self.requestManager.netId = 1003;
        
        NSDictionary * telephoneDictionary = @{@"mobilephone":_teleTextField.text};

        [SVProgressHUD showInView:self.view status:@"获取中..." networkIndicator:NO posY:-1 maskType:SVProgressHUDMaskTypeClear];
        
        [self.requestManager startWithUrl:[BASE_URL stringByAppendingString:@"app/user/register/getcode.json"] data:telephoneDictionary];
    }
}

#pragma mark --同意协议
- (IBAction)agreeBtnClick:(UIButton *)sender
{
    sender.selected = !sender.selected;
    self.isAgreeUserProtocol = sender.selected;
}


#pragma mark --验证输入合法性
- (void)validatieInput:(UIBarButtonItem*)BI
{
    if (!self.isAgreeUserProtocol) {

        [self aletrViewWithMessage:@"您尚未同意条款"];
        DLog(@"您尚未同意条款");
        return;
    }
    NSString* teleNum = [self.teleTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if (teleNum.length !=11) {
        if (teleNum.length == 0) {
            [self aletrViewWithMessage:@"请输入手机号码!"];
            DLog(@"请输入手机号码!");
            return;
        }else{
            [self aletrViewWithMessage:@"请输入正确的手机号码!"];
        }
    }else{
        if (_idCodeTextfield.text.length == 0) {
            [[TKAlertCenter shareInstance] postAlertWithMessage:@"请输入验证码"];
            return;
        }else if ([self.getIdentifyingCodeBtn.titleLabel.text isEqualToString:@"获取验证码"]) {
            [self aletrViewWithMessage:@"验证码已失效,请重新获得验证码"];
            [self.mytimer invalidate];
            self.mytimer = nil;
            return;
        }
        if (self.passwordTextField.text.length == 0) {
            [self aletrViewWithMessage:@"请输入密码"];
            return;
        }else if (self.passwordTextField.text.length < 6 || self.passwordTextField.text.length > 30) {
            [self aletrViewWithMessage:@"密码长度需要6-30个字符!"];
            DLog(@"密码长度需要6-30个字符!");
            return;
        }else{
            if (self.passwordTextField2.text.length == 0) {
                [self aletrViewWithMessage:@"请再次输入您的密码"];
                return;
            }else if (![self.passwordTextField.text isEqualToString:self.passwordTextField2.text]) {
                [self aletrViewWithMessage:@"两次密码输入的不一致"];
                DLog(@"两次密码输入的不一致");
                return;
            }
        }
    }
    

    [self registerUser];
}

#pragma mark data request
- (void)registerUser
{
    NSDictionary *usrInfoDictonary = @{@"mobilephone":_teleTextField.text,@"inputcode":_idCodeTextfield.text,@"password":[_passwordTextField.text md5],@"appversion":APP_VERSION,@"osName":@"ios",@"channelId":@"ddddd", @"recommandCode":_codeTextField.text};

    self.requestManager.netId = 1001;
    
    [SVProgressHUD showInView:self.view status:@"注册中..." networkIndicator:NO posY:-1 maskType:SVProgressHUDMaskTypeClear];
    
    [self.requestManager startWithUrl:[BASE_URL stringByAppendingString:@"/app/user/register/checkcode.json"] data:usrInfoDictonary];

}

- (void)config
{
    self.requestManager.netId = 1002;
    
    [self.requestManager startWithUrl:[BASE_URL stringByAppendingString:@"app/config.json"] data:nil];
    
}

- (void)requestFailure:(int)netId error:(NSError *)error
{
    DLog(@"netId ==== %d,error === %@",netId,error);
}

- (void)requestSuccess:(int)netId responseObject:(JSONObj*)json
{
    int code = json.code;

    [SVProgressHUD dismiss];
    
    if (code == 100) {

        id data = json.data;

        if (netId == 1001) {
            UserInfo* userInfo = [UserInfo objectWithKeyValues:data];
            [UserInfoDB operateUserInfo:userInfo];

            [[TKAlertCenter shareInstance] postAlertWithMessage:@"注册成功!"];
            
            [self.mytimer invalidate];
            self.mytimer = nil;
            [self.getIdentifyingCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
            
            CompleteRegistController* registFishVC = [[CompleteRegistController alloc] init];
            [self.navigationController pushViewController:registFishVC animated:true];
            
        }else if (netId==1002) {
         
            Config *config = [Config objectWithKeyValues:data];
            
            if (config.pathPrivacy.length>0) {
                _agreeBtn.hidden = false;
                _titleButton.hidden = false;
                
                _pathPrivacy = config.pathPrivacy;
            }
            
        }else if (netId == 1003) {

            [[TKAlertCenter shareInstance] postAlertWithMessage:json.msg];
            self.sendIdCodeMobilePhone = [data objectForKey:@"mobilephone"];
            self.second = 60;
            self.mytimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeDown) userInfo:nil repeats:YES];
            self.getIdentifyingCodeBtn.userInteractionEnabled = NO;

        }
    }
    else {
        [[TKAlertCenter shareInstance] postAlertWithMessage:json.msg];
    }
}

#pragma mark 验证码倒计时
- (void)timeDown
{
    self.second -=1;
    if (self.second>0)
    {
        NSString *sec = [NSString stringWithFormat:@"%d",self.second];
        [self.getIdentifyingCodeBtn setTitle:sec forState:UIControlStateNormal];
//        self.getIdentifyingCodeBtn.userInteractionEnabled = NO;
    }else
    {
        [self.getIdentifyingCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        self.getIdentifyingCodeBtn.userInteractionEnabled = YES;
    }
}

- (void)aletrViewWithMessage:(NSString*)mesg
{
    [[TKAlertCenter shareInstance] postAlertWithMessage:mesg];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    
    [self.view endEditing:YES];
    
}
- (IBAction)titleButtonClick:(id)sender
{
    
    PrivacyViewController *controller = [[PrivacyViewController alloc] init];
    controller.urlStr = _pathPrivacy;
    
    [self.navigationController pushViewController:controller animated:true];
    
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (range.location >= 11)
        return NO; // return NO to not change text
    return YES;
}

#pragma mark 懒加载属性
-(BaseHTTPRequestOperationManager *)requestManager
{
    if (!_requestManager)
    {
        _requestManager = [[BaseHTTPRequestOperationManager alloc] init];
        _requestManager.delegate = self;
    }
    return _requestManager;
}



- (void)removeKeyboardObserer{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    
    //移除监听,当键退出时收出消息
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

@end

posted @ 2016-04-18 17:23  筱简單  阅读(350)  评论(0编辑  收藏  举报