| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485 |
- //
- // HJAuthViewController.m
- // HappyJob
- //
- // Created by Bob on 2019/4/19.
- // Copyright © 2019 Bob. All rights reserved.
- //
- #import "HJAuthViewController.h"
- #import "FBScrollLayoutView.h"
- #import "UIButton+HJGradientButton.h"
- #import "HJAuthSectionView.h"
- #import "HJAuthingViewController.h"
- #import "HJAuthSuccessViewController.h"
- #import "UIButton+Layout.h"
- #import "HJAuthNavBarView.h"
- #import "UIView+FBProgressHUD.h"
- #import "HJRealApproveAPIManager.h"
- #import "HJMeDataBox.h"
- #import "UIViewController+FBImagePicker.h"
- static const CGFloat LeadSpacing = 15;
- static const CGFloat TailSpacing = 15;
- static const CGFloat FixedSpacing = 30;
- @interface HJAuthViewController () <FBScrollLayoutViewDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate, FBAPIManagerDelegate, FBAPIManagerInterceptor>
- @property (nonatomic, strong) FBScrollLayoutView *scrollLayoutView;
- @property (nonatomic, strong) HJAuthNavBarView *navBarView;
- @property (nonatomic, strong) UIButton *applyButton;
- @property (nonatomic, strong) HJAuthSectionView *authSectionView1;
- @property (nonatomic, strong) HJAuthSectionView *authSectionView2;
- @property (nonatomic, strong) HJAuthSectionView *authSectionView3;
- @property (nonatomic, strong) UITextField *nameTextField;
- @property (nonatomic, strong) UITextField *cardTextField;
- @property (nonatomic, strong) UIView *lineH;
- @property (nonatomic, strong) UIButton *cardForegroundButton;
- @property (nonatomic, strong) UIButton *cardBackgroundButton;
- @property (nonatomic, strong) UIImageView *cardPhotoIcon;
- @property (nonatomic, strong) UIButton *cardPhotoButton;
- @property (nonatomic, strong) UILabel *remarkLabel1;
- @property (nonatomic, strong) UILabel *remarkLabel2;
- @property (nonatomic, strong) HJMeDataBox *dataBox;
- @property (nonatomic, strong) HJRealApproveAPIManager *realApproveAPIManager;
- @property (nonatomic, strong) UIButton *selectedCardButton;
- @end
- @implementation HJAuthViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.title = @"实名认证";
- self.view.backgroundColor = [UIColor whiteColor];
-
- [self.view addSubview:self.navBarView];
- [self.navBarView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.left.right.equalTo(self.view);
- make.height.mas_equalTo(HJNavBarDefaultHeight);
- }];
-
- [self.view addSubview:self.scrollLayoutView];
- [self.scrollLayoutView.contentView addSubview:self.authSectionView1];
- [self.scrollLayoutView.contentView addSubview:self.nameTextField];
- [self.scrollLayoutView.contentView addSubview:self.lineH];
- [self.scrollLayoutView.contentView addSubview:self.cardTextField];
- [self.scrollLayoutView.contentView addSubview:self.authSectionView2];
- [self.scrollLayoutView.contentView addSubview:self.cardForegroundButton];
- [self.scrollLayoutView.contentView addSubview:self.cardBackgroundButton];
- [self.scrollLayoutView.contentView addSubview:self.authSectionView3];
- [self.scrollLayoutView.contentView addSubview:self.cardPhotoIcon];
- [self.scrollLayoutView.contentView addSubview:self.cardPhotoButton];
- [self.scrollLayoutView.contentView addSubview:self.remarkLabel1];
- [self.scrollLayoutView.contentView addSubview:self.remarkLabel2];
- [self.scrollLayoutView.footerView addSubview:self.applyButton];
-
- [self.scrollLayoutView makeConstraints];
- }
- #pragma mark - FBScrollLayoutViewDelegate
- - (void)scrollLayoutViewMakeConstraints:(FBScrollLayoutView *)scrollLayoutView {
- [scrollLayoutView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self.navBarView.mas_bottom);
- make.left.right.equalTo(self.view);
- make.bottom.equalTo(self.mas_bottomLayoutGuide);
- }];
- }
- - (void)subviewsMakeConstraintsInScrollLayoutView:(FBScrollLayoutView *)scrollLayoutView contentView:(UIView *)contentView {
- [self.authSectionView1 mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.left.right.equalTo(contentView);
- make.height.mas_equalTo(40);
- }];
- [self.nameTextField mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self.authSectionView1.mas_bottom).with.offset(3);
- make.left.equalTo(contentView).with.offset(40);
- make.right.equalTo(contentView).with.offset(-40);
- make.height.mas_equalTo(30);
- }];
- [self.lineH mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self.nameTextField.mas_bottom).with.offset(3);
- make.left.right.equalTo(contentView);
- make.height.mas_equalTo(1);
- }];
- [self.cardTextField mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self.lineH.mas_bottom).with.offset(3);
- make.left.right.height.equalTo(self.nameTextField);
- }];
- // 身份证件照片
- [self.authSectionView2 mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self.cardTextField.mas_bottom).with.offset(3);
- make.left.right.height.equalTo(self.authSectionView1);
- }];
- // 二等分布局
- [self.cardForegroundButton mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self.authSectionView2.mas_bottom).with.offset(24);
- make.left.equalTo(contentView).with.offset(LeadSpacing);
- make.size.mas_equalTo(self.cardForegroundButton.frame.size);
- }];
- [self.cardBackgroundButton mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self.authSectionView2.mas_bottom).with.offset(24);
- make.right.equalTo(contentView).with.offset(-TailSpacing);
- make.size.mas_equalTo(self.cardBackgroundButton.frame.size);
- }];
- // 手持身份证照片
- [self.authSectionView3 mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self.cardBackgroundButton.mas_bottom).with.offset(24);
- make.left.right.height.equalTo(self.authSectionView1);
- }];
- // 二等分布局
- [self.cardPhotoIcon mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self.authSectionView3.mas_bottom).with.offset(24);
- make.left.equalTo(contentView).with.offset(LeadSpacing);
- make.size.mas_equalTo(self.cardPhotoIcon.frame.size);
- }];
- [self.cardPhotoButton mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self.authSectionView3.mas_bottom).with.offset(24);
- make.right.equalTo(contentView).with.offset(-TailSpacing);
- make.size.mas_equalTo(self.cardPhotoButton.frame.size);
- }];
-
- [self.remarkLabel1 mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self.cardPhotoIcon.mas_bottom).with.offset(30);
- make.centerX.equalTo(contentView);
- }];
- [self.remarkLabel2 mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self.remarkLabel1.mas_bottom).with.offset(5);
- make.centerX.equalTo(contentView);
- }];
- [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.bottom.equalTo(self.remarkLabel2.mas_bottom).with.offset(8);
- }];
- }
- - (void)subviewsMakeConstraintsInScrollLayoutView:(FBScrollLayoutView *)scrollLayoutView footerView:(UIView *)footerView {
- [footerView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.height.mas_equalTo(60);
- }];
- [self.applyButton mas_makeConstraints:^(MASConstraintMaker *make) {
- make.edges.equalTo(footerView).with.insets(UIEdgeInsetsMake(11, 9, 7, 9));
- }];
- }
- #pragma mark - UIImagePickerControllerDelegate
- - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
- NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
- //当选择的类型是图片
- if ([type isEqualToString:@"public.image"]) {
- //先把图片转成NSData
- UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
- if (self.selectedCardButton == self.cardForegroundButton) {
- self.realApproveAPIManager.cardFrontImage = image;
- }
- if (self.selectedCardButton == self.cardBackgroundButton) {
- self.realApproveAPIManager.cardBackImage = image;
- }
- if (self.selectedCardButton == self.cardPhotoButton) {
- self.realApproveAPIManager.cardHoldImage = image;
- }
- // NSData *data = UIImagePNGRepresentation(image);
- // if (data == nil) {
- // data = UIImageJPEGRepresentation(image, 1.0);
- // }
- // //图片保存的路径
- // //这里将图片放在沙盒的documents文件夹中
- // NSString *DocumentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
- // //文件管理器
- // NSFileManager *fileManager = [NSFileManager defaultManager];
- // //把刚刚图片转换的data对象拷贝至沙盒中并保存为image.png
- // [fileManager createDirectoryAtPath:DocumentsPath withIntermediateDirectories:YES attributes:nil error:nil];
- // [fileManager createFileAtPath:[DocumentsPath stringByAppendingString:@"/userHeader.png"] contents:data attributes:nil];
-
- //关闭相册界面
- [self.selectedCardButton setBackgroundImage:image forState:UIControlStateSelected];
- self.selectedCardButton.selected = YES;
- [self dismissViewControllerAnimated:YES completion:^{
- self.selectedCardButton = nil;
- }];
- }
- }
- - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
- [self dismissViewControllerAnimated:YES completion:^{
- self.selectedCardButton = nil;
- }];
- }
- #pragma mark - FBAPIManagerInterceptor
- - (BOOL)manager:(FBBaseAPIManager *)manager shouldStartCallAPIWithParams:(NSDictionary *)params {
- [self.view fb_showLoading];
-
- NSString *errorMessage = nil;
- if (self.nameTextField.text.length == 0) {
- [self.nameTextField becomeFirstResponder];
- errorMessage = @"请您先填写真实姓名";
- } else if (self.nameTextField.text.length > 30) {
- [self.nameTextField becomeFirstResponder];
- errorMessage = @"姓名不能超过30个字符";
- } else if (self.cardTextField.text.length != 18) {
- [self.cardTextField becomeFirstResponder];
- errorMessage = @"请您先填写正确的身份证号";
- } else if (!self.cardForegroundButton.selected) {
- errorMessage = @"请您先上传证件正面照";
- } else if (!self.cardBackgroundButton.selected) {
- errorMessage = @"请您先上传证件背面照";
- } else if (!self.cardPhotoButton.selected) {
- errorMessage = @"请您先上传手持证件照";
- }
-
- if (errorMessage != nil) {
- [self.view fb_showFailureWithStatus:errorMessage];
- return NO;
- }
-
- return YES;
- }
- - (void)managerShouldFinishCallAPI:(FBBaseAPIManager *)manager {
- [self.view fb_dismiss];
- }
- #pragma mark - FBAPIManagerDelegate
- - (void)managerCallAPIDidSuccess:(FBBaseAPIManager *)manager {
- if (manager == self.realApproveAPIManager) {
- id fetchData = [manager fetchDataWithBox:self.dataBox];
- if ([fetchData isKindOfClass:[NSString class]]) {
- [self.view fb_showSuccessWithStatus:fetchData completion:^{
- [self.navigationController popToRootViewControllerAnimated:YES];
- }];
- }
- }
- }
- - (void)managerCallAPIDidFailed:(FBBaseAPIManager *)manager {
- if (manager == self.realApproveAPIManager) {
- id fetchData = [manager fetchDataWithBox:self.dataBox];
- if ([fetchData isKindOfClass:[NSString class]]) {
- [self.view fb_showFailureWithStatus:fetchData];
- }
- }
- }
- #pragma mark - event response
- - (void)applyButtonClicked:(UIButton *)sender {
- self.realApproveAPIManager.realName = self.nameTextField.text;
- self.realApproveAPIManager.cardNumber = self.cardTextField.text;
- [self.realApproveAPIManager start];
- }
- - (void)backButtonClicked:(UIButton *)sender {
- [self.navigationController popToRootViewControllerAnimated:YES];
- }
- - (void)openImagePicker:(UIButton *)sender {
- // 被点击的上传按钮
- self.selectedCardButton = sender;
-
- [self fb_openImagePickerWithPhotoHandler:^(UIImagePickerController * _Nonnull imagePicker) {
- imagePicker.delegate = self;
- } cameraHandler:^(UIImagePickerController * _Nonnull imagePicker) {
- imagePicker.delegate = self;
- } cancelHandler:^{
- self.selectedCardButton = nil;
- }];
- }
- #pragma mark - private methods
- - (UIButton *)createButtonWithTitle:(NSString *)title imageNamed:(NSString *)imageNamed {
- UIImage *image = [UIImage imageNamed:imageNamed];
- UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
- button.layer.borderWidth = 1;
- button.layer.borderColor = [UIColor lightGrayColor].CGColor;
- button.layer.cornerRadius = 6;
- button.layer.masksToBounds = YES;
- [button setImage:image forState:UIControlStateNormal];
- [button setImage:[UIImage new] forState:UIControlStateSelected];
- [button setBackgroundImage:[UIImage new] forState:UIControlStateNormal];
- if (title != nil) {
- button.titleLabel.font = [UIFont systemFontOfSize:HJHorizontalScale(11)];
- button.titleLabel.textAlignment = NSTextAlignmentCenter;
- [button setTitle:title forState:UIControlStateNormal];
- [button setTitle:@"" forState:UIControlStateSelected];
- [button setTitleColor:[UIColor colorWithRed:204/255.0 green:204/255.0 blue:204/255.0 alpha:1.0] forState:UIControlStateNormal];
- }
-
- //按钮固定尺寸
- CGSize size = CGSizeMake((SCREEN_WIDTH-LeadSpacing-TailSpacing-FixedSpacing)/2, 103);
- //文字图片上下显示
- CGSize imageSize = image.size;
- CGSize titleSize = CGSizeMake(105, 12);
- CGFloat maxWidth = size.width;
- CGFloat padding = 13;
- button.imageRect = CGRectMake((maxWidth - imageSize.width) / 2, 30, imageSize.width, imageSize.height);
- button.titleRect = CGRectMake((maxWidth - titleSize.width) / 2, 30 + imageSize.height + padding, titleSize.width, titleSize.height);
- button.frame = CGRectMake(0, 0, size.width, size.height);
- return button;
- }
- #pragma mark - getters and setters
- - (HJMeDataBox *)dataBox {
- if (_dataBox == nil) {
- _dataBox = [[HJMeDataBox alloc] init];
- }
- return _dataBox;
- }
- - (HJRealApproveAPIManager *)realApproveAPIManager {
- if (_realApproveAPIManager == nil) {
- _realApproveAPIManager = [[HJRealApproveAPIManager alloc] init];
- _realApproveAPIManager.APIManagerDelegate = self;
- _realApproveAPIManager.APIManagerInterceptor = self;
- }
- return _realApproveAPIManager;
- }
- - (HJAuthNavBarView *)navBarView {
- if (_navBarView == nil) {
- _navBarView = [[HJAuthNavBarView alloc] init];
- _navBarView.backgroundColor = [UIColor whiteColor];
- _navBarView.titleLabel.text = @"实名认证";
- _navBarView.titleLabel.textColor = [UIColor colorWithRed:34/255.0 green:34/255.0 blue:34/255.0 alpha:1.0];
- [_navBarView.backButton setImage:[UIImage imageNamed:@"navbar_back_black"] forState:UIControlStateNormal];
- [_navBarView.backButton addTarget:self action:@selector(backButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
- }
- return _navBarView;
- }
- - (UILabel *)remarkLabel1 {
- if (_remarkLabel1 == nil) {
- _remarkLabel1 = [[UILabel alloc] init];
- _remarkLabel1.text = @"1,身份证信息仅供开心工作平台提供诚信保证使用;";
- _remarkLabel1.textColor = [UIColor lightGrayColor];
- _remarkLabel1.font = [UIFont systemFontOfSize:10];
- }
- return _remarkLabel1;
- }
- - (UILabel *)remarkLabel2 {
- if (_remarkLabel2 == nil) {
- _remarkLabel2 = [[UILabel alloc] init];
- _remarkLabel2.text = @"2,开心工作承诺不向其他第三方透露您的个人信息。";
- _remarkLabel2.textColor = [UIColor lightGrayColor];
- _remarkLabel2.font = [UIFont systemFontOfSize:10];
- }
- return _remarkLabel2;
- }
- - (UIImageView *)cardPhotoIcon {
- if (_cardPhotoIcon == nil) {
- _cardPhotoIcon = [[UIImageView alloc] init];
- _cardPhotoIcon.layer.borderWidth = 1;
- _cardPhotoIcon.layer.borderColor = [UIColor lightGrayColor].CGColor;
- _cardPhotoIcon.layer.cornerRadius = 6;
- _cardPhotoIcon.layer.masksToBounds = YES;
- _cardPhotoIcon.image = [UIImage imageNamed:@"auth_card_photo"];
- _cardPhotoIcon.contentMode = UIViewContentModeCenter;
- CGSize size = CGSizeMake((SCREEN_WIDTH-LeadSpacing-TailSpacing-FixedSpacing)/2, 103);
- _cardPhotoIcon.frame = CGRectMake(0, 0, size.width, size.height);
- }
- return _cardPhotoIcon;
- }
- - (UIButton *)cardPhotoButton {
- if (_cardPhotoButton == nil) {
- _cardPhotoButton = [self createButtonWithTitle:@"点击上传手持证件照" imageNamed:@"auth_photo"];
- [_cardPhotoButton addTarget:self action:@selector(openImagePicker:) forControlEvents:UIControlEventTouchUpInside];
- }
- return _cardPhotoButton;
- }
- - (UIButton *)cardForegroundButton {
- if (_cardForegroundButton == nil) {
- _cardForegroundButton = [self createButtonWithTitle:@"点击上传证件正面照" imageNamed:@"auth_card_foreground"];
- [_cardForegroundButton addTarget:self action:@selector(openImagePicker:) forControlEvents:UIControlEventTouchUpInside];
- }
- return _cardForegroundButton;
- }
- - (UIButton *)cardBackgroundButton {
- if (_cardBackgroundButton == nil) {
- _cardBackgroundButton = [self createButtonWithTitle:@"点击上传证件背面照" imageNamed:@"auth_card_background"];
- [_cardBackgroundButton addTarget:self action:@selector(openImagePicker:) forControlEvents:UIControlEventTouchUpInside];
- }
- return _cardBackgroundButton;
- }
- - (UITextField *)nameTextField {
- if (_nameTextField == nil) {
- _nameTextField = [[UITextField alloc] init];
- _nameTextField.placeholder = @"您的姓名";
- _nameTextField.borderStyle = UITextBorderStyleNone;
- _nameTextField.keyboardType = UIKeyboardTypeDefault;
- _nameTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
- _nameTextField.font = [UIFont systemFontOfSize:14];
- }
- return _nameTextField;
- }
- - (UITextField *)cardTextField {
- if (_cardTextField == nil) {
- _cardTextField = [[UITextField alloc] init];
- _cardTextField.placeholder = @"您的身份证号码";
- _cardTextField.borderStyle = UITextBorderStyleNone;
- _cardTextField.keyboardType = UIKeyboardTypeASCIICapable;
- _cardTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
- _cardTextField.font = [UIFont systemFontOfSize:14];
- }
- return _cardTextField;
- }
- - (UIView *)lineH {
- if (_lineH == nil) {
- _lineH = [[UIView alloc] init];
- _lineH.backgroundColor = [UIColor colorWithRed:238/255.0 green:238/255.0 blue:238/255.0 alpha:1.0];
- }
- return _lineH;
- }
- - (HJAuthSectionView *)authSectionView1 {
- if (_authSectionView1 == nil) {
- _authSectionView1 = [[HJAuthSectionView alloc] init];
- _authSectionView1.titleLabel.text = @"身份信息";
- }
- return _authSectionView1;
- }
- - (HJAuthSectionView *)authSectionView2 {
- if (_authSectionView2 == nil) {
- _authSectionView2 = [[HJAuthSectionView alloc] init];
- _authSectionView2.titleLabel.text = @"身份证件照片";
- }
- return _authSectionView2;
- }
- - (HJAuthSectionView *)authSectionView3 {
- if (_authSectionView3 == nil) {
- _authSectionView3 = [[HJAuthSectionView alloc] init];
- _authSectionView3.titleLabel.text = @"手持身份证照片";
- }
- return _authSectionView3;
- }
- - (UIButton *)applyButton {
- if (_applyButton == nil) {
- _applyButton = [UIButton hj_createGradientButtonWithTitle:@"提交"];
- [_applyButton addTarget:self action:@selector(applyButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
- }
- return _applyButton;
- }
- - (FBScrollLayoutView *)scrollLayoutView {
- if (_scrollLayoutView == nil) {
- _scrollLayoutView = [[FBScrollLayoutView alloc] init];
- _scrollLayoutView.delegate = self;
- }
- return _scrollLayoutView;
- }
- @end
|