ChatNewRowVC.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. //
  2. // ChatNewRowVC.m
  3. // smartRhino
  4. //
  5. // Created by niuzhen on 2019/12/10.
  6. // Copyright © 2019 tederen. All rights reserved.
  7. //
  8. #import "ChatNewRowVC.h"
  9. #import "ShowPhotoCameraAlertView.h"
  10. #import "LYLPhotoTailoringTool.h"
  11. #import "SendInfoGroupVC.h"
  12. #import "MailListVC.h"
  13. #import "AddLookUpUserSecondVC.h"
  14. #import "TDGroupQrcodeVC.h"
  15. #import "TDGroupInfoListModel.h"
  16. #import "NewRowUserCell.h"
  17. #import "MOFSPickerManager.h"
  18. #import "GroupSquareModel.h"
  19. @interface ChatNewRowVC ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
  20. @property (strong,nonatomic) IBOutlet UIButton *rightSureBtn;
  21. @property (weak, nonatomic) IBOutlet UITextField *groupNameTf;
  22. @property (weak, nonatomic) IBOutlet UILabel *GroupTypeL;
  23. @property (weak, nonatomic) IBOutlet UIButton *GroupTypeBtn;
  24. @property (weak, nonatomic) IBOutlet UIImageView *groupImgView;
  25. @property (weak, nonatomic) IBOutlet UILabel *avatarTipLab;
  26. @property (weak, nonatomic) IBOutlet UITableView *tableView;
  27. @property (nonatomic, strong) NSString *groupName;
  28. @property (nonatomic, strong) NSString *groupIntroduce ;
  29. @property (nonatomic, strong) NSString *avatarUrl;
  30. @property (nonatomic, strong) NSString *imId;
  31. @property (nonatomic, strong) UIImage *groupImage;
  32. @property (nonatomic, strong) NSMutableArray * dataArray;
  33. @property (nonatomic, strong) NSMutableArray * showArray;
  34. @property (nonatomic, strong) NSMutableArray * selectArray;
  35. @property (nonatomic, strong) NSMutableArray * selectIdArray;
  36. @property (nonatomic, assign) NSInteger selectId;
  37. @property (nonatomic, assign) BOOL isWords;
  38. @property (nonatomic, assign) BOOL isAdmin;
  39. @property (nonatomic, assign) BOOL isUDisturb ;
  40. @end
  41. @implementation ChatNewRowVC
  42. +(ChatNewRowVC *)initChatNewRowVC{
  43. ChatNewRowVC *controller = [StoryboardManager.shared.TDGroup instantiateViewControllerWithIdentifier:@"ChatNewRowVC"];
  44. return controller;
  45. }
  46. - (void)viewDidLoad {
  47. [super viewDidLoad];
  48. self.tableView.delegate = self;
  49. self.tableView.dataSource = self;
  50. self.fd_prefersNavigationBarHidden = YES;
  51. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(recviveChaoSongRen:) name:NOTIFI_ChaoSongRen object:nil];
  52. WEAKSELF
  53. [self.rightSureBtn setAction:^{
  54. weakSelf.rightSureBtn.enabled = NO;
  55. [weakSelf createGroupChat];
  56. }];
  57. self.groupNameTf.delegate = self;
  58. self.groupNameTf.returnKeyType = UIReturnKeyDone;
  59. [self.groupNameTf becomeFirstResponder];
  60. [self.GroupTypeBtn setAction:^{
  61. [weakSelf.groupNameTf resignFirstResponder];
  62. if (weakSelf.showArray.count > 0) {
  63. [weakSelf showType];
  64. }
  65. }];
  66. [self getData];
  67. }
  68. - (void)getData
  69. {
  70. WS(weakSelf);
  71. self.dataArray = [NSMutableArray array];
  72. self.showArray = [NSMutableArray array];
  73. SHOWLOADING
  74. NSString * url = [NSString stringWithFormat:@"%@%@",Host(API_APP_GROUP_Group_Category),@"0"];
  75. [[HttpManager sharedHttpManager] GET:url parameters:@{} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
  76. NSLog(@"%@",responseObject);
  77. REMOVESHOW
  78. if ([responseObject isKindOfClass:[NSArray class]]) {
  79. for (NSDictionary * dict in responseObject) {
  80. GroupSquareModel * model = [GroupSquareModel modelWithDictionary:dict];
  81. [weakSelf.dataArray addObject:model];
  82. [weakSelf.showArray addObject:model.PiazzaName];
  83. }
  84. }
  85. } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
  86. REMOVESHOW
  87. }];
  88. }
  89. - (void)showType
  90. {
  91. WS(weakSelf);
  92. [MOFSPickerManager shareManger].pickView.toolBar.cancelBar.textColor = UIColorHex(#888888);
  93. [MOFSPickerManager shareManger].pickView.toolBar.commitBar.textColor = UIColorHex(#3979D3);
  94. MOFSPickerManager *manager = [[MOFSPickerManager alloc] init];
  95. [manager showPickerViewWithDataArray:self.showArray tag:2 title:nil cancelTitle:@"取消" commitTitle:@"确定" commitBlock:^(NSString *string) {
  96. for (GroupSquareModel * model in weakSelf.dataArray) {
  97. if ([model.PiazzaName isEqualToString:string]) {
  98. weakSelf.selectId = model.Id;
  99. dispatch_async(dispatch_get_main_queue(), ^{
  100. weakSelf.GroupTypeL.text = string;
  101. });
  102. }
  103. }
  104. } cancelBlock:^{
  105. }];
  106. }
  107. -(IBAction)userDidUpImgAction:(id)sender{
  108. WEAKSELF
  109. [self.groupNameTf resignFirstResponder];
  110. [[ShowPhotoCameraAlertView initShowPhotoCameraAlertViewConfirm:^(ShowPhotoCameraType type) {
  111. if(type == ShowPhotoCameraType1){
  112. [[LYLPhotoTailoringTool sharedTool] selectPhotoWithPhoroOrCamera:SelectPhotoWithTypePhoroOrCamera2 crop:YES showImgBlock:^(NSString *imageUrlStr) {
  113. } choosImgBlock:^(UIImage *image) {
  114. weakSelf.groupImage = image;
  115. weakSelf.groupImgView.image = image;
  116. weakSelf.avatarTipLab.text = @"";
  117. }];
  118. }else if (type == ShowPhotoCameraType2){
  119. NSLog(@"相册");
  120. TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:1 delegate:nil];
  121. [imagePickerVc setNavLeftBarButtonSettingBlock:^(UIButton *leftButton){
  122. leftButton.hidden = YES;
  123. }];
  124. imagePickerVc.showSelectBtn = NO;
  125. imagePickerVc.allowCrop = YES;
  126. imagePickerVc.cropRect = CGRectMake(0, (SCREEN_HEIGHT - SCREEN_WIDTH) * 0.5, SCREEN_WIDTH, SCREEN_WIDTH);
  127. imagePickerVc.allowTakePicture = NO;
  128. imagePickerVc.allowTakeVideo = NO;
  129. imagePickerVc.allowPickingOriginalPhoto = NO;
  130. imagePickerVc.allowPickingGif = NO;
  131. [imagePickerVc setIsStatusBarDefault:YES];
  132. [imagePickerVc setNaviTitleColor:[UIColor blackColor]];
  133. [imagePickerVc setBarItemTextColor:[UIColor blackColor]];
  134. [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
  135. PHAsset *asset = assets.firstObject;
  136. switch (asset.mediaType) {
  137. case PHAssetMediaTypeVideo: {
  138. } break;
  139. case PHAssetMediaTypeImage: {
  140. UIImage * image = photos.firstObject;
  141. weakSelf.groupImage = image;
  142. weakSelf.groupImgView.image = image;
  143. weakSelf.avatarTipLab.text = @"";
  144. } break;
  145. case PHAssetMediaTypeAudio:
  146. break;
  147. case PHAssetMediaTypeUnknown:
  148. break;
  149. default:
  150. break;
  151. }
  152. }];
  153. [self presentViewController:imagePickerVc animated:YES completion:nil];
  154. }
  155. } cancle:^{
  156. }] showWithAnimation:kAlertAnimationBottom];
  157. }
  158. -(IBAction)userDidAddFriendeAction:(id)sender{
  159. [self.view endEditing:YES];
  160. Department *defaltGroupSetion = [[Department alloc]init];
  161. defaltGroupSetion.Name = @"单位通讯录";
  162. defaltGroupSetion.Id = 0;
  163. Department *myGroupSetion = [[Department alloc]init];
  164. myGroupSetion.Name = @"自建通讯录";
  165. defaltGroupSetion.Id = 0;
  166. AddLookUpUserSecondVC *vc = [[AddLookUpUserSecondVC alloc] init];
  167. vc.departSourceDataArray = @[defaltGroupSetion,myGroupSetion].mutableCopy;
  168. vc.isDefalutPage = 1;
  169. vc.titleStr = @"选人";
  170. USERDEFAULTSSET([NSMutableArray array], MAILSELECT);
  171. vc.UserIds = self.selectIdArray;
  172. USERDEFAULTSSET([NSMutableArray array], MAILSELECT);
  173. [self.navigationController pushViewController:vc animated:YES];
  174. }
  175. - (void)recviveChaoSongRen:(NSNotification *)notification{
  176. NSMutableArray <SelectModel *>* array = notification.userInfo[@"selectPeople"];
  177. NSMutableArray <SelectModel *>* array0 = self.selectArray;
  178. NSMutableArray <SelectModel *>* array1 = notification.userInfo[@"selectPeople"];
  179. if (array0.count == 0) {
  180. [self.selectArray addObjectsFromArray:array];
  181. }else{
  182. for (NSInteger x = 0; x < array.count; x++) {
  183. for (NSInteger y = 0 ; y < array0.count; y++) {
  184. if (array[x].UserId == array0[y].UserId) {
  185. [array1 removeObjectAtIndex:x];
  186. }
  187. }
  188. }
  189. NSLog(@"结果数据组%@",array1);
  190. [self.selectArray addObjectsFromArray:array1];
  191. }
  192. if (self.selectArray.count > 0) {
  193. for (NSInteger i = 0; i < self.selectArray.count; i ++) {
  194. SelectModel * model = self.selectArray[i];
  195. [self.selectIdArray addObject:@(model.UserId)];
  196. }
  197. [self.tableView reloadData];
  198. }
  199. }
  200. - (void)createGroupChat{
  201. if (self.groupNameTf.text.length == 0) {
  202. SHOWERROR(@"请输入小组名称")
  203. return;
  204. }else{
  205. self.groupName = self.groupNameTf.text;
  206. }
  207. if ([self.GroupTypeL.text isEqualToString:@"请选择"]) {
  208. SHOWERROR(@"请选择小组类型")
  209. return;
  210. }
  211. WEAKSELF
  212. self.rightSureBtn.enabled = NO;
  213. if (self.groupImage) {
  214. [self postGroupAvatar:self.groupImage withCompletion:^(BOOL success, NSString *imageUrl) {
  215. if (success) {
  216. weakSelf.avatarUrl = imageUrl;
  217. [weakSelf postGroupMsg];
  218. }
  219. }];
  220. }else{
  221. SHOWERROR(@"请上传小组头像");
  222. self.rightSureBtn.enabled = YES;
  223. return;
  224. }
  225. }
  226. - (void)postGroupMsg{
  227. NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
  228. [dict setValue:self.groupName forKey:@"Name"];
  229. [dict setValue:self.groupIntroduce forKey:@"Introduce"];
  230. [dict setValue:@([AppUserModel sharedAppUserModel].Id) forKey:@"UserId"];
  231. [dict setValue:@(self.isWords) forKey:@"IsWords"];
  232. [dict setValue:@(self.isAdmin) forKey:@"IsAdmin"];
  233. [dict setValue:self.avatarUrl forKey:@"AvatarUrl"];
  234. [dict setValue:self.selectIdArray forKey:@"UserIds"];
  235. [dict setValue:@"" forKey:@"QRCoder"];
  236. [dict setValue:@(self.isUDisturb) forKey:@"IsUDisturb"];
  237. [dict setValue:@(self.GroupFolderId) forKey:@"GroupFolderId"];
  238. [dict setValue:@(self.selectId) forKey:@"GroupCategroyId"];
  239. WS(weakSelf);
  240. SHOWLOADING
  241. [[HttpManager sharedHttpManager] PUTUrl:Host(APP_Group_Insert) parameters:dict responseStyle:JOSN success:^(id _Nonnull responseObject) {
  242. REMOVESHOW
  243. weakSelf.rightSureBtn.enabled = YES;
  244. NSLog(@"%@",responseObject);
  245. dispatch_async(dispatch_get_main_queue(), ^{
  246. TDGroupQrcodeVC * vc = [TDGroupQrcodeVC initTDGroupQrcodeVC];
  247. TDGroupInfoListModel * model = [TDGroupInfoListModel modelWithDictionary:responseObject];
  248. vc.model = model;
  249. vc.isCreate = YES;
  250. [weakSelf.navigationController pushViewController:vc animated:YES];
  251. });
  252. } failure:^(NSError * _Nonnull error) {
  253. REMOVESHOW
  254. weakSelf.rightSureBtn.enabled = YES;
  255. SHOWERROR(@"创建失败")
  256. }];
  257. }
  258. - (void)postGroupAvatar:(UIImage*)image withCompletion:(void(^)(BOOL success,NSString *imageUrl))completionHandler{
  259. NSString *urlString = [[NSString alloc] initWithFormat:@"%@%@",BaseUrl,Modify_UserImage_Post];
  260. [[HttpManager sharedHttpManager] HeaderUploadUrl:urlString parameters:@{} pictureData:UIImageJPEGRepresentation(image,1.0f) pictureKey:@"file" success:^(id _Nonnull responseObject) {
  261. if (completionHandler) {
  262. completionHandler(YES,responseObject[@"AbsolutePath"]);
  263. }
  264. } failure:^(NSError * _Nonnull error) {
  265. if (completionHandler) {
  266. completionHandler(NO,nil);
  267. }
  268. }];
  269. }
  270. - (NSMutableArray *)selectArray
  271. {
  272. if (!_selectArray) {
  273. _selectArray = [NSMutableArray array];
  274. }
  275. return _selectArray;
  276. }
  277. - (NSMutableArray *)selectIdArray
  278. {
  279. if (!_selectIdArray) {
  280. _selectIdArray = [NSMutableArray array];
  281. }
  282. return _selectIdArray;
  283. }
  284. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  285. {
  286. return self.selectArray.count;
  287. }
  288. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  289. {
  290. return 65.f;
  291. }
  292. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  293. {
  294. SelectModel *model = [self.selectArray objectAtIndex:indexPath.row];
  295. NewRowUserCell *cell = [NewRowUserCell configCell:tableView indexPath:indexPath];
  296. [cell.imagV sd_setImageWithURL:[NSURL URLWithString:model.AvatarUrl] placeholderImage:kUserDefaultHeadImage];
  297. cell.nameL.text = model.UserName;
  298. WS(weakSelf);
  299. [cell.CloseBtn setAction:^{
  300. [weakSelf.selectArray removeObjectAtIndex:indexPath.row];
  301. dispatch_async(dispatch_get_main_queue(), ^{
  302. [weakSelf.tableView reloadData];
  303. });
  304. }];
  305. return cell;
  306. }
  307. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
  308. if (textField == self.groupNameTf) {
  309. if (range.length == 1 && string.length == 0) {
  310. return YES;
  311. }
  312. else if (self.groupNameTf.text.length + string.length >= 12) {
  313. NSMutableString *oldStr = [[NSMutableString alloc]initWithString:textField.text];
  314. [oldStr insertString:string atIndex:range.location];
  315. self.groupNameTf.text = [oldStr substringToIndex:12];
  316. return NO;
  317. }
  318. }
  319. return YES;
  320. }
  321. -(void)textFieldDidEndEditing:(UITextField *)textField{
  322. if (textField.text.length > 12) {
  323. textField.text = [textField.text substringToIndex:12];
  324. SHOWERROR(@"文件夹名称最多12个字");
  325. }
  326. }
  327. - (BOOL)textFieldShouldReturn:(UITextField *)textField
  328. {
  329. [textField resignFirstResponder];
  330. return YES;
  331. }
  332. - (void)dealloc
  333. {
  334. [[NSNotificationCenter defaultCenter] removeObserver:self];
  335. }
  336. @end