// // EditMeViewController.m // watch // // Created by xTT on 2017/7/13. // Copyright © 2017年 xTT. All rights reserved. // #import "EditMeViewController.h" #import "ShowWXViewController.h" #import //#import "WXApiObject.h" #import "WechatAuthSDK.h" @interface EditMeViewController () @property (strong, nonatomic) User *editUser; @end @implementation EditMeViewController @synthesize myDataSource = _myDataSource; - (NSMutableArray *)myDataSource{ if (!_myDataSource) { NSArray *tempArr = @[@"头像",@"昵称",@"手机账号",@"微信",@"密码"]; if(cUser.account_status.intValue == 0){ if(cUser.wx_status.intValue == 1){ //微信 tempArr = @[@"头像",@"昵称",@"手机号码"]; }else{ //imei tempArr = @[@"头像",@"昵称",@"手机号码"]; } }else{ // 手机登录 或者 是 已有的手机账号绑定了某微信号,使用某微信号登录 tempArr = @[@"头像",@"昵称",@"手机账号",@"微信",@"密码"]; } _myDataSource = [[NSMutableArray alloc] initWithArray:@[tempArr]]; } return _myDataSource; } - (User *)editUser{ if (!_editUser) { _editUser = [[User alloc] init]; _editUser.openid = cUser.openid; _editUser.image = cUser.image; _editUser.name = cUser.name; _editUser.phone = cUser.phone; } return _editUser; } -(void)viewWillAppear:(BOOL)animated{ if(self.willLoadBlack){ self.willLoadBlack(); } [self refreshData]; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.zx_navTitle = @"编辑资料"; WEAKSELF [self.myTableView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.equalTo(self.view); make.top.equalTo(self.view).offset(iPhoneX_NavHeight); make.bottom.equalTo(self.view.mas_bottom); }]; self.myTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{ [weakSelf refreshData]; }]; [self zx_setRightBtnWithText:@"保存" clickedBlock:^(ZXNavItemBtn * _Nonnull btn) { [self.view endEditing:YES]; //取消 所有的输入框的焦点 if (self.editUser.phone.length != 11) { [SVProgressHUD showErrorWithStatus:@"请输入11位号码"]; }else if(self.editUser.name.length == 0){ [SVProgressHUD showErrorWithStatus:@"请输入昵称"]; }else if(self.editUser.name.length >8) { [SVProgressHUD showErrorWithStatus:@"昵称不能大于8个字符"]; } else{ [self.editUser saveUserInfoSuccess:^(id responseObject) { // cUser.image = self.editUser.image; cUser.phone = self.editUser.phone; cUser.name = self.editUser.name; cUser.avator = self.editUser.avator; [self.navigationController popViewControllerAnimated:YES]; } failure:^(id faiObject) { }]; } }]; } -(void)refreshData{ WEAKSELF [cUser getUserInfoSuccess:^(id responseObject) { [weakSelf.myTableView reloadData]; [weakSelf.myTableView.mj_header endRefreshing]; } failure:^(id faiObject) { [weakSelf.myTableView.mj_header endRefreshing]; }]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ NSString *title = self.myDataSource[indexPath.section][indexPath.row]; if ([title isEqualToString:@"头像"]) { baseCell *cell = [tableView dequeueReusableCellWithIdentifier:@"baseCell"]; if (!cell) { cell = [[baseCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"baseCell"]; UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)]; imageView.layer.cornerRadius = 3; imageView.layer.masksToBounds = YES; [imageView sd_setImageWithURL:[NSURL URLWithString:cUser.avator] placeholderImage:[myHelper getImageWithName:@"默认头像"] options:SDWebImageRetryFailed]; } cell.textLabel.text = title; UIImageView *imageView = (UIImageView *)cell.accessoryView; if (self.editUser.image) { imageView.image = self.editUser.image; } return cell; }else{ TextFieldCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TextFieldCell"]; if (!cell) { cell = [[TextFieldCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"TextFieldCell"]; cell.accessoryType = UITableViewCellAccessoryNone; cell.textField.textAlignment = NSTextAlignmentRight; } cell.textLabel.text = title; if ([title isEqualToString:@"昵称"]) { cell.maxLimitLength = 20; cell.textField.text = self.editUser.name; cell.textField.placeholder = @"8位字符内"; }else if ([title isEqualToString:@"手机账号"]){ cell.textField.text = self.editUser.phone; cell.textField.textColor = [UIColor lightGrayColor]; cell.userInteractionEnabled = NO; }else if ([title isEqualToString:@"微信"]){ if (cUser.wx_status == nil || cUser.wx_status.intValue == 0) { cell.textField.text = @"立即绑定"; cell.textField.textColor = mainColor; }else{ cell.textField.text = cUser.wx_name; cell.textField.textColor = [UIColor lightGrayColor]; } cell.textField.userInteractionEnabled = NO; }else if ([title isEqualToString:@"密码"]){ cell.textField.text = @"修改"; cell.textField.textColor = mainColor; cell.textField.userInteractionEnabled = NO; }else if ([title isEqualToString:@"手机号码"]){ //微信登录 和 imei登录 才会显示 手机号码 , 手机登录的是显示手机账号 cell.textField.keyboardType = UIKeyboardTypeNamePhonePad; if(cUser.phone == nil ||cUser.phone.length == 0){ cell.textField.text = @"立即绑定"; cell.textField.textColor = mainColor; cell.textField.userInteractionEnabled = NO; }else{ cell.textField.text = self.editUser.phone; cell.textField.textColor = [UIColor darkGrayColor]; cell.textField.userInteractionEnabled = YES; } } cell.block = ^(NSString *sender, TextFieldCell *cell) { if ([title isEqualToString:@"昵称"]) { self.editUser.name = sender; }else if ([title isEqualToString:@"手机号码"]){ self.editUser.phone = sender; } }; return cell; } return nil; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ NSString *title = self.myDataSource[indexPath.section][indexPath.row]; if ([title isEqualToString:@"头像"]){ UIAlertController *sheet = [myHelper getAlertWithPicture:^(UIImagePickerController *imagePickerVC) { imagePickerVC.delegate = self; [self presentViewController:imagePickerVC animated:YES completion:^{}]; }]; if(sheet){ [self presentViewController:sheet animated:YES completion:nil]; } }else if ([title isEqualToString:@"密码"]){ UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; baseViewController *vc = [sb instantiateViewControllerWithIdentifier:@"ChangePasswordViewController"]; [self.navigationController pushViewController:vc animated:YES]; }else if ([title isEqualToString:@"微信"]){ if (cUser.wx_status.intValue == 0 || cUser.wx_status == nil) { WEAKSELF [[APHandleManager sharedManager] authorizationWachat:^(NSDictionary *authInfo) { NSMutableDictionary * dic = [NSMutableDictionary dictionary]; [dic setValue:authInfo[@"unionid"] forKey:@"id"]; [dic setValue:authInfo[@"nickname"] forKey:@"name"]; [dic setValue:authInfo[@"unionid"] forKey:@"unionid"]; [User userBindWX:dic success:^(id responseObject) { //清除数据 刷新列表 weakSelf.editUser = nil; [weakSelf refreshData]; } failure:^(id faiObject) { [weakSelf refreshData]; }]; } failure:^(HandleStatus code) { }]; // SSDKPlatformType platformType = SSDKPlatformTypeWechat; // [ShareSDK cancelAuthorize:SSDKPlatformTypeWechat]; // // // // WEAKSELF // [ShareSDK getUserInfo:platformType // onStateChanged:^(SSDKResponseState state, SSDKUser *user, NSError *error) // { // if (state == SSDKResponseStateSuccess) // { // NSMutableDictionary * dic = [NSMutableDictionary dictionary]; // [dic setValue:user.uid forKey:@"id"]; // [dic setValue:user.nickname forKey:@"name"]; // [dic setValue:[user.rawData objectForKey:@"unionid"] forKey:@"unionid"]; // // //2 是微信登录 // [User userBindWX:dic success:^(id responseObject) { // //清除数据 刷新列表 // weakSelf.editUser = nil; // [weakSelf refreshData]; // // } failure:^(id faiObject) { // [weakSelf refreshData]; // }]; // }else{ // NSLog(@"%@",error); // } // }]; }else{ //跳转到解绑页面 xLog(@"跳转到解绑页面"); ShowWXViewController *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"ShowWXViewController"]; [self.navigationController pushViewController:vc animated:YES]; } }else if ([title isEqualToString:@"手机号码"]){ WEAKSELF if(self.editUser.phone == nil || self.editUser.phone.length == 0){ UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"请输入手机号"message:@""preferredStyle:UIAlertControllerStyleAlert]; [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { textField.keyboardType = UIKeyboardTypePhonePad; }]; [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {}]]; [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { UITextField *textField = alert.textFields[0]; if(textField.text.length != 11){ [SVProgressHUD showErrorWithStatus:@"请输入11位号码"]; return ; } [User wxUserApplyPhone:textField.text success:^(id responseObject) { xLog(@"微信账号绑定 手机号 成功"); weakSelf.editUser = nil; [weakSelf refreshData]; } failure:^(id faiObject) { }]; }]]; [self presentViewController:alert animated:YES completion:nil]; } } } #pragma mark UIImagePickerControllerDelegate - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated { if ([UIDevice currentDevice].systemVersion.floatValue < 11) { return; } if ([viewController isKindOfClass:NSClassFromString(@"PUPhotoPickerHostViewController")]) { [viewController.view.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { if (obj.frame.size.width < 42) { [viewController.view sendSubviewToBack:obj]; *stop = YES; } }]; } } - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { [picker dismissViewControllerAnimated:YES completion:^{}]; UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage]; self.editUser.image = image; [self.myTableView reloadData]; } - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { [picker dismissViewControllerAnimated:YES completion:nil]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #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