JXCommentContent.m 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. _reply_count = [dict[@"reply_count"] longLongValue];
  38. _isLiked = [dict[@"isLiked"] boolValue];
  39. _createdAt = [dict[@"createdAt"] longLongValue];
  40. _isVerified = [dict[@"isVerified"] boolValue];
  41. }
  42. return self;
  43. }
  44. + (instancetype)commentWithDictionary:(NSDictionary *)dict {
  45. return [[self alloc] initWithDictionary:dict];
  46. }
  47. - (NSDictionary *)toDictionary {
  48. return @{
  49. @"id": @(self.commentId),
  50. @"userId": @(self.userId),
  51. @"userName": self.userName,
  52. @"userAvatar": self.userAvatar,
  53. @"content": self.content,
  54. @"likeCount": @(self.likeCount),
  55. @"replyCount": @(self.replyCount),
  56. @"reply_count": @(self.reply_count),
  57. @"isLiked": @(self.isLiked),
  58. @"createdAt": @(self.createdAt),
  59. @"isVerified": @(self.isVerified)
  60. };
  61. }
  62. #pragma mark - UI格式化方法
  63. - (NSString *)formattedTime {
  64. // 将毫秒时间戳转换为秒
  65. NSTimeInterval timestamp = self.createdAt / 1000.0;
  66. NSDate *date = [NSDate dateWithTimeIntervalSince1970:timestamp];
  67. // 获取当前日期和评论日期的年份
  68. NSCalendar *calendar = [NSCalendar currentCalendar];
  69. NSInteger currentYear = [calendar component:NSCalendarUnitYear fromDate:[NSDate date]];
  70. NSInteger commentYear = [calendar component:NSCalendarUnitYear fromDate:date];
  71. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  72. if (currentYear == commentYear) {
  73. // 今年内:显示 "月-日"
  74. [formatter setDateFormat:@"MM-dd"];
  75. } else {
  76. // 非今年:显示 "年-月-日"
  77. [formatter setDateFormat:@"yyyy-MM-dd"];
  78. }
  79. return [formatter stringFromDate:date];
  80. }
  81. - (NSString *)formattedLikeCount {
  82. return [self formatCount:self.likeCount];
  83. }
  84. - (NSString *)formatCount:(long long)count {
  85. if (count >= 10000) {
  86. return [NSString stringWithFormat:@"%.1fw", count / 10000.0];
  87. } else if (count >= 1000) {
  88. return [NSString stringWithFormat:@"%.1fk", count / 1000.0];
  89. } else {
  90. return [NSString stringWithFormat:@"%lld", count];
  91. }
  92. }
  93. #pragma mark - NSObject
  94. - (NSString *)description {
  95. return [NSString stringWithFormat:@"<JXCommentContent: id=%lld, userName=%@, content=%@, time=%@>",
  96. self.commentId,
  97. self.userName,
  98. self.content,
  99. [self formattedTime]];
  100. }
  101. @end