// // ContactsViewController.m // LekangGuard // // Created by ecell on 2022/11/28. // #import "ContactsViewController.h" #import "OtherTableViewCell.h" #import "ChoosePersonAddressVC.h" #import "AmendContactsViewController.h" #define DDTEXT GJText(@"温馨提示:\n电话本最多保存50个号码(有短号的号码需要把长短号都保存)") @interface ContactsViewController () @property (nonatomic ,strong) UIView *topView; /// 底部按钮View @property (nonatomic ,strong) UIView *buttonViews; @property (nonatomic ,weak) UIButton *leftBtn; @property (nonatomic ,weak) UIButton *rightBtn; @property (nonatomic ,strong) UITableView *contactsTabel; /// 删除模式 @property (nonatomic ,assign) BOOL isDeletMode; /// 用户身份(0:游客,1:普通用户(关注的人),2:管理员) @property (nonatomic ,assign) NSInteger identity; @end @implementation ContactsViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.zx_navTitle = self.viewType == 1 ? GJText(@"电话本") : GJText(@"管理员"); self.view.backgroundColor = KKBackgroundGrey; if (self.viewType == 1) { self.isDeletMode = NO; [self setRightBarBtnType]; [self.view addSubview:self.buttonViews]; [self.buttonViews mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.equalTo(self.view); make.bottom.equalTo(self.view.mas_bottom); make.height.mas_equalTo(85); }]; } [self.view addSubview:self.contactsTabel]; [self.contactsTabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.equalTo(self.view); make.top.equalTo(self.view).offset(iPhoneX_NavHeight); if (self.viewType == 1) make.bottom.equalTo(self.buttonViews.mas_top).inset(1); else make.bottom.equalTo(self.view.mas_bottom); }]; [self GetQueryAddressList]; } - (UITableView *)contactsTabel { if (!_contactsTabel) { _contactsTabel = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped]; _contactsTabel.backgroundColor = KKClearColor; _contactsTabel.delegate = self; _contactsTabel.dataSource = self; _contactsTabel.rowHeight = Adapted(70); CGRect rect = [UICommon GetTextWidth:DDTEXT ViewHeight:SCREEN_WIDTH fontSize:Font_(13) type:@"h"]; if (self.viewType == 1) { _contactsTabel.tableHeaderView.height = Adapted(rect.size.height+20); _contactsTabel.tableHeaderView = self.topView; } // _contactsTabel.separatorStyle = UITableViewCellSeparatorStyleNone; [_contactsTabel registerClass:OtherTableViewCell.class forCellReuseIdentifier:NSStringFromClass(OtherTableViewCell.class)]; } return _contactsTabel; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { OtherTableViewCell *cell = [OtherTableViewCell cellWithTableView:tableView indexPath:indexPath]; cell.contactsModel = self.modelListArr[indexPath.section][indexPath.row]; cell.isDeletMode = self.isDeletMode; return cell; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return self.modelListArr.count; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [self.modelListArr[section] count]; } - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return 0.01; } - (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 31)]; view.backgroundColor = RGB(248, 248, 254); UILabel * titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(14, 0, SCREEN_WIDTH, 31)]; titleLabel.font = FontADA_(12); titleLabel.textColor = RGB(102, 102, 102); [view addSubview:titleLabel]; NSInteger tempCount = [self.modelListArr[section] count]; if(section == 0) { titleLabel.text = F(GJText(@"家庭成员(%ld人)"),tempCount); } else if (section == 1) { if(tempCount == 0) { return nil; } titleLabel.text = F(GJText(@"联系人(%ld人)"),tempCount); } return view; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { NSInteger tempCount = [self.modelListArr[section] count]; if (section == 1) { if(tempCount == 0) { return 0.01; } } return 31; } //MARK: 设置cell 的 交互 样式 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { /** UITableViewCellEditingStyleNone, UITableViewCellEditingStyleDelete, UITableViewCellEditingStyleInsert */ //设置右滑 删除的 样式 // return UITableViewCellEditingStyleDelete; return UITableViewCellEditingStyleNone; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { ContactsModel *contacts = self.modelListArr[indexPath.section][indexPath.row]; if(self.isDeletMode) { //删除模式 if(contacts.openId) { //如果是成员或者管理员,不操作 return; } contacts.isSelect = !contacts.isSelect; [self.contactsTabel reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone]; BOOL temp = NO; for (NSArray * secArr in self.modelListArr) { for (ContactsModel *con in secArr) { if(con.isSelect == NO){ temp = YES; } } } if(temp){ //有个一个没选到 [self.leftBtn setTitle:GJText(@"全选") forState:0]; }else{ [self.leftBtn setTitle:GJText(@"全不选") forState:0]; } } else { if(self.identity == 2)/// 如果自己是管理员可操作其他联系人 { //如果自己是管理员 kWeakSelf(self) // NSMutableArray* buttonArr = [NSMutableArray array]; // if(contacts.openId)/// 判断当前点击的是否是家庭成员 // { // //是成员 // if(contacts.identity != 2) // { // [buttonArr addObject:GJText(@"设置为管理员")]; // } //是自己---直接跳 if([contacts.openId isEqualToString:APIManager.sharedManager.loginModel.openid]){ [self jumpContactVCWith:contacts]; return; } // } // else // { // //普通 // [buttonArr addObject:GJText(@"编辑联系人")]; // } EasyAlertView *alertV = [EasyAlertView alertViewWithTitle:nil subtitle:F(@"%@ %@",GJText(@"联系人"),contacts.name) AlertViewType:AlertViewTypeSystemActionSheet config:nil]; [alertV addAlertItem:^EasyAlertItem *{ return [EasyAlertItem itemWithTitle:GJText(@"取消") type:AlertItemTypeSystemCancel callback:nil]; }]; [alertV addAlertItemWithTitle:GJText(@"删除") type:AlertItemTypeSystemDestructive callback:^(EasyAlertView *showview, long index) { }]; if(contacts.identity != 2 && contacts.openId) { [alertV addAlertItemWithTitle:GJText(@"设置为管理员") type:AlertItemTypeSystemDefault callback:^(EasyAlertView *showview, long index) { NSString *msg = [NSString stringWithFormat:@"%@ %@ ?",GJText(@"确认将管理员转给"),contacts.relation]; EasyAlertView *alertV = [EasyAlertView alertViewWithTitle:GJText(@"提示") subtitle:msg AlertViewType:AlertViewTypeSystemAlert config:nil]; [alertV addAlertItem:^EasyAlertItem *{ return [EasyAlertItem itemWithTitle:GJText(@"取消") type:AlertItemTypeSystemCancel callback:nil]; }]; [alertV addAlertItem:^EasyAlertItem *{ return [EasyAlertItem itemWithTitle:GJText(@"确定") type:AlertItemTypeSystemDefault callback:^(EasyAlertView *showview, long index) { [weakself SetAdmin:contacts.Id identity:2]; }]; }]; [alertV showAlertView]; }]; } if(contacts.setBind == 0) { [alertV addAlertItemWithTitle:GJText(@"编辑联系人") type:AlertItemTypeSystemDefault callback:^(EasyAlertView *showview, long index) { [weakself jumpContactVCWith:contacts]; }]; } [alertV showAlertView]; } else { //成员 //是自己---直接跳 if([contacts.openId isEqualToString:APIManager.sharedManager.loginModel.openid]) { [self jumpContactVCWith:contacts]; return; } else if(contacts.openId) { // 非自己的成员(也包括管理员) return; } else { //普通联系人 kWeakSelf(self) EasyAlertView *alertV = [EasyAlertView alertViewWithTitle:nil subtitle:[NSString stringWithFormat:@"%@ %@",GJText(@"联系人"),contacts.name] AlertViewType:AlertViewTypeSystemActionSheet config:nil]; [alertV addAlertItem:^EasyAlertItem *{ return [EasyAlertItem itemWithTitle:GJText(@"取消") type:AlertItemTypeSystemCancel callback:nil]; }]; [alertV addAlertItem:^EasyAlertItem *{ return [EasyAlertItem itemWithTitle:GJText(@"删除") type:AlertItemTypeSystemDestructive callback:^(EasyAlertView *showview, long index) { [weakself deleteContactFuntionWith:@[contacts.Id]]; }]; }]; [alertV addAlertItemWithTitleArray:@[GJText(@"编辑联系人")] callback:^(EasyAlertView *showview, long index) { NSLog(@"%ld",index); [weakself jumpContactVCWith:contacts]; }]; [alertV showAlertView]; } } } } #pragma mark 跳转到 修改/添加 联系人 页面 - (void)jumpContactVCWith:(ContactsModel*)contacts { AmendContactsViewController *vc = [AmendContactsViewController new]; vc.conModel = contacts; vc.isUpdateSucceed = ^{ [self GetQueryAddressList]; }; [self.navigationController pushViewController:vc animated:YES]; } #pragma mark 删除 /// 删除 - (void)deleteContactFuntionWith:(NSArray *)IdArr { if(IdArr.count == 0) { 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:F(GJText(@"确认要删除%ld个联系人?"),IdArr.count) style:LGAlertViewStyleAlert buttonTitles:@[GJText(@"确定")] cancelButtonTitle:GJText(@"取消") destructiveButtonTitle:nil actionHandler:^(LGAlertView * _Nonnull alertView, NSUInteger index, NSString * _Nullable title) { [UICommon MessageUpload:@"加载中"]; [[[APIManager sharedManager] PostBodyWithApi:F(@"%@?imei=%@", DelBatchAddress_URL,APIManager.sharedManager.deviceModel.imei) json:[IdArr yy_modelToJSONString]] subscribeNext:^(id _Nullable x) { [UICommon MessageSuccessText:@"删除成功"]; [self GetQueryAddressList]; self.isDeletMode = NO; [self setRightBarBtnType]; [self.zx_navRightBtn setImage:ImageName_(@"icon_delect_1") forState:0]; [self.zx_navRightBtn setTitle:@"" forState:0]; [self.leftBtn setTitle:GJText(@"通讯录导入") forState:0]; [self.rightBtn setTitle:GJText(@"手动添加") forState:0]; } completed:^{ }]; } cancelHandler:^(LGAlertView * _Nonnull alertView) {} destructiveHandler:^(LGAlertView * _Nonnull alertView) {}]; [alertView showAnimated:YES completionHandler:nil]; } #pragma mark 底部按钮View /// 底部按钮View - (UIView *)buttonViews { if (!_buttonViews) { _buttonViews = [UICommon ui_view:CGRectZero backgroundColor:KKClearColor cornerRadius:0 borderWidth:0 borderColor:KKClearColor]; UIButton *leftBtn = [UICommon ui_buttonSimple:CGRectZero font:FontADA_(14) normalColor:KKWhiteColorColor normalText:GJText(@"通讯录导入") click:^(UIButton *btn) { if ([btn.currentTitle isEqualToString:GJText(@"通讯录导入")]) { ChoosePersonAddressVC *vc = [ChoosePersonAddressVC new]; vc.isAddSucceed = ^{ [self GetQueryAddressList]; }; [self.navigationController pushViewController:vc animated:YES]; } else { //全选 if ([btn.currentTitle isEqualToString:GJText(@"全选")]) { for (NSArray* secArr in self.modelListArr){ for (ContactsModel *dCon in secArr) { //全选的不包括APP 的通讯录类型 if(!dCon.openId && dCon.setBind == 0){ dCon.isSelect = YES; } } } [self.contactsTabel reloadData]; [self.leftBtn setTitle:GJText(@"全不选") forState:0]; } else { //全不选 for (NSArray* secArr in self.modelListArr){ for (ContactsModel *dCon in secArr) { dCon.isSelect = NO; } } [self.contactsTabel reloadData]; [self.leftBtn setTitle:GJText(@"全选") forState:0]; } } }]; leftBtn.layer.cornerRadius = 5; leftBtn.layer.masksToBounds = YES; leftBtn.backgroundColor = KKMainColor; self.leftBtn = leftBtn; [_buttonViews addSubview:leftBtn]; [leftBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(_buttonViews).offset(15); make.top.equalTo(_buttonViews).offset(5); make.right.equalTo(_buttonViews.mas_centerX).inset(15); make.height.mas_equalTo(44); }]; UIButton *rightBtn = [UICommon ui_buttonSimple:CGRectZero font:FontADA_(14) normalColor:KKWhiteColorColor normalText:GJText(@"手动添加") click:^(UIButton *btn) { if ([btn.currentTitle isEqualToString:GJText(@"手动添加")]) { [self jumpContactVCWith:nil]; } else { // 删除 NSMutableArray *deleteIDs = [NSMutableArray array]; for (NSArray * secArr in self.modelListArr) { for (ContactsModel *con in secArr) { if(con.isSelect){ [deleteIDs addObject:con.Id]; } } } [self deleteContactFuntionWith:deleteIDs]; } }]; rightBtn.layer.cornerRadius = 5; rightBtn.layer.masksToBounds = YES; rightBtn.backgroundColor = KKMainColor; self.rightBtn = rightBtn; [_buttonViews addSubview:rightBtn]; [rightBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(_buttonViews.mas_centerX).offset(15); make.top.equalTo(_buttonViews).offset(5); make.right.equalTo(_buttonViews.mas_right).inset(15); make.height.mas_equalTo(44); }]; } return _buttonViews; } #pragma mark 头部提示View /// 头部提示View - (UIView *)topView { if (!_topView) { CGRect rect = [UICommon GetTextWidth:DDTEXT ViewHeight:SCREEN_WIDTH fontSize:Font_(13) type:@"h"]; _topView = [UIView new]; _topView.backgroundColor = KKClearColor; _topView.frame = CGRectMake(0, 0, SCREEN_WIDTH, Adapted(rect.size.height+30)); UILabel *topLabelView = [UICommon ui_label:CGRectZero lines:0 align:NSTextAlignmentLeft font:FontADA_(13) textColor:[UIColor systemGrayColor] text:DDTEXT Radius:0]; [_topView addSubview:topLabelView]; [topLabelView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.equalTo(_topView).inset(15); make.centerY.equalTo(_topView); }]; } return _topView; } #pragma mark 查询通讯录成员 /// 查询通讯录成员 - (void)GetQueryAddressList { [UICommon MessageUpload:@"加载中"]; NSMutableDictionary *parameters = [NSMutableDictionary dictionary]; [parameters setValue:APIManager.sharedManager.deviceModel.imei forKey:@"imei"]; [[[APIManager sharedManager] APGET:QueryAddressList_URL parameters:parameters resultClass:nil] subscribeNext:^(NSArray *arr) { [UICommon HidenLoading]; if (ARRAYHASVALUE(arr)) { [self.modelListArr removeAllObjects]; NSMutableArray *fArr = [NSMutableArray new]; NSMutableArray *oArr = [NSMutableArray new]; for (NSDictionary *dic in arr) { ContactsModel *model = [ContactsModel yy_modelWithJSON:dic]; if ([model.openId isEqualToString:APIManager.sharedManager.loginModel.openid]) self.identity = model.identity; model.isSelect = NO; if (self.viewType == 1) { if (model.setBind == 1) [fArr addObject:model]; else [oArr addObject:model]; } else { if (model.setBind == 1) [fArr addObject:model]; } } if (fArr.count > 0) [self.modelListArr addObject:fArr]; if (oArr.count > 0) [self.modelListArr addObject:oArr]; } [self.contactsTabel reloadData]; } error:^(NSError * _Nullable error) { NSDictionary *dic = error.userInfo; [UICommon MessageErrorText:dic[NSLocalizedDescriptionKey]]; }]; } #pragma mark 转让管理员 /// 转让管理员 - (void)SetAdmin:(NSString *)Id identity:(NSInteger)identity { [UICommon MessageUpload:@"加载中"]; NSMutableDictionary *parameters = [NSMutableDictionary dictionary]; [parameters setValue:Id forKey:@"id"]; [parameters setValue:@(identity) forKey:@"identity"]; [parameters setValue:APIManager.sharedManager.deviceModel.imei forKey:@"imei"]; [[[APIManager sharedManager] APPOST:SetAdmin_URL parameters:parameters isJson:YES resultClass:nil] subscribeNext:^(id _Nullable x) { [UICommon HidenLoading]; [UICommon MessageSuccessText:@"管理员转让成功"]; [self GetQueryAddressList]; } error:^(NSError * _Nullable error) { NSDictionary *dic = error.userInfo; [UICommon MessageErrorText:dic[NSLocalizedDescriptionKey]]; }]; } - (void)setRightBarBtnType { [self.zx_navRightBtn setImage:ImageName_(@"icon_delect_1") forState:0] ; [self zx_rightClickedBlock:^(ZXNavItemBtn * _Nonnull btn) { self.isDeletMode = btn.currentImage == nil ? NO : YES; [self.zx_navRightBtn setImage:!self.isDeletMode ? ImageName_(@"icon_delect_1") : ImageName_(@"") forState:0]; [self.zx_navRightBtn setTitle:!self.isDeletMode ? @"" : GJText(@"取消") forState:0]; [self.leftBtn setTitle:self.isDeletMode ? GJText(@"全选") : GJText(@"通讯录导入") forState:0]; [self.rightBtn setTitle:self.isDeletMode ? GJText(@"删除") : GJText(@"手动添加") forState:0]; [self.contactsTabel reloadData]; }]; } /* #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