feat: 添加点赞功能支持及 Swift API Helper 集成
主要变更: 1. 在 EPMomentAPISwiftHelper 中新增点赞/取消点赞功能,支持动态 ID 和用户 UID。 2. 更新 EPMomentCell 以使用新的 Swift API Helper 进行点赞操作,简化点赞逻辑。 3. 优化点赞状态和数量的更新逻辑,确保用户界面及时反映点赞结果。 此更新旨在提升用户互动体验,简化点赞操作流程。
This commit is contained in:
@@ -74,5 +74,38 @@ import Foundation
|
|||||||
}
|
}
|
||||||
}, uid: uid, type: type, worldId: "", content: content, resList: resList)
|
}, 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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -9,10 +9,10 @@
|
|||||||
#import "EPMomentCell.h"
|
#import "EPMomentCell.h"
|
||||||
#import "MomentsInfoModel.h"
|
#import "MomentsInfoModel.h"
|
||||||
#import "AccountInfoStorage.h"
|
#import "AccountInfoStorage.h"
|
||||||
#import "Api+Moments.h"
|
|
||||||
#import "NetImageView.h"
|
#import "NetImageView.h"
|
||||||
#import "EPEmotionColorStorage.h"
|
#import "EPEmotionColorStorage.h"
|
||||||
#import "SDPhotoBrowser.h"
|
#import "SDPhotoBrowser.h"
|
||||||
|
#import "YuMi-Swift.h" // Swift 互操作
|
||||||
|
|
||||||
@interface EPMomentCell () <SDPhotoBrowserDelegate>
|
@interface EPMomentCell () <SDPhotoBrowserDelegate>
|
||||||
|
|
||||||
@@ -51,6 +51,9 @@
|
|||||||
/// 当前数据模型
|
/// 当前数据模型
|
||||||
@property (nonatomic, strong) MomentsInfoModel *currentModel;
|
@property (nonatomic, strong) MomentsInfoModel *currentModel;
|
||||||
|
|
||||||
|
/// API Helper (Swift 版本)
|
||||||
|
@property (nonatomic, strong) EPMomentAPISwiftHelper *apiHelper;
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@implementation EPMomentCell
|
@implementation EPMomentCell
|
||||||
@@ -307,31 +310,32 @@
|
|||||||
- (void)performLikeAction:(BOOL)isLike {
|
- (void)performLikeAction:(BOOL)isLike {
|
||||||
NSLog(@"[EPMomentCell] %@ 动态: %@", isLike ? @"点赞" : @"取消点赞", self.currentModel.dynamicId);
|
NSLog(@"[EPMomentCell] %@ 动态: %@", isLike ? @"点赞" : @"取消点赞", self.currentModel.dynamicId);
|
||||||
|
|
||||||
NSString *uid = [[AccountInfoStorage instance] getUid];
|
|
||||||
NSString *dynamicId = self.currentModel.dynamicId;
|
NSString *dynamicId = self.currentModel.dynamicId;
|
||||||
NSString *status = isLike ? @"1" : @"0"; // 0=取消,1=点赞
|
|
||||||
NSString *likedUid = self.currentModel.uid;
|
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) {
|
// 使用 Swift API Helper
|
||||||
if (code == 200) {
|
[self.apiHelper likeMomentWithDynamicId:dynamicId
|
||||||
// 更新点赞状态
|
isLike:isLike
|
||||||
self.currentModel.isLike = isLike;
|
likedUid:likedUid
|
||||||
NSInteger likeCount = [self.currentModel.likeCount integerValue];
|
worldId:worldId
|
||||||
likeCount += isLike ? 1 : -1;
|
completion:^{
|
||||||
likeCount = MAX(0, likeCount); // 防止负数
|
// 更新点赞状态
|
||||||
self.currentModel.likeCount = @(likeCount).stringValue;
|
self.currentModel.isLike = isLike;
|
||||||
|
NSInteger likeCount = [self.currentModel.likeCount integerValue];
|
||||||
// 更新 UI
|
likeCount += isLike ? 1 : -1;
|
||||||
self.likeButton.selected = self.currentModel.isLike;
|
likeCount = MAX(0, likeCount); // 防止负数
|
||||||
[self.likeButton setTitle:[NSString stringWithFormat:@" %ld", (long)likeCount] forState:UIControlStateNormal];
|
self.currentModel.likeCount = @(likeCount).stringValue;
|
||||||
[self.likeButton setTitle:[NSString stringWithFormat:@" %ld", (long)likeCount] forState:UIControlStateSelected];
|
|
||||||
|
// 更新 UI
|
||||||
NSLog(@"[EPMomentCell] %@ 成功", isLike ? @"点赞" : @"取消点赞");
|
self.likeButton.selected = self.currentModel.isLike;
|
||||||
} else {
|
[self.likeButton setTitle:[NSString stringWithFormat:@" %ld", (long)likeCount] forState:UIControlStateNormal];
|
||||||
NSLog(@"[EPMomentCell] %@ 失败: %@", isLike ? @"点赞" : @"取消点赞", msg);
|
[self.likeButton setTitle:[NSString stringWithFormat:@" %ld", (long)likeCount] forState:UIControlStateSelected];
|
||||||
}
|
|
||||||
} dynamicId:dynamicId uid:uid status:status likedUid:likedUid worldId:worldId];
|
NSLog(@"[EPMomentCell] %@ 成功", isLike ? @"点赞" : @"取消点赞");
|
||||||
|
} failure:^(NSInteger code, NSString * _Nonnull msg) {
|
||||||
|
NSLog(@"[EPMomentCell] %@ 失败 (code: %ld): %@", isLike ? @"点赞" : @"取消点赞", (long)code, msg);
|
||||||
|
}];
|
||||||
}
|
}
|
||||||
|
|
||||||
// 评论功能已隐藏
|
// 评论功能已隐藏
|
||||||
@@ -481,4 +485,11 @@
|
|||||||
return _imageViews;
|
return _imageViews;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (EPMomentAPISwiftHelper *)apiHelper {
|
||||||
|
if (!_apiHelper) {
|
||||||
|
_apiHelper = [[EPMomentAPISwiftHelper alloc] init];
|
||||||
|
}
|
||||||
|
return _apiHelper;
|
||||||
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
Reference in New Issue
Block a user