ChatMessageModel.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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. [FileNetApi downLoadWToken:getURL(self.url) thrid:self.msgId succ:^(int code, NSDictionary * res) {
  306. // NSLog(@"11---downLoadWToken:%d",code);
  307. if(res!=nil){
  308. NSMutableDictionary *mutableDict = [self.formerMessage mutableCopy];
  309. NSMutableDictionary *extend = [self.formerMessage[@"extend"] mutableCopy];
  310. [extend setObject:res[@"filePath"] forKey:@"localurl"];
  311. [mutableDict setObject:extend forKey:@"extend"];
  312. self.localurl = res[@"filePath"];
  313. NSDictionary *NewMsg = [mutableDict copy];
  314. [ChatsStore.shareInstance reciveMsg:NewMsg];
  315. if (loading) {
  316. loading(100);
  317. }
  318. }
  319. else{
  320. if (code>=0&&code<200) {
  321. if (loading) {
  322. loading(code);
  323. }
  324. }
  325. }
  326. } fail:^(NSError * _Nonnull error) {
  327. NSLog(@"error:%@",error);
  328. }];
  329. }
  330. else{
  331. if (loading) {
  332. loading(0);
  333. }
  334. }
  335. }
  336. }
  337. else{
  338. NSLog(@"downLoadWToken xiazaishipin-----");
  339. }
  340. }
  341. -(void)xiazaiwenjian:(void(^)(NSInteger persent))loading{
  342. NSLog(@"xiazaiwenjian---");
  343. if (self.isSender) {
  344. return;
  345. }
  346. else{
  347. // NSLog(@"self.localurl2:%@",self.localurl);
  348. if (self.localurl.length != 0 && [[NSFileManager defaultManager] fileExistsAtPath:self.localurl]){
  349. // NSLog(@"文件已经存在本地");
  350. if (loading) {
  351. loading(100);
  352. }
  353. return;
  354. }
  355. [FileNetApi downLoadWToken:getURL(self.url) thrid:self.msgId succ:^(int code, NSDictionary * res) {
  356. if(res!=nil){
  357. NSMutableDictionary *mutableDict = [self.formerMessage mutableCopy];
  358. NSMutableDictionary *extend = [self.formerMessage[@"extend"] mutableCopy];
  359. [extend setObject:res[@"filePath"] forKey:@"localurl"];
  360. [mutableDict setObject:extend forKey:@"extend"];
  361. self.localurl = res[@"filePath"];
  362. NSDictionary *NewMsg = [mutableDict copy];
  363. [ChatsStore.shareInstance reciveMsg:NewMsg];
  364. if (loading) {
  365. loading(100);
  366. }
  367. }
  368. else{
  369. if (code>=0&&code<200) {
  370. if (loading) {
  371. loading(code);
  372. }
  373. }
  374. }
  375. } fail:^(NSError * _Nonnull error) {
  376. NSLog(@"error:%@",error);
  377. NSLog(@"%@",self.url);
  378. }];
  379. }
  380. }
  381. -(void)xiazaiyuyin:(void(^)(NSInteger persent))loading{
  382. NSLog(@"xiazaiwenjian---");
  383. if (self.isSender) {
  384. return;
  385. }
  386. else{
  387. // NSLog(@"self.localurl2:%@",self.localurl);
  388. if (self.localurl.length != 0 && [[NSFileManager defaultManager] fileExistsAtPath:self.localurl]){
  389. // NSLog(@"文件已经存在本地");
  390. if (loading) {
  391. loading(100);
  392. }
  393. return;
  394. }
  395. [FileNetApi downLoadWToken:getURL(self.url) thrid:self.msgId succ:^(int code, NSDictionary * res) {
  396. if(res!=nil){
  397. NSMutableDictionary *mutableDict = [self.formerMessage mutableCopy];
  398. NSMutableDictionary *extend = [self.formerMessage[@"extend"] mutableCopy];
  399. [extend setObject:res[@"filePath"] forKey:@"localurl"];
  400. [mutableDict setObject:extend forKey:@"extend"];
  401. self.localurl = res[@"filePath"];
  402. NSDictionary *NewMsg = [mutableDict copy];
  403. [ChatsStore.shareInstance reciveMsg:NewMsg];
  404. if (loading) {
  405. loading(100);
  406. }
  407. }
  408. else{
  409. if (code>=0&&code<200) {
  410. if (loading) {
  411. loading(code);
  412. }
  413. }
  414. }
  415. } fail:^(NSError * _Nonnull error) {
  416. NSLog(@"error:%@",error);
  417. NSLog(@"%@",self.url);
  418. }];
  419. }
  420. }
  421. -(void)setuploadPersent:(NSInteger)persent localtime:(NSInteger)localtime{
  422. if(self.uploadPersentChange && localtime==self.localtime){
  423. self.uploadPersentChange(persent,localtime);
  424. }
  425. }
  426. // 根据语音时长计算气泡宽度
  427. - (CGFloat)voiceBubbleWidth {
  428. // 基础宽度
  429. CGFloat minWidth = 60.0f;
  430. CGFloat maxWidth = 200.0f;
  431. // 最大限制时长(如60秒)
  432. NSInteger maxDuration = 60;
  433. NSInteger duration = MIN(self.voiceDuration, maxDuration);
  434. // 线性增长计算
  435. CGFloat width = minWidth + (maxWidth - minWidth) * (duration / (CGFloat)maxDuration);
  436. return width;
  437. }
  438. @end