// // ChoosePersonAddressVC.m // LekangGuard // // Created by ecell on 2022/11/29. // #import "ChoosePersonAddressVC.h" #import "ChooseContactTableViewCell.h" #import "PPGetAddressBook.h" @interface ChoosePersonAddressVC () @property (nonatomic ,strong) UITableView *addressTable; @property (strong,nonatomic) NSMutableDictionary *dataSouce; @property (strong,nonatomic) NSMutableArray *nameKeys; @end @implementation ChoosePersonAddressVC - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.zx_navTitle = GJText(@"添加联系人"); [self.view addSubview:self.addressTable]; [self.addressTable mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.equalTo(self.view); make.top.equalTo(self.view).offset(iPhoneX_NavHeight); make.bottom.equalTo(self.view.mas_bottom); }]; if (!self.isAddFamily) { [self zx_setRightBtnWithText:GJText(@"提交") clickedBlock:^(ZXNavItemBtn * _Nonnull btn) { [self saveAction]; }]; } [self initData]; } #pragma mark 提交 /// 提交 - (void)saveAction { BOOL temp = false; for (NSString * key in self.dataSouce) { NSArray*arr = self.dataSouce[key]; for (PPPersonModel *model in arr) { if(model.isSelect) { temp = true; } } } if(!temp) { LGAlertView *alertView = [[LGAlertView alloc] initWithTitle:GJText(@"提示") message:GJText(@"请至少选择一个要提交的联系人") style:LGAlertViewStyleAlert buttonTitles:@[GJText(@"确定")] cancelButtonTitle:nil destructiveButtonTitle:nil actionHandler:^(LGAlertView * _Nonnull alertView, NSUInteger index, NSString * _Nullable title) { } cancelHandler:^(LGAlertView * _Nonnull alertView) { } destructiveHandler:^(LGAlertView * _Nonnull alertView) { }]; [alertView showAnimated:YES completionHandler:nil]; return; } LGAlertView *alertView = [[LGAlertView alloc] initWithTitle:GJText(@"提示") message:GJText(@"确认提交当前所选联系人?") style:LGAlertViewStyleAlert buttonTitles:@[GJText(@"确定")] cancelButtonTitle:GJText(@"容我想想") destructiveButtonTitle:nil actionHandler:^(LGAlertView * _Nonnull alertView, NSUInteger index, NSString * _Nullable title) { if(index == 0){ [self UpdataAddressImport]; } } cancelHandler:^(LGAlertView * _Nonnull alertView) { } destructiveHandler:^(LGAlertView * _Nonnull alertView) { }]; [alertView showAnimated:YES completionHandler:nil]; } #pragma mark 上传联系人 /// 上传联系人 - (void)UpdataAddressImport { NSMutableArray *newArr = [NSMutableArray array]; for (NSString * key in self.dataSouce) { NSArray*arr = self.dataSouce[key]; for (PPPersonModel *model in arr) { if(model.isSelect) { // MARK: 添加短号 lsz for (NSString *mobile in model.mobileArray) { [newArr addObject:@{@"name":model.name, @"phone":mobile, @"addMethod":@(0), @"image":@(9), @"imei":APIManager.sharedManager.deviceModel.imei, @"relation":GJText(@"自定义")}]; } } } } if (newArr.count > 50) { LGAlertView *alertView = [[LGAlertView alloc] initWithTitle:GJText(@"提示") message:GJText(@"上传联系人不能超过50个") style:LGAlertViewStyleAlert buttonTitles:nil cancelButtonTitle:GJText(@"确定") destructiveButtonTitle:nil actionHandler:^(LGAlertView * _Nonnull alertView, NSUInteger index, NSString * _Nullable title) { } cancelHandler:^(LGAlertView * _Nonnull alertView) { } destructiveHandler:^(LGAlertView * _Nonnull alertView) { }]; [alertView showAnimated:YES completionHandler:nil]; return; } [UICommon MessageUpload:@"加载中"]; [[[APIManager sharedManager] PostBodyWithApi:AddressImport_URL json:[newArr yy_modelToJSONString]] subscribeNext:^(id _Nullable x) { [UICommon HidenLoading]; [UICommon MessageSuccessText:@"添加成功"]; AfterDispatch(1, ^{ self.isAddSucceed(); [self.navigationController popViewControllerAnimated:YES]; }); } error:^(NSError * _Nullable error) { NSDictionary *dic = error.userInfo; [UICommon MessageErrorText:dic[NSLocalizedDescriptionKey]]; }]; } #pragma mark 获取联系人 /// 获取联系人 - (void)initData { WeakSelf //获取按联系人姓名首字拼音A~Z排序(已经对姓名的第二个字做了处理) [PPGetAddressBook getOrderAddressBook:^(NSDictionary *addressBookDict, NSArray *nameKeys) { //addressBookDict: 装着所有联系人的字典 //nameKeys: A~Z拼音字母数组; weakSelf.nameKeys = [NSMutableArray arrayWithArray:nameKeys]; weakSelf.dataSouce = [NSMutableDictionary dictionaryWithDictionary:addressBookDict]; //刷新 tableView [weakSelf.addressTable reloadData]; } authorizationFailure:^{ LGAlertView *alertView = [[LGAlertView alloc] initWithTitle:GJText(@"请允许访问通讯录,并打开权限") message:@"" style:LGAlertViewStyleAlert buttonTitles:@[GJText(@"设置")] cancelButtonTitle:GJText(@"取消") destructiveButtonTitle:nil actionHandler:^(LGAlertView * _Nonnull alertView, NSUInteger index, NSString * _Nullable title) { NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString]; if([[UIApplication sharedApplication] canOpenURL:url]) { NSURL*url =[NSURL URLWithString:UIApplicationOpenSettingsURLString]; [[UIApplication sharedApplication] openURL:url options:@{UIApplicationOpenURLOptionsSourceApplicationKey:@YES} completionHandler:^(BOOL success) { }]; } } cancelHandler:^(LGAlertView * _Nonnull alertView) { } destructiveHandler:^(LGAlertView * _Nonnull alertView) { }]; [alertView showAnimated:YES completionHandler:nil]; }]; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { NSArray *arr = self.dataSouce[self.nameKeys[section]]; return arr.count; } -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return self.nameKeys.count; } -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { return self.nameKeys[section]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { ChooseContactTableViewCell *cell = [ChooseContactTableViewCell cellWithTableView:tableView indexPath:indexPath]; NSArray *arr = self.dataSouce[self.nameKeys[indexPath.section]]; PPPersonModel *model = arr[indexPath.row]; cell.perModel = model; cell.isAddFamily = self.isAddFamily; return cell; } -(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { return self.nameKeys; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // ChoosePACell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChoosePACellID" forIndexPath:indexPath]; NSMutableArray *Marr = [NSMutableArray arrayWithArray:self.dataSouce[self.nameKeys[indexPath.section]]]; PPPersonModel * model = Marr[indexPath.row]; if (!self.isAddFamily) { if (model.mobileArray.count == 0) { LGAlertView *alertView = [[LGAlertView alloc] initWithTitle:GJText(@"添加的通讯录号码不能为空") message:@"" style:LGAlertViewStyleAlert buttonTitles:@[GJText(@"确定")] cancelButtonTitle:nil destructiveButtonTitle:nil actionHandler:^(LGAlertView * _Nonnull alertView, NSUInteger index, NSString * _Nullable title) { } cancelHandler:^(LGAlertView * _Nonnull alertView) { } destructiveHandler:^(LGAlertView * _Nonnull alertView) { }]; [alertView showAnimated:YES completionHandler:nil]; return; } model.isSelect = !model.isSelect; [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone]; } else { self.isAddFamilySucceed(model.name, model.mobileArray[0]); [self.navigationController popViewControllerAnimated:YES]; } } - (UITableView *)addressTable { if (!_addressTable) { _addressTable = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain]; _addressTable.backgroundColor = KKClearColor; _addressTable.delegate = self; _addressTable.dataSource = self; _addressTable.rowHeight = Adapted(60); //_addressTable.separatorStyle = UITableViewCellSeparatorStyleNone; [_addressTable registerClass:ChooseContactTableViewCell.class forCellReuseIdentifier:NSStringFromClass(ChooseContactTableViewCell.class)]; } return _addressTable; } /* #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