KSChatComplainController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. //
  2. // KSChatComplainController.m
  3. // TeacherDaya
  4. //
  5. // Created by Kyle on 2021/5/12.
  6. // Copyright © 2021 DayaMusic. All rights reserved.
  7. //
  8. #import "KSChatComplainController.h"
  9. #import "ChatComplainBodyView.h"
  10. #import "LLPhotoBrowser.h"
  11. #import "KSImageButton.h"
  12. #import "KSMediaManager.h"
  13. #import "UIImage+ResizeImage.h"
  14. #import "MSSBrowseDefine.h"
  15. #define MAXBIGPHOTONUMBER (3)
  16. #define MAXPHOTONUMBER (3)
  17. #define COLUMNNUMBER (3)
  18. #define KBUTTON_LEFTSPACE (22)
  19. #define DELETEIMAGENAME @"fb_icon_tpsc"
  20. @interface KSChatComplainController ()<LLPhotoBrowserDelegate>
  21. @property (nonatomic, strong) KSMediaManager *mediaManager;
  22. @property (nonatomic, strong) ChatComplainBodyView *bodyView;
  23. @property (nonatomic, strong) NSMutableArray *imageArray; // 图片数组
  24. @property (nonatomic, strong) NSMutableArray *imageAsset; // 图片 asset
  25. @end
  26. @implementation KSChatComplainController
  27. - (void)viewDidLoad {
  28. [super viewDidLoad];
  29. // Do any additional setup after loading the view.
  30. [self allocTitle:@"投诉"];
  31. [self configUI];
  32. }
  33. - (void)configUI {
  34. _bodyView = [ChatComplainBodyView shareInstance];
  35. _bodyView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - kNaviBarHeight - iPhoneXSafeBottomMargin);
  36. MJWeakSelf;
  37. [_bodyView submitAction:^(NSString *content) {
  38. [weakSelf complainAction:content];
  39. }];
  40. [self.view addSubview:_bodyView];
  41. [self createImageButtonAndPhotosButton];
  42. }
  43. - (void)complainAction:(NSString *)content {
  44. // 先判断
  45. if (self.imageArray.count == 0) {
  46. [self MBPShow:@"请选择要上传的照片"];
  47. return;
  48. }
  49. [self showhud];
  50. NSMutableArray *imageDataArray = [NSMutableArray array];
  51. for (UIImage *image in self.imageArray) {
  52. NSData *imgData = [UIImage turnsImaegDataByImage:image];
  53. [imageDataArray addObject:imgData];
  54. }
  55. [[KSUploadManager shareInstance] mutilUploadImage:imageDataArray fileName:@"complainImg" successCallback:^(NSMutableArray * _Nonnull fileUrlArray) {
  56. NSMutableArray *urlArray = [NSMutableArray array];
  57. for (NSString *url in fileUrlArray) {
  58. [urlArray addObject:url];
  59. }
  60. NSString *attachments = [urlArray componentsJoinedByString:@","];
  61. [self uplodMessage:attachments content:content];
  62. } faliure:^(NSError * _Nullable error, NSString * _Nullable descMessaeg) {
  63. [self removehub];
  64. [self MBPShow:descMessaeg];
  65. }];
  66. }
  67. - (void)uplodMessage:(NSString *)attachments content:(NSString *)content {
  68. NSString *type = self.fromGroup ? @"GROUP" : @"PERSON";
  69. // [KSRequestManager sysImComplaintRequest:KS_POST type:type fileUrl:attachments memo:content targetId:self.targetId success:^(NSDictionary * _Nonnull dic) {
  70. // [self removehub];
  71. // if ([dic integerValueForKey:@"code"] == 200 && [dic boolValueForKey:@"status"]) {
  72. // MJWeakSelf;
  73. // [self KSShowMsg:@"已收到您的投诉,我们会认真审核并处理" promptCompletion:^{
  74. // [weakSelf.navigationController popViewControllerAnimated:YES];
  75. // }];
  76. // }
  77. // else {
  78. // [self MBPShow:MESSAGEKEY];
  79. // }
  80. // } faliure:^(NSError * _Nonnull error) {
  81. // [self removehub];
  82. // }];
  83. }
  84. /**
  85. 图片和按钮的布局
  86. */
  87. #pragma marl 图片和按钮布局
  88. - (void)createImageButtonAndPhotosButton {
  89. for (UIView *subView in self.bodyView.imageContentView.subviews) {
  90. [subView removeFromSuperview];
  91. }
  92. CGFloat space = 15;
  93. CGFloat buttonWidth = (kScreenWidth - KBUTTON_LEFTSPACE * 2 - space * 2) / 3.0f;
  94. CGFloat buttonHeight = 80;
  95. NSInteger section = self.imageArray.count/3 + 1;
  96. if (self.imageArray.count==MAXPHOTONUMBER) {
  97. section = MAXPHOTONUMBER / 3;
  98. }
  99. CGFloat currentX = 0;
  100. CGFloat currentY = 0;
  101. for (NSInteger i = 0; i < self.imageArray.count; i++) {
  102. currentX = i % 3 * (buttonWidth + space);
  103. currentY = i / 3 * (buttonHeight + space);
  104. UIImage *image = self.imageArray [i];
  105. CGRect frame = CGRectMake(currentX + KBUTTON_LEFTSPACE, currentY, buttonWidth, buttonHeight);
  106. MJWeakSelf;
  107. KSImageButton *imgButton;
  108. id asset = self.imageAsset[i];
  109. imgButton = [KSImageButton createButtonWithFrame:frame displayImage:image imgAsset:asset tag:1000 + i clickReturnWithBlock:^(UIImage *image, id asset, BUTTON_CLICK_ACTION actionType, NSInteger index) {
  110. // 展示大图
  111. LLPhotoBrowser *photoBrowser = [[LLPhotoBrowser alloc] initWithImages:weakSelf.imageArray currentIndex:index-1000];
  112. photoBrowser.delegate = weakSelf;
  113. photoBrowser.modalPresentationStyle = UIModalPresentationFullScreen;
  114. [self presentViewController:photoBrowser animated:YES completion:nil];
  115. }];
  116. [self.bodyView.imageContentView addSubview:imgButton];
  117. imgButton = nil;
  118. }
  119. currentX = self.imageArray .count % 3 * (buttonWidth + space);
  120. if (self.imageArray.count != MAXPHOTONUMBER) {
  121. //计算下一个添加按钮位置
  122. if (self.imageArray.count % 3 == 0) {
  123. currentY = (self.imageArray.count+1) / 3 * (buttonHeight + space);
  124. }
  125. UIButton *cameraButton = [UIButton buttonWithType:UIButtonTypeCustom];
  126. cameraButton.frame = CGRectMake(currentX + KBUTTON_LEFTSPACE, currentY, buttonWidth, buttonHeight);
  127. CAShapeLayer *border = [CAShapeLayer layer];
  128. border.strokeColor = HexRGB(0xCFCFCF).CGColor;
  129. border.fillColor = nil;
  130. border.path = [UIBezierPath bezierPathWithRoundedRect:cameraButton.bounds cornerRadius:5.0f].CGPath;
  131. ;
  132. border.frame = cameraButton.bounds;
  133. border.lineWidth = 1.f;
  134. border.lineCap = @"square";
  135. border.lineDashPattern = @[@4, @4];
  136. [cameraButton.layer addSublayer:border];
  137. [cameraButton setImage:[UIImage imageNamed:@"image_add"] forState:UIControlStateNormal];
  138. [cameraButton addTarget:self action:@selector(addImgAction:) forControlEvents:UIControlEventTouchUpInside];
  139. [self.bodyView.imageContentView addSubview:cameraButton];
  140. }
  141. // 刷新scroll contentsize
  142. CGFloat imageContentHeight = buttonHeight * section + (section-1) * space;
  143. CGFloat height = kScreenHeight - kNaviBarHeight - iPhoneXSafeBottomMargin > 590 + imageContentHeight ? kScreenHeight - kNaviBarHeight - iPhoneXSafeBottomMargin : 590 + imageContentHeight;
  144. CGRect frame = self.bodyView.frame;
  145. frame.size.height = height;
  146. self.bodyView.frame = frame;
  147. self.scrollView.contentSize = CGSizeMake(kScreenWidth, height);
  148. }
  149. #pragma mark 点击+按钮
  150. - (void)addImgAction:(UIButton *)sender {
  151. [self.view endEditing:YES];
  152. UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:IS_IPAD ? UIAlertControllerStyleAlert : UIAlertControllerStyleActionSheet];
  153. [alertVC addAction:[UIAlertAction actionWithTitle:@"相机拍摄" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  154. // 调用相机
  155. [self takePhotos];
  156. }]];
  157. [alertVC addAction:[UIAlertAction actionWithTitle:@"从手机相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  158. // 调用相册
  159. [self pushImagePickerController];
  160. }]];
  161. [alertVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  162. }]];
  163. alertVC.modalPresentationStyle = UIModalPresentationFullScreen;
  164. [self presentViewController:alertVC animated:true completion:nil];
  165. }
  166. - (void)takePhotos {
  167. // 调用相册
  168. self.mediaManager = [[KSMediaManager alloc] init];
  169. self.mediaManager.mediaType = MEDIATYPE_PHOTO;
  170. self.mediaManager.maxPhotoNumber = MAXPHOTONUMBER;
  171. self.mediaManager.baseCtrl = self;
  172. self.mediaManager.imageArray = [self.imageArray mutableCopy];
  173. self.mediaManager.imageAsset = [self.imageAsset mutableCopy];
  174. self.mediaManager.needCropImage = NO;
  175. MJWeakSelf;
  176. [self.mediaManager noAlertCallback:^(NSString * _Nullable videoUrl, NSMutableArray * _Nullable imageArray, NSMutableArray * _Nullable imageAsset) {
  177. [weakSelf displayChooseImage:imageArray imageAsset:imageAsset];
  178. }];
  179. [self.mediaManager takePhoto];
  180. }
  181. - (void)pushImagePickerController {
  182. // 调用相册
  183. self.mediaManager = [[KSMediaManager alloc] init];
  184. self.mediaManager.mediaType = MEDIATYPE_PHOTO;
  185. self.mediaManager.maxPhotoNumber = MAXPHOTONUMBER;
  186. self.mediaManager.baseCtrl = self;
  187. self.mediaManager.imageArray = [self.imageArray mutableCopy];
  188. self.mediaManager.imageAsset = [self.imageAsset mutableCopy];
  189. self.mediaManager.needCropImage = NO;
  190. MJWeakSelf;
  191. [self.mediaManager noAlertCallback:^(NSString * _Nullable videoUrl, NSMutableArray * _Nullable imageArray, NSMutableArray * _Nullable imageAsset) {
  192. [weakSelf displayChooseImage:imageArray imageAsset:imageAsset];
  193. }];
  194. [self.mediaManager pushImagePickerController];
  195. }
  196. - (void)displayChooseImage:(NSMutableArray *)imageArray imageAsset:(NSMutableArray *)imageAsset {
  197. // 添加图片
  198. self.imageArray = [imageArray mutableCopy];
  199. self.imageAsset = [imageAsset mutableCopy];
  200. // 重新布局
  201. [self createImageButtonAndPhotosButton];
  202. }
  203. - (void)deleteImageWithIndex:(NSInteger)index {
  204. [self.view endEditing:YES];
  205. [self.imageArray removeObjectAtIndex:index];
  206. [self.imageAsset removeObjectAtIndex:index];
  207. // 重新布局
  208. [self createImageButtonAndPhotosButton];
  209. }
  210. #pragma mark -- LLPhotoBrowserDelegate
  211. - (void)photoBrowser:(LLPhotoBrowser *)photoBrowser backImages:(NSMutableArray *)images {
  212. NSMutableArray * returnImgArray = [images mutableCopy];
  213. NSMutableArray *imageArray = [NSMutableArray array];
  214. NSMutableArray *imageAsset = [NSMutableArray array];
  215. for (NSInteger i = 0; i < returnImgArray.count; i++) {
  216. id source = returnImgArray[i];
  217. for (NSInteger i = 0; i < self.imageArray.count; i++) {
  218. UIImage *imagePic = self.imageArray[i];
  219. if ([imagePic isEqual:source]) {
  220. [imageArray addObject:imagePic];
  221. [imageAsset addObject:self.imageAsset[i]];
  222. }
  223. }
  224. }
  225. self.imageArray = [imageArray mutableCopy];
  226. self.imageAsset = [imageAsset mutableCopy];
  227. //布局
  228. [self createImageButtonAndPhotosButton];
  229. }
  230. #pragma mark - 懒加载
  231. - (NSMutableArray *)imageArray {
  232. if (!_imageArray) {
  233. _imageArray = [NSMutableArray array];
  234. }
  235. return _imageArray;
  236. }
  237. - (NSMutableArray *)imageAsset {
  238. if (!_imageAsset) {
  239. _imageAsset = [NSMutableArray array];
  240. }
  241. return _imageAsset;
  242. }
  243. /*
  244. #pragma mark - Navigation
  245. // In a storyboard-based application, you will often want to do a little preparation before navigation
  246. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  247. // Get the new view controller using [segue destinationViewController].
  248. // Pass the selected object to the new view controller.
  249. }
  250. */
  251. @end