// // 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" @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]; }]; }; 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 }; //图片非必选 if (self.imagesUrl.count <= 0) { //未选择图片 设置空数组 [self uploadFeedinfo:dic imgUrlList:self.imagesUrl]; }else{ ////获取图片链接 [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) { GIGA_ShowToast(@"请选择图片"); 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. } @end