ChatsStore.m 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. //
  2. // ChatsStore.m
  3. // AIIM
  4. //
  5. // Created by gan on 2025/4/1.
  6. //
  7. #import <Foundation/Foundation.h>
  8. #import "ChatsStore.h"
  9. #import "GDBManager.h"
  10. #import "ChatNetApi.h"
  11. #import "GroupNetApi.h"
  12. #import "UDManager.h"
  13. #import "GWebSocket.h"
  14. #import "ChatListStore.h"
  15. #import "CryptoAES.h"
  16. @implementation ChatsStore
  17. + (ChatsStore *_Nonnull)shareInstance{
  18. static id gShareInstance = nil;
  19. static dispatch_once_t onceToken;
  20. dispatch_once(&onceToken, ^{
  21. gShareInstance = [[self alloc] init];
  22. });
  23. return gShareInstance;
  24. }
  25. - (instancetype)init {
  26. if (self = [super init]) {
  27. self.msgList = [[NSMutableArray alloc] init];
  28. }
  29. return self;
  30. }
  31. -(void)reciveMsg:(NSDictionary *_Nonnull)msg{
  32. msg = [self encodingMsg:msg];
  33. NSString *mine = msg[@"mine"];
  34. if(mine.boolValue){
  35. [self deleteMineTmpMsg:msg];
  36. }
  37. [GDBManager.shareInstance insertRplaceLocalmsg:msg succ:^(NSArray * _Nullable array) {
  38. //插入数据库成功
  39. if([self.chatId isEqualToString:msg[@"chatId"]]){
  40. // NSLog(@"----------------------2");
  41. if(self.delegate){
  42. [self.msgList addObject:msg];
  43. NSLog(@"----------------------3:%lu",(unsigned long)self.msgList.count);
  44. [self.delegate ChatsChange:self.msgList type:1];
  45. }
  46. }
  47. NSString *m = msg[@"mine"];
  48. if(!m.boolValue){
  49. [[GWebSocket shareInstance] sendRecNote:msg];
  50. }
  51. else{
  52. // [[GWebSocket shareInstance] sendRecNote:msg];
  53. }
  54. } fail:^(NSString * _Nullable error) {
  55. //插入数据库成功
  56. }];
  57. }
  58. -(void)addMg:(NSArray *_Nonnull)array chatId:(NSString *)chatId{
  59. // NSLog(@"addMg:%@",array);
  60. NSArray *newArray = [self deletebadeData:array];
  61. // NSLog(@"addMg:%@",newArray);
  62. [GDBManager.shareInstance batchInsertLocalMessages:newArray success:^(NSArray * _Nullable resultArray) {
  63. [self reloadData:chatId];
  64. for (NSDictionary * dict in array) {
  65. [[GWebSocket shareInstance] sendRecNote:dict];
  66. }
  67. } failure:^(NSString * _Nullable error) {
  68. NSLog(@"%@",error);
  69. [self reloadData:chatId];
  70. }];
  71. }
  72. -(void)addQunNextMg:(NSArray *_Nonnull)array chatId:(NSString *)chatId timestp:(NSInteger)timestp{
  73. NSArray *newArray = [self deletebadeData:array];
  74. [GDBManager.shareInstance batchInsertLocalMessages:newArray success:^(NSArray * _Nullable resultArray) {
  75. [self loadNextData:chatId timestp:timestp];
  76. for (NSDictionary * dict in array) {
  77. [[GWebSocket shareInstance] sendRecNote:dict];
  78. }
  79. } failure:^(NSString * _Nullable error) {
  80. NSLog(@"%@",error);
  81. }];
  82. }
  83. -(void)deleteMineTmpMsg:(NSDictionary *_Nonnull)msg{
  84. // NSLog(@"deleteMineTmpMsg:%@",msg);
  85. NSString *localtime =msg[@"localtime"];
  86. if([localtime isKindOfClass:[NSString class]]){
  87. NSMutableArray * temp = self.msgList.copy;
  88. for (NSDictionary *dis in temp) {
  89. //NSString *oldtimestamp = dis[@"localtime"];
  90. NSString *timestamp = dis[@"timestamp"];
  91. if([localtime isEqualToString:timestamp]){
  92. [self.msgList removeObjectAtIndex:[temp indexOfObject:dis]];
  93. NSLog(@"----------------------12");
  94. break;
  95. }
  96. }
  97. [GDBManager.shareInstance deleteMyLocalmsg:msg];
  98. }
  99. }
  100. -(void)reloadData:(NSString *_Nonnull)chatId type:(NSInteger)type{
  101. // NSLog(@"chatId:%@,%ld",chatId,(long)type);
  102. [self reloadData:chatId];
  103. _reloadC=0;
  104. [self loadOutLineMsg:chatId type:type];
  105. }
  106. -(void)loadOutLineMsg:(NSString *_Nonnull)chatId type:(NSInteger)type{
  107. [ChatNetApi getOutLineMsg:chatId type:type succ:^(int code, NSDictionary *res) {
  108. self->_reloadC=4;
  109. NSArray *array = res[@"data"];
  110. // NSLog(@"getOutLineMsg2 res:%lu",(unsigned long)array.count);
  111. if(array.count>0){
  112. NSMutableArray *tempArray = [[NSMutableArray alloc] init];
  113. for (NSDictionary *msg in array) {
  114. NSDictionary *enmsg = [self encodingMsg:msg];
  115. NSMutableDictionary *mutablemsg = [enmsg mutableCopy];
  116. if([self.userid isEqual:mutablemsg[@"fromId"]]){
  117. [mutablemsg setObject:[NSNumber numberWithBool:YES] forKey:@"mine"];
  118. }else{
  119. if([@"0" isEqual:enmsg[@"type"]]){//友聊
  120. [mutablemsg setObject:enmsg[@"fromId"] forKey:@"chatId"];
  121. // NSLog(@"-----1-------友聊:%@",mutablemsg);
  122. }
  123. }
  124. [tempArray addObject:mutablemsg];
  125. [ChatListStore.shareInstance reciveMsg:(NSDictionary *)mutablemsg];
  126. }
  127. NSLog(@"chatId:%@,%ld",chatId,(long)type);
  128. [self addMg:tempArray chatId:chatId];
  129. }
  130. else{
  131. [self reloadData:chatId];
  132. }
  133. } fail:^(NSError * _Nonnull error) {
  134. // [MBProgressHUD showWithText:@"网络错误"];
  135. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  136. if(self->_reloadC<3){
  137. self->_reloadC=self->_reloadC+1;
  138. [self loadOutLineMsg:chatId type:type];
  139. }
  140. });
  141. }];
  142. }
  143. -(void)reloadQunData:(NSString *_Nonnull)chatId type:(NSInteger)type{
  144. NSDate *now = [NSDate date];
  145. NSTimeInterval trt = [now timeIntervalSince1970];
  146. NSInteger time = trt*1000;
  147. NSLog(@"getqunOutLineMsg----");
  148. [ChatNetApi getqunOutLineMsg:chatId timestamp:time size:16 succ:^(int code, NSDictionary * res) {
  149. // NSLog(@"getqunOutLineMsg res:%@",res);
  150. NSArray *array = res[@"data"];
  151. NSLog(@"getqunOutLineMsg res:%lu",(unsigned long)array.count);
  152. if(array.count>0){
  153. NSMutableArray *tempArray = [[NSMutableArray alloc] init];
  154. for (NSDictionary *msg in array) {
  155. NSDictionary *enmsg = [self encodingMsg:msg];
  156. NSMutableDictionary *mutablemsg = [enmsg mutableCopy];
  157. if([self.userid isEqual:mutablemsg[@"fromId"]]){
  158. [mutablemsg setObject:[NSNumber numberWithBool:YES] forKey:@"mine"];
  159. }
  160. [tempArray addObject:mutablemsg];
  161. [ChatListStore.shareInstance reciveMsg:(NSDictionary *)mutablemsg];
  162. }
  163. [self addMg:tempArray chatId:chatId];
  164. }
  165. else{
  166. [self reloadData:chatId];
  167. }
  168. } fail:^(NSError * _Nonnull error) {
  169. }];
  170. }
  171. -(void)reloadData:(NSString *_Nonnull)chatId{
  172. [GDBManager.shareInstance selectLocalmsg:chatId page:1 succ:^(NSArray * _Nullable array) {
  173. NSLog(@"array------:%@",chatId);
  174. if([self.chatId isEqualToString:chatId]){
  175. if(self.delegate){
  176. NSMutableOrderedSet *orderedSet = [[NSMutableOrderedSet alloc] initWithArray:array];//去重,顺序不变
  177. NSArray *msgs = [orderedSet array];
  178. msgs=[self arraysort:msgs];
  179. [self.msgList removeAllObjects];
  180. [self.msgList addObjectsFromArray:msgs];
  181. [self.delegate ChatsChange:self.msgList type:1];
  182. }
  183. }
  184. // NSMutableOrderedSet *orderedSet = [[NSMutableOrderedSet alloc] initWithArray:array];//去重,顺序不变
  185. // NSArray *msgs = [orderedSet array];
  186. // msgs=[self arraysort:msgs];
  187. // [self.msgList removeAllObjects];
  188. // [self.msgList addObjectsFromArray:msgs];
  189. // if(self.delegate){
  190. // [self.delegate ChatsChange:self.msgList];
  191. // }
  192. } fail:^(NSString * _Nullable error) {
  193. }];
  194. }
  195. -(void)loadNextData:(NSString *)chatId timestp:(NSInteger)timestp{
  196. [GDBManager.shareInstance selectnextmsg:chatId timestp:timestp succ:^(NSArray * _Nullable array) {
  197. // NSLog(@"loadNextData:%lu",(unsigned long)[array count]);
  198. if([array count]>0){
  199. NSLog(@"loadNextData1:%lu",(unsigned long)[array count]);
  200. if(self.msgList.count>0){
  201. NSArray *newArray = [array arrayByAddingObjectsFromArray:self.msgList];
  202. newArray=[self arraysort:newArray];
  203. self.msgList = [newArray mutableCopy]; // 注意这里使用了 mutableCopy 来转换回 NSMutableArray
  204. }
  205. else{
  206. array=[self arraysort:array];
  207. self.msgList = [array mutableCopy]; // 注意这里使用了 mutableCopy 来转换回 NSMutableArray
  208. }
  209. if([self.chatId isEqualToString:chatId]){
  210. if(self.delegate){
  211. [self.delegate ChatsChange:self.msgList type:2];
  212. }
  213. }
  214. }
  215. } fail:^(NSString * _Nullable error) {
  216. ;
  217. }];
  218. }
  219. -(void)loadQunNextData:(NSString *)chatId timestp:(NSInteger)timestp{
  220. [ChatNetApi getqunOutLineMsg:chatId timestamp:timestp size:16 succ:^(int code, NSDictionary * res) {
  221. // NSLog(@"loadQunNextData res:%@",res);
  222. NSArray *array = res[@"data"];
  223. NSLog(@"getqunOutLinNextData res:%lu",(unsigned long)array.count);
  224. if(array.count>0){
  225. NSMutableArray *tempArray = [[NSMutableArray alloc] init];
  226. for (NSDictionary *msg in array) {
  227. NSMutableDictionary *mutablemsg = [msg mutableCopy];
  228. if([self.userid isEqual:mutablemsg[@"fromId"]]){
  229. [mutablemsg setObject:[NSNumber numberWithBool:YES] forKey:@"mine"];
  230. }
  231. [tempArray addObject:mutablemsg];
  232. }
  233. [self addQunNextMg:tempArray chatId:chatId timestp:timestp];
  234. }
  235. else{
  236. [self loadNextData:chatId timestp:timestp];
  237. }
  238. } fail:^(NSError * _Nonnull error) {
  239. [self loadNextData:chatId timestp:timestp];
  240. }];
  241. }
  242. -(void)getGroupUserList:(NSString *_Nonnull)chatId{
  243. [self reloadData:chatId];//先加载本地
  244. // NSLog(@"getqunOutLineMsg------------");
  245. [GroupNetApi getGroupUserList:chatId succ:^(int code, NSDictionary *res) {
  246. // NSLog(@"getqunOutLineMsgres:%@",res);
  247. self.groupUserList = res[@"data"];
  248. // [self reloadData:self.chatId type:1];
  249. [self reloadQunData:chatId type:1];
  250. } fail:^(NSError * _Nonnull error) {
  251. NSLog(@"getqunOutLineMsgerrer");
  252. }];
  253. }
  254. //获取群组历史记录 先获取群成员列表
  255. -(void)getGroupHistory:(NSString *_Nonnull)groupId type:(NSString *_Nullable)type{
  256. if([type isEqualToString:@""]){
  257. [GroupNetApi getGroupUserList:groupId succ:^(int code, NSDictionary *res) {
  258. //NSLog(@"res:%@",res);
  259. self.groupUserList = res[@"data"];
  260. [self reloadData:self.chatId];
  261. } fail:^(NSError * _Nonnull error) {
  262. NSLog(@"errer");
  263. }];
  264. }
  265. else{
  266. [self reloadDataWithType:groupId type:type];
  267. }
  268. }
  269. //按消息分类查询本消息
  270. -(void)reloadDataWithType:(NSString *_Nonnull)chatId type:(NSString *)type{
  271. [GDBManager.shareInstance selectLocalmsgType:chatId messageType:type succ:^(NSArray * _Nullable array) {
  272. //NSLog(@"array:%@",array);
  273. if([self.chatId isEqualToString:chatId]){
  274. if(self.delegate){
  275. NSArray *msgs=[self arraysort:array];
  276. [self.msgList removeAllObjects];
  277. [self.msgList addObjectsFromArray:msgs];
  278. [self.delegate ChatsChange:self.msgList type:1];
  279. }
  280. }
  281. // NSArray *msgs=[self arraysort:array];
  282. // [self.msgList removeAllObjects];
  283. // [self.msgList addObjectsFromArray:msgs];
  284. // if(self.delegate){
  285. // [self.delegate ChatsChange:self.msgList];
  286. // }
  287. } fail:^(NSString * _Nullable error) {
  288. }];
  289. }
  290. //查询本地所有类型消息下一页
  291. -(void)loadNextDataAll:(NSString *)chatId timestp:(NSInteger)timestp{
  292. [GDBManager.shareInstance selectnextmsg:chatId timestp:timestp succ:^(NSArray * _Nullable array) {
  293. // NSLog(@"loadNextData:%lu",(unsigned long)[array count]);
  294. if([self.chatId isEqualToString:chatId]){
  295. if(self.msgList.count>0){
  296. NSArray *newArray = [array arrayByAddingObjectsFromArray:self.msgList];
  297. newArray=[self arraysort:newArray];
  298. self.msgList = [newArray mutableCopy]; // 注意这里使用了 mutableCopy 来转换回 NSMutableArray
  299. }
  300. else{
  301. array=[self arraysort:array];
  302. self.msgList = [array mutableCopy]; // 注意这里使用了 mutableCopy 来转换回 NSMutableArray
  303. }
  304. if(self.delegate){
  305. [self.delegate ChatsChange:self.msgList type:2];
  306. }
  307. }
  308. } fail:^(NSString * _Nullable error) {
  309. ;
  310. }];
  311. }
  312. //安消息分类查询本地消息下一页
  313. -(void)loadNextDataWithType:(NSString *_Nonnull)chatId type:(NSString *_Nullable)type timestp:(NSInteger)timestp{
  314. [GDBManager.shareInstance selectNextmsgType:chatId messageType:type timestp:timestp succ:^(NSArray * _Nullable array) {
  315. if([self.chatId isEqualToString:chatId]){
  316. if(self.msgList.count>0){
  317. NSArray *newArray = [array arrayByAddingObjectsFromArray:self.msgList];
  318. newArray=[self arraysort:newArray];
  319. self.msgList = [newArray mutableCopy]; // 注意这里使用了 mutableCopy 来转换回 NSMutableArray
  320. }
  321. else{
  322. array=[self arraysort:array];
  323. self.msgList = [array mutableCopy]; // 注意这里使用了 mutableCopy 来转换回 NSMutableArray
  324. }
  325. if(self.delegate){
  326. [self.delegate ChatsChange:self.msgList type:2];
  327. }
  328. }
  329. } fail:^(NSString * _Nullable error) {
  330. if([self.chatId isEqualToString:chatId]){
  331. if(self.delegate){
  332. [self.delegate ChatsChange:self.msgList type:2];
  333. }
  334. }
  335. }];
  336. }
  337. //删除废数据
  338. -(NSArray *)deletebadeData:(NSArray *)array{
  339. NSMutableArray *marray = [NSMutableArray new];
  340. NSInteger index = 0;
  341. for (NSDictionary *msgA in array) {
  342. BOOL msrk = true;
  343. NSString *idA=msgA[@"id"];
  344. index++;
  345. if(index==array.count){
  346. [marray addObject:msgA];
  347. break;
  348. }
  349. if([idA isKindOfClass:[NSNull class]]){
  350. msrk=false;
  351. }
  352. else{
  353. for (NSInteger i = index;i<array.count;i++) {
  354. NSDictionary *msgB = [array objectAtIndex:i];
  355. NSString *idB=msgB[@"id"];
  356. if([idA isEqualToString:idB]){
  357. NSDictionary * extendB = [msgB[@"extend"] isKindOfClass:NSDictionary.class]?msgB[@"extend"]:@{};
  358. NSString *urlB = extendB[@"url"] ?: @"";
  359. if(urlB.length>0){
  360. msrk=false;
  361. }
  362. break;
  363. }
  364. }
  365. }
  366. if(msrk){
  367. [marray addObject:msgA];
  368. }
  369. }
  370. if(marray.count>0){
  371. return marray;
  372. }
  373. else{
  374. return array;
  375. }
  376. }
  377. //替换指定数据数据
  378. -(NSArray *_Nullable)replaydisData:(NSDictionary *_Nullable)dis inarray:(NSArray *_Nullable)array{
  379. NSMutableArray *marray = [NSMutableArray new];
  380. NSString *locatTime = dis[@"localtime"];
  381. for (NSDictionary *msgA in array) {
  382. if(![locatTime isEqualToString:msgA[@"localtime"]]){
  383. [marray addObject:msgA];
  384. }
  385. else{
  386. [marray addObject:dis];
  387. }
  388. }
  389. return [marray copy];
  390. }
  391. -(NSArray *)arraysort:(NSArray *)array{
  392. NSArray *sortedArray = [array sortedArrayUsingComparator:^NSComparisonResult(NSDictionary *obj1, NSDictionary *obj2) {
  393. NSString *newtime =obj1[@"timestamp"];
  394. NSString *dtime =obj2[@"timestamp"];
  395. // NSLog(@"newtime:%@",newtime);
  396. // NSLog(@"dtime:%@",[dtime class]);
  397. if([newtime isKindOfClass:[NSNull class]]){
  398. return NSOrderedAscending;
  399. }
  400. if([dtime isKindOfClass:[NSNull class]]){
  401. return NSOrderedDescending;
  402. }
  403. if(newtime.longLongValue<dtime.longLongValue){
  404. return NSOrderedAscending;
  405. }
  406. else if(newtime.longLongValue==dtime.longLongValue){
  407. return NSOrderedSame;
  408. }
  409. else{
  410. return NSOrderedDescending;
  411. }
  412. }];
  413. return sortedArray;
  414. }
  415. -(void)updatereadTime:(NSDictionary *)dis{
  416. if(self.delegate){
  417. NSString *nstr = dis[@"userId"];
  418. NSInteger time = [dis[@"timestamp"] longLongValue];
  419. if([self.chatId isEqualToString:nstr]){
  420. self.lastreadTime = time;
  421. if(self.delegate){
  422. [self.delegate ChatsChange:self.msgList type:3];
  423. }
  424. }
  425. }
  426. [GDBManager.shareInstance insertLastreadtime:dis succ:^(NSArray * _Nullable array) {
  427. } fail:^(NSString * _Nullable error) {
  428. }];
  429. }
  430. -(NSDictionary *)getchatReadTime:(NSString *)chatId{
  431. [GDBManager.shareInstance selectchatLastreadtime:chatId succ:^(NSArray * _Nullable array) {
  432. NSLog(@"getchatReadTime:%@",chatId);
  433. NSLog(@"getchatReadTime:%@",array);
  434. if(array.count>0){
  435. NSDictionary *dic = array[0];
  436. NSString *temp =dic[@"timestamp"];
  437. self.lastreadTime = temp.integerValue;
  438. }
  439. } fail:^(NSString * _Nullable error) {
  440. }];
  441. return nil;
  442. }
  443. //消息列表中删除消息操作
  444. -(void)deleteMyLocalMsg:(NSDictionary *_Nonnull)msg{
  445. [GDBManager.shareInstance deleteLocalmsg:msg];
  446. NSString *localtime =msg[@"localtime"];
  447. NSString * msgId = msg[@"id"];
  448. NSMutableArray * temp = self.msgList.copy;
  449. for (NSDictionary *dis in temp) {
  450. NSString *timestamp = dis[@"localtime"];
  451. NSString *tempMsgId = dis[@"id"];
  452. if (temp.lastObject == dis) {
  453. NSMutableDictionary * deleteMsg = [NSMutableDictionary dictionaryWithDictionary:msg];
  454. [deleteMsg setValue:@"【已删除】" forKey:@"content"];
  455. [ChatListStore.shareInstance reciveMsg:deleteMsg];
  456. }
  457. if([localtime isEqual:timestamp] && [msgId isEqualToString:tempMsgId]){
  458. [self.msgList removeObjectAtIndex:[temp indexOfObject:dis]];
  459. break;
  460. }
  461. }
  462. if(self.delegate){
  463. [self.delegate ChatsChange:self.msgList type:3];
  464. }
  465. }
  466. //信息解密存储
  467. -(NSDictionary *)encodingMsg:(NSDictionary *)msg{
  468. NSMutableDictionary *temp = [msg mutableCopy];
  469. NSString *content = msg[@"content"];
  470. content = [CryptoAES.shareInstance decryptDataL:content?: @""];
  471. [temp setObject:content forKey:@"content"];
  472. return [temp copy];
  473. }
  474. @end