ChatsStore.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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. [ChatNetApi getOutLineMsg:chatId type:type succ:^(int code, NSDictionary *res) {
  104. NSArray *array = res[@"data"];
  105. // NSLog(@"getOutLineMsg2 res:%lu",(unsigned long)array.count);
  106. if(array.count>0){
  107. NSMutableArray *tempArray = [[NSMutableArray alloc] init];
  108. for (NSDictionary *msg in array) {
  109. NSDictionary *enmsg = [self encodingMsg:msg];
  110. NSMutableDictionary *mutablemsg = [enmsg mutableCopy];
  111. if([self.userid isEqual:mutablemsg[@"fromId"]]){
  112. [mutablemsg setObject:[NSNumber numberWithBool:YES] forKey:@"mine"];
  113. }else{
  114. if([@"0" isEqual:enmsg[@"type"]]){//友聊
  115. [mutablemsg setObject:enmsg[@"fromId"] forKey:@"chatId"];
  116. // NSLog(@"-----1-------友聊:%@",mutablemsg);
  117. }
  118. }
  119. [tempArray addObject:mutablemsg];
  120. [ChatListStore.shareInstance reciveMsg:(NSDictionary *)mutablemsg];
  121. }
  122. NSLog(@"chatId:%@,%ld",chatId,(long)type);
  123. [self addMg:tempArray chatId:chatId];
  124. }
  125. else{
  126. [self reloadData:chatId];
  127. }
  128. } fail:^(NSError * _Nonnull error) {
  129. ;
  130. }];
  131. }
  132. -(void)reloadQunData:(NSString *_Nonnull)chatId type:(NSInteger)type{
  133. NSDate *now = [NSDate date];
  134. NSTimeInterval trt = [now timeIntervalSince1970];
  135. NSInteger time = trt*1000;
  136. NSLog(@"getqunOutLineMsg----");
  137. [ChatNetApi getqunOutLineMsg:chatId timestamp:time size:16 succ:^(int code, NSDictionary * res) {
  138. // NSLog(@"getqunOutLineMsg res:%@",res);
  139. NSArray *array = res[@"data"];
  140. NSLog(@"getqunOutLineMsg res:%lu",(unsigned long)array.count);
  141. if(array.count>0){
  142. NSMutableArray *tempArray = [[NSMutableArray alloc] init];
  143. for (NSDictionary *msg in array) {
  144. NSDictionary *enmsg = [self encodingMsg:msg];
  145. NSMutableDictionary *mutablemsg = [enmsg mutableCopy];
  146. if([self.userid isEqual:mutablemsg[@"fromId"]]){
  147. [mutablemsg setObject:[NSNumber numberWithBool:YES] forKey:@"mine"];
  148. }
  149. [tempArray addObject:mutablemsg];
  150. [ChatListStore.shareInstance reciveMsg:(NSDictionary *)mutablemsg];
  151. }
  152. [self addMg:tempArray chatId:chatId];
  153. }
  154. else{
  155. [self reloadData:chatId];
  156. }
  157. } fail:^(NSError * _Nonnull error) {
  158. }];
  159. }
  160. -(void)reloadData:(NSString *_Nonnull)chatId{
  161. [GDBManager.shareInstance selectLocalmsg:chatId page:1 succ:^(NSArray * _Nullable array) {
  162. NSLog(@"array------:%@",chatId);
  163. if([self.chatId isEqualToString:chatId]){
  164. if(self.delegate){
  165. NSMutableOrderedSet *orderedSet = [[NSMutableOrderedSet alloc] initWithArray:array];//去重,顺序不变
  166. NSArray *msgs = [orderedSet array];
  167. msgs=[self arraysort:msgs];
  168. [self.msgList removeAllObjects];
  169. [self.msgList addObjectsFromArray:msgs];
  170. [self.delegate ChatsChange:self.msgList type:1];
  171. }
  172. }
  173. // NSMutableOrderedSet *orderedSet = [[NSMutableOrderedSet alloc] initWithArray:array];//去重,顺序不变
  174. // NSArray *msgs = [orderedSet array];
  175. // msgs=[self arraysort:msgs];
  176. // [self.msgList removeAllObjects];
  177. // [self.msgList addObjectsFromArray:msgs];
  178. // if(self.delegate){
  179. // [self.delegate ChatsChange:self.msgList];
  180. // }
  181. } fail:^(NSString * _Nullable error) {
  182. }];
  183. }
  184. -(void)loadNextData:(NSString *)chatId timestp:(NSInteger)timestp{
  185. [GDBManager.shareInstance selectnextmsg:chatId timestp:timestp succ:^(NSArray * _Nullable array) {
  186. // NSLog(@"loadNextData:%lu",(unsigned long)[array count]);
  187. if([array count]>0){
  188. NSLog(@"loadNextData1:%lu",(unsigned long)[array count]);
  189. if(self.msgList.count>0){
  190. NSArray *newArray = [array arrayByAddingObjectsFromArray:self.msgList];
  191. newArray=[self arraysort:newArray];
  192. self.msgList = [newArray mutableCopy]; // 注意这里使用了 mutableCopy 来转换回 NSMutableArray
  193. }
  194. else{
  195. array=[self arraysort:array];
  196. self.msgList = [array mutableCopy]; // 注意这里使用了 mutableCopy 来转换回 NSMutableArray
  197. }
  198. if([self.chatId isEqualToString:chatId]){
  199. if(self.delegate){
  200. [self.delegate ChatsChange:self.msgList type:2];
  201. }
  202. }
  203. }
  204. } fail:^(NSString * _Nullable error) {
  205. ;
  206. }];
  207. }
  208. -(void)loadQunNextData:(NSString *)chatId timestp:(NSInteger)timestp{
  209. [ChatNetApi getqunOutLineMsg:chatId timestamp:timestp size:16 succ:^(int code, NSDictionary * res) {
  210. // NSLog(@"loadQunNextData res:%@",res);
  211. NSArray *array = res[@"data"];
  212. NSLog(@"getqunOutLinNextData res:%lu",(unsigned long)array.count);
  213. if(array.count>0){
  214. NSMutableArray *tempArray = [[NSMutableArray alloc] init];
  215. for (NSDictionary *msg in array) {
  216. NSMutableDictionary *mutablemsg = [msg mutableCopy];
  217. if([self.userid isEqual:mutablemsg[@"fromId"]]){
  218. [mutablemsg setObject:[NSNumber numberWithBool:YES] forKey:@"mine"];
  219. }
  220. [tempArray addObject:mutablemsg];
  221. }
  222. [self addQunNextMg:tempArray chatId:chatId timestp:timestp];
  223. }
  224. else{
  225. [self loadNextData:chatId timestp:timestp];
  226. }
  227. } fail:^(NSError * _Nonnull error) {
  228. [self loadNextData:chatId timestp:timestp];
  229. }];
  230. }
  231. -(void)getGroupUserList:(NSString *_Nonnull)chatId{
  232. [self reloadData:chatId];//先加载本地
  233. // NSLog(@"getqunOutLineMsg------------");
  234. [GroupNetApi getGroupUserList:chatId succ:^(int code, NSDictionary *res) {
  235. // NSLog(@"getqunOutLineMsgres:%@",res);
  236. self.groupUserList = res[@"data"];
  237. // [self reloadData:self.chatId type:1];
  238. [self reloadQunData:chatId type:1];
  239. } fail:^(NSError * _Nonnull error) {
  240. NSLog(@"getqunOutLineMsgerrer");
  241. }];
  242. }
  243. //获取群组历史记录 先获取群成员列表
  244. -(void)getGroupHistory:(NSString *_Nonnull)groupId type:(NSString *_Nullable)type{
  245. if([type isEqualToString:@""]){
  246. [GroupNetApi getGroupUserList:groupId succ:^(int code, NSDictionary *res) {
  247. //NSLog(@"res:%@",res);
  248. self.groupUserList = res[@"data"];
  249. [self reloadData:self.chatId];
  250. } fail:^(NSError * _Nonnull error) {
  251. NSLog(@"errer");
  252. }];
  253. }
  254. else{
  255. [self reloadDataWithType:groupId type:type];
  256. }
  257. }
  258. //按消息分类查询本消息
  259. -(void)reloadDataWithType:(NSString *_Nonnull)chatId type:(NSString *)type{
  260. [GDBManager.shareInstance selectLocalmsgType:chatId messageType:type succ:^(NSArray * _Nullable array) {
  261. //NSLog(@"array:%@",array);
  262. if([self.chatId isEqualToString:chatId]){
  263. if(self.delegate){
  264. NSArray *msgs=[self arraysort:array];
  265. [self.msgList removeAllObjects];
  266. [self.msgList addObjectsFromArray:msgs];
  267. [self.delegate ChatsChange:self.msgList type:1];
  268. }
  269. }
  270. // NSArray *msgs=[self arraysort:array];
  271. // [self.msgList removeAllObjects];
  272. // [self.msgList addObjectsFromArray:msgs];
  273. // if(self.delegate){
  274. // [self.delegate ChatsChange:self.msgList];
  275. // }
  276. } fail:^(NSString * _Nullable error) {
  277. }];
  278. }
  279. //查询本地所有类型消息下一页
  280. -(void)loadNextDataAll:(NSString *)chatId timestp:(NSInteger)timestp{
  281. [GDBManager.shareInstance selectnextmsg:chatId timestp:timestp succ:^(NSArray * _Nullable array) {
  282. // NSLog(@"loadNextData:%lu",(unsigned long)[array count]);
  283. if([self.chatId isEqualToString:chatId]){
  284. if(self.msgList.count>0){
  285. NSArray *newArray = [array arrayByAddingObjectsFromArray:self.msgList];
  286. newArray=[self arraysort:newArray];
  287. self.msgList = [newArray mutableCopy]; // 注意这里使用了 mutableCopy 来转换回 NSMutableArray
  288. }
  289. else{
  290. array=[self arraysort:array];
  291. self.msgList = [array mutableCopy]; // 注意这里使用了 mutableCopy 来转换回 NSMutableArray
  292. }
  293. if(self.delegate){
  294. [self.delegate ChatsChange:self.msgList type:2];
  295. }
  296. }
  297. } fail:^(NSString * _Nullable error) {
  298. ;
  299. }];
  300. }
  301. //安消息分类查询本地消息下一页
  302. -(void)loadNextDataWithType:(NSString *_Nonnull)chatId type:(NSString *_Nullable)type timestp:(NSInteger)timestp{
  303. [GDBManager.shareInstance selectNextmsgType:chatId messageType:type timestp:timestp succ:^(NSArray * _Nullable array) {
  304. if([self.chatId isEqualToString:chatId]){
  305. if(self.msgList.count>0){
  306. NSArray *newArray = [array arrayByAddingObjectsFromArray:self.msgList];
  307. newArray=[self arraysort:newArray];
  308. self.msgList = [newArray mutableCopy]; // 注意这里使用了 mutableCopy 来转换回 NSMutableArray
  309. }
  310. else{
  311. array=[self arraysort:array];
  312. self.msgList = [array mutableCopy]; // 注意这里使用了 mutableCopy 来转换回 NSMutableArray
  313. }
  314. if(self.delegate){
  315. [self.delegate ChatsChange:self.msgList type:2];
  316. }
  317. }
  318. } fail:^(NSString * _Nullable error) {
  319. if([self.chatId isEqualToString:chatId]){
  320. if(self.delegate){
  321. [self.delegate ChatsChange:self.msgList type:2];
  322. }
  323. }
  324. }];
  325. }
  326. //删除废数据
  327. -(NSArray *)deletebadeData:(NSArray *)array{
  328. NSMutableArray *marray = [NSMutableArray new];
  329. NSInteger index = 0;
  330. for (NSDictionary *msgA in array) {
  331. BOOL msrk = true;
  332. NSString *idA=msgA[@"id"];
  333. index++;
  334. if(index==array.count){
  335. [marray addObject:msgA];
  336. break;
  337. }
  338. if([idA isKindOfClass:[NSNull class]]){
  339. msrk=false;
  340. }
  341. else{
  342. for (NSInteger i = index;i<array.count;i++) {
  343. NSDictionary *msgB = [array objectAtIndex:i];
  344. NSString *idB=msgB[@"id"];
  345. if([idA isEqualToString:idB]){
  346. NSDictionary * extendB = [msgB[@"extend"] isKindOfClass:NSDictionary.class]?msgB[@"extend"]:@{};
  347. NSString *urlB = extendB[@"url"] ?: @"";
  348. if(urlB.length>0){
  349. msrk=false;
  350. }
  351. break;
  352. }
  353. }
  354. }
  355. if(msrk){
  356. [marray addObject:msgA];
  357. }
  358. }
  359. if(marray.count>0){
  360. return marray;
  361. }
  362. else{
  363. return array;
  364. }
  365. }
  366. //替换指定数据数据
  367. -(NSArray *_Nullable)replaydisData:(NSDictionary *_Nullable)dis inarray:(NSArray *_Nullable)array{
  368. NSMutableArray *marray = [NSMutableArray new];
  369. NSString *locatTime = dis[@"localtime"];
  370. for (NSDictionary *msgA in array) {
  371. if(![locatTime isEqualToString:msgA[@"localtime"]]){
  372. [marray addObject:msgA];
  373. }
  374. else{
  375. [marray addObject:dis];
  376. }
  377. }
  378. return [marray copy];
  379. }
  380. -(NSArray *)arraysort:(NSArray *)array{
  381. NSArray *sortedArray = [array sortedArrayUsingComparator:^NSComparisonResult(NSDictionary *obj1, NSDictionary *obj2) {
  382. NSString *newtime =obj1[@"timestamp"];
  383. NSString *dtime =obj2[@"timestamp"];
  384. // NSLog(@"newtime:%@",newtime);
  385. // NSLog(@"dtime:%@",[dtime class]);
  386. if([newtime isKindOfClass:[NSNull class]]){
  387. return NSOrderedAscending;
  388. }
  389. if([dtime isKindOfClass:[NSNull class]]){
  390. return NSOrderedDescending;
  391. }
  392. if(newtime.longLongValue<dtime.longLongValue){
  393. return NSOrderedAscending;
  394. }
  395. else if(newtime.longLongValue==dtime.longLongValue){
  396. return NSOrderedSame;
  397. }
  398. else{
  399. return NSOrderedDescending;
  400. }
  401. }];
  402. return sortedArray;
  403. }
  404. -(void)updatereadTime:(NSDictionary *)dis{
  405. if(self.delegate){
  406. NSString *nstr = dis[@"userId"];
  407. NSInteger time = [dis[@"timestamp"] longLongValue];
  408. if([self.chatId isEqualToString:nstr]){
  409. self.lastreadTime = time;
  410. if(self.delegate){
  411. [self.delegate ChatsChange:self.msgList type:3];
  412. }
  413. }
  414. }
  415. [GDBManager.shareInstance insertLastreadtime:dis succ:^(NSArray * _Nullable array) {
  416. } fail:^(NSString * _Nullable error) {
  417. }];
  418. }
  419. -(NSDictionary *)getchatReadTime:(NSString *)chatId{
  420. [GDBManager.shareInstance selectchatLastreadtime:chatId succ:^(NSArray * _Nullable array) {
  421. NSLog(@"getchatReadTime:%@",chatId);
  422. NSLog(@"getchatReadTime:%@",array);
  423. if(array.count>0){
  424. NSDictionary *dic = array[0];
  425. NSString *temp =dic[@"timestamp"];
  426. self.lastreadTime = temp.integerValue;
  427. }
  428. } fail:^(NSString * _Nullable error) {
  429. }];
  430. return nil;
  431. }
  432. //消息列表中删除消息操作
  433. -(void)deleteMyLocalMsg:(NSDictionary *_Nonnull)msg{
  434. [GDBManager.shareInstance deleteLocalmsg:msg];
  435. NSString *localtime =msg[@"localtime"];
  436. NSString * msgId = msg[@"id"];
  437. NSMutableArray * temp = self.msgList.copy;
  438. for (NSDictionary *dis in temp) {
  439. NSString *timestamp = dis[@"localtime"];
  440. NSString *tempMsgId = dis[@"id"];
  441. if (temp.lastObject == dis) {
  442. NSMutableDictionary * deleteMsg = [NSMutableDictionary dictionaryWithDictionary:msg];
  443. [deleteMsg setValue:@"【已删除】" forKey:@"content"];
  444. [ChatListStore.shareInstance reciveMsg:deleteMsg];
  445. }
  446. if([localtime isEqual:timestamp] && [msgId isEqualToString:tempMsgId]){
  447. [self.msgList removeObjectAtIndex:[temp indexOfObject:dis]];
  448. break;
  449. }
  450. }
  451. if(self.delegate){
  452. [self.delegate ChatsChange:self.msgList type:3];
  453. }
  454. }
  455. //信息解密存储
  456. -(NSDictionary *)encodingMsg:(NSDictionary *)msg{
  457. NSMutableDictionary *temp = [msg mutableCopy];
  458. NSString *content = msg[@"content"];
  459. content = [CryptoAES.shareInstance decryptDataL:content?: @""];
  460. [temp setObject:content forKey:@"content"];
  461. return [temp copy];
  462. }
  463. @end