diff --git a/YuMi/E-P/NewMoments/Services/EPMomentAPISwiftHelper.swift b/YuMi/E-P/NewMoments/Services/EPMomentAPISwiftHelper.swift index 56f3cd7..ad6aa06 100644 --- a/YuMi/E-P/NewMoments/Services/EPMomentAPISwiftHelper.swift +++ b/YuMi/E-P/NewMoments/Services/EPMomentAPISwiftHelper.swift @@ -74,5 +74,38 @@ import Foundation } }, uid: uid, type: type, worldId: "", content: content, resList: resList) } + + /// 点赞/取消点赞动态 + /// - Parameters: + /// - dynamicId: 动态 ID + /// - isLike: true=点赞,false=取消点赞 + /// - likedUid: 动态发布者 UID + /// - worldId: 话题 ID + /// - completion: 成功回调 + /// - failure: 失败回调 (错误码, 错误信息) + @objc func likeMoment( + dynamicId: String, + isLike: Bool, + likedUid: String, + worldId: Int, + completion: @escaping () -> Void, + failure: @escaping (Int, String) -> Void + ) { + guard let uid = AccountInfoStorage.instance().getUid() else { + failure(-1, "用户未登录") + return + } + + let status = isLike ? "1" : "0" + let worldIdStr = String(format: "%ld", worldId) + + Api.momentsLike({ (data, code, msg) in + if code == 200 { + completion() + } else { + failure(Int(code), msg ?? "点赞操作失败") + } + }, dynamicId: dynamicId, uid: uid, status: status, likedUid: likedUid, worldId: worldIdStr) + } } diff --git a/YuMi/E-P/NewMoments/Views/EPMomentCell.m b/YuMi/E-P/NewMoments/Views/EPMomentCell.m index 4c811be..4d69453 100644 --- a/YuMi/E-P/NewMoments/Views/EPMomentCell.m +++ b/YuMi/E-P/NewMoments/Views/EPMomentCell.m @@ -9,10 +9,10 @@ #import "EPMomentCell.h" #import "MomentsInfoModel.h" #import "AccountInfoStorage.h" -#import "Api+Moments.h" #import "NetImageView.h" #import "EPEmotionColorStorage.h" #import "SDPhotoBrowser.h" +#import "YuMi-Swift.h" // Swift 互操作 @interface EPMomentCell () @@ -51,6 +51,9 @@ /// 当前数据模型 @property (nonatomic, strong) MomentsInfoModel *currentModel; +/// API Helper (Swift 版本) +@property (nonatomic, strong) EPMomentAPISwiftHelper *apiHelper; + @end @implementation EPMomentCell @@ -307,31 +310,32 @@ - (void)performLikeAction:(BOOL)isLike { NSLog(@"[EPMomentCell] %@ 动态: %@", isLike ? @"点赞" : @"取消点赞", self.currentModel.dynamicId); - NSString *uid = [[AccountInfoStorage instance] getUid]; NSString *dynamicId = self.currentModel.dynamicId; - NSString *status = isLike ? @"1" : @"0"; // 0=取消,1=点赞 NSString *likedUid = self.currentModel.uid; - NSString *worldId = [NSString stringWithFormat:@"%ld", self.currentModel.worldId]; + long worldId = self.currentModel.worldId; - [Api momentsLike:^(BaseModel * _Nullable data, NSInteger code, NSString * _Nullable msg) { - if (code == 200) { - // 更新点赞状态 - self.currentModel.isLike = isLike; - NSInteger likeCount = [self.currentModel.likeCount integerValue]; - likeCount += isLike ? 1 : -1; - likeCount = MAX(0, likeCount); // 防止负数 - self.currentModel.likeCount = @(likeCount).stringValue; - - // 更新 UI - self.likeButton.selected = self.currentModel.isLike; - [self.likeButton setTitle:[NSString stringWithFormat:@" %ld", (long)likeCount] forState:UIControlStateNormal]; - [self.likeButton setTitle:[NSString stringWithFormat:@" %ld", (long)likeCount] forState:UIControlStateSelected]; - - NSLog(@"[EPMomentCell] %@ 成功", isLike ? @"点赞" : @"取消点赞"); - } else { - NSLog(@"[EPMomentCell] %@ 失败: %@", isLike ? @"点赞" : @"取消点赞", msg); - } - } dynamicId:dynamicId uid:uid status:status likedUid:likedUid worldId:worldId]; + // 使用 Swift API Helper + [self.apiHelper likeMomentWithDynamicId:dynamicId + isLike:isLike + likedUid:likedUid + worldId:worldId + completion:^{ + // 更新点赞状态 + self.currentModel.isLike = isLike; + NSInteger likeCount = [self.currentModel.likeCount integerValue]; + likeCount += isLike ? 1 : -1; + likeCount = MAX(0, likeCount); // 防止负数 + self.currentModel.likeCount = @(likeCount).stringValue; + + // 更新 UI + self.likeButton.selected = self.currentModel.isLike; + [self.likeButton setTitle:[NSString stringWithFormat:@" %ld", (long)likeCount] forState:UIControlStateNormal]; + [self.likeButton setTitle:[NSString stringWithFormat:@" %ld", (long)likeCount] forState:UIControlStateSelected]; + + NSLog(@"[EPMomentCell] %@ 成功", isLike ? @"点赞" : @"取消点赞"); + } failure:^(NSInteger code, NSString * _Nonnull msg) { + NSLog(@"[EPMomentCell] %@ 失败 (code: %ld): %@", isLike ? @"点赞" : @"取消点赞", (long)code, msg); + }]; } // 评论功能已隐藏 @@ -481,4 +485,11 @@ return _imageViews; } +- (EPMomentAPISwiftHelper *)apiHelper { + if (!_apiHelper) { + _apiHelper = [[EPMomentAPISwiftHelper alloc] init]; + } + return _apiHelper; +} + @end