ChatNewGroupVC.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. //
  2. // ChatNewGroupVC.m
  3. // smartRhino
  4. //
  5. // Created by armin on 2019/11/5.
  6. // Copyright © 2019 tederen. All rights reserved.
  7. //
  8. #import "ChatNewGroupVC.h"
  9. #import "ShowPhotoCameraAlertView.h"
  10. #import "LYLPhotoTailoringTool.h"
  11. #import "SendInfoGroupVC.h"
  12. #import "MailListVC.h"
  13. #import "AddLookUpUserSecondVC.h"
  14. #import "NewRowUserCell.h"
  15. #import "ChatQrcodeVC.h"
  16. @interface ChatNewGroupVC ()<UITableViewDelegate,UITableViewDataSource>
  17. @property (strong,nonatomic) IBOutlet UIButton *rightSureBtn;
  18. @property (weak, nonatomic) IBOutlet UITextField *groupNameTf;
  19. @property (weak, nonatomic) IBOutlet UITextField *groupIntroTf;
  20. @property (weak, nonatomic) IBOutlet UIImageView *groupImgView;
  21. @property (weak, nonatomic) IBOutlet UILabel *avatarTipLab;
  22. @property (weak, nonatomic) IBOutlet UITableView *tableView;
  23. @property (nonatomic, strong) NSString *groupName ;
  24. @property (nonatomic, strong) NSString *groupIntroduce ;
  25. @property (nonatomic, strong) NSString *avatarUrl ;
  26. @property (nonatomic, strong) NSString *imId ;
  27. @property (nonatomic, strong) UIImage *groupImage ;
  28. @property (nonatomic, strong) NSMutableArray * selectArray;
  29. @property (nonatomic, strong) NSMutableArray * selectIdArray;
  30. @property (nonatomic, assign) bool isWords ;
  31. @property (nonatomic, assign) bool isAdmin ;
  32. @end
  33. @implementation ChatNewGroupVC
  34. +(ChatNewGroupVC *)initChatNewGroupVC{
  35. ChatNewGroupVC *controller = [StoryboardManager.shared.chatMsg instantiateViewControllerWithIdentifier:@"ChatNewGroupVC"];
  36. return controller;
  37. }
  38. - (void)viewDidLoad {
  39. [super viewDidLoad];
  40. self.tableView.delegate = self;
  41. self.tableView.dataSource = self;
  42. self.view.backgroundColor = UIColorHex(#F0EFF4);
  43. if (self.dataArray.count > 0) {
  44. [self.selectArray addObjectsFromArray:self.dataArray];
  45. [self.tableView reloadData];
  46. }
  47. self.fd_prefersNavigationBarHidden = YES;
  48. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(recviveChaoSongRen:) name:NOTIFI_ChaoSongRen object:nil];
  49. WEAKSELF
  50. [self.rightSureBtn setAction:^{
  51. [weakSelf createGroupChat];
  52. }];
  53. }
  54. -(IBAction)userDidUpImgAction:(id)sender{
  55. WEAKSELF
  56. [self.groupNameTf resignFirstResponder];
  57. [self.groupIntroTf resignFirstResponder];
  58. [[ShowPhotoCameraAlertView initShowPhotoCameraAlertViewConfirm:^(ShowPhotoCameraType type) {
  59. if(type == ShowPhotoCameraType1){
  60. NSLog(@"相册");
  61. [[LYLPhotoTailoringTool sharedTool] selectPhotoWithPhoroOrCamera:SelectPhotoWithTypePhoroOrCamera2 crop:YES showImgBlock:^(NSString *imageUrlStr) {
  62. NSLog(@"111111");
  63. } choosImgBlock:^(UIImage *image) {
  64. NSLog(@"aaaaaa");
  65. weakSelf.groupImage = image;
  66. weakSelf.groupImgView.image = image;
  67. weakSelf.avatarTipLab.text = @"";
  68. }];
  69. }else if (type == ShowPhotoCameraType2){
  70. NSLog(@"相册");
  71. TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:1 delegate:nil];
  72. [imagePickerVc setNavLeftBarButtonSettingBlock:^(UIButton *leftButton){
  73. leftButton.hidden = YES;
  74. }];
  75. imagePickerVc.showSelectBtn = NO;
  76. imagePickerVc.allowCrop = YES;
  77. imagePickerVc.cropRect = CGRectMake(0, (SCREEN_HEIGHT - SCREEN_WIDTH) * 0.5, SCREEN_WIDTH, SCREEN_WIDTH);
  78. imagePickerVc.allowTakePicture = NO;
  79. imagePickerVc.allowTakeVideo = NO;
  80. imagePickerVc.allowPickingOriginalPhoto = NO;
  81. imagePickerVc.allowPickingGif = NO;
  82. [imagePickerVc setIsStatusBarDefault:YES];
  83. [imagePickerVc setNaviTitleColor:[UIColor blackColor]];
  84. [imagePickerVc setBarItemTextColor:[UIColor blackColor]];
  85. [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
  86. PHAsset *asset = assets.firstObject;
  87. switch (asset.mediaType) {
  88. case PHAssetMediaTypeVideo: {
  89. } break;
  90. case PHAssetMediaTypeImage: {
  91. UIImage * image = photos.firstObject;
  92. weakSelf.groupImage = image;
  93. weakSelf.groupImgView.image = image;
  94. weakSelf.avatarTipLab.text = @"";
  95. } break;
  96. case PHAssetMediaTypeAudio:
  97. break;
  98. case PHAssetMediaTypeUnknown:
  99. break;
  100. default: break;
  101. }
  102. }];
  103. [self presentViewController:imagePickerVc animated:YES completion:nil];
  104. }
  105. } cancle:^{
  106. }] showWithAnimation:kAlertAnimationBottom];
  107. }
  108. -(IBAction)userDidAddFriendeAction:(id)sender{
  109. Department *defaltGroupSetion = [[Department alloc]init];
  110. defaltGroupSetion.Name = @"单位通讯录";
  111. defaltGroupSetion.Id = 0;
  112. Department *myGroupSetion = [[Department alloc]init];
  113. myGroupSetion.Name = @"自建通讯录";
  114. defaltGroupSetion.Id = 0;
  115. AddLookUpUserSecondVC *vc = [[AddLookUpUserSecondVC alloc] init];
  116. vc.departSourceDataArray = @[defaltGroupSetion,myGroupSetion].mutableCopy;
  117. vc.isDefalutPage = 1;
  118. vc.grouperArray = self.dataArray;
  119. vc.titleStr = @"添加成员";
  120. [self.navigationController pushViewController:vc animated:YES];
  121. }
  122. - (void)recviveChaoSongRen:(NSNotification *)notification{
  123. NSMutableArray <SelectModel *>* array = notification.userInfo[@"selectPeople"];
  124. NSMutableArray <SelectModel *>* array0 = self.selectArray;
  125. NSMutableArray <SelectModel *>* array1 = notification.userInfo[@"selectPeople"];
  126. if (array0.count == 0) {
  127. [self.selectArray addObjectsFromArray:array];
  128. }else{
  129. for (NSInteger x = 0; x < array.count; x++) {
  130. for (NSInteger y = 0 ; y<array0.count; y++) {
  131. if (array[x].UserId == array0[y].UserId) {
  132. [array1 removeObjectAtIndex:x];
  133. }
  134. }
  135. }
  136. NSLog(@"结果数据组%@",array1);
  137. [self.selectArray addObjectsFromArray:array1];
  138. }
  139. // if (self.selectArray.count > 0) {
  140. // for (NSInteger i = 0; i < self.selectArray.count; i ++) {
  141. // SelectModel * model = self.selectArray[i];
  142. // [self.selectIdArray addObject:@(model.Id)];
  143. // }
  144. // }
  145. [self.tableView reloadData];
  146. }
  147. //创建群聊
  148. /*
  149. {
  150. "Name": "string",
  151. "Introduce": "string",
  152. "AvatarUrl": "string",
  153. "UserId": 0,
  154. "ImId": "string",
  155. "IsWords": true,
  156. "IsAdmin": true
  157. }
  158. */
  159. - (void)createGroupChat{
  160. if (self.groupNameTf.text.length==0) {
  161. SHOWERROR(@"请输入群聊名称")
  162. return;
  163. }else{
  164. self.groupName = self.groupNameTf.text;
  165. }
  166. if (self.groupIntroTf.text.length==0) {
  167. SHOWERROR(@"请添加小组介绍")
  168. return;
  169. }else{
  170. self.groupIntroduce = self.groupIntroTf.text;
  171. }
  172. WEAKSELF
  173. if (self.groupImage) {
  174. self.rightSureBtn.enabled = NO;
  175. [self postGroupAvatar:self.groupImage withCompletion:^(BOOL success, NSString *imageUrl) {
  176. if (success) {
  177. weakSelf.avatarUrl = imageUrl;
  178. [weakSelf postGroupMsg];
  179. }
  180. }];
  181. }else{
  182. SHOWERROR(@"请上传群聊头像")
  183. }
  184. }
  185. - (void)postGroupMsg{
  186. [self.selectIdArray removeAllObjects];
  187. if (self.selectArray.count > 0) {
  188. for (NSInteger i = 0; i < self.selectArray.count; i ++) {
  189. SelectModel * model = self.selectArray[i];
  190. [self.selectIdArray addObject:@(model.UserId)];
  191. }
  192. }
  193. NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
  194. [dict setValue:self.groupName forKey:@"Name"];
  195. [dict setValue:self.groupIntroduce forKey:@"Introduce"];
  196. [dict setValue:@([AppUserModel sharedAppUserModel].Id) forKey:@"UserId"];
  197. [dict setValue:@(self.isWords) forKey:@"IsWords"];
  198. [dict setValue:@(self.isAdmin) forKey:@"IsAdmin"];
  199. [dict setValue:self.avatarUrl forKey:@"AvatarUrl"];
  200. [dict setValue:self.selectIdArray forKey:@"UserIds"];
  201. NSString *imid = [self EaseCreatGroupChatWithJoinType:0];
  202. self.imId = imid;
  203. [dict setValue:self.imId forKey:@"GroupChatImId"];//创建环信群聊时生成的id
  204. NSString *urlString = [[NSString alloc] initWithFormat:@"%@%@",BaseUrl,API_GROUPCHAT_ADD];
  205. WS(weakSelf);
  206. SHOWLOADING
  207. [[HttpManager sharedHttpManager] POSTUrl:urlString parameters:dict responseStyle:JOSN success:^(id _Nonnull responseObject) {
  208. REMOVESHOW
  209. SHOWSUCCESS(@"创建成功")
  210. weakSelf.rightSureBtn.enabled = YES;
  211. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.2 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
  212. ChatQrcodeVC * vc = [ChatQrcodeVC initChatQrcodeVC];
  213. vc.dict = responseObject;
  214. vc.isCreate = YES;
  215. [weakSelf.navigationController pushViewController:vc animated:YES];
  216. });
  217. } failure:^(NSError * _Nonnull error) {
  218. REMOVESHOW
  219. SHOWERROR([ZYCTool handerResultData:error])
  220. weakSelf.rightSureBtn.enabled = YES;
  221. }];
  222. }
  223. - (void)postGroupAvatar:(UIImage*)image withCompletion:(void(^)(BOOL success,NSString *imageUrl))completionHandler{
  224. NSString *urlString = [[NSString alloc] initWithFormat:@"%@%@",BaseUrl,Modify_UserImage_Post];
  225. SHOWLOADING
  226. [[HttpManager sharedHttpManager] HeaderUploadUrl:urlString parameters:@{} pictureData:UIImageJPEGRepresentation(image,1.0f) pictureKey:@"file" success:^(id _Nonnull responseObject) {
  227. REMOVESHOW
  228. if (completionHandler) {
  229. completionHandler(YES,responseObject[@"AbsolutePath"]);
  230. }
  231. } failure:^(NSError * _Nonnull error) {
  232. REMOVESHOW
  233. if (completionHandler) {
  234. completionHandler(NO,nil);
  235. }
  236. }];
  237. }
  238. #pragma mark - 创建群聊
  239. - (NSString *)EaseCreatGroupChatWithJoinType:(int)type {
  240. EMGroupOptions *setting = [[EMGroupOptions alloc] init];
  241. setting.maxUsersCount = 2000;
  242. if (type == 0) {
  243. setting.style = EMGroupStylePublicOpenJoin;//公开的 都可以进
  244. } else {
  245. setting.style = EMGroupStylePublicJoinNeedApproval;//需要审核
  246. }
  247. EMError *error = nil;
  248. EMGroup *group = [[EMClient sharedClient].groupManager createGroupWithSubject:@"" description:@"" invitees:nil message:nil setting:setting error:&error];
  249. if (group && !error) {
  250. return group.groupId;
  251. } else {
  252. return @"";
  253. }
  254. }
  255. - (NSMutableArray *)selectArray
  256. {
  257. if (!_selectArray) {
  258. _selectArray = [NSMutableArray array];
  259. }
  260. return _selectArray;
  261. }
  262. - (NSMutableArray *)selectIdArray
  263. {
  264. if (!_selectIdArray) {
  265. _selectIdArray = [NSMutableArray array];
  266. }
  267. return _selectIdArray;
  268. }
  269. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  270. {
  271. return self.selectArray.count;
  272. }
  273. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  274. {
  275. return 65.f;
  276. }
  277. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  278. {
  279. SelectModel *model = [self.selectArray objectAtIndex:indexPath.row];
  280. NewRowUserCell *cell = [NewRowUserCell configCell:tableView indexPath:indexPath];
  281. [cell.imagV sd_setImageWithURL:[NSURL URLWithString:model.AvatarUrl] placeholderImage:kUserDefaultHeadImage];
  282. cell.nameL.text = model.UserName;
  283. WS(weakSelf);
  284. [cell.CloseBtn setAction:^{
  285. [weakSelf.selectArray removeObjectAtIndex:indexPath.row];
  286. dispatch_async(dispatch_get_main_queue(), ^{
  287. [weakSelf.tableView reloadData];
  288. });
  289. }];
  290. return cell;
  291. }
  292. - (void)dealloc
  293. {
  294. [[NSNotificationCenter defaultCenter] removeObserver:self];
  295. }
  296. @end