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.

504 lines
18 KiB

//
// SelectContactVC.m
// tongxin
//
// Created by WeiChaoZheng on 2018/6/27.
// Copyright © 2018xTT. All rights reserved.
//
#import "SelectContactVC.h"
#import "NewCollectionTableViewCell.h"
#import "NewTableViewCell.h"
#import "EditDeviceViewController.h"
/**
选择 绑定设备的 关系
*/
@interface SelectContactVC ()<UITextFieldDelegate>
{
}
@property (nonatomic ,strong) NSString *relation;
/**
弱引用的 关系cell
*/
//添加设备的模式下才有的 关系名称cell
@property (assign , nonatomic) NewTableViewCell *contactCell;
@property (assign , nonatomic) NewTableViewCell *phoneCell;
@property (assign , nonatomic) NewTableViewCell *dphoneCell;
// 通讯录添加的模式下才有的 联系人cell
@property (assign , nonatomic) NewTableViewCell *nameCell;
@property (strong , nonatomic) UIBarButtonItem *rightBarButton;
/**
默认关系名称
*/
@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)
{
if(self.isAddContact)
self.myDataSource = [NSMutableArray arrayWithArray:@[@[@"联系人",@"电话号码",@"短号/亲情号"],@[@"选择头像"]]];
else
self.myDataSource = [NSMutableArray arrayWithArray:@[@[@"联系人",@"电话号码"],@[@"选择头像"]]];
}
else
{
if(self.isAddDevice){
self.myDataSource = [NSMutableArray arrayWithArray:@[@[self.relation,@"我的号码"],@[@"选择头像"]]];
}else{
self.myDataSource = [NSMutableArray arrayWithArray:@[@[self.relation],@[@"选择头像"]]];
}
}
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];
}];
if(self.isContact){
self.zx_navRightBtn.enabled = NO;
[self.zx_navRightBtn setTitleColor:mainColorK forState:0];
}
}
- (void)viewDidLoad {
[super viewDidLoad];
if(self.isContact){
if(self.isAddContact){
self.zx_navTitle = @"添加联系人";
}else{
self.zx_navTitle = @"修改联系人";
}
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:nil];
}else{
self.zx_navTitle = @"选择关系";
}
[self.view setBackgroundColor:[UIColor whiteColor]];
[self initTableView];
}
- (void)dianhao
{
self.contact = self.contactCell.aTextField.text;
self.phone = self.dphoneCell.aTextField.text;
self.name = [NSString stringWithFormat:@"%@(短号)",self.nameCell.aTextField.text];
// 通讯录
if(self.nameCell.aTextField.text.length == 0){
[UICommon MessageErrorText:@"请输入联系人名称"];
return;
}else if (self.phoneCell.aTextField.text.length == 0 ){
[UICommon MessageErrorText:@"请输入电话号码"];
return;
}else if (!self.contactId){
if (self.isContact && self.isAddContact) {
[UICommon MessageErrorText:@"请选择头像"];
} else {
[UICommon MessageErrorText:@"请选择与设备的关系"];
}
return;
}
self.contactModel.name = [NSString stringWithFormat:@"%@(短号)",self.nameCell.aTextField.text];
self.contactModel.phone = self.dphoneCell.aTextField.text;
self.contactModel.relationship_image_id = self.contactId;
WEAKSELF
//根据 模型modelid 是否有判断 是使用 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");
}];
}
-(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){
[UICommon MessageErrorText:@"请输入联系人名称"];
return;
}else if (self.phoneCell.aTextField.text.length == 0 ){
[UICommon MessageErrorText:@"请输入电话号码"];
return;
}else if (!self.contactId){
if (self.isContact && self.isAddContact) {
[UICommon MessageErrorText:@"请选择头像"];
} else {
[UICommon MessageErrorText:@"请选择与设备的关系"];
}
return;
}
self.contactModel.name = self.nameCell.aTextField.text;
self.contactModel.phone = self.phoneCell.aTextField.text;
self.contactModel.relationship_image_id = self.contactId;
WEAKSELF
//根据 模型modelid 是否有判断 是使用 POST(添加) 还是 PATCH(修改)
[self.contactModel saveSuccess:^{
if(weakSelf.selectContactBack){
weakSelf.selectContactBack(weakSelf.contact, weakSelf.contactId, weakSelf.phone, weakSelf.name);
}
if (self.isAddContact)
[self dianhao];
else
[weakSelf goBack:nil];
} failure:^{
xLog(@"cuo wu");
}];
return;
}else if(!self.isAddDevice){
if(self.contactCell.aTextField.text.length == 0){
[UICommon MessageErrorText:[NSString stringWithFormat:@"请输入%@",self.relation]];
return;
}else if (self.contactCell.aTextField.text.length >8) {
[UICommon MessageErrorText:@"关系不能大于8位"];
return;
}else if (self.isAddDevice && self.phoneCell.aTextField.text.length == 0 ){
[UICommon MessageErrorText:@"请输入电话号码"];
return;
}else if (!self.contactId){
[UICommon MessageErrorText:@"请选择与设备的关系"];
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){
[UICommon MessageErrorText:[NSString stringWithFormat:@"请输入%@",self.relation]];
return;
}else if (self.contactCell.aTextField.text.length >8) {
[UICommon MessageErrorText:@"关系不能大于8位"];
return;
}else if (self.isAddDevice && self.phoneCell.aTextField.text.length == 0 ){
[UICommon MessageErrorText:@"请输入你的号码"];
return;
}else if (!self.contactId){
[UICommon MessageErrorText:@"请选择与设备的关系"];
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(), ^{
[UICommon MessageSuccessText:@"已向管理员发送关注设备申请" isImg:YES];
});
} 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.view.backgroundColor = RGB(248, 247, 247);
//self.tableView.estimatedRowHeight = 0;
self.myTableView.estimatedSectionHeaderHeight = 0;
self.myTableView.estimatedSectionFooterHeight = 0;
self.myTableView.frame = CGRectMake(0, iPhoneX_NavHeight, SWIDTH, SHEIGHT-iPhoneX_NavHeight);
[UICommon setTavleViewlink:self.myTableView distance:16];
[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 = KKClearColor;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return section == 0 ? 16 : 12;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section == 1)
{
return 334+36;
}else{
return 60;
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if(indexPath.section == 1)
{ // 关系 九宫格
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);
[weakSelf textChange];
};
return cell;
}
else
{
NewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewTableViewCellID" forIndexPath:indexPath];
[cell.backView mas_updateConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(cell).inset(16);
}];
cell.lineImage.hidden = YES;
cell.backgroundColor = KKWhiteColorColor;
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;
}
else if([title isEqualToString:@"短号/亲情号"]){
//我的号码
cell.aTextField.placeholder = @"请输入短号/亲情号(可不输入)";
cell.aTextField.delegate = self; //限制
cell.aTextField.text = self.dphone;
cell.aTextField.keyboardType = UIKeyboardTypePhonePad;
self.dphoneCell =cell;
}
CGSize size = [cell.aTitleLabel.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:Font_(16),NSFontAttributeName,nil]];
1 year ago
[cell.aTitleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(cell.backView);
make.centerY.equalTo(cell.contentView);
make.width.mas_equalTo(Adapted(size.width+10));
}];
[cell.aTextField mas_remakeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(cell.contentView);
make.right.equalTo(cell.backView.mas_right);
make.left.equalTo(cell.aTitleLabel.mas_right).offset(15);
make.height.mas_equalTo(30);
}];
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;
}
-(void)textChange
{
NSLog(@"-----Change-----\n%@--\n%@--\n%@",self.contactCell.aTextField.text,self.contactId,self.phoneCell.aTextField.text);
if (self.contactCell.aTextField.text.length > 0 && self.phoneCell.aTextField.text.length > 0 && self.contactId)
{
self.zx_navRightBtn.enabled = YES;
[self.zx_navRightBtn setTitleColor:mainColor forState:0];
}
else
{
self.zx_navRightBtn.enabled = NO;
[self.zx_navRightBtn setTitleColor:mainColorK forState:0];
}
}
//当前对像要销毁的时候,创建的通知要进行移除:(否则可能报错,坏内存访问)
-(void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end