You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

370 lines
14 KiB

//
// SelectContactVC.m
// tongxin
//
// Created by WeiChaoZheng on 2018/6/27.
// Copyright © 2018年 xTT. All rights reserved.
//
#import "SelectContactVC.h"
#import "NewCollectionTableViewCell.h"
#import "NewTableViewCell.h"
#import "EditDeviceViewController.h"
/**
选择 绑定设备的 关系
*/
@interface SelectContactVC ()<UITextFieldDelegate>
{
NSInteger lastIndex;
}
@property (nonatomic ,strong) NSString *relation;
/**
弱引用的 关系cell
*/
//添加设备的模式下才有的 关系名称cell
@property (assign , nonatomic) NewTableViewCell *contactCell;
@property (assign , nonatomic) NewTableViewCell *phoneCell;
// 通讯录添加的模式下才有的 联系人cell
@property (assign , nonatomic) NewTableViewCell *nameCell;
/**
默认关系名称
*/
@property (strong , nonatomic) NSArray *contactNamrArr;
@end
@implementation SelectContactVC
@synthesize myDataSource = _myDataSource;
-(NSMutableArray *)myDataSource{
if(!_myDataSource){
if(self.isContact){
_myDataSource = [NSMutableArray arrayWithArray:@[@[@"联系人",@"电话号码",@"选择头像"]]];
}
else
{
_myDataSource = [NSMutableArray arrayWithArray:@[@[self.deviceType.integerValue == 3 ? @"与我的关系" : @"关系",@"电话号码",@"选择头像"]]];
}
}
return _myDataSource;
}
-(Contacts *)contactModel{
if(!_contactModel){
_contactModel = [Contacts new];
}
return _contactModel;
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
// self.contactNamrArr = @[@"爸爸",@"妈妈",@"爷爷",@"奶奶",@"外公",@"外婆",@"叔叔",@"阿姨",@"亲人"];
self.contactNamrArr = [myHelper getRelationshipNameArr:self.deviceType.integerValue];
self.relation = self.deviceType.integerValue == 3 ? @"与我的关系" : @"关系";
if(self.isContact){
self.myDataSource = [NSMutableArray arrayWithArray:@[@[@"联系人",@"电话号码",@"选择头像"]]];
}else{
if(self.isAddDevice){
self.myDataSource = [NSMutableArray arrayWithArray:@[@[self.relation,@"我的号码",@"选择头像"]]];
}else{
self.myDataSource = [NSMutableArray arrayWithArray:@[@[self.relation,@"选择头像"]]];
}
}
lastIndex = [self.myDataSource.firstObject count]-1;
NSString * rightBtnName = @"保存";
if(self.isAddDevice){
rightBtnName = @"下一步";
}
if(self.adminInfo){
rightBtnName = @"申请关注";
//弹框
NSString *adminPhone = self.adminInfo[@"admin_phone"];
adminPhone = [NSString stringWithFormat:@"%@****%@",[adminPhone substringToIndex:3],[adminPhone substringFromIndex:7]];
NSString *msg = [NSString stringWithFormat: @"设备已被%@绑定,请选择关系后向管理员申请关注",adminPhone];
LGAlertView *alertView = [[LGAlertView alloc] initWithTitle:@"提示" message:msg style:LGAlertViewStyleAlert buttonTitles:@[@"确定"] cancelButtonTitle:@"" destructiveButtonTitle:nil actionHandler:^(LGAlertView *alertView, NSString *title, NSUInteger index) {
} cancelHandler:nil destructiveHandler:nil];
[alertView showAnimated:YES completionHandler:nil];
}
[self zx_setRightBtnWithText:rightBtnName clickedBlock:^(ZXNavItemBtn * _Nonnull btn) {
[self rightBarItemClick];
}];
}
- (void)viewDidLoad {
[super viewDidLoad];
if(self.isContact){
if(self.isAddContact){
self.title = @"添加联系人";
}else{
self.title = @"修改联系人";
}
}else{
self.title = @"选择关系";
}
[self.view setBackgroundColor:[UIColor whiteColor]];
[self initTableView];
}
-(void)rightBarItemClick
{
WEAKSELF
self.contact = self.contactCell.aTextField.text;
self.phone = self.phoneCell.aTextField.text;
self.name = self.nameCell.aTextField.text;
if(self.isContact){
// 通讯录
if(self.nameCell.aTextField.text.length == 0){
[SVProgressHUD showErrorWithStatus:@"请输入联系人名称"];
return;
}else if (self.phoneCell.aTextField.text.length == 0 ){
[SVProgressHUD showErrorWithStatus:@"请输入电话号码"];
return;
}else if (!self.contactId){
if (self.isContact && self.isAddContact) {
[SVProgressHUD showErrorWithStatus:@"请选择头像"];
} else {
[SVProgressHUD showErrorWithStatus:@"请选择与设备的关系"];
}
return;
}
self.contactModel.name = self.nameCell.aTextField.text;
self.contactModel.phone = self.phoneCell.aTextField.text;
self.contactModel.relationship_image_id = self.contactId;
WEAKSELF
//根据 模型model 的 id 是否有判断 是使用 POST(添加) 还是 PATCH(修改)
[self.contactModel saveSuccess:^{
if(weakSelf.selectContactBack){
weakSelf.selectContactBack(weakSelf.contact, weakSelf.contactId, weakSelf.phone, weakSelf.name);
}
[weakSelf goBack:nil];
} failure:^{
xLog(@"cuo wu");
}];
return;
}
else if(!self.isAddDevice)
{
if(self.contactCell.aTextField.text.length == 0){
[SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"请输入%@",self.relation]];
return;
}else if (self.contactCell.aTextField.text.length >8) {
[SVProgressHUD showErrorWithStatus:@"关系不能大于8位"];
return;
}else if (self.isAddDevice && self.phoneCell.aTextField.text.length == 0 ){
[SVProgressHUD showErrorWithStatus:@"请输入电话号码"];
return;
}else if (!self.contactId){
[SVProgressHUD showErrorWithStatus:@"请选择与设备的关系"];
return;
}
if(self.selectContactBack){
if(!self.phone){
self.phone = cUser.cDevice.phone;
}
self.selectContactBack(self.contact, self.contactId, self.phone, self.name);
}
[self goBack:nil];
}
else
{
//添加设备的情况
if(self.contactCell.aTextField.text.length == 0){
[SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"请输入%@",self.relation]];
return;
}else if (self.contactCell.aTextField.text.length >8) {
[SVProgressHUD showErrorWithStatus:@"关系不能大于8位"];
return;
}else if (self.isAddDevice && self.phoneCell.aTextField.text.length == 0 ){
[SVProgressHUD showErrorWithStatus:@"请输入你的号码"];
return;
}else if (!self.contactId){
[SVProgressHUD showErrorWithStatus:@"请选择与设备的关系"];
return;
}
if([self.zx_navRightBtn.currentTitle isEqualToString:@"申请关注"])
{
NSMutableDictionary *params = [NSMutableDictionary dictionary];
[params setObject:self.adminInfo[@"admin_openid"] forKey:@"admin_openid"];
[params setObject:self.imei forKey:@"imei"];
//添加 我的手机号 phone , 关系image contactID, 和 关系名称contact
[params setObject:self.phone forKey:@"my_phone"];
[params setObject:self.contactId forKey:@"relationship_image_id"];
[params setObject:self.contact forKey:@"relationship"];
[cUser postDeviceInAdmin:params success:^(id responseObject) {
//弹窗提示向管理员发送关注设备申请
[weakSelf.navigationController popToRootViewControllerAnimated:YES];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[SVProgressHUD showSuccessWithStatus:@"已向管理员发送关注设备申请"];
});
} failure:^(id faiObject) {
xLog(@"申请关注失败");
}];
}
else
{
//下一步 应该是 管理员初始绑定
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
EditDeviceViewController *vc = [sb instantiateViewControllerWithIdentifier:@"EditDeviceViewController"];
vc.isAdd = YES;
vc.deviceType = self.deviceType;
vc.IMEI = self.imei;
vc.editDevice = [Device new];
vc.editDevice.imei = self.imei;
vc.editDevice.my_phone = self.phone;
vc.editDevice.relationship_image_id = self.contactId;
vc.editDevice.relationship = self.contact;
[self.navigationController pushViewController:vc animated:YES];
}
}
}
-(void)initTableView
{
self.myTableView.frame = CGRectMake(0, iPhoneX_NavHeight+10, SWIDTH, SHEIGHT-iPhoneX_NavHeight-10);
[self.view addSubview:self.myTableView];
//注册 cell
[self.myTableView registerNib:[UINib nibWithNibName:@"NewCollectionTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"NewCollectionTableViewCellID"];
[self.myTableView registerNib:[UINib nibWithNibName:@"NewTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"NewTableViewCellID"];
//去线
self.myTableView.separatorStyle = UITableViewCellSelectionStyleNone;
self.myTableView.backgroundColor = [UIColor whiteColor];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row == lastIndex) {
return 334+36;
}else{
return 60;
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if(indexPath.row == lastIndex){ // 关系 九宫格
NewCollectionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewCollectionTableViewCellID" forIndexPath:indexPath];
WEAKSELF
cell.deviceType = self.deviceType;
cell.contactcode = self.contactId.intValue;
cell.selectContactBlock = ^(NSString *contact, int code) {
// 如果输入的 名称是默认的 或者是 空的 都替换
if([weakSelf.contactNamrArr containsObject:weakSelf.contactCell.aTextField.text]
|| weakSelf.contactCell.aTextField.text.length == 0){
weakSelf.contactCell.aTextField.text = contact;
}
weakSelf.contact = contact;
weakSelf.contactId = @(code);
};
return cell;
}else{
NewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewTableViewCellID" forIndexPath:indexPath];
NSString * title = self.myDataSource[0][indexPath.row];
cell.aTitleLabel.text = title;
cell.aTextField.textAlignment = NSTextAlignmentRight;
if([title isEqualToString:self.relation]){
cell.aTextField.placeholder = @"请输入关系";
cell.aTextField.delegate = self;
cell.aTextField.tag = 9527;
if(cell.aTextField.text.length == 0){
cell.aTextField.text = self.contact;
}
self.contactCell = cell;
}else if([title isEqualToString:@"联系人"]){
cell.aTextField.placeholder = @"请输入联系人名称";
cell.aTextField.text = self.name;
cell.aTextField.delegate = self;
self.nameCell = cell;
self.contactCell = cell;
}else if([title isEqualToString:@"电话号码"]){
cell.aTextField.placeholder = @"请输入电话号码";
cell.aTextField.delegate = self; //限制
cell.aTextField.text = self.phone;
cell.aTextField.keyboardType = UIKeyboardTypePhonePad;
self.phoneCell =cell;
}else if([title isEqualToString:@"我的号码"]){
//我的号码
cell.aTextField.placeholder = @"请输入我的号码";
cell.aTextField.delegate = self; //限制
cell.aTextField.text = self.phone;
cell.aTextField.keyboardType = UIKeyboardTypePhonePad;
self.phoneCell =cell;
}
return cell;
}
return nil;
}
//MARK: 限制字数 和输入的 字符
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if([textField.superview isEqual:self.phoneCell]){
if([@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" containsString:string]){
// 最终的结果
NSString *str = textField.text;
NSString *resultStr = [str stringByReplacingCharactersInRange:range withString:string];
self.phone = resultStr;
if(resultStr.length > 15){
return NO;
}else{
return YES;
}
}else{
if([string isEqualToString:@""]){
return YES;
}
return NO;
}
}else if ([textField.superview isEqual:self.contactCell] || [textField.superview isEqual:self.nameCell] ){
// 最终的结果
NSString *str = textField.text;
NSString *resultStr = [str stringByReplacingCharactersInRange:range withString:string];
self.contact = resultStr;
self.name = resultStr;
}else if(textField.tag == 9527) {
NSString *str = textField.text;
NSString *resultStr = [str stringByReplacingCharactersInRange:range withString:string];
// add by lsz 限制
//if (resultStr.length > 8) {
// return NO;
//} else {
// return YES;
//}
}
return YES;
}
@end