| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- //
- // JXCommentContent.m
- // AICity
- //
- // Created by TogetherWatch on 2025-10-20.
- // Feature: 010-ui-ios - iOS平台完整功能实现(对标Android)
- //
- #import "JXCommentContent.h"
- @implementation JXCommentContent
- #pragma mark - 初始化方法
- - (instancetype)init {
- self = [super init];
- if (self) {
- _commentId = 0;
- _userId = 0;
- _userName = @"";
- _userAvatar = @"";
- _content = @"";
- _likeCount = 0;
- _replyCount = 0;
- _isLiked = NO;
- _createdAt = 0;
- _isVerified = NO;
- }
- return self;
- }
- - (instancetype)initWithDictionary:(NSDictionary *)dict {
- self = [super init];
- if (self) {
- _commentId = [dict[@"id"] longLongValue];
- _userId = [dict[@"userId"] longLongValue];
- _userName = dict[@"userName"] ?: @"";
- _userAvatar = dict[@"userAvatar"] ?: @"";
- _content = dict[@"content"] ?: @"";
- _likeCount = [dict[@"likeCount"] longLongValue];
- _replyCount = [dict[@"replyCount"] longLongValue];
- _reply_count = [dict[@"reply_count"] longLongValue];
- _isLiked = [dict[@"isLiked"] boolValue];
- _createdAt = [dict[@"createdAt"] longLongValue];
- _isVerified = [dict[@"isVerified"] boolValue];
- }
- return self;
- }
- + (instancetype)commentWithDictionary:(NSDictionary *)dict {
- return [[self alloc] initWithDictionary:dict];
- }
- - (NSDictionary *)toDictionary {
- return @{
- @"id": @(self.commentId),
- @"userId": @(self.userId),
- @"userName": self.userName,
- @"userAvatar": self.userAvatar,
- @"content": self.content,
- @"likeCount": @(self.likeCount),
- @"replyCount": @(self.replyCount),
- @"reply_count": @(self.reply_count),
- @"isLiked": @(self.isLiked),
- @"createdAt": @(self.createdAt),
- @"isVerified": @(self.isVerified)
- };
- }
- #pragma mark - UI格式化方法
- - (NSString *)formattedTime {
- // 将毫秒时间戳转换为秒
- NSTimeInterval timestamp = self.createdAt / 1000.0;
- NSDate *date = [NSDate dateWithTimeIntervalSince1970:timestamp];
-
- // 获取当前日期和评论日期的年份
- NSCalendar *calendar = [NSCalendar currentCalendar];
- NSInteger currentYear = [calendar component:NSCalendarUnitYear fromDate:[NSDate date]];
- NSInteger commentYear = [calendar component:NSCalendarUnitYear fromDate:date];
-
- NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
-
- if (currentYear == commentYear) {
- // 今年内:显示 "月-日"
- [formatter setDateFormat:@"MM-dd"];
- } else {
- // 非今年:显示 "年-月-日"
- [formatter setDateFormat:@"yyyy-MM-dd"];
- }
-
- return [formatter stringFromDate:date];
- }
- - (NSString *)formattedLikeCount {
- return [self formatCount:self.likeCount];
- }
- - (NSString *)formatCount:(long long)count {
- if (count >= 10000) {
- return [NSString stringWithFormat:@"%.1fw", count / 10000.0];
- } else if (count >= 1000) {
- return [NSString stringWithFormat:@"%.1fk", count / 1000.0];
- } else {
- return [NSString stringWithFormat:@"%lld", count];
- }
- }
- #pragma mark - NSObject
- - (NSString *)description {
- return [NSString stringWithFormat:@"<JXCommentContent: id=%lld, userName=%@, content=%@, time=%@>",
- self.commentId,
- self.userName,
- self.content,
- [self formattedTime]];
- }
- @end
|