ChatListStore.m 29 KB

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