JXCommentContent.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //
  2. // JXCommentContent.m
  3. // AICity
  4. //
  5. // Created by TogetherWatch on 2025-10-20.
  6. // Feature: 010-ui-ios - iOS平台完整功能实现(对标Android)
  7. //
  8. #import "JXCommentContent.h"
  9. @implementation JXCommentContent
  10. #pragma mark - 初始化方法
  11. - (instancetype)init {
  12. self = [super init];
  13. if (self) {
  14. _commentId = 0;
  15. _userId = 0;
  16. _userName = @"";
  17. _userAvatar = @"";
  18. _content = @"";
  19. _likeCount = 0;
  20. _replyCount = 0;
  21. _isLiked = NO;
  22. _createdAt = 0;
  23. _isVerified = NO;
  24. }
  25. return self;
  26. }
  27. - (instancetype)initWithDictionary:(NSDictionary *)dict {
  28. self = [super init];
  29. if (self) {
  30. _commentId = [dict[@"id"] longLongValue];
  31. _userId = [dict[@"userId"] longLongValue];
  32. _userName = dict[@"userName"] ?: @"";
  33. _userAvatar = dict[@"userAvatar"] ?: @"";
  34. _content = dict[@"content"] ?: @"";
  35. _likeCount = [dict[@"likeCount"] longLongValue];
  36. _replyCount = [dict[@"replyCount"] longLongValue];
  37. _isLiked = [dict[@"isLiked"] boolValue];
  38. _createdAt = [dict[@"createdAt"] longLongValue];
  39. _isVerified = [dict[@"isVerified"] boolValue];
  40. }
  41. return self;
  42. }
  43. + (instancetype)commentWithDictionary:(NSDictionary *)dict {
  44. return [[self alloc] initWithDictionary:dict];
  45. }
  46. - (NSDictionary *)toDictionary {
  47. return @{
  48. @"id": @(self.commentId),
  49. @"userId": @(self.userId),
  50. @"userName": self.userName,
  51. @"userAvatar": self.userAvatar,
  52. @"content": self.content,
  53. @"likeCount": @(self.likeCount),
  54. @"replyCount": @(self.replyCount),
  55. @"isLiked": @(self.isLiked),
  56. @"createdAt": @(self.createdAt),
  57. @"isVerified": @(self.isVerified)
  58. };
  59. }
  60. #pragma mark - UI格式化方法
  61. - (NSString *)formattedTime {
  62. // 将毫秒时间戳转换为秒
  63. NSTimeInterval timestamp = self.createdAt / 1000.0;
  64. NSDate *date = [NSDate dateWithTimeIntervalSince1970:timestamp];
  65. // 获取当前日期和评论日期的年份
  66. NSCalendar *calendar = [NSCalendar currentCalendar];
  67. NSInteger currentYear = [calendar component:NSCalendarUnitYear fromDate:[NSDate date]];
  68. NSInteger commentYear = [calendar component:NSCalendarUnitYear fromDate:date];
  69. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  70. if (currentYear == commentYear) {
  71. // 今年内:显示 "月-日"
  72. [formatter setDateFormat:@"MM-dd"];
  73. } else {
  74. // 非今年:显示 "年-月-日"
  75. [formatter setDateFormat:@"yyyy-MM-dd"];
  76. }
  77. return [formatter stringFromDate:date];
  78. }
  79. - (NSString *)formattedLikeCount {
  80. return [self formatCount:self.likeCount];
  81. }
  82. - (NSString *)formatCount:(long long)count {
  83. if (count >= 10000) {
  84. return [NSString stringWithFormat:@"%.1fw", count / 10000.0];
  85. } else if (count >= 1000) {
  86. return [NSString stringWithFormat:@"%.1fk", count / 1000.0];
  87. } else {
  88. return [NSString stringWithFormat:@"%lld", count];
  89. }
  90. }
  91. #pragma mark - NSObject
  92. - (NSString *)description {
  93. return [NSString stringWithFormat:@"<JXCommentContent: id=%lld, userName=%@, content=%@, time=%@>",
  94. self.commentId,
  95. self.userName,
  96. self.content,
  97. [self formattedTime]];
  98. }
  99. @end