ChatMsgListVC.m 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. //
  2. // ChatMsgListVC.m
  3. // smartRhino
  4. //
  5. // Created by armin on 2019/11/2.
  6. // Copyright © 2019 tederen. All rights reserved.
  7. //
  8. #import "ChatMsgListVC.h"
  9. #import "ChatMsgListCell.h"
  10. #import "GHRefreshCollectionView.h"
  11. #import "ChatMsgCollectionCell.h"
  12. #import "YCMenuView.h"
  13. #import "ChatMsgTopSearchVC.h"
  14. #import "WorkingGroupMainVC.h"
  15. #import "ShowNewGroupAlert.h"
  16. #import "ChatMsgNoticeVC.h"
  17. #import "MoreAppVC.h"
  18. #import "ChatNewGroupVC.h"
  19. #import "NoticeUnreadVC.h"
  20. #import "MailListVC.h"
  21. #import "EMChatViewController.h"
  22. #import "TDInterLeterHomeViewController.h"
  23. #import "EMRealtimeSearch.h"
  24. #import "EMConversationHelper.h"
  25. #import "MoreAppModel.h"
  26. #import "NavigationController.h"
  27. #import "TabBarController.h"
  28. #import "MyTDGroupViewController.h"
  29. #import "WaitWorkVC.h"
  30. #import "ChatMsgWorkModel.h"
  31. #import "MyTDTopicViewController.h"
  32. #import "TDInterLeterHomeViewController.h"
  33. #import "ChatListMoveVC.h"
  34. #import "NSDate+Extension.h"
  35. #import "SWQRCodeConfig.h"
  36. #import "SWQRCodeViewController.h"
  37. #import "MailListDetailVC.h"
  38. #import "ChatJoinGroupVC.h"
  39. #import "TDQrJoinVC.h"
  40. #import "MyTDGroupView.h"
  41. #import "ChatTestingVC.h"
  42. #import "ReplayMeVC.h"
  43. #import "TDGroupInfoListVC.h"
  44. #import "ChatMsgEditVC.h"
  45. static NSString *kConversation_IsRead = @"kHaveAtMessage";
  46. static int kConversation_AtYou = 1;
  47. static int kConversation_AtAll = 2;
  48. @interface ChatMsgListVC ()<UITableViewDelegate,UITableViewDataSource,EMChatManagerDelegate, EMGroupManagerDelegate, EMConversationsDelegate>
  49. @property (strong,nonatomic) UITableView *tableView;
  50. //菜单列表数据源
  51. @property (strong,nonatomic) NSMutableArray *taskActionArray;
  52. @property (strong, nonatomic) MyTDGroupView *SearchView;
  53. @property (strong,nonatomic) NSMutableArray *dataArray;
  54. @property (strong,nonatomic) NSMutableArray *chatListIMArray;
  55. @property (strong,nonatomic) NSMutableArray *selectArray;
  56. @property (assign,nonatomic) BOOL isAllselect;
  57. @property (copy,nonatomic) NSString *ConfirmationDate;
  58. @property (copy,nonatomic) NSString *ReplyDate;
  59. @property (assign,nonatomic) NSInteger ReplyCount;
  60. @property (assign,nonatomic) NSInteger UReadVerifyCount;
  61. @end
  62. @implementation ChatMsgListVC
  63. - (void)addTableRefresh
  64. {
  65. WS(weakSelf);
  66. self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
  67. [weakSelf headRefresh];
  68. }];
  69. }
  70. - (void)removeTableRefresh
  71. {
  72. [self.tableView.mj_header removeFromSuperview];
  73. }
  74. - (void)headRefresh{
  75. [self.chatListIMArray removeAllObjects];
  76. [self initChatMsgIMData];
  77. }
  78. - (UITableView *)tableView
  79. {
  80. if (!_tableView) {
  81. _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
  82. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  83. }
  84. return _tableView;
  85. }
  86. - (void)conversationListDidUpdate:(NSArray *)aConversationList
  87. {
  88. [self _loadAllConversationsFromDBWithIsShowHud:NO];
  89. }
  90. - (void)messagesDidReceive:(NSArray *)aMessages
  91. {
  92. [self headRefresh];
  93. }
  94. - (void)_reSortedConversationModelsAndReloadView
  95. {
  96. NSArray *sorted = [self.dataArray sortedArrayUsingComparator:^(EMConversationModel *obj1, EMConversationModel *obj2) {
  97. EMMessage *message1 = [obj1.emModel latestMessage];
  98. EMMessage *message2 = [obj2.emModel latestMessage];
  99. if(message1.timestamp > message2.timestamp) {
  100. return(NSComparisonResult)NSOrderedAscending;
  101. } else {
  102. return(NSComparisonResult)NSOrderedDescending;
  103. }}];
  104. NSMutableArray *conversationModels = [NSMutableArray array];
  105. for (EMConversationModel *model in sorted) {
  106. if (!model.emModel.latestMessage) {
  107. [EMClient.sharedClient.chatManager deleteConversation:model.emModel.conversationId
  108. isDeleteMessages:NO
  109. completion:nil];
  110. continue;
  111. }
  112. [conversationModels addObject:model];
  113. }
  114. [self.dataArray removeAllObjects];
  115. [self.dataArray addObjectsFromArray:conversationModels];
  116. [self replaceChatData];
  117. }
  118. - (void)_loadAllConversationsFromDBWithIsShowHud:(BOOL)aIsShowHUD
  119. {
  120. __weak typeof(self) weakself = self;
  121. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  122. NSArray *conversations = [[EMClient sharedClient].chatManager getAllConversations];
  123. NSArray *sorted = [conversations sortedArrayUsingComparator:^(EMConversation *obj1, EMConversation *obj2) {
  124. EMMessage *message1 = [obj1 latestMessage];
  125. EMMessage *message2 = [obj2 latestMessage];
  126. if(message1.timestamp > message2.timestamp) {
  127. return(NSComparisonResult)NSOrderedAscending;
  128. } else {
  129. return(NSComparisonResult)NSOrderedDescending;
  130. }}];
  131. [weakself.dataArray removeAllObjects];
  132. NSArray *models = [EMConversationHelper modelsFromEMConversations:sorted];
  133. [weakself.dataArray addObjectsFromArray:models];
  134. [weakself replaceChatData];
  135. });
  136. }
  137. #pragma mark - EMConversationsDelegate
  138. - (void)didConversationUnreadCountToZero:(EMConversationModel *)aConversation
  139. {
  140. [self replaceChatData];
  141. }
  142. - (void)didResortConversationsLatestMessage
  143. {
  144. [self _reSortedConversationModelsAndReloadView];
  145. }
  146. - (void)viewDidLoad {
  147. [super viewDidLoad];
  148. self.fd_prefersNavigationBarHidden = YES;
  149. self.view.backgroundColor = RGB(240, 239, 244);
  150. [[EMClient sharedClient].chatManager addDelegate:self delegateQueue:nil];
  151. [[EMClient sharedClient].groupManager addDelegate:self delegateQueue:nil];
  152. [[EMConversationHelper shared] addDelegate:self];
  153. self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  154. [self.view addSubview:self.tableView];
  155. [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
  156. make.edges.mas_offset(UIEdgeInsetsZero);
  157. }];
  158. self.tableView.delegate = self;
  159. self.tableView.dataSource = self;
  160. self.tableView.backgroundColor = [UIColor clearColor];
  161. [self headRefresh];
  162. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushTDGroupController:) name:PUSHTDGROUPCOTROLLER object:nil];
  163. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushEMChatViewController:) name:PUSHEMCHATVIEWCOTROLLER object:nil];
  164. // [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushSubViewController:) name:DRAWERPUSHVC object:nil];
  165. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(JPushToVC:) name:JPUSHSUCCESS object:nil];
  166. }
  167. - (void)pushEditVC
  168. {
  169. WS(weakSelf);
  170. ChatMsgEditVC * vc = [ChatMsgEditVC initChatMsgEditVC];
  171. vc.dataArray = self.chatListIMArray.mutableCopy;
  172. vc.upDataBlock = ^{
  173. [weakSelf headRefresh];
  174. };
  175. vc.hidesBottomBarWhenPushed = YES;
  176. [self.navigationController pushViewController:vc animated:NO];
  177. }
  178. #pragma mark - JPush
  179. - (void)JPushToVC:(NSNotification *)noti
  180. {
  181. PushModel * model = [PushModel modelWithDictionary:noti.userInfo];
  182. for (ChatMsgListImModel * cmodel in self.chatListIMArray) {
  183. if (cmodel.ImId == [model.ImId integerValue] && model.chatType == ChatType_SingleChat) {
  184. [self enterChatView:cmodel];
  185. }else{
  186. [self getGroupInfo:cmodel];
  187. }
  188. }
  189. }
  190. -(void)initChatMsgIMData{
  191. WS(weakSelf);
  192. NSString * url = [NSString stringWithFormat:@"%@0",Host(Often_Contact_List)];
  193. [[HttpManager sharedHttpManager] GETUrl:url parameters:@{} responseStyle:JOSN success:^(id _Nonnull responseObject) {
  194. NSLog(@"%@",responseObject);
  195. [weakSelf.chatListIMArray removeAllObjects];
  196. [weakSelf.tableView.mj_header endRefreshing];
  197. if ([responseObject[@"OftenContactInfos"] isKindOfClass:[NSArray class]]) {
  198. for (NSDictionary * dict in responseObject[@"OftenContactInfos"]) {
  199. ChatMsgListImModel * model = [ChatMsgListImModel modelWithDictionary:dict];
  200. [weakSelf.chatListIMArray addObject:model];
  201. }
  202. }
  203. weakSelf.ConfirmationDate = responseObject[@"ConfirmationDate"];
  204. weakSelf.ReplyDate = responseObject[@"ReplyDate"];
  205. weakSelf.ReplyCount = [responseObject[@"ReplyCount"] integerValue];
  206. weakSelf.UReadVerifyCount = [responseObject[@"UReadVerifyCount"] integerValue];
  207. [weakSelf _loadAllConversationsFromDBWithIsShowHud:NO];
  208. } failure:^(NSError * _Nonnull error) {
  209. NSLog(@"%@",error);
  210. }];
  211. }
  212. - (NSString *)isNullWithString:(id)value
  213. {
  214. if ([value isKindOfClass:[NSNull class]]) {
  215. return @"";
  216. }else{
  217. if ([value isKindOfClass:[NSString class]]) {
  218. return value;
  219. }else{
  220. return [value stringValue];
  221. }
  222. }
  223. }
  224. //- (BOOL)hidesBottomBarWhenPushed{
  225. // return NO;
  226. //}
  227. //- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(nonnull NSIndexPath *)indexPath
  228. //{
  229. // return YES;
  230. //}
  231. //#pragma mark - 左滑按钮 iOS8以上
  232. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_11_0
  233. - (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(tvos){// delete action
  234. WS(weakSelf);
  235. ChatMsgListImModel * model = weakSelf.chatListIMArray[indexPath.row];
  236. switch (model.AttributeValue) {
  237. case 2:
  238. {
  239. UIContextualAction *action1 = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:@"删除" handler:^(UIContextualAction * _Nonnull action,__kindof UIView * _Nonnull sourceView,void (^ _Nonnull completionHandler)(BOOL)) {
  240. completionHandler (YES);
  241. UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"确认删除" preferredStyle:(UIAlertControllerStyleAlert)];
  242. UIAlertAction * sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
  243. [weakSelf chatListDelete:@[model].copy];
  244. }];
  245. UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  246. }];
  247. [alert addAction:sureAction];
  248. [alert addAction:cancelAction];
  249. [weakSelf presentViewController:alert animated:YES completion:^{
  250. }];
  251. }];
  252. action1.backgroundColor = RGB(255, 59, 47);
  253. UIContextualAction *action2 = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:@"重命名" handler:^(UIContextualAction * _Nonnull action,__kindof UIView * _Nonnull sourceView,void (^ _Nonnull completionHandler)(BOOL)) {
  254. completionHandler (NO);
  255. [weakSelf workGroupReName:model];
  256. }];
  257. action2.backgroundColor = RGB(255, 149, 3);
  258. UIContextualAction *action3 = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:model.IsTop ? @"取消置顶" : @"置顶" handler:^(UIContextualAction * _Nonnull action,__kindof UIView * _Nonnull sourceView,void (^ _Nonnull completionHandler)(BOOL)) {
  259. completionHandler (YES);
  260. [weakSelf chatListIsTop:model];
  261. }];
  262. action3.backgroundColor = RGB(197, 201, 204);
  263. UISwipeActionsConfiguration *actions = [UISwipeActionsConfiguration configurationWithActions:@[action1, action2, action3]];
  264. actions.performsFirstActionWithFullSwipe = NO;
  265. return actions;
  266. }
  267. break;
  268. default:
  269. {
  270. UIContextualAction *action1 = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:@"删除" handler:^(UIContextualAction * _Nonnull action,__kindof UIView * _Nonnull sourceView,void (^ _Nonnull completionHandler)(BOOL)) {
  271. completionHandler (YES);
  272. UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"确认删除" preferredStyle:(UIAlertControllerStyleAlert)];
  273. UIAlertAction * sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
  274. [weakSelf chatListDelete:[NSMutableArray arrayWithArray:@[model]]];
  275. }];
  276. UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  277. }];
  278. [alert addAction:sureAction];
  279. [alert addAction:cancelAction];
  280. [weakSelf presentViewController:alert animated:YES completion:^{
  281. }];
  282. }];
  283. action1.backgroundColor = RGB(255, 59, 47);
  284. UIContextualAction *action2 = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:@"移动" handler:^(UIContextualAction * _Nonnull action,__kindof UIView * _Nonnull sourceView,void (^ _Nonnull completionHandler)(BOOL)) {
  285. completionHandler (YES);
  286. [weakSelf chatListMove:@[@(model.Id)]];
  287. }];
  288. action2.backgroundColor = RGB(61, 156, 248);
  289. UIContextualAction *action3 = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:model.IsTop ? @"取消置顶" : @"置顶" handler:^(UIContextualAction * _Nonnull action,__kindof UIView * _Nonnull sourceView,void (^ _Nonnull completionHandler)(BOOL)) {
  290. completionHandler (YES);
  291. [weakSelf chatListIsTop:model];
  292. }];
  293. action3.backgroundColor = RGB(197, 201, 204);
  294. UISwipeActionsConfiguration *actions = [UISwipeActionsConfiguration configurationWithActions:@[action1, action2, action3]];
  295. actions.performsFirstActionWithFullSwipe = NO;
  296. return actions;
  297. }
  298. break;
  299. }
  300. }
  301. #else
  302. - (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
  303. WS(weakSelf);
  304. ChatMsgListImModel * model = weakSelf.chatListIMArray[indexPath.row];
  305. switch (model.AttributeValue) {
  306. case 2:
  307. {
  308. UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
  309. UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"确认删除" preferredStyle:(UIAlertControllerStyleAlert)];
  310. UIAlertAction * sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
  311. [weakSelf chatListDelete:@[model].copy];
  312. }];
  313. UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  314. }];
  315. [alert addAction:sureAction];
  316. [alert addAction:cancelAction];
  317. [weakSelf presentViewController:alert animated:YES completion:^{
  318. }];
  319. }];
  320. action1.backgroundColor = RGB(255, 59, 47);
  321. UITableViewRowAction *action2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"重命名" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
  322. [weakSelf workGroupReName:model];
  323. }];
  324. action2.backgroundColor = RGB(255, 149, 3);
  325. UITableViewRowAction *action3 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:model.IsTop ? @"取消置顶" : @"置顶" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
  326. [weakSelf chatListIsTop:model];
  327. }];
  328. action3.backgroundColor = RGB(197, 201, 204);
  329. return @[action1, action2, action3];
  330. }
  331. break;
  332. default:
  333. {
  334. UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
  335. UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"确认删除" preferredStyle:(UIAlertControllerStyleAlert)];
  336. UIAlertAction * sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
  337. [weakSelf chatListDelete:[NSMutableArray arrayWithArray:@[model]]];
  338. }];
  339. UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  340. }];
  341. [alert addAction:sureAction];
  342. [alert addAction:cancelAction];
  343. [weakSelf presentViewController:alert animated:YES completion:^{
  344. }];
  345. }];
  346. action1.backgroundColor = RGB(255, 59, 47);
  347. UITableViewRowAction *action2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"移动" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
  348. [weakSelf chatListMove:@[@(model.Id)]];
  349. }];
  350. action2.backgroundColor = RGB(61, 156, 248);
  351. UITableViewRowAction *action3 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:model.IsTop ? @"取消置顶" : @"置顶" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
  352. [weakSelf chatListIsTop:model];
  353. }];
  354. action3.backgroundColor = RGB(197, 201, 204);
  355. return @[action1, action2, action3];
  356. }
  357. break;
  358. }
  359. }
  360. #endif
  361. - (void)workGroupReName:(ChatMsgListImModel *)model
  362. {
  363. WS(weakSelf);
  364. [[ShowNewGroupAlert initShowNewGroupAlertWithTitle:@"重命名" changeStr:model.FolderName confirm:^(NSString * _Nonnull groupName) {
  365. NSLog(@"%@",groupName);
  366. [[HttpManager sharedHttpManager] POSTUrl:Host(API_Find_ReName) parameters:@{@"FolderName":groupName,@"Id":@(model.Id)} responseStyle:DATA success:^(id _Nonnull responseObject) {
  367. [weakSelf initChatMsgIMData];
  368. } failure:^(NSError * _Nonnull error) {
  369. NSLog(@"%@",error);
  370. SHOWERROR([ZYCTool handerResultData:error])
  371. }];
  372. } cancle:^{
  373. }] show];
  374. }
  375. - (void)getUserInfoInDocument:(NSString *)ImId{
  376. NSMutableArray * array = [[NSMutableArray alloc] initWithArray:USERDEFAULTSGET(@"ChatList")];
  377. for (NSMutableDictionary * dict in array) {
  378. if ([[dict objectForKey:@"ImId"] isEqualToString:ImId]) {
  379. [array removeObject:dict];
  380. }
  381. }
  382. USERDEFAULTSSET(array,@"ChatList");
  383. }
  384. - (void)chatListDelete:(NSMutableArray *)array
  385. {
  386. NSMutableArray * addArray = [NSMutableArray array];
  387. for (ChatMsgListImModel * model in array) {
  388. [addArray addObject:@(model.Id)];
  389. [self getUserInfoInDocument:[NSString stringWithFormat:@"%ld",(long)model.ImId]];
  390. }
  391. SHOWLOADING
  392. WS(weakSelf);
  393. NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
  394. [dic setValue:addArray forKey:@"MiddleIds"];
  395. [dic setValue:@(10) forKey:@"FolderType"];
  396. [[HttpManager sharedHttpManager] POSTUrl:Host(API_Find_DeleteRecord) parameters:dic responseStyle:DATA success:^(id _Nonnull responseObject) {
  397. REMOVESHOW
  398. [weakSelf initChatMsgIMData];
  399. } failure:^(NSError * _Nonnull error) {
  400. REMOVESHOW
  401. NSLog(@"%@",error);
  402. SHOWERROR([ZYCTool handerResultData:error])
  403. }];
  404. }
  405. - (void)chatListMove:(NSArray *)array
  406. {
  407. ChatListMoveVC * vc = [ChatListMoveVC initChatListMoveVC];
  408. vc.Id = 0;
  409. vc.titleStr = @"移动到";
  410. vc.listArray = [NSMutableArray arrayWithArray:array];
  411. [self.navigationController pushViewController:vc animated:YES];
  412. }
  413. - (void)chatListIsTop:(ChatMsgListImModel *)model
  414. {
  415. WS(weakSelf);
  416. SHOWLOADING
  417. [[HttpManager sharedHttpManager] PUTUrl:[NSString stringWithFormat:@"%@%ld",Host(API_Find_Top),(long)model.Id] parameters:@{} responseStyle:DATA success:^(id _Nonnull responseObject) {
  418. REMOVESHOW
  419. [weakSelf reCreateTable];
  420. } failure:^(NSError * _Nonnull error) {
  421. REMOVESHOW
  422. SHOWERROR([ZYCTool handerResultData:error])
  423. }];
  424. }
  425. - (void)reCreateTable
  426. {
  427. [self.chatListIMArray removeAllObjects];
  428. // ChatMsgListImModel * amodel = [[ChatMsgListImModel alloc] init];
  429. // amodel.AttributeValue = 3;
  430. // amodel.Name = @"通讯录";
  431. // amodel.AvatarUrl = @"chat_phone_list";
  432. // [self.chatListIMArray addObject:amodel];
  433. // ChatMsgListImModel * bmodel = [[ChatMsgListImModel alloc] init];
  434. // bmodel.AttributeValue = 4;
  435. // bmodel.Name = @"验证信息";
  436. // bmodel.AvatarUrl = @"chat_yan_message";
  437. // [self.chatListIMArray addObject:bmodel];
  438. // ChatMsgListImModel * cmodel = [[ChatMsgListImModel alloc] init];
  439. // cmodel.AttributeValue = 5;
  440. // cmodel.Name = @"回复我的";
  441. // cmodel.AvatarUrl = @"chat_replay_me";
  442. // [self.chatListIMArray addObject:cmodel];
  443. WS(weakSelf);
  444. NSString * url = [NSString stringWithFormat:@"%@0",Host(Often_Contact_List)];
  445. [[HttpManager sharedHttpManager] GETUrl:url parameters:@{} responseStyle:JOSN success:^(id _Nonnull responseObject) {
  446. NSLog(@"%@",responseObject);
  447. [weakSelf.tableView.mj_header endRefreshing];
  448. if ([responseObject[@"OftenContactInfos"] isKindOfClass:[NSArray class]]) {
  449. for (NSDictionary * dict in responseObject[@"OftenContactInfos"]) {
  450. ChatMsgListImModel * model = [ChatMsgListImModel modelWithDictionary:dict];
  451. [weakSelf.chatListIMArray addObject:model];
  452. }
  453. }
  454. weakSelf.ConfirmationDate = responseObject[@"ConfirmationDate"];
  455. dispatch_async(dispatch_get_main_queue(), ^{
  456. [weakSelf.tableView removeFromSuperview];
  457. weakSelf.tableView = nil;
  458. [weakSelf.view addSubview:weakSelf.tableView];
  459. weakSelf.tableView.delegate = weakSelf;
  460. weakSelf.tableView.dataSource = weakSelf;
  461. weakSelf.tableView.backgroundColor = [UIColor clearColor];
  462. [weakSelf.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
  463. make.edges.mas_offset(UIEdgeInsetsZero);
  464. }];
  465. });
  466. [weakSelf _loadAllConversationsFromDBWithIsShowHud:NO];
  467. } failure:^(NSError * _Nonnull error) {
  468. NSLog(@"%@",error);
  469. }];
  470. }
  471. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  472. return 1;
  473. }
  474. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  475. return self.chatListIMArray.count;
  476. }
  477. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  478. ChatMsgListImModel * model = self.chatListIMArray[indexPath.row];
  479. switch (model.AttributeValue) {
  480. case 2:
  481. {
  482. return [ChatMsgListCell configCell2Height];
  483. }
  484. break;
  485. default:
  486. {
  487. return [ChatMsgListCell configCell3Height];
  488. }
  489. break;
  490. }
  491. }
  492. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  493. ChatMsgListImModel * model = [self.chatListIMArray objectAtIndex:indexPath.row];
  494. switch (model.AttributeValue) {
  495. case 2:
  496. {
  497. ChatMsgListCell *cell = [ChatMsgListCell configCell2:tableView indexPath:indexPath];
  498. cell.cell2IconImgView.image = IMG(@"find_1");
  499. cell.cell2TitleLabel.text = model.FolderName;
  500. cell.cell2isTop.hidden = !model.IsTop;
  501. cell.cell2TimeLabel.hidden = NO;
  502. cell.cell2TimeLabel.text = [NSDate getTimeStringAutoShort2:model.LastModifiedDate];
  503. cell.cell2TimeLabel.textColor = RGB(153, 153, 153);
  504. cell.enterBtn.hidden = YES;
  505. return cell;
  506. }
  507. break;
  508. default:
  509. {
  510. ChatMsgListCell *cell = [ChatMsgListCell configCell3:tableView indexPath:indexPath];
  511. cell.IsTopView.hidden = YES;
  512. [cell setCell3DataWithModel:model];
  513. return cell;
  514. }
  515. }
  516. }
  517. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  518. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  519. ChatMsgListImModel *model = [self.chatListIMArray objectAtIndex:indexPath.row];
  520. switch (model.AttributeValue) {
  521. case 2:
  522. {
  523. WorkingGroupMainVC *vc = [WorkingGroupMainVC initWorkingGroupMainVC];
  524. vc.GroupId = [NSString stringWithFormat:@"%ld",(long)model.Id];
  525. vc.GroupTitle = model.FolderName;
  526. vc.hidesBottomBarWhenPushed = YES;
  527. [self.navigationController pushViewController:vc animated:YES];
  528. }
  529. break;
  530. default:
  531. {
  532. //进入聊天界面
  533. if (model.ChatType == ChatType_SingleChat) {
  534. [self enterChatView:model];
  535. }else{
  536. [self getGroupInfo:model];
  537. }
  538. }
  539. break;
  540. }
  541. }
  542. #pragma mark -当环信收到新消息时,更新数据
  543. - (void)replaceChatData
  544. {
  545. WS(weakSelf);
  546. [self.chatListIMArray enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  547. ChatMsgListImModel * chatModel = (ChatMsgListImModel *)obj;
  548. [weakSelf.dataArray enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  549. EMConversationModel * model = (EMConversationModel *)obj;
  550. if (chatModel.ImId == [model.emModel.conversationId integerValue]) {
  551. EMConversation *conversation = model.emModel;
  552. if (!chatModel.IsDisturb) {
  553. chatModel.redNum = model.emModel.unreadMessagesCount;
  554. }
  555. chatModel.ChatText = [weakSelf _getDetailWithModel:conversation];
  556. }
  557. }];
  558. }];
  559. dispatch_async(dispatch_get_main_queue(), ^{
  560. [weakSelf.tableView reloadData];
  561. });
  562. }
  563. - (NSAttributedString *)_getDetailWithModel:(EMConversation *)aConversation
  564. {
  565. NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:@""];
  566. EMMessage *lastMessage = [aConversation latestMessage];
  567. if (!lastMessage) {
  568. return attributedStr;
  569. }
  570. NSString *latestMessageTitle = @"";
  571. EMMessageBody *messageBody = lastMessage.body;
  572. switch (messageBody.type) {
  573. case EMMessageBodyTypeText:
  574. {
  575. NSString *str = [EMEmojiHelper convertEmoji:((EMTextMessageBody *)messageBody).text];
  576. latestMessageTitle = str;
  577. }
  578. break;
  579. case EMMessageBodyTypeImage:
  580. latestMessageTitle = @"[图片]";
  581. break;
  582. case EMMessageBodyTypeVoice:
  583. latestMessageTitle = @"[音频]";
  584. break;
  585. case EMMessageBodyTypeLocation:
  586. latestMessageTitle = @"[位置]";
  587. break;
  588. case EMMessageBodyTypeVideo:
  589. latestMessageTitle = @"[视频]";
  590. break;
  591. case EMMessageBodyTypeFile:
  592. latestMessageTitle = @"[文件]";
  593. break;
  594. default:
  595. break;
  596. }
  597. NSDictionary *ext = aConversation.ext;
  598. if (ext && [ext[kConversation_IsRead] intValue] == kConversation_AtAll) {
  599. NSString *allMsg = @"[有全体消息]";
  600. latestMessageTitle = [NSString stringWithFormat:@"%@ %@", allMsg, latestMessageTitle];
  601. attributedStr = [[NSMutableAttributedString alloc] initWithString:latestMessageTitle];
  602. [attributedStr setAttributes:@{NSForegroundColorAttributeName : [UIColor colorWithRed:1.0 green:.0 blue:.0 alpha:0.5]} range:NSMakeRange(0, allMsg.length)];
  603. } else if (ext && [ext[kConversation_IsRead] intValue] == kConversation_AtYou) {
  604. NSString *atStr = @"[有人@我]";
  605. latestMessageTitle = [NSString stringWithFormat:@"%@ %@", atStr, latestMessageTitle];
  606. attributedStr = [[NSMutableAttributedString alloc] initWithString:latestMessageTitle];
  607. [attributedStr setAttributes:@{NSForegroundColorAttributeName : [UIColor colorWithRed:1.0 green:.0 blue:.0 alpha:0.5]} range:NSMakeRange(0, atStr.length)];
  608. } else {
  609. attributedStr = [[NSMutableAttributedString alloc] initWithString:latestMessageTitle];
  610. }
  611. return attributedStr;
  612. }
  613. - (BOOL)checkChatListArray
  614. {
  615. if (self.chatListIMArray.count == 0) {
  616. return NO;
  617. }
  618. for (ChatMsgListImModel * model in self.chatListIMArray) {
  619. if (!model.isSelected) {
  620. return NO;
  621. }
  622. }
  623. return YES;
  624. }
  625. - (void)enterChatView:(ChatMsgListImModel *)chatModel
  626. {
  627. EMConversation *conversation = [[EMClient sharedClient].chatManager getConversation:[NSString stringWithFormat:@"%ld",(long)chatModel.ImId] type: EMConversationTypeChat createIfNotExist:YES];
  628. EMConversationModel *model = [[EMConversationModel alloc] initWithEMModel:conversation];
  629. EMChatViewController *controller = [[EMChatViewController alloc] initWithCoversationModel:model];
  630. if (chatModel.ChatType == ChatType_SingleChat) {
  631. controller.toUserId = chatModel.ToUserId;
  632. controller.ReceiveIcon = chatModel.AvatarUrl;
  633. SelectModel * sModel = [[SelectModel alloc] init];
  634. sModel.UserId = chatModel.ToUserId;
  635. sModel.UserName = chatModel.Name;
  636. sModel.AvatarUrl = chatModel.AvatarUrl;
  637. controller.sModel = sModel;
  638. }
  639. model.name = chatModel.Name;
  640. controller.ImId = chatModel.ImId;
  641. controller.GroupId = chatModel.Id;
  642. controller.chatType = chatModel.ChatType;
  643. controller.listId = chatModel.Id;
  644. controller.isTop = chatModel.IsTop;
  645. controller.hidesBottomBarWhenPushed = YES;
  646. [self.navigationController pushViewController:controller animated:YES];
  647. }
  648. - (void)getGroupInfo:(ChatMsgListImModel *)chatModel
  649. {
  650. self.tableView.allowsSelection = NO;
  651. WS(weakSelf);
  652. [[HttpManager sharedHttpManager] POSTUrl:Host(Group_chat_info) parameters:@{@"GroupChatImId":@(chatModel.ImId)} responseStyle:JOSN success:^(id _Nonnull responseObject) {
  653. weakSelf.tableView.allowsSelection = YES;
  654. dispatch_async(dispatch_get_main_queue(), ^{
  655. EMConversation *conversation = [[EMClient sharedClient].chatManager getConversation:[NSString stringWithFormat:@"%ld",(long)chatModel.ImId] type: EMConversationTypeGroupChat createIfNotExist:YES];
  656. EMConversationModel *model = [[EMConversationModel alloc] initWithEMModel:conversation];
  657. EMChatViewController *controller = [[EMChatViewController alloc] initWithCoversationModel:model];
  658. controller.toUserId = chatModel.ImId;
  659. controller.groupUserInfoArray = responseObject[@"Item"];
  660. model.name = chatModel.Name;
  661. controller.GroupId = chatModel.Id;
  662. controller.ImId = chatModel.ImId;
  663. controller.chatType = chatModel.ChatType;
  664. controller.isTop = chatModel.IsTop;
  665. controller.hidesBottomBarWhenPushed = YES;
  666. [weakSelf.navigationController pushViewController:controller animated:YES];
  667. });
  668. } failure:^(NSError * _Nonnull error) {
  669. weakSelf.tableView.allowsSelection = YES;
  670. }];
  671. }
  672. -(void)isDel:(NSMutableArray *)array
  673. {
  674. WS(weakSelf);
  675. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"确认删除?" preferredStyle:UIAlertControllerStyleAlert];
  676. UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  677. }];
  678. UIAlertAction *ok = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  679. [weakSelf chatListDelete:array];
  680. }];
  681. [alertController addAction:cancel];
  682. [alertController addAction:ok];
  683. [self presentViewController:alertController animated:YES completion:nil];
  684. }
  685. -(NSMutableArray *)chatListIMArray{
  686. if(!_chatListIMArray){
  687. _chatListIMArray = [[NSMutableArray alloc] init];
  688. }
  689. return _chatListIMArray;
  690. }
  691. -(NSMutableArray *)selectArray{
  692. if(!_selectArray){
  693. _selectArray = [[NSMutableArray alloc] init];
  694. }
  695. return _selectArray;
  696. }
  697. -(NSMutableArray *)dataArray{
  698. if(!_dataArray){
  699. _dataArray = [[NSMutableArray alloc] init];
  700. }
  701. return _dataArray;
  702. }
  703. - (MyTDGroupView *)SearchView
  704. {
  705. if (!_SearchView) {
  706. _SearchView = [[MyTDGroupView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 36)];
  707. }
  708. return _SearchView;
  709. }
  710. - (void)getChatRecord{
  711. }
  712. #pragma mark - 接收通知跳转到群聊页面
  713. - (void)pushEMChatViewController:(NSNotification *)notification
  714. {
  715. NSDictionary * groupDict = notification.userInfo;
  716. NSString * groupId = [NSString stringWithFormat:@"%@",[groupDict objectForKey:@"GroupChatImId"]];
  717. WS(weakSelf);
  718. [[HttpManager sharedHttpManager] POSTUrl:Host(Group_chat_info) parameters:@{@"GroupChatImId":groupId} responseStyle:JOSN success:^(id _Nonnull responseObject) {
  719. EMConversation *conversation = [[EMClient sharedClient].chatManager getConversation:[NSString stringWithFormat:@"%@",groupId] type: EMConversationTypeGroupChat createIfNotExist:YES];
  720. EMConversationModel *model = [[EMConversationModel alloc] initWithEMModel:conversation];
  721. EMChatViewController *controller = [[EMChatViewController alloc] initWithCoversationModel:model];
  722. controller.toUserId = [[groupDict objectForKey:@"GroupChatImId"] integerValue];
  723. controller.groupUserInfoArray = responseObject[@"Item"];
  724. model.name = [groupDict objectForKey:@"Name"];
  725. controller.GroupId = [[groupDict objectForKey:@"Id"] integerValue];
  726. controller.chatType = ChatType_GroupChat;
  727. controller.hidesBottomBarWhenPushed = YES;
  728. [weakSelf.navigationController pushViewController:controller animated:YES];
  729. } failure:^(NSError * _Nonnull error) {
  730. }];
  731. }
  732. - (void)pushTDGroupController:(NSNotification *)notification
  733. {
  734. MyTDGroupViewController *vc = [[MyTDGroupViewController alloc] init];
  735. vc.isPush = YES;
  736. vc.pushDict = notification.userInfo;
  737. [self.navigationController pushViewController:vc animated:NO];
  738. }
  739. - (void)dealloc
  740. {
  741. [[EMClient sharedClient].chatManager removeDelegate:self];
  742. [[EMClient sharedClient].groupManager removeDelegate:self];
  743. [[EMConversationHelper shared] removeDelegate:self];
  744. [[NSNotificationCenter defaultCenter] removeObserver:self];
  745. }
  746. @end