// // FKidCardBindController.m // FirstLink // // Created by jack on 15/11/3. // Copyright © 2015年 FirstLink. All rights reserved. // #import "FKBindIdCardController.h" #import "FKBindIdCardReform.h" #import "FKBindIdCardRequest.h" #import "SubmitDiscountPickView.h" #import "FKIdCardItem.h" #import "FKPersonOrderController.h" #import "FLControllerHelper.h" #import "FLImageHelper.h" #import "FKTargetConfigUtil.h" #import #import "ShakeRedPacketController.h" #import "WebViewController.h" @interface FKBindIdCardController () @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, strong) UIButton *checkBox; @property (nonatomic, strong) SubmitDiscountPickView *pickerView; @property (nonatomic, strong) TTTAttributedLabel *licenseLabel; @end @implementation FKBindIdCardController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; [self addAllSubViews]; [self.hudView show:YES]; [FKBindIdCardRequest requestIdCardListDelegate:self]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self configNavBar]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; } - (void)configNavBar{ } - (void)addAllSubViews{ [self.view addSubview:self.licenseLabel]; [self.licenseLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.equalTo(self.view.mas_bottom).offset(-20); make.centerX.equalTo(self.view).offset(10); make.height.mas_equalTo(IS_IPHONE_X ? 54 : 40); }]; [self.view addSubview:self.checkBox]; [self.checkBox mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(self.licenseLabel).offset(-1); make.right.equalTo(self.licenseLabel.mas_left).offset(-2); make.size.mas_equalTo(CGSizeMake(30, 30)); }]; [self.view addSubview:self.tableView]; [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.equalTo(self.licenseLabel.mas_top); make.left.right.top.equalTo(self.view); }]; } - (void)networkDidSuccessResponse:(NSDictionary *)response identify:(int)identify header:(MSGHeader *)header{ [self.hudView hide:NO]; if (header.code.intValue == RESPONSE_MSG_NORMAL) { if (identify == ID_CARD_LIST_REQUEST) { self.idCardCenter.idCardItemList = [FKBindIdCardReform parserIdCardListWithData:response]; // 设置默认身份证, 新增操作无默认 [self.idCardCenter setDefaultIdCard]; [self.pickerView refreshTitleArray:[self.idCardCenter attributeIdCardNumArray] withSelectedIndex:self.idCardCenter.selectedIndex]; [self.tableView reloadData]; } else if (identify == UPLOAD_IDCARD_INFO_REQUEST){ [self upLoadFinish]; } else if (identify == UPLOAD_IDCARD_INVERSE_REQUEST) { [self.tableView reloadData]; } else if (identify == UPLOAD_IDCARD_FACE_REQUEST) { [self.hudView show:YES]; [FKBindIdCardRequest requestIdCardAutoRecognize:[self.idCardCenter facePicUrl] delegate:self]; } else if (identify == ID_CARD_AUTO_RECOGNIZE_REQUEST) { NSString *idCardNum = [FKBindIdCardReform parserIdCardNum:response]; if (idCardNum.length > 0) { self.idCardCenter.inputIdCardNum = idCardNum; } [self.tableView reloadData]; } } else { [FLProgressHUDHelper showText:header.msg inView:self.view]; if (identify == ID_CARD_AUTO_RECOGNIZE_REQUEST) { [self idCardAutoRecognizeError]; } } } - (void)networkDidReceiveError:(NSError *)error identify:(int)identify header:(MSGHeader *)header{ [self.hudView hide:NO]; [FLProgressHUDHelper showText:header.msg inView:self.view]; if (identify == ID_CARD_AUTO_RECOGNIZE_REQUEST) { [self idCardAutoRecognizeError]; } } #pragma mark - tableView dataSource - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return 2; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ if (indexPath.row == 0) { FKBindTipCell *tipCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([FKBindTipCell class])]; [self configTipCell:tipCell]; return tipCell; } else if (indexPath.row == 1){ FKBindIdCardPickCell *pickCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([FKBindIdCardPickCell class])]; pickCell.delegate = self; [self configPickCell:pickCell]; return pickCell; } return nil; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ if (indexPath.row == 0) return 100; if (indexPath.row == 1) return 460; return 0; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ [self.view endEditing:YES]; } #pragma mark - pick cell delegate - (void)didClickBtnWithActionType:(kPickCellActionType)actionType{ switch (actionType) { case kAcitonTypePickFace: case kAcitonTypePickInverse:{ self.idCardCenter.selectToFace = actionType == kAcitonTypePickFace ? YES : NO; [FLControllerHelper launchAppCamera:self type:UIImagePickerControllerSourceTypePhotoLibrary]; } break; case kAcitonTypeRemoveFace: case kAcitonTypeRemoveInverse:{ [self.idCardCenter removeImgAtLocation:actionType == kAcitonTypeRemoveFace ? YES : NO]; } break; case kAcitonTypeMoreIdCard:{ [self.view endEditing:YES]; [self.pickerView showInView:self.view animated:YES]; } break; case kAcitonTypeSave:{ [self saveData]; } break; case kAcitonTypeCancel:{ [self goToBack]; } break; default: break; } [self.tableView reloadData]; } #pragma mark - mehod - (void)configPickCell:(FKBindIdCardPickCell *)pickCell{ pickCell.textField.text = self.idCardCenter.inputIdCardNum; pickCell.faceImgView.image = nil; pickCell.inverseImgView.image = nil; if (self.idCardCenter.facePicUrl) { [pickCell.faceImgView fl_setImageWithURL:[NSURL URLWithString:[FKBindIdCardPickCell cdnImgUrlStrWithUrlString:self.idCardCenter.facePicUrl]]]; } if (self.idCardCenter.inversePicUrl) { [pickCell.inverseImgView fl_setImageWithURL:[NSURL URLWithString:[FKBindIdCardPickCell cdnImgUrlStrWithUrlString:self.idCardCenter.inversePicUrl]]]; } pickCell.faceRemoveBtn.hidden = (self.idCardCenter.facePicUrl) ? NO : YES; pickCell.inverseRemoveBtn.hidden = (self.idCardCenter.inversePicUrl) ? NO : YES; [self configActionBtnInPickCell:pickCell]; } // 子类重写 - (void)configActionBtnInPickCell:(FKBindIdCardPickCell *)pickCell{ } // 子类重写 - (void)configTipCell:(FKBindIdCardPickCell *)pickCell{ } /** 保存用户输入的身份证号 */ - (void)saveInputIdCardNum{ FKBindIdCardPickCell *pickCell = [self getPickCell]; if (pickCell) self.idCardCenter.inputIdCardNum = pickCell.textField.text; } - (FKBindIdCardPickCell *)getPickCell{ FKBindIdCardPickCell *pickCell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]]; if ([pickCell isKindOfClass:[FKBindIdCardPickCell class]]) { return pickCell; } return nil; } /** 保存 */ - (void)saveData{ [self saveInputIdCardNum]; // 保存用户输入身份证号 if (!self.checkBox.isSelected) { [FLProgressHUDHelper showText:@"请先接受免责声明和隐私声明" inView:self.view]; return; } NSError *error = nil; BOOL infoComplete = [self.idCardCenter checkIdInfoCompleteWithError:&error]; if (!infoComplete) { [FLProgressHUDHelper showText:error.localizedDescription inView:self.view]; return; } [self.hudView show:YES]; [self.idCardCenter saveAllInfoWithDelegate:self]; } /** 选择已有身份证后,刷新UIc */ - (void)selectDoneIdCardWithIndex:(NSUInteger)index{ [self.idCardCenter savePickedIdCardAtIndex:index]; [self.tableView reloadData]; } - (void)idCardAutoRecognizeError { self.idCardCenter.facePicUrl = nil; self.idCardCenter.inputIdCardNum = nil; [self.tableView reloadData]; } #pragma mark - action - (void)goToBack{ [self.navigationController popViewControllerAnimated:YES]; } - (void)upLoadFinish{ UIButton *successBtn = [self createCustomSuccessBtn]; WeakSelf(weakSelf); [FLProgressHUDHelper showCustomView:successBtn inView:self.view withComplitionBlock:^{ // 默认回退,红包页面摇红包 [weakSelf goToBack]; }]; } - (UIButton *)createCustomSuccessBtn{ UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setImage:[UIImage imageNamed:@"whitRightSelect"] forState:UIControlStateNormal]; [button setTitle:@" 上传成功" forState:UIControlStateNormal]; button.titleLabel.font = [UIFont systemFontOfSize:16]; [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [button setBounds:CGRectMake(0, 0, 100, 40)]; return button; } - (IBAction)clickCheckBoxAction:(UIButton *)sender { [sender setSelected:!sender.isSelected]; } #pragma mark - imagePicker delegate - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType]; if ([mediaType isEqualToString:(NSString *)kUTTypeImage]){ UIImage *image = nil; if ([picker allowsEditing]){ image = [info objectForKey:UIImagePickerControllerEditedImage]; } else { image = [info objectForKey:UIImagePickerControllerOriginalImage]; } [self.hudView show:YES]; UIImage *scaleImage = [FLImageHelper imageWithImage:image scaledToSize:CGSizeMake(UPLOAD_HEAD_PHOTO_LENGTH, (UPLOAD_HEAD_PHOTO_LENGTH/image.size.width)*image.size.height)]; [self.idCardCenter upLoadImg:scaleImage isFace:self.idCardCenter.selectToFace delegate:self]; [self saveInputIdCardNum]; [self.tableView reloadData]; } [picker dismissViewControllerAnimated:YES completion:nil]; } #pragma mark - TTTAttributedLabelDelegate - (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithAddress:(NSDictionary *)addressComponents { WebViewController *controller = [[WebViewController alloc] init]; controller.url = @"https://api.fine3q.com/link-site/mobile/public/disclaimer_idcard.html"; controller.hidesBottomBarWhenPushed = YES; [self.navigationController pushViewController:controller animated:YES]; } #pragma mark - property - (UITableView *)tableView{ if (_tableView == nil) { _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain]; _tableView.dataSource = self; _tableView.delegate = self; _tableView.separatorStyle = UITableViewCellSeparatorStyleNone; if (@available(iOS 11.0, *)) { _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; } [_tableView registerClass:[FKBindTipCell class] forCellReuseIdentifier:NSStringFromClass([FKBindTipCell class])]; [_tableView registerClass:[FKBindIdCardPickCell class] forCellReuseIdentifier:NSStringFromClass([FKBindIdCardPickCell class])]; } return _tableView; } - (SubmitDiscountPickView *)pickerView { if (_pickerView == nil) { _pickerView = [[SubmitDiscountPickView alloc]init]; WeakSelf(weakSelf); _pickerView.selectDone = ^(NSInteger selectedIndex){ [weakSelf selectDoneIdCardWithIndex:selectedIndex]; }; } return _pickerView; } - (UIButton *)checkBox { if (!_checkBox) { _checkBox = [UIButton buttonWithType:UIButtonTypeCustom]; [_checkBox setImage:[UIImage imageNamed:@"AddressUnselected"] forState:UIControlStateNormal]; [_checkBox setImage:[UIImage imageNamed:@"AddressSelected"] forState:UIControlStateSelected]; [_checkBox addTarget:self action:@selector(clickCheckBoxAction:) forControlEvents:UIControlEventTouchUpInside]; [_checkBox setSelected:YES]; } return _checkBox; } - (UILabel*)licenseLabel { if (!_licenseLabel) { _licenseLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero]; _licenseLabel.backgroundColor = [UIColor clearColor]; _licenseLabel.font = [UIFont systemFontOfSize:14]; _licenseLabel.textColor = UIColorFromRGB(0xcccccc); _licenseLabel.textAlignment = NSTextAlignmentCenter; _licenseLabel.leading = MAIN_ITEM_DESC_LEADING; _licenseLabel.userInteractionEnabled = YES; _licenseLabel.delegate = self; int licenseLength = 11; _licenseLabel.linkAttributes = @{NSForegroundColorAttributeName:UIColorFromRGB(0xf85a5a), NSUnderlineStyleAttributeName:[NSNumber numberWithInt:NSUnderlineStyleNone]}; NSString *text = [NSString stringWithFormat:@"我接受%@的《免责声明和隐私声明》", [FKTargetConfigUtil appName]]; [_licenseLabel setText:text afterInheritingLabelAttributesAndConfiguringWithBlock:^NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) { NSRange range = NSMakeRange(mutableAttributedString.length - licenseLength, licenseLength); UIFont *font = [UIFont systemFontOfSize:14.0]; CTFontRef cfFont = CTFontCreateWithName((__bridge CFStringRef)font.fontName, font.pointSize, NULL); if (range.length > 0) { [mutableAttributedString removeAttribute:(__bridge NSString *)kCTFontAttributeName range:range]; [mutableAttributedString removeAttribute:(__bridge NSString *)kCTForegroundColorAttributeName range:range]; [mutableAttributedString addAttribute:(__bridge NSString *)kCTFontAttributeName value:(__bridge id)cfFont range:range]; [mutableAttributedString addAttribute:(__bridge NSString*)kCTForegroundColorAttributeName value:(__bridge id)UIColorFromRGB(0xf85a5a).CGColor range:range]; } CFRelease(cfFont); [mutableAttributedString replaceCharactersInRange:range withString:[[mutableAttributedString string] substringWithRange:range]]; return mutableAttributedString; }]; NSRange linkRange = NSMakeRange(_licenseLabel.attributedText.length - licenseLength, licenseLength); [_licenseLabel addLinkToAddress:nil withRange:linkRange]; } return _licenseLabel; } @end