// // AddWatchViewController.m // LekangGuard // // Created by ecell on 2022/10/12. // #import "AddWatchViewController.h" #define View_height Adapted(55) @interface AddWatchViewController () /// 头像Btn @property (nonatomic ,weak) UIButton *iconBtn; /// 姓名Field @property (nonatomic ,weak) UITextField *nameField; /// 设备电话号码Field @property (nonatomic ,weak) UITextField *phoneField; /// 身高Field @property (nonatomic ,weak) UITextField *heightField; /// 体重Field @property (nonatomic ,weak) UITextField *weightField; /// 生日Btn @property (nonatomic ,weak) UIButton *birthdayBtn; /// 身高Btn @property (nonatomic ,weak) UIButton *heightBtn; /// 体重Btn @property (nonatomic ,weak) UIButton *weightBtn; /// 性别 @property (nonatomic ,assign) NSInteger sex; /// 像头文件 @property (nonatomic ,strong) NSData *iconFile; @end @implementation AddWatchViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = KKBackgroundGrey; self.zx_navTitle = GJText(@"添加设备"); self.sex = 0; [self zx_setRightBtnWithText:GJText(@"添加") clickedBlock:^(ZXNavItemBtn * _Nonnull btn) { [self SetAddDevice]; }]; [self DeviceMsgView]; } #pragma mark 设备信息View /// 设备信息View - (void)DeviceMsgView { NSArray *titleArr; if (self.model.virtualFlag) { titleArr = @[GJText(@"头像"), GJText(@"名称"), GJText(@"性别"), GJText(@"设备电话号码")]; } else { titleArr = @[GJText(@"头像"), GJText(@"名称"), GJText(@"性别"), GJText(@"学校"), GJText(@"年级"), GJText(@"班级"), GJText(@"设备电话号码")]; } UIView *bgView = [UICommon ui_view:CGRectZero backgroundColor:KKWhiteColorColor cornerRadius:0 borderWidth:0 borderColor:KKClearColor]; [self.view addSubview:bgView]; [bgView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.view).offset(iPhoneX_NavHeight); make.left.right.equalTo(self.view); make.height.mas_equalTo(titleArr.count*View_height); }]; for (int i = 0; i < titleArr.count; i++) { UILabel *titleLabel = [UICommon ui_label:CGRectZero lines:0 align:NSTextAlignmentLeft font:FontADA_(14) textColor:KKTextBlackColor text:titleArr[i] Radius:0]; [bgView addSubview:titleLabel]; CGRect rect = [UICommon GetTextWidth:titleLabel.text ViewHeight:View_height fontSize:FontADA_(14) type:@"w"]; [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(bgView).offset(15); make.top.equalTo(bgView).offset(View_height*i); make.height.mas_equalTo(View_height); make.width.mas_equalTo(rect.size.width+5); }]; if (i < titleArr.count-1) { UILabel *line = [UILabel new]; line.backgroundColor = KKLineColor; [bgView addSubview:line]; [line mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.equalTo(bgView).inset(15); make.top.equalTo(titleLabel.mas_bottom); make.height.mas_equalTo(0.5); }]; } if (i == 0) { /// 头像 UIButton *iconBtn = [UICommon ui_buttonSimple:CGRectZero font:FontADA_(0) normalColor:KKWhiteColorColor normalText:@"" click:^(id x) { [UICommon resignKeyboard]; [self selectIcon]; }]; [iconBtn setImage:ImageName_(@"icon_girl_head_portrait") forState:0]; self.iconBtn = iconBtn; [bgView addSubview:iconBtn]; [iconBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(bgView.mas_right).inset(15); make.centerY.equalTo(titleLabel); make.width.height.mas_equalTo(Adapted(42)); }]; } else if (i != 0 && i != 2) { /// 姓名和电话号码 UITextField *textfield = [UICommon ui_textField:CGRectZero textColor:KKTextBlackColor backColor:KKClearColor font:FontADA_(13) maxTextNum:i == titleArr.count-1 ? 11 : 20 placeholderColor:KKGrey143 placeholder:i == 1 ? GJText(@"请输入名称") : GJText(@"请输入设备电话号码") toMaxNum:^(UITextField *textField) { } change:^(UITextField *textField) { }]; textfield.textAlignment = NSTextAlignmentRight; textfield.enabled = NO; textfield.textColor = KKGrey143; if (i == 1) { if (self.model.virtualFlag) { textfield.enabled = YES; textfield.textColor = KKTextBlackColor; } textfield.text = self.model.name; self.nameField = textfield; } if (!self.model.virtualFlag) { if (i == 3) { textfield.text = self.model.schName; } if (i == 4) { textfield.text = self.model.gradeName; } if (i == 5) { textfield.text = self.model.className; } } if (i == titleArr.count-1) { textfield.enabled = YES; textfield.textColor = KKTextBlackColor; textfield.keyboardType = UIKeyboardTypePhonePad; textfield.delegate = self; textfield.text = self.model.phone; self.phoneField = textfield; } [bgView addSubview:textfield]; [textfield mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(bgView).inset(15); make.left.equalTo(titleLabel.mas_right).offset(5); make.centerY.equalTo(titleLabel); make.height.mas_equalTo(Adapted(44)); }]; } else if (i == 2) { /// 性别 if (self.model.sex.length > 0) self.sex = self.model.sex.integerValue; NSArray *sexArr = @[GJText(@"女"), GJText(@"男")]; UIButton *sexBtn = [UICommon ui_buttonSimple:CGRectZero font:FontADA_(13) normalColor:KKTextBlackColor normalText:sexArr[self.sex] click:^(UIButton *btn) { [BRStringPickerView showPickerWithTitle:GJText(@"请选择性别") dataSourceArr:sexArr selectIndex:self.sex resultBlock:^(BRResultModel * _Nullable resultModel) { [UICommon resignKeyboard]; [btn setTitle:resultModel.value forState:0]; self.sex = resultModel.index; }]; }]; sexBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight; [bgView addSubview:sexBtn]; [sexBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(bgView.mas_right).inset(15); make.centerY.equalTo(titleLabel); make.size.mas_equalTo(CGSizeMake(Adapted(150), Adapted(44))); }]; } } [self UserMsgView:bgView]; } #pragma mark 关系信息View /// 关系信息View - (void)UserMsgView:(UIView *)view { NSInteger h = 50; NSInteger w = 5; NSMutableArray *hArr = [NSMutableArray new]; NSMutableArray *wArr = [NSMutableArray new]; for (int i = 0; i < 20; i++) { h+=5; w+=5; [hArr addObject:F(@"%ld",h)]; [wArr addObject:F(@"%ld",w)]; } NSArray *titleArr = @[GJText(@"与我的关系"), GJText(@"生日"), GJText(@"身高(cm)"), GJText(@"体重(kg)")]; UIView *bgView = [UICommon ui_view:CGRectZero backgroundColor:KKWhiteColorColor cornerRadius:0 borderWidth:0 borderColor:KKClearColor]; [self.view addSubview:bgView]; [bgView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(view.mas_bottom).offset(15); make.left.right.equalTo(self.view); make.height.mas_equalTo(titleArr.count*View_height); }]; for (int i = 0; i < titleArr.count; i++) { UILabel *titleLabel = [UICommon ui_label:CGRectZero lines:0 align:NSTextAlignmentCenter font:FontADA_(14) textColor:KKTextBlackColor text:titleArr[i] Radius:0]; [bgView addSubview:titleLabel]; [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(bgView).offset(15); make.top.equalTo(bgView).offset(View_height*i); make.height.mas_equalTo(View_height); }]; if (i < titleArr.count-1) { UILabel *line = [UILabel new]; line.backgroundColor = KKLineColor; [bgView addSubview:line]; [line mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.equalTo(bgView).inset(15); make.top.equalTo(titleLabel.mas_bottom); make.height.mas_equalTo(0.5); }]; } if (i == 0) { UILabel *nameLabel = [UICommon ui_label:CGRectZero lines:0 align:NSTextAlignmentCenter font:FontADA_(13) textColor:KKTextBlackColor text:self.relationship Radius:0]; [bgView addSubview:nameLabel]; [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(bgView.mas_right).inset(15); make.centerY.equalTo(titleLabel); }]; UIImageView *iconImg = [UICommon ui_imageView:CGRectZero fileName:@""]; iconImg.image = [UICommon getClickRelationshipImageWithCodeID:self.relationshipId deviceType:self.deviceType]; [bgView addSubview:iconImg]; [iconImg mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(nameLabel.mas_left).inset(15); make.centerY.equalTo(titleLabel); make.width.height.mas_equalTo(Adapted(42)); }]; } else //if (i == 1) { UIButton *commBtn = [UICommon ui_buttonSimple:CGRectZero font:FontADA_(13) normalColor:KKTextBlackColor normalText:i == 1 ? [UICommon getSysTime] : i == 2 ? @"100" : @"20" click:^(UIButton *btn) { [UICommon resignKeyboard]; if (btn.tag == 1) { [BRDatePickerView showDatePickerWithMode:BRDatePickerModeYMD title:GJText(@"请选择生日") selectValue:[UICommon getSysTime] minDate:nil maxDate:nil isAutoSelect:NO resultBlock:^(NSDate * _Nullable selectDate, NSString * _Nullable selectValue) { [self.birthdayBtn setTitle:selectValue forState:0]; }]; } else if (i == 2) { [BRStringPickerView showPickerWithTitle:GJText(@"请选择身高") dataSourceArr:hArr selectIndex:2 resultBlock:^(BRResultModel * _Nullable resultModel) { [self.heightBtn setTitle:resultModel.value forState:0]; }]; } else { [BRStringPickerView showPickerWithTitle:GJText(@"请选择体重") dataSourceArr:wArr selectIndex:2 resultBlock:^(BRResultModel * _Nullable resultModel) { [self.weightBtn setTitle:resultModel.value forState:0]; }]; } }]; commBtn.tag = i; if (i == 1) self.birthdayBtn = commBtn; else if (i == 2) self.heightBtn = commBtn; else self.weightBtn = commBtn; commBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight; [bgView addSubview:commBtn]; [commBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(bgView.mas_right).inset(15); make.centerY.equalTo(titleLabel); make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH-150, Adapted(44))); }]; } // else // { // /// 姓名和电话号码 // UITextField *textfield = [UICommon ui_textField:CGRectZero textColor:KKTextBlackColor backColor:KKClearColor font:FontADA_(13) maxTextNum:20 placeholderColor:KKGrey143 placeholder:i == 2 ? GJText(@"请输入身高") : GJText(@"请输入体重") toMaxNum:^(UITextField *textField) { // // } change:^(UITextField *textField) { // // }]; // textfield.text = i == 2 ? @"100" : @"20"; // textfield.textAlignment = NSTextAlignmentRight; // textfield.keyboardType = UIKeyboardTypeNumberPad; // if (i == 2) self.heightField = textfield; // else self.weightField = textfield; // [bgView addSubview:textfield]; // [textfield mas_makeConstraints:^(MASConstraintMaker *make) { // make.right.equalTo(bgView).inset(10); // make.centerY.equalTo(titleLabel); // make.size.mas_equalTo(CGSizeMake(Adapted(200), Adapted(44))); // }]; // } } } #pragma mark 添加设备 /// 添加设备 - (void)SetAddDevice { if (self.phoneField.text.length <= 0) { [UICommon MessageErrorText:GJText(@"请输入号码")]; return; } if (self.nameField.text.length <= 0) { [UICommon MessageErrorText:GJText(@"请输入姓名")]; return; } self.iconFile = UIImageJPEGRepresentation(self.iconBtn.currentImage, 1.0f); [UICommon MessageUpload:@"加载中"]; NSMutableDictionary *parameters = [NSMutableDictionary dictionary]; [parameters setValue:self.birthdayBtn.currentTitle forKey:@"birthday"]; [parameters setValue:self.heightBtn.currentTitle forKey:@"height"]; [parameters setValue:self.imei forKey:@"imei"]; [parameters setValue:self.phone forKey:@"my_phone"]; [parameters setValue:self.nameField.text forKey:@"name"]; [parameters setValue:self.phoneField.text forKey:@"phone"]; [parameters setValue:self.relationship forKey:@"relationship"]; [parameters setValue:@(self.relationshipId) forKey:@"relationshipId"]; [parameters setValue:@(self.sex) forKey:@"sex"]; [parameters setValue:self.weightBtn.currentTitle forKey:@"weight"]; [parameters setValue:@(self.model.virtualFlag) forKey:@"virtualFlag"]; if (self.model.virtualFlag) { [parameters setValue:self.model.schId forKey:@"schId"]; [parameters setValue:self.model.gradeId forKey:@"gradeId"]; [parameters setValue:self.model.classId forKey:@"classId"]; } [[[APIManager sharedManager] uploadImageWithURL:AddDevice_URL parameters:parameters images:@[self.iconBtn.currentImage]] subscribeNext:^(id _Nullable x) { [UICommon HidenLoading]; [UICommon MessageSuccessText:@"添加成功"]; [[NSNotificationCenter defaultCenter] postNotificationName:UPDATA_DeviceList object:nil]; AfterDispatch(1, ^{ [self.navigationController popToRootViewControllerAnimated:YES]; }); } error:^(NSError * _Nullable error) { NSDictionary *dic = error.userInfo; [UICommon MessageErrorText:dic[NSLocalizedDescriptionKey]]; }]; } - (void)selectIcon { UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; [alertController addAction:[UIAlertAction actionWithTitle:GJText(@"拍照") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera; if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) { UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.delegate = self; picker.sourceType = sourceType; [self presentViewController:picker animated:YES completion:nil]; }else { NSLog(@"模拟其中无法打开照相机,请在真机中使用"); } }]]; [alertController addAction:[UIAlertAction actionWithTitle:GJText(@"从相册选择") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { UIImagePickerController * picker = [[UIImagePickerController alloc] init]; if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) { picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; picker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:picker.sourceType]; } picker.delegate = self; [self presentViewController:picker animated:YES completion:nil]; }]]; [alertController addAction:[UIAlertAction actionWithTitle:GJText(@"取消") style:UIAlertActionStyleCancel handler:nil]]; [self presentViewController:alertController animated:YES completion:nil]; return; } #pragma mark 协议的方法 //用户点击图像选取器中的“cancel”按钮时被调用,这说明用户想要中止选取图像的操作 - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { [picker dismissViewControllerAnimated:YES completion:nil]; } //用户点击选取器中的“choose”按钮时被调用,告知委托对象,选取操作已经完成,同时将返回选取图片的实例 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { UIImage *curImg = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; [picker dismissViewControllerAnimated:YES completion:nil]; [self.iconBtn setImage:[UICommon reduceImage:curImg percent:0.5] forState:0]; } - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { if (textField != self.phoneField) return NO; if([@"0123456789" containsString:string]) { // 最终的结果 NSString *str = textField.text; NSString *resultStr = [str stringByReplacingCharactersInRange:range withString:string]; if(resultStr.length > 15){ return NO; }else{ return YES; } } else { if([string isEqualToString:@""]){ return YES; } return NO; } } - (BOOL)textFieldShouldReturn:(UITextField *)textField { [UICommon resignKeyboard]; return YES; } /* #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