AppendEvaluateVC.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. //
  2. // AppendEvaluateVC.m
  3. // yunbaolive
  4. //
  5. // Created by ybRRR on 2020/3/16.
  6. // Copyright © 2020 cat. All rights reserved.
  7. //
  8. #import "AppendEvaluateVC.h"
  9. #import "TZImagePickerController.h"
  10. #import <Qiniu/QiniuSDK.h>
  11. #import "YBStorageObj.h"
  12. @interface AppendEvaluateVC ()<TZImagePickerControllerDelegate>
  13. {
  14. UIScrollView *_backScroll;
  15. MyTextView *evaluateText;
  16. UIView *_photoView;
  17. CGFloat imgWidth;
  18. NSMutableArray *imageArray;
  19. UIButton *imageBtn;
  20. UIButton *videoBtn;
  21. UIButton *subVideoBtn;
  22. NSString *videoPath;
  23. UIImage *videoCoverImage;
  24. NSString *videoCorverStr;
  25. NSString *videoHerfStr;
  26. NSString *picThumbsStr;
  27. NSMutableArray *imageUrlArray;
  28. }
  29. @end
  30. @implementation AppendEvaluateVC
  31. - (void)viewDidLoad {
  32. [super viewDidLoad];
  33. self.view.backgroundColor = Normal_BackColor;
  34. self.titleL.text = YZMsg(@"发表追评");
  35. self.rightBtn.hidden = NO;
  36. [self.rightBtn setTitle:YZMsg(@"发布") forState:0];
  37. [self.rightBtn setTitleColor:Pink_Cor forState:0];
  38. imgWidth = (_window_width - 75)/4;
  39. imageArray = [NSMutableArray array];
  40. picThumbsStr = @"";
  41. videoHerfStr = @"";
  42. videoCorverStr =@"";
  43. videoPath = @"";
  44. [self createUI];
  45. }
  46. -(void)createUI{
  47. _backScroll = [[UIScrollView alloc]init];
  48. _backScroll.frame = CGRectMake(0, 64+statusbarHeight, _window_width, _window_height);
  49. _backScroll.backgroundColor = Normal_SubColor;
  50. [self.view addSubview:_backScroll];
  51. UIImageView *headImg = [[UIImageView alloc]init];
  52. headImg.frame = CGRectMake(15, 10, 60, 60);
  53. headImg.layer.cornerRadius = 5;
  54. headImg.layer.masksToBounds = YES;
  55. headImg.contentMode = UIViewContentModeScaleAspectFill;
  56. [headImg sd_setImageWithURL:[NSURL URLWithString:self.model.spec_thumb]];
  57. [_backScroll addSubview:headImg];
  58. UILabel *goodsname = [[UILabel alloc]init];
  59. goodsname.frame = CGRectMake(headImg.right+10, headImg.top+10, 200, 20);
  60. goodsname.font = [UIFont systemFontOfSize:14];
  61. goodsname.textColor = [UIColor whiteColor];
  62. goodsname.text = self.model.goods_name;
  63. [_backScroll addSubview:goodsname];
  64. UILabel *specname = [[UILabel alloc]init];
  65. specname.frame = CGRectMake(headImg.right+10, goodsname.bottom+5, 200, 20);
  66. specname.font = [UIFont systemFontOfSize:13];
  67. specname.textColor = Normal_TextColor;
  68. specname.text =[NSString stringWithFormat:@"%@ x%@",self.model.spec_name,self.model.nums];
  69. [_backScroll addSubview:specname];
  70. [PublicObj lineViewWithFrame:CGRectMake(0, headImg.bottom+15, _window_width, 1) andColor:Line_Cor andView:_backScroll];
  71. evaluateText = [[MyTextView alloc]init];
  72. evaluateText.frame = CGRectMake(10, headImg.bottom+30, _window_width-20, 125);
  73. evaluateText.placeholderColor = Normal_TextColor;
  74. evaluateText.placeholder =YZMsg(@"已经用了一段时间了,有更多宝贝使用心得?分享给想买的他们吧");
  75. evaluateText.font = [UIFont systemFontOfSize:14];
  76. evaluateText.backgroundColor = Normal_SubColor;
  77. evaluateText.textColor = [UIColor whiteColor];
  78. [_backScroll addSubview:evaluateText];
  79. _photoView = [[UIView alloc]init];
  80. _photoView.frame = CGRectMake(0, evaluateText.bottom, _window_width, imgWidth+10);
  81. [_backScroll addSubview:_photoView];
  82. NSArray *arr = @[@"添加图片",@"添加视频"];
  83. NSArray *titlearr = @[YZMsg(@"添加图片"),YZMsg(@"添加视频")];
  84. imageBtn = [UIButton buttonWithType:0];
  85. imageBtn.frame = CGRectMake(15,0, imgWidth, imgWidth);
  86. CAShapeLayer *border = [CAShapeLayer layer];
  87. //虚线的颜色
  88. border.strokeColor = Normal_TextColor.CGColor;
  89. //填充的颜色
  90. border.fillColor = [UIColor clearColor].CGColor;
  91. //设置路径
  92. border.path = [UIBezierPath bezierPathWithRect:imageBtn.bounds].CGPath;
  93. border.frame = imageBtn.bounds;
  94. //虚线的宽度
  95. border.lineWidth = 1.f;
  96. //虚线的间隔
  97. border.lineDashPattern = @[@4, @2];
  98. [imageBtn.layer addSublayer:border];
  99. [imageBtn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"evaluate%@",arr[0]]] forState:0];
  100. [imageBtn setTitle:titlearr[0] forState:0];
  101. [imageBtn setTitleColor:Normal_TextColor forState:0];
  102. imageBtn.titleLabel.font = [UIFont systemFontOfSize:10];
  103. imageBtn = [PublicObj setUpImgDownText:imageBtn];
  104. imageBtn.tag = 1000;
  105. [imageBtn addTarget:self action:@selector(imageBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  106. [_photoView addSubview:imageBtn];
  107. videoBtn = [UIButton buttonWithType:0];
  108. videoBtn.frame = CGRectMake(15+(1%4)*(imgWidth + 15),0, imgWidth, imgWidth);
  109. CAShapeLayer *border2 = [CAShapeLayer layer];
  110. //虚线的颜色
  111. border2.strokeColor = Normal_TextColor.CGColor;
  112. //填充的颜色
  113. border2.fillColor = [UIColor clearColor].CGColor;
  114. //设置路径
  115. border2.path = [UIBezierPath bezierPathWithRect:videoBtn.bounds].CGPath;
  116. border2.frame = videoBtn.bounds;
  117. //虚线的宽度
  118. border2.lineWidth = 1.f;
  119. //虚线的间隔
  120. border2.lineDashPattern = @[@4, @2];
  121. [videoBtn.layer addSublayer:border2];
  122. [videoBtn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"evaluate%@",arr[1]]] forState:0];
  123. [videoBtn setTitle:titlearr[1] forState:0];
  124. [videoBtn setTitleColor:Normal_TextColor forState:0];
  125. videoBtn.titleLabel.font = [UIFont systemFontOfSize:10];
  126. videoBtn = [PublicObj setUpImgDownText:videoBtn];
  127. videoBtn.tag = 1001;
  128. [videoBtn addTarget:self action:@selector(imageBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  129. [_photoView addSubview:videoBtn];
  130. _backScroll.size = CGSizeMake(_window_width, _photoView.bottom+10);
  131. }
  132. -(void)imageBtnClick:(UIButton *)sender
  133. {
  134. if (sender.tag == 1000) {
  135. TZImagePickerController *imagePC = [[TZImagePickerController alloc]initWithMaxImagesCount:5-imageArray.count delegate:self];
  136. imagePC.preferredLanguage = [lagType isEqual:ZH_CN] ? @"zh-Hans":@"en";
  137. imagePC.modalPresentationStyle = 0;
  138. imagePC.showSelectBtn = YES;
  139. imagePC.allowCrop = NO;
  140. imagePC.allowPickingOriginalPhoto = NO;
  141. imagePC.oKButtonTitleColorNormal = Pink_Cor;
  142. imagePC.allowTakePicture = YES;
  143. imagePC.allowTakeVideo = NO;
  144. imagePC.allowPickingVideo = NO;
  145. imagePC.allowPickingMultipleVideo = NO;
  146. // imagePC.takePictureImageName = @"ybtakePicture80";
  147. [[[YBBaseAppDelegate sharedAppDelegate] topViewController]presentViewController:imagePC animated:YES completion:nil];
  148. }else if (sender.tag == 1001){
  149. TZImagePickerController *imagePC = [[TZImagePickerController alloc]initWithMaxImagesCount:1 delegate:self];
  150. imagePC.preferredLanguage = [lagType isEqual:ZH_CN] ? @"zh-Hans":@"en";
  151. imagePC.modalPresentationStyle = 0;
  152. imagePC.showSelectBtn = NO;
  153. imagePC.allowCrop = NO;
  154. imagePC.allowPickingOriginalPhoto = NO;
  155. imagePC.oKButtonTitleColorNormal = Pink_Cor;
  156. imagePC.allowPickingImage = NO;
  157. imagePC.allowTakePicture = NO;
  158. imagePC.allowTakeVideo = YES;
  159. imagePC.allowPickingVideo = YES;
  160. imagePC.allowPickingMultipleVideo = NO;
  161. imagePC.videoMaximumDuration = live_MAX_RECORD_TIME;//MAX_RECORD_TIME;
  162. // imagePC.takePictureImageName = @"ybtakevideo80";
  163. [[[YBBaseAppDelegate sharedAppDelegate] topViewController]presentViewController:imagePC animated:YES completion:nil];
  164. }
  165. }
  166. #pragma mark - TZImagePickerControllerDelegate
  167. - (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingVideo:(UIImage *)coverImage sourceAssets:(PHAsset *)asset {
  168. NSLog(@"-dsddddddddd--%@\n===%@",asset,coverImage);
  169. [MBProgressHUD showMessage:@""];
  170. [[TZImageManager manager] getVideoOutputPathWithAsset:asset presetName:AVAssetExportPresetMediumQuality success:^(NSString *outputPath) {
  171. NSLog(@"视频导出到本地完成,沙盒路径为:%@",outputPath);
  172. if (outputPath) {
  173. [MBProgressHUD hideHUD];
  174. videoPath = outputPath;
  175. videoCoverImage = coverImage;
  176. [self addVideoimage];
  177. }else{
  178. [MBProgressHUD hideHUD];
  179. [MBProgressHUD showError:YZMsg(@"请重新选择(iCloud视频请先在本地相册下载后上传)")];
  180. }
  181. } failure:^(NSString *errorMessage, NSError *error) {
  182. [MBProgressHUD hideHUD];
  183. [MBProgressHUD showError:errorMessage];
  184. NSLog(@"视频导出失败:%@,error:%@",errorMessage, error);
  185. }];
  186. }
  187. - (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray<UIImage *> *)photos sourceAssets:(NSArray *)assets isSelectOriginalPhoto:(BOOL)isSelectOriginalPhoto{
  188. NSLog(@"------多选择图片--:%@",photos);
  189. [imageArray addObjectsFromArray:photos];
  190. [self setImageFrame];
  191. }
  192. -(void)setImageFrame{
  193. for (UIButton *btn in _photoView.subviews) {
  194. if (btn.tag >=3000) {
  195. [btn removeFromSuperview];
  196. }
  197. }
  198. if (imageArray.count > 0) {
  199. if (imageArray.count < 3) {
  200. _photoView.size= CGSizeMake(_window_width, imgWidth+10);
  201. }else if (imageArray.count>=3){
  202. _photoView.size= CGSizeMake(_window_width, imgWidth*2+20);
  203. }
  204. for (int i = 0; i < imageArray.count ; i ++) {
  205. UIButton *btn = [UIButton buttonWithType:0];
  206. btn.backgroundColor = RGB(250, 250, 250);
  207. if (i < 4) {
  208. btn.frame = CGRectMake(15+(i%4)*(imgWidth + 15), 0, imgWidth, imgWidth);
  209. }else{
  210. btn.frame = CGRectMake(15+(i%4)*(imgWidth + 15), (imgWidth+10), imgWidth, imgWidth);
  211. }
  212. btn.tag = 3000+i;
  213. [btn setImage:imageArray[i] forState:0];
  214. [_photoView addSubview:btn];
  215. UIButton *delBtn = [UIButton buttonWithType:0];
  216. delBtn.frame = CGRectMake(imgWidth-25, 5, 20, 20);
  217. [delBtn setBackgroundImage:[UIImage imageNamed:@"comm_del"] forState:0];
  218. delBtn.tag = 4000+i;
  219. [delBtn addTarget:self action:@selector(delImageClick:) forControlEvents:UIControlEventTouchUpInside];
  220. [btn addSubview:delBtn];
  221. }
  222. if (imageArray.count < 3) {
  223. imageBtn.frame = CGRectMake(15+(imageArray.count%4)*(imgWidth + 15), 0, imgWidth, imgWidth);
  224. videoBtn.frame = CGRectMake(15+((imageArray.count+1)%4)*(imgWidth + 15), 0, imgWidth, imgWidth);
  225. }else if (imageArray.count == 3){
  226. imageBtn.frame = CGRectMake(15+(imageArray.count%4)*(imgWidth + 15), 0, imgWidth, imgWidth);
  227. videoBtn.frame = CGRectMake(15+((imageArray.count+1)%4)*(imgWidth + 15), (imgWidth+10), imgWidth, imgWidth);
  228. }else if (imageArray.count > 3){
  229. imageBtn.frame = CGRectMake(15+(imageArray.count%4)*(imgWidth + 15), (imgWidth+10), imgWidth, imgWidth);
  230. videoBtn.frame = CGRectMake(15+((imageArray.count+1)%4)*(imgWidth + 15), (imgWidth+10), imgWidth, imgWidth);
  231. }
  232. }else{
  233. imageBtn.frame = CGRectMake(15,0, imgWidth, imgWidth);
  234. videoBtn.frame = CGRectMake(15+(1%4)*(imgWidth + 15),0, imgWidth, imgWidth);
  235. }
  236. if (imageArray.count == 5) {
  237. imageBtn.hidden = YES;
  238. videoBtn.frame = CGRectMake(15+(imageArray.count%4)*(imgWidth + 15), (imgWidth+10), imgWidth, imgWidth);
  239. }else{
  240. imageBtn.hidden = NO;
  241. }
  242. _backScroll.size = CGSizeMake(_window_width, _photoView.bottom+10);
  243. }
  244. -(void)delImageClick:(UIButton *)sender{
  245. NSInteger index = sender.tag-4000;
  246. [imageArray removeObjectAtIndex:index];
  247. [self setImageFrame];
  248. }
  249. -(void)addVideoimage{
  250. subVideoBtn = [UIButton buttonWithType:0];
  251. subVideoBtn.frame = CGRectMake(0, 0, videoBtn.width, videoBtn.height);
  252. [subVideoBtn setBackgroundImage:videoCoverImage forState:0];
  253. [videoBtn addSubview:subVideoBtn];
  254. UIButton *delBtn = [UIButton buttonWithType:0];
  255. delBtn.frame = CGRectMake(subVideoBtn.width-25, 5, 20, 20);
  256. [delBtn setBackgroundImage:[UIImage imageNamed:@"comm_del"] forState:0];
  257. [delBtn addTarget:self action:@selector(delVideoClick) forControlEvents:UIControlEventTouchUpInside];
  258. [subVideoBtn addSubview:delBtn];
  259. }
  260. -(void)delVideoClick{
  261. videoPath = @"";
  262. [subVideoBtn removeFromSuperview];
  263. subVideoBtn = nil;
  264. }
  265. -(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
  266. {
  267. [self.view endEditing:YES];
  268. }
  269. -(void)clickNaviRightBtn{
  270. [MBProgressHUD showMessage:@""];
  271. NSLog(@"-------image:%@ \n===---video:%@",imageArray,videoPath);
  272. if (videoPath.length > 0 || imageArray.count > 0) {
  273. YBWeakSelf;
  274. [[YBStorageObj storageManeger]getCOSType:^(int code) {
  275. dispatch_async(dispatch_get_main_queue(), ^{
  276. if (code == 0) {
  277. [weakSelf startUpload];
  278. }
  279. });
  280. }];
  281. }else{
  282. [self doUploadEvaluate];
  283. }
  284. }
  285. -(void)startUpload{
  286. YBWeakSelf;
  287. dispatch_group_t group = dispatch_group_create();
  288. dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
  289. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  290. //如果有视频或图片先上传视频
  291. if (videoPath.length > 0) {
  292. dispatch_group_async(group, queue, ^{
  293. NSData *imageData = UIImagePNGRepresentation(videoCoverImage);
  294. if (!imageData) {
  295. [MBProgressHUD hideHUD];
  296. [MBProgressHUD showError:YZMsg(@"请重新选择")];
  297. return;
  298. }
  299. UIImage *herfImg = [UIImage imageWithData:imageData];
  300. NSString *imageName = [PublicObj getNameBaseCurrentTime:@"_action_video_cover.png"];
  301. [[YBStorageObj storageManeger]yb_storageImg:herfImg andName:imageName progress:^(CGFloat percent) {
  302. }complete:^(int code, NSString *key) {
  303. //图片成功
  304. videoCorverStr = key;
  305. dispatch_semaphore_signal(semaphore);
  306. }];
  307. dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
  308. });
  309. dispatch_group_async(group, queue, ^{
  310. //传视频
  311. NSString *videoName = [PublicObj getNameBaseCurrentTime:@"_action_video.mp4"];
  312. [[YBStorageObj storageManeger]yb_storageVideoOrVoice:videoPath andName:videoName progress:^(CGFloat percent) {
  313. } complete:^(int code, NSString *key) {
  314. videoHerfStr = key;
  315. dispatch_semaphore_signal(semaphore);
  316. }];
  317. dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
  318. });
  319. }
  320. if (imageArray.count > 0) {
  321. imageUrlArray = [NSMutableArray array];
  322. picThumbsStr = @"";
  323. for (int i = 0; i < imageArray.count ; i ++) {
  324. UIImage *image =imageArray[i];
  325. NSData *imageData = UIImagePNGRepresentation(image);
  326. if (!imageData) {
  327. [MBProgressHUD hideHUD];
  328. [MBProgressHUD showError:YZMsg(@"图片错误")];
  329. return;
  330. }
  331. dispatch_group_async(group, queue, ^{
  332. NSString *imageName = [PublicObj getNameBaseCurrentTime:[NSString stringWithFormat:@"_action_image%d_cover.png", i]];
  333. [[YBStorageObj storageManeger]yb_storageImg:image andName:imageName progress:^(CGFloat percent) {
  334. }complete:^(int code, NSString *key) {
  335. [imageUrlArray addObject:key];
  336. dispatch_semaphore_signal(semaphore);
  337. }];
  338. dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
  339. });
  340. }
  341. }
  342. dispatch_group_notify(group, queue, ^{
  343. dispatch_async(dispatch_get_main_queue(), ^{
  344. for (NSString *str in imageUrlArray) {
  345. if (picThumbsStr.length == 0) {
  346. picThumbsStr = str;
  347. }else{
  348. picThumbsStr = [NSString stringWithFormat:@"%@,%@",picThumbsStr,str];
  349. }
  350. }
  351. [weakSelf doUploadEvaluate];
  352. });
  353. NSLog(@"任务完成执行");
  354. });
  355. }
  356. #pragma mark-----正式发布-----------
  357. -(void)doUploadEvaluate{
  358. NSString *url = [purl stringByAppendingFormat:@"?service=Buyer.appendEvaluateGoodsOrder"];
  359. NSDictionary *signdic = @{@"uid":[Config getOwnID],@"token":[Config getOwnToken],@"orderid":self.model.idStr,@"time":[NSNumber numberWithLong: (long)[[NSDate date] timeIntervalSince1970]]};
  360. NSString *sign = [PublicObj sortString:signdic];
  361. NSDictionary *dic = @{
  362. @"uid":[Config getOwnID],
  363. @"token":[Config getOwnToken],
  364. @"orderid":self.model.idStr,
  365. @"content":evaluateText.text,
  366. @"thumbs":picThumbsStr,
  367. @"video_url":videoHerfStr,
  368. @"video_thumb":videoCorverStr,
  369. @"time":[NSNumber numberWithLong: (long)[[NSDate date] timeIntervalSince1970]],
  370. @"sign":sign
  371. };
  372. [YBNetworking postWithUrl:@"Buyer.appendEvaluateGoodsOrder" Dic:dic Suc:^(int code, id info, NSString *msg) {
  373. [MBProgressHUD hideHUD];
  374. if (code ==0) {
  375. [MBProgressHUD showError: msg];
  376. [[YBBaseAppDelegate sharedAppDelegate]popViewController:YES];
  377. }else{
  378. [MBProgressHUD showError:msg];
  379. }
  380. } Fail:^(id fail) {
  381. }];
  382. }
  383. @end