ChatListStore.m 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  1. //
  2. // ChatListStore.m
  3. // AIIM
  4. //
  5. // Created by gan on 2025/4/1.
  6. //
  7. #import <Foundation/Foundation.h>
  8. #import "ChatListStore.h"
  9. #import "GDBManager.h"
  10. #import "ChatNetApi.h"
  11. #import "UserNetApi.h"
  12. #import "GroupNetApi.h"
  13. #import <Bugly/Bugly.h>
  14. #import "CryptoAES.h"
  15. #import "AppDelegate.h"
  16. @interface ChatListStore()
  17. @property(nonatomic, strong) NSArray *toparray;
  18. @property(nonatomic, strong) NSArray *nmarray;
  19. @end
  20. @implementation ChatListStore
  21. + (ChatListStore *_Nonnull)shareInstance{
  22. static id gShareInstance = nil;
  23. static dispatch_once_t onceToken;
  24. dispatch_once(&onceToken, ^{
  25. gShareInstance = [[self alloc] init];
  26. });
  27. return gShareInstance;
  28. }
  29. - (instancetype)init {
  30. if (self = [super init]) {
  31. }
  32. return self;
  33. }
  34. -(void)reloadData:(BOOL)mark{
  35. self.chatlist=@[];
  36. [GDBManager.shareInstance selectLocalchats:@"'true'" succ:^(NSArray * _Nullable array) {
  37. NSLog(@"array:%@",array);
  38. if(array.count>0){
  39. array = [self saveArray:array];
  40. NSMutableOrderedSet *orderedSet = [[NSMutableOrderedSet alloc] initWithArray:array];//去重,顺序不变
  41. NSArray *chats = [orderedSet array];
  42. chats=[self arraysort:chats];
  43. self.chatlist =chats;
  44. self.toparray =chats;
  45. }
  46. else{
  47. self.toparray = @[];
  48. }
  49. [self loadnmData:mark];
  50. } fail:^(NSString * _Nullable error) {
  51. [self loadnmData:mark];
  52. }];
  53. }
  54. -(void)loadnmData:(BOOL)mark{
  55. [GDBManager.shareInstance selectLocalchats:@"'false'" succ:^(NSArray * _Nullable array) {
  56. NSLog(@"array:%@",array);
  57. if(array.count>0){
  58. array = [self saveArray:array];
  59. NSMutableOrderedSet *orderedSet = [[NSMutableOrderedSet alloc] initWithArray:array];//去重,顺序不变
  60. NSArray *chats = [orderedSet array];
  61. chats=[self arraysort:chats];
  62. self.nmarray = chats;
  63. NSArray *reschats;
  64. if(self.chatlist.count>0){
  65. reschats=[self.chatlist arrayByAddingObjectsFromArray:chats];
  66. }
  67. else{
  68. reschats =chats;
  69. }
  70. self.chatlist=reschats;
  71. if(self.delegate){
  72. [self.delegate ChatListChange:reschats.copy];
  73. }
  74. NSInteger unreadCount = 0;
  75. for (NSDictionary *item in reschats) {
  76. NSString *unreadCountStr = item[@"unreadCount"];
  77. if(unreadCountStr.intValue>0){
  78. unreadCount = unreadCount + unreadCountStr.intValue;
  79. }
  80. }
  81. dispatch_main_async_safe(^{
  82. AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  83. [app updateBadgeCount:unreadCount];
  84. })
  85. }
  86. else{
  87. self.nmarray =@[];
  88. }
  89. if(mark){
  90. [self getnmChatlist];
  91. }
  92. } fail:^(NSString * _Nullable error) {
  93. if(mark){
  94. [self getnmChatlist];
  95. }
  96. }];
  97. }
  98. -(void)gettopChatlist{
  99. [ChatNetApi gettopchats:nil succ:^(int code, NSDictionary * res) {
  100. NSLog(@"top res:%@",res);
  101. self.toparray = res[@"data"];
  102. if(self.toparray.count>0){
  103. self.toparray = [self saveArray:self.toparray];
  104. self.toparray=[self arraysort:self.toparray];
  105. NSMutableOrderedSet *orderedSet = [[NSMutableOrderedSet alloc] initWithArray:self.toparray];//去重,顺序不变
  106. NSArray *chats = [orderedSet array];
  107. [self updatelcDB:chats top:@"true"];
  108. }
  109. [self getnmChatlist];
  110. } fail:^(NSError * _Nonnull error) {
  111. [self getnmChatlist];
  112. }];
  113. }
  114. -(void)getnmChatlist{
  115. NSDictionary *userinfo = [UDManager.shareInstance getDDManager:dkuserinfo];
  116. [ChatNetApi getchats:nil succ:^(int code, NSDictionary * res) {
  117. NSLog(@"getnmChatlist res:%@",res);
  118. NSArray *netArray = res[@"data"];
  119. [Bugly reportException:[NSException exceptionWithName:[NSString stringWithFormat:@"getnmChatlist:%@",userinfo[@"name"]] reason:[NSString stringWithFormat:@"netArray:%lu",(unsigned long)netArray.count] userInfo:nil]];
  120. if(netArray.count>0){
  121. netArray = [self saveArray:netArray];
  122. netArray=[self arraysort:netArray];
  123. NSMutableOrderedSet *orderedSet = [[NSMutableOrderedSet alloc] initWithArray:netArray];//去重,顺序不变
  124. NSArray *chats = [orderedSet array];
  125. [self doWithNewList:chats];
  126. // [self updatelcDB:chats top:@"false"];
  127. }
  128. else{
  129. [Bugly reportException:[NSException exceptionWithName:@"getchats--空" reason:userinfo[@"name"] userInfo:nil]];
  130. }
  131. //NSLog(@"self.chatlist:%@",self.chatlist);
  132. } fail:^(NSError * _Nonnull error) {
  133. [Bugly reportException:[NSException exceptionWithName:@"getchats--失败" reason:[NSString stringWithFormat:@"error:%@",error] userInfo:nil]];
  134. }];
  135. }
  136. -(void)updatelcDB:(NSArray *)array top:(NSString *)top{
  137. // NSLog(@"updatelcDB:%@",self.chatlist);
  138. BOOL hadChange = NO;
  139. for (NSDictionary *item in array) {
  140. // NSLog(@"updatelcDB item:%@",item);
  141. BOOL mark = true;
  142. NSString *newtime =item[@"lastTime"];
  143. if([newtime isKindOfClass:[NSNull class]]){
  144. continue;
  145. }
  146. NSString *userid =item[@"id"];
  147. if([userid isKindOfClass:[NSNull class]]){
  148. continue;
  149. }
  150. else{
  151. if(userid.length==0){
  152. continue;
  153. }
  154. }
  155. for (NSDictionary *Ditem in self.chatlist) {
  156. if([Ditem[@"id"] isEqualToString:item[@"id"]]){//窗口已经存在
  157. // NSLog(@"item:%@",Ditem);
  158. NSString *dtop = Ditem[@"top"];
  159. if([top isEqualToString:dtop]){
  160. NSString *dtime =Ditem[@"lastTime"];
  161. if(![dtime isKindOfClass:[NSNull class]]){
  162. if(newtime.longLongValue<dtime.longLongValue){
  163. mark=false;
  164. }
  165. }
  166. }
  167. break;
  168. }
  169. }
  170. if(mark){
  171. // NSLog(@"mark:1");
  172. NSDictionary *chatD=@{
  173. @"id":item[@"id"],
  174. @"name":item[@"name"],
  175. @"avatar":item[@"avatar"],
  176. @"type":item[@"type"],
  177. @"lastMessage":item[@"lastMessage"]?:@"",
  178. @"lastTime":item[@"lastTime"]?:@"",
  179. @"unreadCount":item[@"unreadCount"],
  180. @"loaded":item[@"loaded"]?:@"",
  181. @"loading":item[@"loading"]?:@"",
  182. @"top":top
  183. };
  184. [self updateChat:chatD isreload:false];
  185. hadChange = YES;
  186. }
  187. // NSLog(@"mark:2");
  188. }
  189. if(hadChange){
  190. // 使用GCD创建一个在3秒后执行的定时器
  191. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  192. // NSLog(@"Timer fired using GCD!");
  193. [self reloadData:false];
  194. });
  195. }
  196. }
  197. -(void)reciveMsg:(NSDictionary *_Nonnull)msg{
  198. // NSLog(@"reciveMsg------:%@",msg);
  199. NSInteger addcount = 1;
  200. if([msg[@"chatId"] isEqual:self.chatId]){
  201. addcount=0;
  202. }
  203. NSString *lastMsg = [self setLastMsg:msg];
  204. NSString *messageType = msg[@"messageType"];
  205. for (NSDictionary *item in self.chatlist) {
  206. //NSLog(@"item:%@",item);
  207. if([msg[@"chatId"] isEqual:item[@"id"]]){//窗口已经存在
  208. NSString *newtime =msg[@"timestamp"];
  209. NSString *dtime =item[@"lastTime"];
  210. NSString *count = item[@"unreadCount"];
  211. if(addcount==0){
  212. }
  213. else{
  214. addcount = addcount+count.intValue;
  215. }
  216. NSString *countstr = [NSString stringWithFormat:@"%ld",(long)addcount];
  217. if([messageType isEqualToString:@"11"]||[messageType isEqualToString:@"12"]){
  218. if(newtime.longLongValue>=dtime.longLongValue){
  219. NSDictionary *chatD=@{
  220. @"id":item[@"id"],
  221. @"name":item[@"name"],
  222. @"avatar":item[@"avatar"],
  223. @"type":item[@"type"],
  224. @"lastMessage":lastMsg,
  225. @"lastTime":msg[@"timestamp"],
  226. @"unreadCount":countstr,
  227. @"loaded":item[@"loaded"],
  228. @"loading":item[@"loading"],
  229. @"top":item[@"top"]?:@"false"
  230. };
  231. [self updateChat:chatD isreload:YES];
  232. [self updataUserInfo:chatD];
  233. }
  234. }
  235. else{
  236. if(newtime.longLongValue>dtime.longLongValue){
  237. NSDictionary *chatD=@{
  238. @"id":item[@"id"],
  239. @"name":item[@"name"],
  240. @"avatar":item[@"avatar"],
  241. @"type":item[@"type"],
  242. @"lastMessage":lastMsg,
  243. @"lastTime":msg[@"timestamp"],
  244. @"unreadCount":countstr,
  245. @"loaded":item[@"loaded"],
  246. @"loading":item[@"loading"],
  247. @"top":item[@"top"]?:@"false"
  248. };
  249. [self updateChat:chatD isreload:YES];
  250. [self updataUserInfo:chatD];
  251. }
  252. }
  253. return;
  254. }
  255. }
  256. [self addNewChats:msg];
  257. }
  258. -(void)updateChat:(NSDictionary *)chat isreload:(BOOL)reload{
  259. chat = [self encodingMsg:chat];
  260. // NSLog(@"1----------updateChat--------------");
  261. NSDictionary *userinfo = [UDManager.shareInstance getDDManager:dkuserinfo];
  262. [GDBManager.shareInstance insertLocalchat:chat succ:^(NSArray * _Nullable array) {
  263. NSLog(@"111chat");
  264. if(self.delegate &&reload){
  265. [self reloadData:false];
  266. }
  267. } fail:^(NSString * _Nullable error) {
  268. NSLog(@"111222chat");
  269. [Bugly reportException:[NSException exceptionWithName:[NSString stringWithFormat:@"insertLocalchat fail:%@",userinfo[@"name"]] reason:[NSString stringWithFormat:@"error:%@",chat] userInfo:nil]];
  270. }];
  271. }
  272. -(void)addNewChats:(NSDictionary *_Nonnull)msg{//新增聊天窗
  273. NSInteger addcount = 0;
  274. if([msg[@"chatId"] isEqual:self.chatId]){
  275. addcount=0;
  276. }
  277. else{
  278. addcount=1;
  279. }
  280. NSString *countstr = [NSString stringWithFormat:@"%ld",(long)addcount];
  281. NSString *lastMsg = [self setLastMsg:msg];
  282. if([msg[@"type"] isEqualToString:@"0"]){//友聊
  283. [UserNetApi getUserinfo_id:msg[@"chatId"] succ:^(int code, NSDictionary * res) {
  284. NSDictionary * userinfo=res[@"data"];
  285. if(userinfo){
  286. NSDictionary *chatD=@{
  287. @"id":msg[@"chatId"],
  288. @"name":userinfo[@"name"],
  289. @"avatar":userinfo[@"avatar"],
  290. @"type":msg[@"type"],
  291. @"lastMessage":lastMsg,
  292. @"lastTime":msg[@"timestamp"],
  293. @"unreadCount":countstr,
  294. @"loaded":@"false",
  295. @"loading":@"false",
  296. @"top":@"false"
  297. };
  298. [self updateChat:chatD isreload:YES];
  299. }
  300. } fail:^(NSError * _Nonnull error) {
  301. NSDictionary *chatD=@{
  302. @"id":msg[@"chatId"],
  303. @"name":@"loading...",
  304. @"avatar":@"avatar",
  305. @"type":msg[@"type"],
  306. @"lastMessage":lastMsg,
  307. @"lastTime":msg[@"timestamp"],
  308. @"unreadCount":countstr,
  309. @"loaded":@"false",
  310. @"loading":@"false",
  311. @"top":@"false"
  312. };
  313. [self updateChat:chatD isreload:YES];
  314. }];
  315. }else{//群聊
  316. NSLog(@"addNewChats----:%@",msg);
  317. [GroupNetApi getGroupInfo:msg[@"chatId"] succ:^(int code, NSDictionary * res) {
  318. NSLog(@"getGroupInfo:%@",res);
  319. NSDictionary * userinfo=res[@"data"];
  320. if(userinfo){
  321. NSDictionary *chatD=@{
  322. @"id":msg[@"chatId"],
  323. @"name":userinfo[@"name"],
  324. @"avatar":userinfo[@"avatar"],
  325. @"type":msg[@"type"],
  326. @"lastMessage":lastMsg,
  327. @"lastTime":msg[@"timestamp"],
  328. @"unreadCount":countstr,
  329. @"loaded":@"false",
  330. @"loading":@"false",
  331. @"top":@"false"
  332. };
  333. [self updateChat:chatD isreload:YES];
  334. }
  335. else{
  336. }
  337. } fail:^(NSError * _Nonnull error) {
  338. NSDictionary *chatD=@{
  339. @"id":msg[@"chatId"],
  340. @"name":@"loading...",
  341. @"avatar":@"avatar",
  342. @"type":msg[@"type"],
  343. @"lastMessage":lastMsg,
  344. @"lastTime":msg[@"timestamp"],
  345. @"unreadCount":countstr,
  346. @"loaded":@"false",
  347. @"loading":@"false",
  348. @"top":@"false"
  349. };
  350. [self updateChat:chatD isreload:YES];
  351. }];
  352. }
  353. }
  354. -(void)updataUserInfo:(NSDictionary *)chatDis{
  355. NSString *type = chatDis[@"type"];
  356. NSString *chatId = chatDis[@"id"];
  357. NSString *oldName = chatDis[@"name"];
  358. NSString *oldavatar = chatDis[@"avatar"];
  359. if([type isEqualToString:@"0"]){//友聊
  360. [UserNetApi getUserinfo_id:chatId succ:^(int code, NSDictionary * res) {
  361. NSDictionary * userinfo=res[@"data"];
  362. if(userinfo){
  363. if([oldName isEqualToString:userinfo[@"name"]]&&[oldavatar isEqualToString:userinfo[@"avatar"]]){
  364. return;
  365. }else{
  366. NSMutableDictionary *mdic = [chatDis mutableCopy];
  367. [mdic setObject:userinfo[@"name"] forKey:@"name"];
  368. [mdic setObject:userinfo[@"avatar"] forKey:@"avatar"];
  369. [self updateChat:mdic isreload:YES];
  370. }
  371. }
  372. } fail:^(NSError * _Nonnull error) {
  373. }];
  374. }
  375. else{
  376. [GroupNetApi getGroupInfo:chatId succ:^(int code, NSDictionary * res) {
  377. NSLog(@"getGroupInfo:%@",res);
  378. NSDictionary * userinfo=res[@"data"];
  379. if(userinfo){
  380. if([oldName isEqualToString:userinfo[@"name"]]&&[oldavatar isEqualToString:userinfo[@"avatar"]]){
  381. return;
  382. }else{
  383. NSMutableDictionary *mdic = [chatDis mutableCopy];
  384. [mdic setObject:userinfo[@"name"] forKey:@"name"];
  385. [mdic setObject:userinfo[@"avatar"] forKey:@"avatar"];
  386. [self updateChat:mdic isreload:YES];
  387. }
  388. }
  389. else{
  390. }
  391. } fail:^(NSError * _Nonnull error) {
  392. }];
  393. }
  394. }
  395. - (void)deleteChat:(NSDictionary *_Nonnull)chat{
  396. weakSelf(self);
  397. [GDBManager.shareInstance deleteChatListItem:chat succ:^(NSArray * _Nullable array) {
  398. // [GDBManager.shareInstance deleteLocalMessageTable:chat[@"id"]];
  399. NSMutableArray * tempArray = [NSMutableArray arrayWithArray:weakself.chatlist];
  400. [tempArray removeObject:chat];
  401. weakself.chatlist = tempArray;
  402. } fail:^(NSString * _Nullable error) {
  403. NSLog(@"error:%@",error);
  404. }];
  405. [GDBManager.shareInstance deletechatLocalmsg:chat[@"id"]];
  406. }
  407. -(NSString *)setLastMsg:(NSDictionary *)msg{
  408. NSString *content = msg[@"content"]?:@"";
  409. NSString *magType=msg[@"messageType"];
  410. // NSLog(@"setLastMsg:%@",msg);
  411. if(magType.intValue==0){
  412. return content;
  413. }
  414. // if(magType.intValue==1){
  415. // return @"【图片】";
  416. // }
  417. // if(magType.intValue==2){
  418. // return @"【文件】";
  419. // }
  420. // if(magType.intValue==3){
  421. // return @"【语音】";
  422. // }
  423. // if(magType.intValue==4){
  424. // return @"【撤回】";
  425. // }
  426. // if(magType.intValue==5){
  427. // return @"【转发】";
  428. // }
  429. if(magType.intValue==7){
  430. BOOL video = msg[@"video"];
  431. if(video){
  432. return @"【视频通话】";
  433. }
  434. return @"【语音通话】";
  435. }
  436. return content;
  437. }
  438. -(void)chatDidOpen:(NSDictionary *_Nonnull)chatmsg{
  439. BOOL notHadChat=YES;
  440. NSString *chatid = chatmsg[@"chatId"];
  441. for (NSDictionary *item in self.chatlist) {
  442. if([chatid isEqualToString:item[@"id"]]){//窗口已经存在
  443. NSLog(@"窗口已经存在:%@",item);
  444. notHadChat=NO;
  445. NSString *countstr = @"0";
  446. NSDictionary *chatD=@{
  447. @"id":item[@"id"],
  448. @"name":item[@"name"],
  449. @"avatar":item[@"avatar"],
  450. @"type":item[@"type"],
  451. @"lastMessage":item[@"lastMessage"],
  452. @"lastTime":item[@"lastTime"],
  453. @"unreadCount":countstr,
  454. @"loaded":item[@"loaded"],
  455. @"loading":item[@"loading"],
  456. @"top":item[@"top"]?:@"false"
  457. };
  458. [self updateChat:chatD isreload:YES];
  459. return;
  460. }
  461. }
  462. if(notHadChat){//还没有窗口,创建一个新的窗口
  463. [self tryToCreateChate:chatmsg];
  464. }
  465. }
  466. -(void)tryToCreateChate:(NSDictionary *_Nonnull)chatmsg{
  467. NSDictionary *userinfo = [UDManager.shareInstance getDDManager:dkuserinfo];
  468. [Bugly reportException:[NSException exceptionWithName:@"tryToCreateChate" reason:userinfo[@"name"] userInfo:nil]];
  469. NSDate *now = [NSDate date];
  470. NSTimeInterval trt = [now timeIntervalSince1970];
  471. NSInteger time = trt*1000;
  472. NSString *strtime = [NSString stringWithFormat:@"%ld",(long)time];
  473. NSDictionary *chatD=@{
  474. @"id":chatmsg[@"chatId"],
  475. @"name":chatmsg[@"name"],
  476. @"avatar":chatmsg[@"avatar"],
  477. @"type":chatmsg[@"type"],
  478. @"lastMessage":@"",
  479. @"lastTime":strtime,
  480. @"unreadCount":@"0",
  481. @"loaded":@"",
  482. @"loading":@"",
  483. @"top":@"false"
  484. };
  485. [self updateChat:chatD isreload:YES];
  486. }
  487. - (void)topChat:(NSDictionary *_Nonnull)chat{
  488. [self updatelcDB:@[chat] top:@"true"];
  489. }
  490. - (void)cancelTopChat:(NSDictionary *_Nonnull)chat{
  491. [self updatelcDB:@[chat] top:@"false"];
  492. }
  493. -(NSArray *)arraysort:(NSArray *)array{
  494. NSArray *sortedArray = [array sortedArrayUsingComparator:^NSComparisonResult(NSDictionary *obj1, NSDictionary *obj2) {
  495. NSString *newtime =obj1[@"lastTime"];
  496. NSString *dtime =obj2[@"lastTime"];
  497. // NSLog(@"newtime:%@",newtime);
  498. // NSLog(@"dtime:%@",[dtime class]);
  499. if([newtime isKindOfClass:[NSNull class]]){
  500. return NSOrderedDescending;
  501. }
  502. if([dtime isKindOfClass:[NSNull class]]){
  503. return NSOrderedAscending;
  504. }
  505. if(newtime.longLongValue<dtime.longLongValue){
  506. return NSOrderedDescending;
  507. }
  508. else if(newtime.longLongValue==dtime.longLongValue){
  509. return NSOrderedSame;
  510. }
  511. else{
  512. return NSOrderedAscending;
  513. }
  514. }];
  515. return sortedArray;
  516. }
  517. -(NSArray *)saveArray:(NSArray *)array{
  518. NSMutableArray *marray = [NSMutableArray new];
  519. for (NSDictionary * item in array) {
  520. NSString *itid = item[@"id"];
  521. if([itid isKindOfClass:[NSNull class]]){
  522. continue;
  523. }
  524. if([itid isEqualToString:@""]){
  525. continue;
  526. }
  527. [marray addObject:item];
  528. }
  529. NSArray *immutableArray = [marray copy];
  530. return immutableArray;
  531. }
  532. //avatar = "https://api.my-im.com/profile/upload/avatar.png";
  533. //id = 1934462258529837058;
  534. //lastMessage = "";
  535. //lastTime = 0;
  536. //loaded = "";
  537. //loading = "";
  538. //name = "IM\U5f00\U53d1\U7ec4";
  539. //top = true;
  540. //type = 1;
  541. //unreadCount = 0;
  542. -(void)doWithNewList:(NSArray *)array{
  543. NSMutableArray *topArray =[self.toparray mutableCopy];
  544. NSMutableArray *tempArray =[NSMutableArray new];
  545. NSMutableArray *nmArray =[self.nmarray mutableCopy];
  546. BOOL mark=YES;
  547. if(topArray.count>0){
  548. for (NSDictionary *item in array) {
  549. NSDictionary * safeDict = [self replaceNullsWithEmptyStringInDictionary:item];
  550. mark=YES;
  551. NSString *newtime =safeDict[@"lastTime"];
  552. if (newtime.intValue==0) {
  553. continue;
  554. }
  555. for(NSDictionary *oitem in self.toparray){
  556. if([safeDict[@"id"] isEqualToString:oitem[@"id"]]){
  557. mark=NO;
  558. NSString *dtime =oitem[@"lastTime"];
  559. if(newtime.longLongValue>=dtime.longLongValue){
  560. NSDictionary *chatD=@{
  561. @"id":oitem[@"id"],
  562. @"name":oitem[@"name"]?:@"",
  563. @"avatar":oitem[@"avatar"]?:@"",
  564. @"type":oitem[@"type"]?:@"",
  565. @"lastMessage":safeDict[@"lastMessage"]?:@"",
  566. @"lastTime":safeDict[@"lastTime"]?:@"",
  567. @"unreadCount":safeDict[@"unreadCount"]?:@"",
  568. @"loaded":@"",
  569. @"loading":@"",
  570. @"top":oitem[@"top"]?:@"false",
  571. };
  572. NSLog(@"chatD:%@",chatD);
  573. chatD = [self encodingMsg:chatD];
  574. NSInteger index = [self.toparray indexOfObject:oitem];
  575. [topArray replaceObjectAtIndex:index withObject:chatD];
  576. [self updateToDBonly:chatD];
  577. }
  578. }
  579. }
  580. if(mark){
  581. [tempArray addObject:item];
  582. }
  583. }
  584. }
  585. else{
  586. tempArray=[array mutableCopy];
  587. }
  588. self.toparray = [topArray copy];
  589. if(tempArray.count>0){
  590. if(nmArray.count>0){
  591. for (NSDictionary *item in tempArray) {
  592. NSDictionary * safeDict = [self replaceNullsWithEmptyStringInDictionary:item];
  593. mark=YES;
  594. NSString *newtime =safeDict[@"lastTime"];
  595. NSLog(@"000000000");
  596. if (newtime.intValue==0) {
  597. continue;
  598. }
  599. for(NSDictionary *oitem in self.nmarray){
  600. if([safeDict[@"id"] isEqualToString:oitem[@"id"]]){
  601. NSLog(@"1111111111");
  602. mark=NO;
  603. NSString *dtime =oitem[@"lastTime"];
  604. if(newtime.longLongValue>=dtime.longLongValue){
  605. NSLog(@"2222222");
  606. NSDictionary *chatD=@{
  607. @"id":oitem[@"id"],
  608. @"name":oitem[@"name"]?:@"",
  609. @"avatar":oitem[@"avatar"]?:@"",
  610. @"type":oitem[@"type"]?:@"",
  611. @"lastMessage":safeDict[@"lastMessage"]?:@"",
  612. @"lastTime":safeDict[@"lastTime"]?:@"",
  613. @"unreadCount":safeDict[@"unreadCount"]?:@"",
  614. @"loaded":@"",
  615. @"loading":@"",
  616. @"top":oitem[@"top"]?:@"false",
  617. };
  618. NSLog(@"chatD:%@",chatD);
  619. chatD = [self encodingMsg:chatD];
  620. NSInteger index = [self.nmarray indexOfObject:oitem];
  621. [nmArray replaceObjectAtIndex:index withObject:chatD];
  622. [self updateToDBonly:chatD];
  623. }
  624. }
  625. }
  626. if(mark){
  627. NSDictionary *chatD=@{
  628. @"id":safeDict[@"id"],
  629. @"name":safeDict[@"name"]?:@"",
  630. @"avatar":safeDict[@"avatar"]?:@"",
  631. @"type":safeDict[@"type"]?:@"",
  632. @"lastMessage":safeDict[@"lastMessage"]?:@"",
  633. @"lastTime":safeDict[@"lastTime"]?:@"",
  634. @"unreadCount":safeDict[@"unreadCount"]?:@"",
  635. @"loaded":@"",
  636. @"loading":@"",
  637. @"top":@"false",
  638. };
  639. chatD = [self encodingMsg:chatD];
  640. [nmArray addObject:chatD];
  641. [self updateToDBonly:chatD];
  642. }
  643. }
  644. }
  645. else{
  646. NSArray *arr= [self replaceNullsWithEmptyStringInArray:tempArray];
  647. // nmArray=[arr mutableCopy];
  648. for (NSDictionary *item in arr) {
  649. NSLog(@"3333333");
  650. NSString *lastT =item[@"lastTime"];
  651. if(lastT.intValue==0){
  652. continue;
  653. }
  654. NSDictionary *chatD=@{
  655. @"id":item[@"id"],
  656. @"name":item[@"name"]?:@"",
  657. @"avatar":item[@"avatar"]?:@"",
  658. @"type":item[@"type"]?:@"",
  659. @"lastMessage":item[@"lastMessage"]?:@"",
  660. @"lastTime":item[@"lastTime"]?:@"",
  661. @"unreadCount":item[@"unreadCount"]?:@"",
  662. @"loaded":@"",
  663. @"loading":@"",
  664. @"top":@"false",
  665. };
  666. chatD = [self encodingMsg:chatD];
  667. [nmArray addObject:chatD.copy];
  668. [self updateToDBonly:chatD];
  669. }
  670. }
  671. }
  672. else{
  673. }
  674. self.nmarray = [nmArray copy];
  675. [self postdataToView];
  676. }
  677. -(void)updateToDBonly:(NSDictionary *)chat{
  678. chat = [self encodingMsg:chat];
  679. [GDBManager.shareInstance insertLocalchat:chat succ:^(NSArray * _Nullable array) {
  680. } fail:^(NSString * _Nullable error) {
  681. ;
  682. }];
  683. }
  684. -(void)postdataToView{
  685. NSMutableOrderedSet *toporderedSet = [[NSMutableOrderedSet alloc] initWithArray:self.toparray];//去重,顺序不变
  686. NSArray *topchats = [toporderedSet array];
  687. topchats=[self arraysort:topchats];
  688. NSMutableOrderedSet *orderedSet = [[NSMutableOrderedSet alloc] initWithArray:self.nmarray];//去重,顺序不变
  689. NSArray *chats = [orderedSet array];
  690. chats=[self arraysort:chats];
  691. NSArray *reschats;
  692. if(topchats.count>0){
  693. reschats=[topchats arrayByAddingObjectsFromArray:chats];
  694. }
  695. else{
  696. reschats =chats;
  697. }
  698. self.chatlist =reschats.copy;
  699. if(self.delegate){
  700. [self.delegate ChatListChange:reschats.copy];
  701. [Bugly reportException:[NSException exceptionWithName:@"postdataToView--1" reason:[NSString stringWithFormat:@"reschats:%lu",(unsigned long)reschats.count] userInfo:nil]];
  702. }
  703. else{
  704. [Bugly reportException:[NSException exceptionWithName:@"postdataToView--2" reason:[NSString stringWithFormat:@"reschats:%lu",(unsigned long)reschats.count] userInfo:nil]];
  705. }
  706. }
  707. // 递归替换字典中的 NSNull 为 @""
  708. - (NSDictionary *)replaceNullsWithEmptyStringInDictionary:(NSDictionary *)dictionary {
  709. NSMutableDictionary *mutableDict = [NSMutableDictionary dictionaryWithDictionary:dictionary];
  710. for (id key in [mutableDict allKeys]) {
  711. id value = [mutableDict objectForKey:key];
  712. if ([value isKindOfClass:[NSNull class]]) {
  713. [mutableDict setObject:@"" forKey:key];
  714. } else if ([value isKindOfClass:[NSDictionary class]]) {
  715. // 递归处理子字典
  716. [mutableDict setObject:[self replaceNullsWithEmptyStringInDictionary:value] forKey:key];
  717. } else if ([value isKindOfClass:[NSArray class]]) {
  718. // 递归处理数组
  719. [mutableDict setObject:[self replaceNullsWithEmptyStringInArray:value] forKey:key];
  720. }
  721. }
  722. return [NSDictionary dictionaryWithDictionary:mutableDict];
  723. }
  724. // 递归替换数组中的 NSNull 为 @""
  725. - (NSArray *)replaceNullsWithEmptyStringInArray:(NSArray *)array {
  726. NSMutableArray *mutableArray = [NSMutableArray arrayWithArray:array];
  727. for (NSInteger i = 0; i < [mutableArray count]; i++) {
  728. id value = [mutableArray objectAtIndex:i];
  729. if ([value isKindOfClass:[NSNull class]]) {
  730. [mutableArray replaceObjectAtIndex:i withObject:@""];
  731. } else if ([value isKindOfClass:[NSDictionary class]]) {
  732. // 递归处理子字典
  733. [mutableArray replaceObjectAtIndex:i withObject:[self replaceNullsWithEmptyStringInDictionary:value]];
  734. } else if ([value isKindOfClass:[NSArray class]]) {
  735. // 递归处理子数组
  736. [mutableArray replaceObjectAtIndex:i withObject:[self replaceNullsWithEmptyStringInArray:value]];
  737. }
  738. }
  739. return [NSArray arrayWithArray:mutableArray];
  740. }
  741. //信息解密存储
  742. -(NSDictionary *)encodingMsg:(NSDictionary *)msg{
  743. NSMutableDictionary *temp = [msg mutableCopy];
  744. NSString *content = msg[@"lastMessage"];
  745. content = [CryptoAES.shareInstance decryptDataL:content?: @""];
  746. [temp setObject:content forKey:@"lastMessage"];
  747. return [temp copy];
  748. }
  749. @end