// // GiGaUserViewController.m // GIGA // // Created by lianxiang on 2018/8/13. // Copyright © 2018年 com.giga.ios. All rights reserved. // #import "GiGaUserViewController.h" #import "GIGaUserInfoAvatorCell.h" #import "GIGaUserIDViewCell.h" #import "GIGaUserWeChatViewCell.h" #import "GiGaUserSexViewCell.h" #import "GIGaUserCityViewCell.h" #import "GiGaUser.h" #import "GiGaUserManager.h" #import "LYSDatePickerController.h" #import "JYAddressPicker.h" #import "GiGaBaseAPiRequest.h" #import "GiGaUserJobViewCell.h" #import #import "GIGaUserFileHelper.h" #import "GiGaUserDefault.h" #import "GiGaNetManager.h" #import "GiGaServerConfig.h" #import "GiGaAPIResult.h" @interface GiGaUserViewController () @property (nonatomic,strong) GiGaUser *user; @property (nonatomic,strong) NSMutableArray *jobArr; @property (nonatomic) BOOL jobSpread; @end @implementation GiGaUserViewController - (NSArray *)jobArr{ if (!_jobArr) { _jobArr = [NSMutableArray new]; } return _jobArr; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. //self.title = @"个人中心"; _jobSpread = NO; [self getJobInfos]; self.user = [[GiGaUserManager shareUser] getCurrentUser]; [self addNavTitile:@"个人信息"]; UIButton *saveBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [saveBtn addTarget:self action:@selector(saveUserInfo) forControlEvents:UIControlEventTouchUpInside]; saveBtn.frame = CGRectMake(0, 0,40, 36); NSAttributedString *attrititle = [GiGaHelper stringWithText:@"保存" textColor:[UIColor whiteColor] textFont:GIGA_TEXTFONTMEDIUM(16) leterSpace:0]; [saveBtn setAttributedTitle:attrititle forState:UIControlStateNormal]; UIBarButtonItem *saveItem = [[UIBarButtonItem alloc] initWithCustomView:saveBtn]; self.navigationItem.rightBarButtonItem = saveItem; [self creatTab]; } -(void)creatTab{ [self.view addSubview:self.tableView]; self.tableView.delegate = self; self.tableView.dataSource = self; self.tableView.backgroundColor = [UIColor whiteColor]; self.tableView.separatorStyle = UITableViewCellSelectionStyleNone; self.tableView.sectionFooterHeight = 0; self.tableView.estimatedSectionFooterHeight= 10; self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 10)]; self.tableView.estimatedSectionHeaderHeight = 0; self.tableView.bounces = YES; self.tableView.frame = CGRectMake(0, 1, KMainW,self.view.bounds.size.height - SAFE_NAV_HEIGHT); [self.tableView registerClass: [GiGaUserJobViewCell class] forCellReuseIdentifier:@"GiGaUserJobViewCell"]; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 4; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ if (section == 0){ return 1; }else if (section == 1){ return 3; }else if (section == 3){ return 1; } return 4; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ if(indexPath.section ==0){ GIGaUserInfoAvatorCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GIGaUserInfoAvatorCell"]; if(!cell){ cell = [[[NSBundle mainBundle] loadNibNamed:@"GIGaUserInfoAvatorCell" owner:self options:nil] lastObject]; UITapGestureRecognizer *tapGestrue = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(userAvatorClick)]; [cell.userAvator addGestureRecognizer:tapGestrue]; } [cell loadCellData:indexPath user:self.user]; return cell; }else if (indexPath.section == 1 && indexPath.row ==0){ GIGaUserIDViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GIGaUserIDViewCell"]; if(!cell){ cell = [[[NSBundle mainBundle] loadNibNamed:@"GIGaUserIDViewCell" owner:self options:nil] lastObject]; } [cell loadCellData:indexPath user: self.user]; return cell; }else if (indexPath.section == 1 && indexPath.row <= 2){ GIGaUserWeChatViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GIGaUserWeChatViewCell"]; if(!cell){ cell = [[[NSBundle mainBundle] loadNibNamed:@"GIGaUserWeChatViewCell" owner:self options:nil] lastObject]; } [cell loadCellData:indexPath user:self.user]; return cell; }else if (indexPath.section == 2 && indexPath.row == 0){ GiGaUserSexViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GiGaUserSexViewCell"]; if(!cell){ cell = [[[NSBundle mainBundle] loadNibNamed:@"GiGaUserSexViewCell" owner:self options:nil] lastObject]; } [cell loadCellData:indexPath user:self.user]; //0 女 1 男 weakify(self); [cell userSelectSexAction:^(int sex) { weakSelf.user.sexCode = sex; }]; GILog(@"sex:%d",self.user.sexCode); return cell; }else if (indexPath.section == 2 && indexPath.row >= 1){ GIGaUserCityViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GIGaUserCityViewCell"]; if(!cell){ cell = [[[NSBundle mainBundle] loadNibNamed:@"GIGaUserCityViewCell" owner:self options:nil] lastObject]; } [cell loadCellData:indexPath user:self.user]; return cell; }else if (indexPath.section == 3){ static NSString *cellID = @"GiGaUserJobViewCell"; GiGaUserJobViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath]; if (!cell) { cell = [[GiGaUserJobViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID]; } [cell userSelectjobAction:^() { self.jobSpread = !self.jobSpread; [UIView performWithoutAnimation:^{ [tableView reloadSections:[NSIndexSet indexSetWithIndex:3] withRowAnimation:UITableViewRowAnimationNone]; }]; }]; [cell setButtnImage:self.jobSpread with:self.jobArr]; [cell userSelectjob:^(NSString *title) { self.user.occupationCode = title; [UIView performWithoutAnimation:^{ [tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:3 inSection:2]] withRowAnimation:UITableViewRowAnimationNone]; }]; }]; return cell; } return nil; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ GILog(@"sex:%d",self.user.sexCode); if (indexPath.section == 2 && indexPath.row == 1) { [LYSDatePickerController alertDatePickerWithController:self type:(LYSDatePickerTypeDay)]; [LYSDatePickerController customPickerDelegate:self]; weakify(self); [LYSDatePickerController customdidSelectDatePicker:^(NSDate *date) { NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"yyyy-MM-dd"]; NSString *currentDate = [dateFormat stringFromDate:date]; GIGaUserCityViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; weakSelf.user.birthday = currentDate; [cell loadCellData:indexPath user:weakSelf.user]; }]; }else if (indexPath.section == 2 && indexPath.row == 2){ NSMutableArray *defalutvaluses = [NSMutableArray new]; if (self.user.provinceId && ![self.user.provinceId isKindOfClass:[NSNull class]]) { [defalutvaluses addObject:self.user.provinceId]; }else{ [defalutvaluses addObject:@""]; } if (self.user.cityId && ![self.user.cityId isKindOfClass:[NSNull class]]) { [defalutvaluses addObject:self.user.cityId]; }else{ [defalutvaluses addObject:@""]; } if (self.user.areaId && ![self.user.areaId isKindOfClass:[NSNull class]]) { [defalutvaluses addObject:self.user.areaId]; }else{ [defalutvaluses addObject:@""]; } JYAddressPicker *addressPicker = [JYAddressPicker jy_showAt:self defaultShow:defalutvaluses]; //JYAddressPicker *addressPicker = [JYAddressPicker jy_showAt:self]; weakify(self); addressPicker.selectedItemBlock = ^(NSArray *addressArr) { NSString *province = [addressArr objectAtIndex:0][@"name"]; NSString *provinceID = [addressArr objectAtIndex:0][@"id"]; NSString *city = [addressArr objectAtIndex:1][@"name"]; NSString *cityID = [addressArr objectAtIndex:1][@"id"]; NSString *county = [addressArr objectAtIndex:2][@"name"]; NSString *countyID = [addressArr objectAtIndex:2][@"id"]; NSString *addres = [NSString stringWithFormat:@"%@%@%@",province,city,county]; GILog(@"%@",addres); GIGaUserCityViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; weakSelf.user.provinceName = province; weakSelf.user.provinceId = provinceID; weakSelf.user.cityName = city; weakSelf.user.cityId = cityID; weakSelf.user.areaName = county; weakSelf.user.areaId = countyID; [cell loadCellData:indexPath user:self.user]; }; } } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ if(indexPath.section==0){ return 133; }else if (indexPath.section==3){ if (self.jobArr.count <= 0) { return 0; }else{ return self.jobSpread == YES ? 246 : 85; } }else { return 44; } } - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{ return 0.1; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ if (section == 1){ return 10; }else if (section == 2){ return 4; } return 0.1; } -(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ if (section == 1 || section == 2) { UIView *view = [[UIView alloc] init]; view.backgroundColor = GIGARGB(222, 222, 222, 1); return view; } return nil; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - 保存用户信息 -(void)saveUserInfo{ GIGaUserWeChatViewCell *nickcell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:1]]; self.user.nickName = nickcell.weiXinCount.text; GIGaUserWeChatViewCell *chatcell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:1]]; self.user.weixin = chatcell.weiXinCount.text; if (!self.user.nickName || [self.user.nickName isKindOfClass:[NSNull class]]) { self.user.nickName = @""; } int leng = [GiGaHelper convertToInt: self.user.nickName]; if ( self.user.nickName.length >40 ) { GIGA_ShowToast(@"用户名1-40字符长度") return; } if (!self.user.weixin || [self.user.weixin isKindOfClass:[NSNull class]]) { self.user.weixin = @""; } if (!self.user.birthday || [self.user.birthday isKindOfClass:[NSNull class]]) { self.user.birthday = @""; } if (!self.user.occupationCode|| [self.user.occupationCode isKindOfClass:[NSNull class]]) { self.user.occupationCode = @""; } if (!self.user.provinceId|| [self.user.provinceId isKindOfClass:[NSNull class]]) { self.user.provinceId = @""; } if (!self.user.cityId|| [self.user.cityId isKindOfClass:[NSNull class]]) { self.user.cityId = @""; } if (!self.user.areaId|| [self.user.areaId isKindOfClass:[NSNull class]]) { self.user.areaId = @""; } [self.view makeToastActivity:CSToastPositionCenter]; NSDictionary *param = @{ @"provinceId":self.user.provinceId, @"cityId":self.user.cityId, @"areaId":self.user.areaId, @"nickName":self.user.nickName, @"weixin":self.user.weixin, @"sexCode":[NSString stringWithFormat:@"%d",self.user.sexCode], @"birthday":self.user.birthday, @"occupationCode":self.user.occupationCode }; GiGaBaseAPiRequest *api =[GiGaBaseAPiRequest initWithRequestPath:kFixUserInfo method:RequestPostMethod parms:param]; [api requstDataWithResult:^(GiGaAPIResult *result) { [self.view hideToastActivity]; if (result.success) { GIGA_ShowToast(@"修改成功"); [[GiGaUserManager shareUser] saveUser:self.user]; NC_POST_NAME_OBJECT(kUserNameUpDateNoti, nil); }else{ GIGA_ShowToast(result.dic[@"msg"]); if (result.code == 401) { //token失效 [GiGaBaseAPiRequest userTokenTimeOutGologinFromVC:self]; } } }]; } #pragma mark - 获取职业数据 -(void)getJobInfos{ NSDictionary *param = @{}; GiGaBaseAPiRequest *api =[GiGaBaseAPiRequest initWithRequestPath:kApiOccuption method:RequestPostMethod parms:param]; [api requstDataWithResult:^(GiGaAPIResult *result) { if (result.success) { // GILog(@"%@",result.dic); NSDictionary *dic = result.dic; NSArray *josA = dic[@"list"]; for (NSDictionary *jobDic in josA ) { [self.jobArr addObject:jobDic[@"dictLabel"]]; } [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:3] withRowAnimation:UITableViewRowAnimationNone]; }else{ NSString *msg = result.dic[@"msg"]; GIGA_ShowToast(msg); } }]; } #pragma mark - 用户头像点击 -(void)userAvatorClick{ [self showCameraAndPhotoAlert]; } -(void)showCameraAndPhotoAlert{ UIAlertController *alertSheet = [UIAlertController alertControllerWithTitle:@"选择头像" message:nil preferredStyle:UIAlertControllerStyleActionSheet]; UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init]; imagePickerController.delegate = self; imagePickerController.allowsEditing = YES; if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { //权限判断应用是否有使用相机的权限 NSString *mediaType = AVMediaTypeVideo;//读取媒体类型 AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];//读取设备授权状态 if(authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied){ NSString *errorStr = @"应用相机权限受限,请在设置中启用"; [self jxt_showAlertWithTitle:@"" message:errorStr appearanceProcess:^(JXTAlertController * _Nonnull alertMaker) { alertMaker.addActionCancelTitle(@"取消"); alertMaker.addActionDefaultTitle(@"去开启"); } actionsBlock:^(NSInteger buttonIndex, UIAlertAction * _Nonnull action, JXTAlertController * _Nonnull alertSelf) { if (buttonIndex == 1) { NSString *urlStr = @"App-Prefs:root=Photos"; if (@available(iOS 10.0, *)) { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlStr] options:@{} completionHandler:nil]; } else { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlStr]]; // Fallback on earlier versions } } }]; return; } // 设置数据源 imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera; [self presentViewController:imagePickerController animated:YES completion:nil]; }]; [alertSheet addAction:cameraAction]; } //相册 if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) { //权限 UIAlertAction *photoAction = [UIAlertAction actionWithTitle:@"从手机相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; [self presentViewController:imagePickerController animated:YES completion:nil]; }]; [alertSheet addAction:photoAction]; }else{ NSString *errorStr = @"应用相权册限受限,请在设置中启用"; [self jxt_showAlertWithTitle:@"" message:errorStr appearanceProcess:^(JXTAlertController * _Nonnull alertMaker) { alertMaker.addActionCancelTitle(@"取消"); alertMaker.addActionDefaultTitle(@"去开启"); } actionsBlock:^(NSInteger buttonIndex, UIAlertAction * _Nonnull action, JXTAlertController * _Nonnull alertSelf) { if (buttonIndex == 1) { NSURL *url = [NSURL URLWithString:@"prefs:root=Photos"]; if ([[UIApplication sharedApplication] canOpenURL:url]) { [[UIApplication sharedApplication] openURL:url]; } } }]; } UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]; [alertSheet addAction:cancelAction]; [self presentViewController:alertSheet animated:YES completion:nil]; } -(void)camreaAction{ } - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { GILog(@"info:%@",info); [picker dismissViewControllerAnimated:YES completion:nil] ; UIImage*img=[info objectForKey:UIImagePickerControllerEditedImage]; UIImage *samall = [GIGaUserFileHelper thumbnailWithImageWithoutScale:img size:CGSizeMake(300, 300)]; NSString *userid = [GiGaUserDefault getCurentUserId]; [GIGaUserFileHelper savaUserAvatorWith:userid image:samall]; GIGaUserInfoAvatorCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]]; cell.userAvator.image = samall; [self uploadImage:samall]; } - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{ [picker dismissViewControllerAnimated:YES completion:nil]; } #pragma mark upload image -(void)uploadImage:(UIImage *)image{ [self.view makeToastActivity:CSToastPositionCenter]; NSString *url = [NSString stringWithFormat:@"%@%@",[GiGaServerConfig getMainUrl],kUploadAvator]; //NSString *userID = [GiGaUserDefault getCurentUserId]; //UIImage *avator =[GIGaFileManager getUserAvatorWith:userID]; NSData*imageData=UIImageJPEGRepresentation(image,1.0f); [GiGaNetManager uploadImage:url imgData:imageData parms:@{@"file":@"file"} responseBlock:^(NSDictionary *responseDict, NSDictionary *responseHeaderFields, NSError *error) { [self.view hideToastActivity]; GiGaAPIResult *reullt = [[GiGaAPIResult alloc] initWithDictionary:responseDict]; if (error) { GILog(@"eror:%@",error.localizedDescription); return ; } if (reullt.success) { GiGaUser *user = [[GiGaUserManager shareUser] getCurrentUser]; NSString *newurl = responseDict[@"url"]; self.user.headImgUrl = newurl; if (newurl) { user.headImgUrl = newurl; } [[GiGaUserManager shareUser] saveUser:user]; NC_POST_NAME_OBJECT(kUserAvatorUpdateNoti, nil); GIGA_ShowToast(@"修改成功") }else{ GIGA_ShowToast(reullt.message); } }]; } @end