RealNameAuthenticationViewController.m 12 KB
//
//  RealNameAuthenticationViewController.m
//  IVTMeLiveTwo
//
//  Created by XRG on 16/9/5.
//  Copyright © 2016年 QLQ. All rights reserved.
//

#import "RealNameAuthenticationViewController.h"
#import "AppDelegate.h"
#import "NSString+IVTMeString.h"

@interface RealNameAuthenticationViewController ()<UITextFieldDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate>
{
    UIButton *currentButton;
    NSData *imageData1;
    NSData *imageData2;
    NSData *imageData3;

}

@property (weak, nonatomic) IBOutlet UIScrollView *myScrollView;

@property (weak, nonatomic) IBOutlet UITextField *realNameTextField;

@property (weak, nonatomic) IBOutlet UITextField *phoneNumberTextField;

@property (weak, nonatomic) IBOutlet UITextField *iCardNumberTextField;

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

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

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

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

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


@property (strong, nonatomic) IBOutlet UIView *rejectView;
@property (weak, nonatomic) IBOutlet UILabel *rejectReasonLabel;

@property (strong, nonatomic) IBOutlet UIView *authenticationingView;

@property (strong, nonatomic) IBOutlet UIView *authenticationPostView;

@property (strong, nonatomic) IBOutlet UIView *referenceView;


@end

@implementation RealNameAuthenticationViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.title = @"认证";

    self.myScrollView.hidden = YES;
    
    [self doRequest];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidChange:) name:UITextFieldTextDidChangeNotification object:nil];
}

- (void)textFieldDidChange:(NSNotification *)notification{
    UITextField *obj = notification.object;
    if ([obj isEqual:self.realNameTextField]) {
        if (obj.text.length > 10) {
            obj.text = [obj.text substringToIndex:10];
        }
    }
    else if ([obj isEqual:self.phoneNumberTextField]){
        if (obj.text.length > 11) {
            obj.text = [obj.text substringToIndex:11];
        }
    }
    else if ([obj isEqual:self.iCardNumberTextField]){
        if (obj.text.length > 18) {
            obj.text = [obj.text substringToIndex:18];
        }
    }
}

- (IBAction)doTap:(id)sender {
    [self.view endEditing:YES];
}

- (void)doRequest{
    [HUDUtil showLoading];

    [[IVTNetworkAPIClient sharedClient] getRealNameAuthenticationStatusSuccess:^(NSDictionary *successData) {
        [HUDUtil hide];
//        NSDictionary *dict = successData[@"checkInfoList"];
//        if (dict) {
//            NSInteger flag = [[dict valueForKey:@"isChecked"] integerValue];
//            if (flag == 0) {
//                self.myScrollView.hidden = YES;
//                [self.authenticationingView removeFromSuperview];
//                [self.authenticationPostView removeFromSuperview];
//                self.rejectView.frame = CGRectMake(0, 100, kScreenWidth, 230);
//                [self.view addSubview:self.rejectView];
//                self.rejectReasonLabel.text = dict[@"reason"];
////                0:未通过;1:通过;2:未审核
//            }
//            else if (flag == 1){
//                self.myScrollView.hidden = YES;
//                [self.authenticationingView removeFromSuperview];
//                [self.rejectView removeFromSuperview];
//                self.authenticationPostView.frame = CGRectMake(0, 100, kScreenWidth, 80);
//                [self.view addSubview:self.authenticationPostView];
//            }
//            else if (flag == 2){
//                self.myScrollView.hidden = YES;
//                [self.authenticationPostView removeFromSuperview];
//                [self.rejectView removeFromSuperview];
//                self.authenticationingView.frame = CGRectMake(0, 100, kScreenWidth, 65);
//                [self.view addSubview:self.authenticationingView];
//            }
//        }
//        else{
            self.myScrollView.hidden = NO;
            [self.authenticationPostView removeFromSuperview];
            [self.rejectView removeFromSuperview];
            [self.authenticationingView removeFromSuperview];
//        }
    } error:^(NSDictionary *successData) {
        NSString *msg = successData[@"description"];
        [HUDUtil showError:msg];
    } failure:^(NSError *failureError) {
        [HUDUtil showError:@"网络异常"];
    }];
    
    
}

- (IBAction)doImage1Button:(id)sender {
    currentButton = self.image1Button;
    [self photoClick];
}

- (IBAction)doReferenceToButton:(id)sender {
    self.referenceView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    [[AppDelegate appDelegate].window addSubview:self.referenceView];
}

- (IBAction)hiddenReferenceView:(id)sender {
    [self.referenceView removeFromSuperview];
}

- (IBAction)doImage2Button:(id)sender {
    currentButton = self.image2Button;
    [self photoClick];
}

- (IBAction)doImage3Button:(id)sender {
    currentButton = self.image3Button;
    [self photoClick];
}

- (void)photoClick{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    [alert addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if (![UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront] && ![UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear]) {
            UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"没找到摄像头" preferredStyle:UIAlertControllerStyleAlert];
            [alertC addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                
            }]];
            [self presentViewController:alertC animated:YES completion:^{
                
            }];
        }
        else{
            UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
            imagePicker.delegate = self;
            imagePicker.allowsEditing = YES;
            imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
            imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
            [self presentViewController:imagePicker animated:YES completion:nil];
        }
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
        imagePicker.delegate = self;
        imagePicker.allowsEditing = YES;
        imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        [self presentViewController:imagePicker animated:YES completion:nil];
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    [self presentViewController:alert animated:YES completion:^{
        
    }];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    UIImage *imagePicker = info[@"UIImagePickerControllerEditedImage"];
    if ([currentButton isEqual:self.image1Button]) {
        imageData1 =  UIImageJPEGRepresentation(imagePicker, 0.4);
        UIImage *image = [UIImage imageWithData:imageData1];
        [self.image1Button setImage:image forState:UIControlStateNormal];
    }
    else if ([currentButton isEqual:self.image2Button]){
        imageData2 =  UIImageJPEGRepresentation(imagePicker, 0.4);
        UIImage *image = [UIImage imageWithData:imageData2];
        [self.image2Button setImage:image forState:UIControlStateNormal];
    }
    else if ([currentButton isEqual:self.image3Button]){
        imageData3 =  UIImageJPEGRepresentation(imagePicker, 0.4);
        UIImage *image = [UIImage imageWithData:imageData3];
        [self.image3Button setImage:image forState:UIControlStateNormal];
    }
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)doSubmitButton:(id)sender {
    if (self.realNameTextField.text != nil && self.phoneNumberTextField.text != nil && self.iCardNumberTextField.text != nil && imageData1 != nil && imageData2 != nil && imageData3 != nil) {
        if (![self.realNameTextField.text isChinaName]) {
            UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"请正确输入姓名" preferredStyle:UIAlertControllerStyleAlert];
            [alertC addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                
            }]];
            [self.navigationController presentViewController:alertC animated:YES completion:nil];
            return;
        }
        if (![self.phoneNumberTextField.text isMobileNumber]) {
            UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"请正确输入手机号" preferredStyle:UIAlertControllerStyleAlert];
            [alertC addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                
            }]];
            [self.navigationController presentViewController:alertC animated:YES completion:nil];
            return;
        }
        if (![self.iCardNumberTextField.text accurateVerifyIDCardNumber]) {
            UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"请正确输入身份证号码" preferredStyle:UIAlertControllerStyleAlert];
            [alertC addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                
            }]];
            [self.navigationController presentViewController:alertC animated:YES completion:nil];
            return;
        }
        [HUDUtil showLoading];
        [[IVTNetworkAPIClient sharedClient] submitRealNameAuthenticationWithRealName:self.realNameTextField.text contact:self.phoneNumberTextField.text idCard:self.iCardNumberTextField.text handCardFile:imageData1 cardFileFront:imageData2 cardFileBack:imageData3 success:^(NSDictionary *successData) {

            [HUDUtil showSuccess:@"认证成功"];

            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:YES];
            });
        } error:^(NSDictionary *successData) {
            NSString *msg = successData[@"description"];
            [HUDUtil showError:msg];
        } failure:^(NSError *failureError) {
            [HUDUtil showError:@"网络异常"];
        }];
    }
    else{
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"请正确输入认证信息" preferredStyle:UIAlertControllerStyleAlert];
        [alertC addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }]];
        [self.navigationController presentViewController:alertC animated:YES completion:nil];
    }
}

- (IBAction)doReSubmitButton:(id)sender {
    self.myScrollView.hidden = NO;
    [self.authenticationPostView removeFromSuperview];
    [self.rejectView removeFromSuperview];
    [self.authenticationingView removeFromSuperview];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#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