// // ModifyDataViewController.m // LekangGuard // // Created by ecell on 2023/1/8. // #import "ModifyDataViewController.h" @interface ModifyDataViewController () @property (nonatomic ,strong) NSArray *titleArr; @property (nonatomic ,strong) NSData *iconFile; @property (nonatomic ,weak) UIImageView *iconImg; @property (nonatomic ,strong) NSString *nameStr; @property (nonatomic ,strong) NSString *phoneStr; @property (nonatomic ,assign) BOOL isSelectIcon; /// @property (nonatomic ,strong) UIImage *curImg; @end @implementation ModifyDataViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.zx_navTitle = GJText(@"我的资料"); self.isSelectIcon = NO; [self zx_setRightBtnWithText:GJText(@"保存") clickedBlock:^(ZXNavItemBtn * _Nonnull btn) { [self UpdateInfo]; }]; self.nameStr = self.model.name; self.phoneStr = self.model.phone; self.titleArr = @[GJText(@"头像"), GJText(@"昵称"), GJText(@"手机号")]; UIView *bgView = [UICommon ui_view:CGRectMake(0, iPhoneX_NavHeight, SCREEN_WIDTH, Adapted(55)*self.titleArr.count) backgroundColor:KKWhiteColorColor cornerRadius:0 borderWidth:0 borderColor:KKWhiteColorColor]; [self.view addSubview:bgView]; for (int i = 0; i < self.titleArr.count; i++) { UIButton *btn = [UICommon ui_buttonSimple:CGRectZero font:FontADA_(14) normalColor:KKTextBlackColor normalText:self.titleArr[i] click:^(UIButton *button) { if (button.tag == 0) { [self selectIcon]; } }]; btn.tag = i; btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; [bgView addSubview:btn]; [btn mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(bgView).offset(Adapted(55)*i); make.left.equalTo(bgView).offset(20); make.right.equalTo(bgView.mas_right); make.height.mas_equalTo(Adapted(55)); }]; if (i == 0) { UIImageView *iconImg = [UIImageView new]; iconImg.layer.cornerRadius = 5; iconImg.layer.masksToBounds = YES; [iconImg sd_setImageWithURL:[NSURL URLWithString:self.model.image] placeholderImage:ImageName_(@"icon_adults_occupies_the_head")]; self.iconImg = iconImg; [bgView addSubview:iconImg]; [iconImg mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(bgView.mas_right).inset(20); make.centerY.equalTo(btn); make.height.width.mas_equalTo(Adapted(45)); }]; } else { UITextField *nameField = [UICommon ui_textField:CGRectZero textColor:KKTextBlackColor backColor:KKClearColor font:FontADA_(14) maxTextNum:i == 1 ? 30 : 11 placeholderColor:KKGrey121 placeholder:@"" toMaxNum:nil change:nil]; nameField.textAlignment = NSTextAlignmentRight; nameField.text = i == 1 ? self.model.name : self.model.phone; nameField.tag = i; [nameField addTarget:self action:@selector(textFieldEditChanged:) forControlEvents:UIControlEventEditingChanged]; [bgView addSubview:nameField]; [nameField mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(bgView).inset(20); make.centerY.equalTo(btn); make.left.equalTo(bgView).offset(20); make.height.mas_equalTo(Adapted(46)); }]; } if (i < self.titleArr.count-1) { UILabel *line = [UILabel new]; line.backgroundColor = KKLineColor; [btn addSubview:line]; [line mas_makeConstraints:^(MASConstraintMaker *make) { //make.left.equalTo(bgView).offset(20); make.left.right.equalTo(bgView); make.bottom.equalTo(btn.mas_bottom); make.height.mas_equalTo(0.5); }]; } } } - (void)textFieldEditChanged:(UITextField *)textField { if (textField.tag == 1) { self.nameStr = textField.text; } else self.phoneStr = textField.text; } #pragma mark 修改账号信息 /// 修改账号信息 - (void)UpdateInfo { if (self.model.phone.length <= 0) { [UICommon MessageErrorText:@"请先绑定设备"]; return; } if (self.nameStr.length <= 0) { [UICommon MessageErrorText:@"昵称不能为空"]; return; } if (self.phoneStr.length <= 0) { [UICommon MessageErrorText:@"手机号不能为空"]; return; } if (!self.curImg) self.curImg = self.iconImg.image; [UICommon MessageUpload:@"加载中"]; NSMutableDictionary *parameters = [NSMutableDictionary dictionary]; [parameters setValue:self.model.imei forKey:@"imei"]; [parameters setValue:self.nameStr forKey:@"name"]; [parameters setValue:self.phoneStr forKey:@"phone"]; [[[APIManager sharedManager] uploadImageWithURL:UpdateInfo_URL parameters:parameters images:self.isSelectIcon ? @[self.curImg] : nil] subscribeNext:^(id _Nullable x) { NSLog(@"%@",x); [UICommon HidenLoading]; [UICommon MessageSuccessText:@"保存成功"]; AfterDispatch(1, ^{ [self.navigationController popViewControllerAnimated:YES]; }); } error:^(NSError * _Nullable error) { NSDictionary *dic = error.userInfo; [UICommon MessageErrorText:dic[NSLocalizedDescriptionKey]]; }]; } - (void)selectIcon { [UICommon resignKeyboard]; 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]; if(curImg) { self.curImg = curImg; self.iconImg.image = [UICommon reduceImage:curImg percent:0.5]; self.isSelectIcon = 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