ChatMsgTopSearchVC.m 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. //
  2. // ChatMsgTopSearchVC.m
  3. // smartRhino
  4. //
  5. // Created by niuzhen on 2020/4/8.
  6. // Copyright © 2020 tederen. All rights reserved.
  7. //
  8. #import "ChatMsgTopSearchVC.h"
  9. #import "MailListByZuCell.h"
  10. #import "MailListDetailVC.h"
  11. #import "ChatSearchModel.h"
  12. #import "ChatMsgListCell.h"
  13. #import "NSDate+Extension.h"
  14. #import "EMChatViewController.h"
  15. #import "WorkingGroupMainVC.h"
  16. @interface ChatMsgTopSearchVC ()<UITableViewDelegate,UITableViewDataSource>
  17. @property (nonatomic, copy) NSMutableArray *listArray;
  18. @end
  19. @implementation ChatMsgTopSearchVC
  20. - (void)viewDidLoad {
  21. [super viewDidLoad];
  22. self.fd_prefersNavigationBarHidden = YES;
  23. self.view.backgroundColor = RGB(255, 255, 255);
  24. self.tableView.delegate = self;
  25. self.tableView.dataSource = self;
  26. self.historySearchType = HistorySearchType_XinYuan;
  27. [self addObserver:self forKeyPath:@"searchText" options:NSKeyValueObservingOptionNew context:nil];
  28. }
  29. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
  30. {
  31. id obj = [change objectForKey:@"new"];
  32. NSString * text = @"";
  33. if ([obj isKindOfClass:[NSString class]]) {
  34. text = obj;
  35. }else{
  36. text = [obj stringValue];
  37. }
  38. if (text.length > 0) {
  39. [self getData];
  40. }
  41. }
  42. - (void)getData
  43. {
  44. WEAKSELF
  45. [self.listArray removeAllObjects];
  46. NSString * url = [NSString stringWithFormat:@"%@%@",Host(API_APP_Often_Contact_Search),self.searchText];
  47. NSString * urlHost = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
  48. [[HttpManager sharedHttpManager] GETUrl:urlHost parameters:@{} responseStyle:JOSN success:^(id _Nonnull responseObject) {
  49. NSLog(@"搜索结果:%@",responseObject);
  50. for (NSDictionary *dic in responseObject) {
  51. ChatSearchModel * model = [ChatSearchModel modelWithDictionary:dic];
  52. [weakSelf.listArray addObject:model];
  53. }
  54. dispatch_async(dispatch_get_main_queue(), ^{
  55. if (weakSelf.listArray.count > 0) {
  56. weakSelf.collectionView.hidden = YES;
  57. weakSelf.noDataView.hidden = YES;
  58. weakSelf.tableView.hidden = NO;
  59. [weakSelf.tableView reloadData];
  60. }else{
  61. weakSelf.noDataView.hidden = NO;
  62. weakSelf.collectionView.hidden = YES;
  63. weakSelf.tableView.hidden = YES;
  64. weakSelf.noDataL.text = @"暂无数据";
  65. }
  66. });
  67. } failure:^(NSError * _Nonnull error) {
  68. SHOWERROR([ZYCTool handerResultData:error])
  69. }];
  70. }
  71. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  72. {
  73. return self.listArray.count;
  74. }
  75. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  76. {
  77. ChatSearchModel * model = [self.listArray objectAtIndex:indexPath.row];
  78. switch (model.TypeValue) {
  79. case ChatSearchUserType:
  80. {
  81. return [MailListByZuCell configCell2Height];
  82. }
  83. break;
  84. case ChatSearchOnlyType:
  85. {
  86. return [ChatMsgListCell configCell3Height];
  87. }
  88. break;
  89. case ChatSearchGroupType:
  90. {
  91. return [ChatMsgListCell configCell3Height];
  92. }
  93. break;
  94. default:
  95. {
  96. return [ChatMsgListCell configCell2Height];
  97. }
  98. break;
  99. }
  100. }
  101. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  102. {
  103. ChatSearchModel * model = [self.listArray objectAtIndex:indexPath.row];
  104. switch (model.TypeValue) {
  105. case ChatSearchUserType:
  106. {
  107. MailListByZuCell *cell = [MailListByZuCell configCell2:tableView indexPath:indexPath];
  108. cell.cell0TitleLabel.text = model.Name;
  109. cell.cell0DepartMentName.text = model.DepartmentName;
  110. [cell.cell0ImgView sd_setImageWithURL:[NSURL URLWithString:model.AvatarUrl] placeholderImage:IMG(@"chatmsg_list_testuser_img")];
  111. return cell;
  112. }
  113. break;
  114. case ChatSearchOnlyType:
  115. {
  116. ChatMsgListCell *cell = [ChatMsgListCell configCell3:tableView indexPath:indexPath];
  117. [cell setCellSearchModel:model];
  118. return cell;
  119. }
  120. break;
  121. case ChatSearchGroupType:
  122. {
  123. ChatMsgListCell *cell = [ChatMsgListCell configCell3:tableView indexPath:indexPath];
  124. [cell setCellSearchModel:model];
  125. return cell;
  126. }
  127. break;
  128. default:
  129. {
  130. ChatMsgListCell *cell = [ChatMsgListCell configCell2:tableView indexPath:indexPath];
  131. cell.cell2TitleLabel.text = model.Name;
  132. cell.cell2TimeLabel.hidden = NO;
  133. cell.IsTopView.hidden = YES;
  134. cell.cell2TimeLabel.text = [NSDate getTimeStringAutoShort2:model.LastModifiedDate];
  135. cell.cell2TimeLabel.textColor = RGB(153, 153, 153);
  136. cell.enterBtn.hidden = YES;
  137. return cell;
  138. }
  139. break;
  140. }
  141. }
  142. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  143. {
  144. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  145. ChatSearchModel *model = [self.listArray objectAtIndex:indexPath.row];
  146. switch (model.TypeValue) {
  147. case ChatSearchUserType:
  148. {
  149. MailListDetailVC *vc = [MailListDetailVC initMailListDetailVC];
  150. vc.indexId = model.Id;
  151. [self.navigationController pushViewController:vc animated:YES];
  152. }
  153. break;
  154. case ChatSearchOnlyType:
  155. {
  156. [self enterChatView:model];
  157. }
  158. break;
  159. case ChatSearchGroupType:
  160. {
  161. [self getGroupInfo:model];
  162. }
  163. break;
  164. default:
  165. {
  166. WorkingGroupMainVC *vc = [WorkingGroupMainVC initWorkingGroupMainVC];
  167. vc.GroupId = [NSString stringWithFormat:@"%ld",(long)model.Id];
  168. vc.GroupTitle = model.Name;
  169. [self.navigationController pushViewController:vc animated:YES];
  170. }
  171. break;
  172. }
  173. }
  174. - (void)enterChatView:(ChatSearchModel *)chatModel
  175. {
  176. EMConversation *conversation = [[EMClient sharedClient].chatManager getConversation:[NSString stringWithFormat:@"%ld",(long)chatModel.ImId] type: EMConversationTypeChat createIfNotExist:YES];
  177. EMConversationModel *model = [[EMConversationModel alloc] initWithEMModel:conversation];
  178. EMChatViewController *controller = [[EMChatViewController alloc] initWithCoversationModel:model];
  179. if (chatModel.TypeValue == ChatSearchOnlyType) {
  180. controller.toUserId = chatModel.ToUserId;
  181. controller.ReceiveIcon = chatModel.AvatarUrl;
  182. SelectModel * sModel = [[SelectModel alloc] init];
  183. sModel.UserId = chatModel.ToUserId;
  184. sModel.UserName = chatModel.Name;
  185. sModel.AvatarUrl = chatModel.AvatarUrl;
  186. controller.sModel = sModel;
  187. }
  188. model.name = chatModel.Name;
  189. controller.ImId = chatModel.ImId;
  190. controller.GroupId = chatModel.Id;
  191. controller.chatType = 1;
  192. controller.listId = chatModel.Id;
  193. controller.isTop = chatModel.IsTop;
  194. [self.navigationController pushViewController:controller animated:YES];
  195. }
  196. - (void)getGroupInfo:(ChatSearchModel *)chatModel
  197. {
  198. WS(weakSelf);
  199. [[HttpManager sharedHttpManager] POSTUrl:Host(Group_chat_info) parameters:@{@"GroupChatImId":@(chatModel.ImId)} responseStyle:JOSN success:^(id _Nonnull responseObject) {
  200. EMConversation *conversation = [[EMClient sharedClient].chatManager getConversation:[NSString stringWithFormat:@"%ld",(long)chatModel.ImId] type: EMConversationTypeGroupChat createIfNotExist:YES];
  201. EMConversationModel *model = [[EMConversationModel alloc] initWithEMModel:conversation];
  202. EMChatViewController *controller = [[EMChatViewController alloc] initWithCoversationModel:model];
  203. controller.toUserId = chatModel.ImId;
  204. controller.groupUserInfoArray = responseObject[@"Item"];
  205. model.name = chatModel.Name;
  206. controller.GroupId = chatModel.Id;
  207. controller.ImId = chatModel.ImId;
  208. controller.chatType = 2;
  209. controller.isTop = chatModel.IsTop;
  210. [weakSelf.navigationController pushViewController:controller animated:YES];
  211. } failure:^(NSError * _Nonnull error) {
  212. }];
  213. }
  214. - (NSMutableArray *)listArray{
  215. if (!_listArray) {
  216. _listArray = [NSMutableArray new];
  217. }
  218. return _listArray;
  219. }
  220. @end