ChatMessageModel.m 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. //
  2. // ChatMessageModel.m
  3. // AIIM
  4. //
  5. // Created by qitewei on 2025/5/14.
  6. //
  7. #import "ChatMessageModel.h"
  8. #import "CryptoAES.h"
  9. #import "ChatsStore.h"
  10. #import "AVFoundation/AVFoundation.h"
  11. #import "FileNetApi.h"
  12. #import <SDWebImage/SDImageCache.h>
  13. #import "MessageStatusManager.h"
  14. @implementation ChatMessageModel
  15. + (instancetype)modelWithDictionary:(NSDictionary *)dict {
  16. ChatMessageModel *model = [[ChatMessageModel alloc] init];
  17. [model setupWithDictionary:dict];
  18. return model;
  19. }
  20. - (void)setupWithDictionary:(NSDictionary *)dict {
  21. NSMutableDictionary * mutableDict = [NSMutableDictionary dictionaryWithDictionary:dict];
  22. [mutableDict jk_each:^(id k, id v) {
  23. if ([v isKindOfClass:[NSNull class]]) {
  24. [mutableDict setObject:@"" forKey:k];
  25. }
  26. }];
  27. //原消息
  28. self.formerMessage = dict;
  29. self.chatId = mutableDict[@"chatId"];
  30. // 消息类型
  31. NSInteger type = [mutableDict[@"messageType"] integerValue];
  32. self.messageType = (ChatMessageType)type;
  33. self.type = [mutableDict[@"type"] integerValue];
  34. self.nickName = mutableDict[@"fromName"]?:@"";
  35. self.avatar = mutableDict[@"fromAvatar"]?:@"";
  36. // 通用内容
  37. // self.content = mutableDict[@"content"] ?: @"";//本地明文存储
  38. self.content =[CryptoAES.shareInstance decryptDataL:mutableDict[@"content"] ?: @""];//本地加密存储
  39. self.msgId = mutableDict[@"id"];
  40. self.fromId = mutableDict[@"fromId"];
  41. // 发送方标识
  42. NSDictionary *userinfo = [UDManager.shareInstance getDDManager:dkuserinfo];
  43. NSString *userId = userinfo[@"id"];
  44. if([self.fromId isEqualToString:userId]){
  45. self.isSender=true;
  46. }
  47. else{
  48. self.isSender=false;
  49. }
  50. // 处理媒体尺寸
  51. CGFloat width = [mutableDict[@"width"] floatValue];
  52. CGFloat height = [mutableDict[@"height"] floatValue];
  53. if (width > 0 && height > 0) {
  54. self.mediaSize = CGSizeMake(width, height);
  55. } else {
  56. // 默认尺寸
  57. self.mediaSize = CGSizeMake(200, 150);
  58. }
  59. self.placeholderImage = kImageMake(@"pictrue_placeholder");
  60. // 文件消息
  61. NSDictionary * extend = [mutableDict[@"extend"] isKindOfClass:NSDictionary.class]?mutableDict[@"extend"]:@{};
  62. self.fileName = extend[@"fileName"] ?: @"未知文件";
  63. self.fileSize = extend[@"size"] ?: @"0KB";
  64. self.url = extend[@"url"] ?: @"";
  65. // NSCharacterSet *allowedCharacters = [NSCharacterSet URLQueryAllowedCharacterSet];
  66. // self.url = [self.url stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacters];
  67. self.localurl = extend[@"localurl"] ?: @"";
  68. self.quoteMessage = ![extend jk_hasKey:@"quoteMessage"] ?NULL: [ChatMessageModel modelWithDictionary:extend[@"quoteMessage"]];
  69. // 语音消息
  70. NSInteger duration = [extend[@"time"] integerValue];
  71. if (duration<=60) {
  72. self.voiceDuration = duration;
  73. }else{
  74. self.voiceDuration = duration/1000;
  75. }
  76. self.voiceWidth = [self voiceBubbleWidth];
  77. //文件传输成功或失败
  78. self.fileError = 0;
  79. NSInteger fileError = [extend[@"fileError"] integerValue];
  80. if (fileError!=0) {
  81. self.fileError = fileError;
  82. }
  83. else{
  84. if(self.url.length>0){
  85. self.fileError = 0;
  86. }
  87. }
  88. //合并转发
  89. self.forwardMsgArray = extend[@"messageList"] ?:@[];
  90. // 通话记录
  91. self.callDuration = [mutableDict[@"duration"] integerValue];
  92. self.isCallSuccess = [mutableDict[@"result"] boolValue];
  93. self.isVideo = [mutableDict[@"video"] boolValue];
  94. //@相关
  95. self.atAll = extend[@"atAll"]?[extend[@"atAll"] boolValue]:NO;
  96. self.atUserIds = extend[@"atUserIds"]?:@[];
  97. // 时间戳(如果需要)
  98. self.timestamp = [mutableDict[@"timestamp"] longLongValue];
  99. self.localtime = [mutableDict[@"localtime"] longLongValue];
  100. self.formatterTime = [self timeF:self.timestamp];
  101. // 从 plist 读取状态
  102. self.messageSendStatus = [[MessageStatusManager sharedInstance] getMessageStatusForMessageId:self.msgId];
  103. // 其他自定义字段...
  104. self.readStatus = [self getReadStatus];
  105. BOOL contain = [[NSFileManager defaultManager] fileExistsAtPath:self.localurl];
  106. NSString *localPath = [self checkHasLocalPath];
  107. if (!contain && localPath.length) {
  108. self.localurl = localPath;
  109. }
  110. }
  111. - (void)exchangeModelWithModel:(ChatMessageModel *)model{
  112. self.msgId = model.msgId;
  113. self.formerMessage = model.formerMessage;
  114. if(self.messageType!=model.messageType){
  115. self.cellHeight=0;
  116. }
  117. self.messageType = model.messageType;
  118. self.type = model.type;
  119. self.isSender = model.isSender;
  120. self.content = model.content;
  121. self.msgId = model.msgId;
  122. self.mediaSize = model.mediaSize;
  123. self.placeholderImage = model.placeholderImage;
  124. self.fileName = model.fileName;
  125. self.fileSize = model.fileSize;
  126. self.url = model.url;
  127. self.localurl = model.localurl;
  128. //文件传输成功或失败
  129. self.fileError = model.fileError;
  130. // 通话记录
  131. self.callDuration = model.callDuration;
  132. self.isCallSuccess = model.isCallSuccess;
  133. self.isVideo = model.isVideo;
  134. self.avatar = model.avatar;
  135. self.nickName = model.nickName;
  136. //@相关
  137. self.atAll = model.atAll;
  138. self.atUserIds = model.atUserIds;
  139. // 时间戳(如果需要)
  140. self.timestamp = model.timestamp;
  141. self.localtime = model.localtime;
  142. self.formatterTime = model.formatterTime;
  143. // 消息状态
  144. self.messageSendStatus = model.messageSendStatus;
  145. // // 其他自定义字段...
  146. // 其他自定义字段...
  147. self.readStatus = [self getReadStatus];
  148. }
  149. -(NSString *)timeF:(NSInteger)time{
  150. //NSLog(@"time:%ld",(long)time);
  151. // 创建日期格式器
  152. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  153. [formatter setDateFormat:@"dd"]; // 设置你想要的日期格式
  154. long long timestamp =(long)time/1000; // 例如:2021-10-01 00:00:00 UTC
  155. // 转换为NSDate
  156. NSDate *date = [NSDate dateWithTimeIntervalSince1970:timestamp];
  157. NSString *dateString = [formatter stringFromDate:date];
  158. NSDate *now = [NSDate date];
  159. NSTimeInterval trt = [now timeIntervalSince1970];
  160. date = [NSDate dateWithTimeIntervalSince1970:trt];
  161. NSString *tdateString = [formatter stringFromDate:date];
  162. NSInteger shijiancha = trt-timestamp;
  163. if(shijiancha<60){
  164. return NSLocalizedString(@"time-oneminint", @"");
  165. }
  166. if(shijiancha>24*3600||dateString.intValue!=tdateString.intValue){
  167. // 假设这是你的时间戳(以秒为单位)
  168. long long timestamp =(long)time/1000; // 例如:2021-10-01 00:00:00 UTC
  169. // 转换为NSDate
  170. NSDate *date = [NSDate dateWithTimeIntervalSince1970:timestamp];
  171. // 创建日期格式器
  172. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  173. [formatter setDateFormat:@"yyyy-MM-dd HH:mm"]; // 设置你想要的日期格式
  174. // 转换为字符串
  175. NSString *dateString = [formatter stringFromDate:date];
  176. return dateString;
  177. }
  178. else{
  179. NSInteger xiaoshi = shijiancha/3600;
  180. NSInteger fenzhong = shijiancha/60;
  181. if(xiaoshi>0){
  182. // 假设这是你的时间戳(以秒为单位)
  183. long long timestamp =(long)time/1000; // 例如:2021-10-01 00:00:00 UTC
  184. // 转换为NSDate
  185. NSDate *date = [NSDate dateWithTimeIntervalSince1970:timestamp];
  186. // 创建日期格式器
  187. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  188. [formatter setDateFormat:@"HH:mm"]; // 设置你想要的日期格式
  189. // 转换为字符串
  190. NSString *dateString = [formatter stringFromDate:date];
  191. return dateString;
  192. }
  193. else{
  194. return [NSString stringWithFormat:@"%ld%@",(long)fenzhong,NSLocalizedString(@"time-outmin", @"")];
  195. }
  196. }
  197. return @"";
  198. }
  199. - (NSString *)getReadStatus{
  200. NSString * readStatus;
  201. self.readstate = 0;
  202. if(self.timestamp == 0){
  203. return @"";
  204. }
  205. if(self.messageType==11||self.messageType==12){
  206. return @"";
  207. }
  208. if(self.type==0){
  209. if (self.localtime != 0 && self.localtime == self.timestamp) {
  210. NSDate *now = [NSDate date];
  211. NSTimeInterval trt = [now timeIntervalSince1970];
  212. NSInteger time = trt*1000;
  213. NSInteger outTime =time-self.localtime;
  214. if(outTime>33000){
  215. if(self.messageType==1||self.messageType==2||self.messageType==5){
  216. readStatus = @"正在发送";
  217. self.readstate = 3;
  218. }else{
  219. readStatus = @"发送失败";
  220. self.readstate = 4;
  221. }
  222. }else{
  223. readStatus = @"正在发送";
  224. self.readstate = 3;
  225. }
  226. }
  227. else{
  228. //判断已读未读
  229. if(self.timestamp>ChatsStore.shareInstance.lastreadTime){
  230. readStatus = @"未读";
  231. self.readstate = 1;
  232. }
  233. else{
  234. readStatus = @"已读";
  235. self.readstate = 2;
  236. }
  237. if(self.fileError==1){
  238. readStatus = @"發送失敗,點擊重發";
  239. self.readstate = 4;
  240. }
  241. }
  242. }else{
  243. return @"";
  244. }
  245. return readStatus;
  246. }
  247. - (void)generateThumbnailWithCompletion:(void(^)(UIImage *thumbnail))completion {
  248. // 如果内存中已有缩略图,直接返回
  249. if (self.videoThumbnailImage) {
  250. if (completion) completion(self.videoThumbnailImage);
  251. return;
  252. }
  253. // 生成缓存 key,优先使用 msgId,如果没有则使用 URL 的 hash
  254. NSString *cacheKey = nil;
  255. if (self.msgId.length > 0) {
  256. cacheKey = [NSString stringWithFormat:@"video_thumbnail_%@", self.msgId];
  257. } else if (self.url.length > 0) {
  258. cacheKey = [NSString stringWithFormat:@"video_thumbnail_%lu", (unsigned long)[self.url hash]];
  259. }
  260. // 从 SDWebImage 缓存中查找
  261. if (cacheKey) {
  262. SDImageCache *imageCache = [SDImageCache sharedImageCache];
  263. UIImage *cachedImage = [imageCache imageFromCacheForKey:cacheKey];
  264. if (cachedImage) {
  265. NSLog(@"从缓存加载缩略图: %@", cacheKey);
  266. self.videoThumbnailImage = cachedImage;
  267. if (completion) completion(cachedImage);
  268. return;
  269. }
  270. }
  271. // 没有缓存,需要生成缩略图
  272. NSURL *videoURL = nil;
  273. if (self.localurl.length != 0 && [[NSFileManager defaultManager] fileExistsAtPath:self.localurl]) {
  274. // 从本地文件生成缩略图
  275. NSLog(@"generateThumbnailWithCompletion localurl:%@",self.localurl);
  276. videoURL = [NSURL fileURLWithPath:self.localurl];
  277. NSString *finalCacheKey = cacheKey;
  278. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  279. AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];
  280. AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
  281. generator.appliesPreferredTrackTransform = YES;
  282. CMTime acttime;
  283. NSError *error = nil;
  284. CGImageRef imageRef = [generator copyCGImageAtTime:CMTimeMake(0, 600) actualTime:&acttime error:&error];
  285. NSLog(@"error:%@",error);
  286. if (!error && imageRef) {
  287. UIImage *thumbnail = [[UIImage alloc] initWithCGImage:imageRef];
  288. CGImageRelease(imageRef);
  289. // 缓存到内存
  290. self.videoThumbnailImage = thumbnail;
  291. // 使用 SDWebImage 缓存到磁盘和内存
  292. if (finalCacheKey) {
  293. SDImageCache *imageCache = [SDImageCache sharedImageCache];
  294. [imageCache storeImage:thumbnail forKey:finalCacheKey completion:^{
  295. NSLog(@"缩略图已缓存: %@", finalCacheKey);
  296. }];
  297. }
  298. if (completion){
  299. completion(thumbnail);
  300. }
  301. } else {
  302. if (completion) completion(nil);
  303. }
  304. });
  305. } else {
  306. // 从远程 URL 生成缩略图
  307. NSLog(@"generateThumbnailWithCompletion url");
  308. if (self.url.length == 0){
  309. if (completion) completion(nil);
  310. return;
  311. }
  312. NSDictionary *options = @{AVURLAssetAllowsCellularAccessKey: @YES,
  313. AVURLAssetPreferPreciseDurationAndTimingKey: @YES};
  314. AVURLAsset *asset = [AVURLAsset URLAssetWithURL:getURL(self.url) options:options];
  315. // 创建图像生成器
  316. AVAssetImageGenerator *generator = [AVAssetImageGenerator assetImageGeneratorWithAsset:asset];
  317. generator.appliesPreferredTrackTransform = YES; // 应用视频的方向
  318. generator.maximumSize = CGSizeMake(320, 0); // 设置最大尺寸,保持宽高比
  319. NSString *finalCacheKey = cacheKey;
  320. // 异步生成缩略图
  321. [generator generateCGImagesAsynchronouslyForTimes:@[[NSValue valueWithCMTime:kCMTimeZero]]
  322. completionHandler:^(CMTime requestedTime, CGImageRef image, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error) {
  323. __block UIImage *thumbnail = nil;
  324. // 处理生成结果
  325. if (result == AVAssetImageGeneratorSucceeded && image) {
  326. // 创建UIImage
  327. thumbnail = [UIImage imageWithCGImage:image];
  328. self.videoThumbnailImage = thumbnail;
  329. // 使用 SDWebImage 缓存到磁盘和内存
  330. if (finalCacheKey) {
  331. SDImageCache *imageCache = [SDImageCache sharedImageCache];
  332. [imageCache storeImage:thumbnail forKey:finalCacheKey completion:^{
  333. NSLog(@"缩略图已缓存: %@", finalCacheKey);
  334. }];
  335. }
  336. }
  337. dispatch_async(dispatch_get_main_queue(), ^{
  338. if (completion) completion(thumbnail);
  339. });
  340. }];
  341. }
  342. }
  343. //下载视频文件
  344. -(void)downloadFileIfNeed:(void(^)(NSInteger persent))loading{
  345. if ([[NSFileManager defaultManager] fileExistsAtPath:self.localurl]){
  346. self.customFileSize = [self getFileSize];
  347. if (loading) {
  348. loading(100);
  349. }
  350. NSLog(@"downloadFileIfNeed 0: %@", self.localurl);
  351. return;
  352. }
  353. NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
  354. NSURL *tempUrl = [NSURL URLWithString:self.url];
  355. NSString *fileName = tempUrl.lastPathComponent;
  356. NSString *fileExtension = [fileName pathExtension];
  357. NSString *newfileName = [NSString stringWithFormat:@"%@.%@", self.msgId,fileExtension];
  358. documentsDirectoryURL = [documentsDirectoryURL URLByAppendingPathComponent:newfileName];
  359. if ([[NSFileManager defaultManager] fileExistsAtPath:documentsDirectoryURL.path]){
  360. NSLog(@"downloadFileIfNeed 1: %@", documentsDirectoryURL.path);
  361. NSMutableDictionary *mutableDict = [self.formerMessage mutableCopy];
  362. NSMutableDictionary *extend = [self.formerMessage[@"extend"] mutableCopy];
  363. [extend setObject:documentsDirectoryURL.path forKey:@"localurl"];
  364. [mutableDict setObject:extend forKey:@"extend"];
  365. self.localurl = documentsDirectoryURL.path;
  366. NSDictionary *NewMsg = [mutableDict copy];
  367. [ChatsStore.shareInstance reciveMsg:NewMsg];
  368. self.customFileSize = [self getFileSize];
  369. if (loading) {
  370. loading(100);
  371. }
  372. return;
  373. }
  374. [FileNetApi downLoadWToken:getURL(self.url) thrid:self.msgId succ:^(int code, NSDictionary * res) {
  375. if(res!=nil){
  376. NSMutableDictionary *mutableDict = [self.formerMessage mutableCopy];
  377. NSMutableDictionary *extend = [self.formerMessage[@"extend"] mutableCopy];
  378. [extend setObject:res[@"filePath"] forKey:@"localurl"];
  379. [mutableDict setObject:extend forKey:@"extend"];
  380. self.localurl = res[@"filePath"];
  381. NSDictionary *NewMsg = [mutableDict copy];
  382. [ChatsStore.shareInstance reciveMsg:NewMsg];
  383. self.customFileSize = [self getFileSize];
  384. if (loading) {
  385. loading(100);
  386. }
  387. }
  388. else{
  389. if (code>=0&&code<200) {
  390. if (loading) {
  391. loading(code);
  392. }
  393. }
  394. }
  395. } fail:^(NSError * _Nonnull error) {
  396. NSLog(@"downLoadWToken error:%@",error);
  397. }];
  398. }
  399. -(void)setuploadPersent:(NSInteger)persent localtime:(NSInteger)localtime{
  400. if(self.uploadPersentChange && localtime==self.localtime){
  401. self.uploadPersentChange(persent,localtime);
  402. }
  403. }
  404. // 根据语音时长计算气泡宽度
  405. - (CGFloat)voiceBubbleWidth {
  406. // 基础宽度
  407. CGFloat minWidth = 60.0f;
  408. CGFloat maxWidth = 200.0f;
  409. // 最大限制时长(如60秒)
  410. NSInteger maxDuration = 60;
  411. NSInteger duration = MIN(self.voiceDuration, maxDuration);
  412. // 线性增长计算
  413. CGFloat width = minWidth + (maxWidth - minWidth) * (duration / (CGFloat)maxDuration);
  414. return width;
  415. }
  416. #pragma mark - 消息状态管理
  417. // 更新消息状态到 plist
  418. - (void)updateMessageStatus:(MessageSendStatus)status {
  419. self.messageSendStatus = status;
  420. if (self.msgId.length > 0) {
  421. [[MessageStatusManager sharedInstance] saveMessageStatus:status forMessageId:self.msgId];
  422. NSLog(@"[ChatMessageModel] 更新消息状态: msgId=%@, status=%ld", self.msgId, (long)status);
  423. }
  424. }
  425. // 从 plist 刷新消息状态
  426. - (void)refreshMessageStatus {
  427. if (self.msgId.length > 0) {
  428. NSInteger status = [[MessageStatusManager sharedInstance] getMessageStatusForMessageId:self.msgId];
  429. self.messageSendStatus = (MessageSendStatus)status;
  430. }
  431. }
  432. - (NSString *)getFileSize {
  433. NSString *filePath = self.localurl;
  434. NSFileManager *fileManager = [NSFileManager defaultManager];
  435. // 获取文件属性
  436. NSError *error = nil;
  437. NSDictionary *attributes = [fileManager attributesOfItemAtPath:filePath error:&error];
  438. NSString *string = @"0KB";
  439. if (error) {
  440. NSLog(@"获取文件属性失败: %@", error);
  441. } else {
  442. // 获取文件大小(单位:字节)
  443. unsigned long long fileSize = [attributes fileSize];
  444. NSLog(@"文件大小: %llu 字节", fileSize);
  445. // 可选:转换为 KB、MB
  446. double fileSizeKB = fileSize / 1024.0;
  447. double fileSizeMB = fileSizeKB / 1024.0;
  448. NSLog(@"文件大小: %.2f KB (%.2f MB)", fileSizeKB, fileSizeMB);
  449. string = [NSString stringWithFormat:@"%.2fMB", fileSizeMB];
  450. }
  451. return string;
  452. }
  453. - (NSString *)customFileSize {
  454. if (!_customFileSize) {
  455. _customFileSize = [self getFileSize];
  456. }
  457. return _customFileSize;
  458. }
  459. - (NSString *)checkHasLocalPath {
  460. if ([[NSFileManager defaultManager] fileExistsAtPath:self.localurl]){
  461. self.customFileSize = [self getFileSize];
  462. NSLog(@"checkHasLocalPath 0: %@", self.localurl);
  463. return self.localurl;
  464. }
  465. NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
  466. NSURL *tempUrl = [NSURL URLWithString:self.url];
  467. NSString *fileName = tempUrl.lastPathComponent;
  468. NSString *fileExtension = [fileName pathExtension];
  469. NSString *newfileName = [NSString stringWithFormat:@"%@.%@", self.msgId,fileExtension];
  470. NSString *path = [documentsDirectoryURL URLByAppendingPathComponent:newfileName].path;
  471. if ([[NSFileManager defaultManager] fileExistsAtPath:path]){
  472. NSLog(@"checkHasLocalPath 0: %@", path);
  473. return path;
  474. }
  475. return nil;
  476. }
  477. - (BOOL)isUploadFile {
  478. return self.messageType == ChatMessageTypeFile ||
  479. self.messageType == ChatMessageTypeVoice ||
  480. self.messageType == ChatMessageTypeImage ||
  481. self.messageType == ChatMessageTypeVideo;
  482. }
  483. @end