// // GiGaFeedBackViewController.m // GIGA // // Created by lianxiang on 2018/9/19. // Copyright © 2018年 com.giga.ios. All rights reserved. // #import "GiGaFeedBackViewController.h" #import "GIGaFeedQQViewCell.h" #import "GIGaFeedPhontosViewCell.h" #import "GiGaFeedContentViewCell.h" #import "GiGaFeedLeiIingViewCell.h" #import "SJPhotoPicker.h" #import "GiGaNetManager.h" #import "GiGaAPIResult.h" #import "GiGaFeedBackKindView.h" #import "GIGaUserFileHelper.h" #import "GiGaServerConfig.h" #import @interface GiGaFeedBackViewController () @property(nonatomic,strong) NSMutableArray *imagesUrl; @property(nonatomic,copy) NSString *kinCode; @property(nonatomic,strong) UIButton *tijiaoBtn; @end @implementation GiGaFeedBackViewController - (void)viewDidLoad { [super viewDidLoad]; [self addNavTitile:@"意见反馈"]; _imagesUrl = [NSMutableArray new]; self.kinCode = @"1"; [self.view addSubview:self.tableView]; self.tableView.delegate = self; self.tableView.dataSource = self; self.tableView.backgroundColor = GIGA_MAIN_BGCOLOR; self.tableView.separatorStyle = UITableViewCellSelectionStyleNone; self.tableView.sectionFooterHeight = 0; self.tableView.estimatedSectionFooterHeight= 0; self.tableView.estimatedSectionHeaderHeight = 0; self.tableView.frame = CGRectMake(0, 1, KMainW, self.view.bounds.size.height - SAFE_NAV_HEIGHT); [self.tableView registerClass:[GIGaFeedPhontosViewCell class] forCellReuseIdentifier:@"GIGaFeedPhontosViewCell"]; // Do any additional setup after loading the view. UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 80)]; UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.layer.masksToBounds = YES; btn.layer.cornerRadius = 20; btn.frame = CGRectMake((self.view.frame.size.width - 245) /2 , 20, 245,40); btn.backgroundColor = [UIColor whiteColor]; NSAttributedString *atti = [GiGaHelper stringWithText:@"提交" textColor:GIGA_MAIN_BGCOLOR textFont:GIGA_TEXTFONTBOLD(18) leterSpace:0]; [btn addTarget:self action:@selector(tijiaoAction:) forControlEvents:UIControlEventTouchUpInside]; [btn setAttributedTitle:atti forState:UIControlStateNormal]; [footer addSubview:btn]; self.tijiaoBtn = btn; self.tableView.tableFooterView = footer; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 4; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return 1; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ if (indexPath.section == 0) { GiGaFeedLeiIingViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GiGaFeedLeiIingViewCell"]; if (!cell) { cell = [[[NSBundle mainBundle] loadNibNamed:@"GiGaFeedLeiIingViewCell" owner:self options:nil] lastObject]; } return cell; }else if (indexPath.section == 1){ GiGaFeedContentViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GiGaFeedContentViewCell"]; if (!cell) { cell = [[[NSBundle mainBundle] loadNibNamed:@"GiGaFeedContentViewCell" owner:self options:nil] lastObject]; } return cell; }else if (indexPath.section == 2){ GIGaFeedQQViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GIGaFeedQQViewCell"]; if (!cell) { cell = [[[NSBundle mainBundle] loadNibNamed:@"GIGaFeedQQViewCell" owner:self options:nil] lastObject]; } return cell; }else if (indexPath.section == 3){ GIGaFeedPhontosViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GIGaFeedPhontosViewCell"]; if (!cell) { cell = [[GIGaFeedPhontosViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"GIGaFeedPhontosViewCell"];; } __weak typeof(cell) weakCell = cell; cell.pickerBalock = ^{ // [[SJPhotoPicker shareSJPhotoPicker] showPhotoPickerToController:self pickedAssets:^(NSArray *assets) { // // if ((weakCell.images.count + assets.count) > 6) { // GIGA_ShowToast(@"最多六张"); // return ; // } // [weakCell.images addObjectsFromArray:assets]; // [weakCell.collectionView reloadData]; // // // }]; [self showAcitonSheet]; }; return cell; } return nil; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ if (indexPath.section == 0) { GiGaFeedBackKindView *piker = [GiGaFeedBackKindView showAt:self atRow:0]; piker.selectedItemBlock = ^(GIGaFeedKind *kind) { GiGaFeedLeiIingViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; cell.kidLabel.text = kind.dictLabel; self.kinCode = kind.dictCode; }; } } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ if (indexPath.section == 0) { return 40; }else if (indexPath.section == 1){ return 142; }else if (indexPath.section == 2){ return 40; }else if (indexPath.section == 3){ return 218; } return 0; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ if (section == 3) { return 36; }else{ return 12; } } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ if (section == 3) { UIView *view = [[UIView alloc] init]; UILabel *label = [[UILabel alloc] init]; label.frame = CGRectMake(15,0, 200, 36); label.font = GIGA_TEXTFONTMEDIUM(13); label.text= @"上传图片(最多6张)"; label.textColor = [UIColor whiteColor]; [view addSubview:label]; return view; } UIView *view = [[UIView alloc] init]; return view; } - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{ return 0.1; } #pragma mark 提交 -(void)tijiaoAction:(UIButton *)btn{ GiGaFeedLeiIingViewCell *kindcell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]]; if ([kindcell.kidLabel.text isEqualToString:@"选择反馈类型"]) { GIGA_ShowToast(@"请选择评论类型"); return; } GiGaFeedContentViewCell *contencell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]]; if (contencell.contentFiled.text.length==0) { GIGA_ShowToast(@"请输入评论"); return; } GIGaFeedQQViewCell *qqcell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:3]]; if (qqcell.QTextFiled.text.length == 0) { qqcell.QTextFiled.text = @""; } NSDictionary *dic =nil; NSString *qqcom = qqcell.QTextFiled.text; if (!qqcom) { qqcom = @"contact"; } dic = @{@"type":self.kinCode,@"content":contencell.contentFiled.text,@"contact":qqcom }; [self uploadImags:dic]; } //先上传图片获取到 imageurls -(void)uploadImags:(NSDictionary *)params{ [self.imagesUrl removeAllObjects]; GIGaFeedPhontosViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:3]]; NSArray *names =cell.imageNames; if (names.count <= 0) { //图片非必须传 [self uploadFeedinfo:params imgUrlList:self.imagesUrl]; GILog(@"未选择图片"); return; } NSLog(@"names:%@",names); [self.view makeToastActivity:CSToastPositionCenter]; //.串行队列同步执行:任务都在当前线程执行(同步),并且顺序执行(串行) //依次上传一次一张 dispatch_queue_t queue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL); for (NSUInteger i= 0; i < names.count; i ++) { dispatch_sync(queue, ^{ NSLog(@"上船图片%ld **** %@",i,names[i]); UIImage *image =[GIGaUserFileHelper getUserFeedbackImag:names[i]] ; if (!image) { GILog(@"获取图片:************%@ nil",names[i]); return; } NSData*imageData=UIImageJPEGRepresentation(image,1.0f); [self uploadImage:imageData name:names[i] info:params totalCount:names.count]; }); } } // 单张上传 -(void)uploadImage:(NSData *)imageData name:(NSString *)name info:(NSDictionary *)info totalCount:(NSInteger)count{ [self.view makeToastActivity:CSToastPositionCenter]; self.tijiaoBtn.userInteractionEnabled = NO; NSString *url = [NSString stringWithFormat:@"%@%@",[GiGaServerConfig getMainUrl],kUploadFeed]; [GiGaNetManager uploadImage:url imgData:imageData parms:@{@"file":@"file"} responseBlock:^(NSDictionary *responseDict, NSDictionary *responseHeaderFields, NSError *error) { [self.view hideToastActivity]; GiGaAPIResult *result = [[GiGaAPIResult alloc] initWithDictionary:responseDict]; if (result.success) { NSString *url = responseDict[@"url"]; if (url) { NSDictionary *dic = [NSDictionary dictionaryWithObject:url forKey:@"imgUrl"]; [self.imagesUrl addObject:dic]; NSLog(@"上船图片%@成功",name); NSLog(@"当前获取图片数组:%@",self.imagesUrl); if (self.imagesUrl.count == count) { [self uploadFeedinfo:info imgUrlList:self.imagesUrl]; } } }else if (result.code == 401){ self.tijiaoBtn.userInteractionEnabled = YES; GIGA_WIndowTost(@"登录超时,请重新登录"); [GiGaBaseAPiRequest userTokenTimeOutGologinFromVC:self]; }else{ GIGA_ShowToast(result.message); self.tijiaoBtn.userInteractionEnabled = YES; } }]; } //body 传参数 -(void)uploadFeedinfo:(NSDictionary*)params imgUrlList:(NSArray *)urlArr{ NSMutableDictionary *finallyparam = [NSMutableDictionary dictionaryWithDictionary:params]; [finallyparam setObject:urlArr forKey:@"imgUrlList"]; NSLog(@"finallyparam%@",finallyparam); [self.view makeToastActivity:CSToastPositionCenter]; NSString *url = [NSString stringWithFormat:@"%@%@",[GiGaServerConfig getMainUrl],kAPiUpLoadFeeeds]; [GiGaNetManager userbodyRequest:url params:finallyparam completionHandler:^(NSURLResponse *response, NSDictionary *resDic, NSError * _Nullable error) { self.tijiaoBtn.userInteractionEnabled = YES; [self.view hideToastActivity]; if (error) { GILog(@"err:%@",error.localizedDescription); }else{ GiGaAPIResult *result = [[GiGaAPIResult alloc] initWithDictionary:resDic]; if (result.success) { GIGA_WIndowTost(@"感谢提出宝贵意见!"); [self.navigationController popViewControllerAnimated:YES]; }else if (result.code == 401){ GIGA_WIndowTost(@"登录超时,请重新登录"); [GiGaBaseAPiRequest userTokenTimeOutGologinFromVC:self]; }else{ GIGA_ShowToast(result.message); } } }]; } //多张 -(void)mutipuleUpload{ // [GiGaNetManager uploadImage:kUploadFeed imageNames:names parms:@{@"file":@"file"} responseBlock:^(NSDictionary *responseDict, NSDictionary *responseHeaderFields, NSError *error) { // [self.view hideToastActivity]; // if (error) { // GILog(@"err:%@",error.localizedDescription); // GIGA_ShowToast(@"error"); // // }else{ // GiGaAPIResult *result = [[GiGaAPIResult alloc] initWithDictionary:responseDict]; // GIGA_ShowToast(result.message); // if (result.success) { // // GIGA_ShowToast(result.message); // [self uploadFeedinfo:params imgUrlList:@[]]; // // }else if (result.code == 401){ // GIGA_WIndowTost(@"登录超时,请重新登录"); // [GiGaBaseAPiRequest userTokenTimeOutGologinFromVC:self]; // // }else{ // GIGA_ShowToast(result.message); // } // } // }]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(void)showAcitonSheet{ [self jxt_showActionSheetWithTitle:@"选择图片" message:@"" appearanceProcess:^(JXTAlertController * _Nonnull alertMaker) { alertMaker.addActionDefaultTitle(@"拍照"); alertMaker.addActionDefaultTitle(@"相册"); alertMaker.addActionCancelTitle(@"取消"); } actionsBlock:^(NSInteger buttonIndex, UIAlertAction * _Nonnull action, JXTAlertController * _Nonnull alertSelf) { if (buttonIndex == 0) { [self digPhotoAuth]; }else if (buttonIndex == 1){ [self fromAlbums]; } }]; } -(void)fromAlbums{ GIGaFeedPhontosViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:3]]; [[SJPhotoPicker shareSJPhotoPicker] showPhotoPickerToController:self pickedAssets:^(NSArray *assets) { if (assets.count == 0) { return ; } for (NSInteger i= 0; i < assets.count; i++) { [cell.images addObject:assets[i]]; if ((cell.images.count) == 6) { GIGA_ShowToast(@"最多六张"); break; } } [cell.collectionView reloadData]; }]; } -(void)satartCamera{ GIGaFeedPhontosViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:3]]; if ((cell.images.count + 1) > 6) { GIGA_ShowToast(@"最多六张"); return ; } UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init]; imagePickerController.delegate = self; imagePickerController.allowsEditing = YES; if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { //权限判断应用是否有使用相机的权限 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]; } } -(void)digPhotoAuth{ [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status){ switch (status) { case PHAuthorizationStatusNotDetermined: { NSLog(@"用户还没有做出选择"); break; } case PHAuthorizationStatusAuthorized: { NSLog(@"用户允许当前应用访问相册"); // 拍照后本app需保存到相册 [self satartCamera]; break; } case PHAuthorizationStatusDenied: { //NSLog(@"用户拒绝当前应用访问相册,我们需要提醒用户打开访问开关"); GIGA_ShowToast(@"拒绝当前应用访问相册,请在系统设置中允许访问"); break; } case PHAuthorizationStatusRestricted: { NSLog(@"家长控制,不允许访问"); break; } default: { NSLog(@"default"); break; } } }]; } - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{ [picker dismissViewControllerAnimated:YES completion:nil]; } - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { [picker dismissViewControllerAnimated:YES completion:nil]; GIGaFeedPhontosViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:3]]; UIImage*imge=[info objectForKey:UIImagePickerControllerEditedImage]; // 保存相片到相机胶卷 NSError *error = nil; //__block PHObjectPlaceholder *createdAsset = nil; NSMutableArray *imageIds = [NSMutableArray array]; [[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{ PHAssetChangeRequest *req = [PHAssetChangeRequest creationRequestForAssetFromImage:imge]; [imageIds addObject:req.placeholderForCreatedAsset.localIdentifier]; } error:&error]; if (error) { NSLog(@"%@",error.localizedDescription); }else{ __block PHAsset *imageAsset = nil; PHFetchResult *result = [PHAsset fetchAssetsWithLocalIdentifiers:imageIds options:nil]; [result enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { imageAsset = obj; *stop = YES; }]; if (imageAsset) { [cell.images addObject:imageAsset]; [cell.collectionView reloadData]; } } } #pragma mark - < 获取相册里的所有图片的PHAsset对象 > - (NSArray *)getAllPhotosAssetInAblumCollection:(PHAssetCollection *)assetCollection ascending:(BOOL)ascending { // 存放所有图片对象 NSMutableArray *assets = [NSMutableArray array]; // 是否按创建时间排序 PHFetchOptions *option = [[PHFetchOptions alloc] init]; option.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:ascending]]; option.predicate = [NSPredicate predicateWithFormat:@"mediaType == %ld", PHAssetMediaTypeImage]; // 获取所有图片对象 PHFetchResult *result = [PHAsset fetchAssetsInAssetCollection:assetCollection options:option]; // 遍历 [result enumerateObjectsUsingBlock:^(PHAsset *asset, NSUInteger idx, BOOL * _Nonnull stop) { [assets addObject:asset]; }]; return assets; } @end