ChatMessageModel.m 19 KB

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