Files
real-e-party-iOS/YuMi/E-P/Moments/Services/EPMomentAPISwiftHelper.swift.backup
2025-10-17 14:52:29 +08:00

113 lines
4.1 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// EPMomentAPISwiftHelper.swift
// YuMi
//
// Created by AI on 2025-10-11.
//
import Foundation
/// 动态 API 封装Swift 现代化版本)
/// 统一封装列表获取和发布功能,完全替代 OC 版本
@objc class EPMomentAPISwiftHelper: NSObject {
/// 拉取最新动态列表
/// - Parameters:
/// - nextID: 下一页 ID首次传空字符串
/// - completion: 成功回调 (动态列表, 下一页ID)
/// - failure: 失败回调 (错误码, 错误信息)
@objc func fetchLatestMomentsWithNextID(
_ nextID: String,
completion: @escaping ([MomentsInfoModel], String) -> Void,
failure: @escaping (Int, String) -> Void
) {
let pageSize = "20"
let types = "0,2" // 图片+文字
Api.momentsLatestList({ (data, code, msg) in
if code == 200, let dict = data?.data as? NSDictionary {
// 使用 MomentsListInfoModel 序列化响应数据(标准化方式)
// 参考: XPMomentsLatestPresenter.m line 25 / EPLoginService.swift line 34
// Swift 中使用 mj_object(withKeyValues:) 而不是 model(withJSON:)
if let listInfo = MomentsListInfoModel.mj_object(withKeyValues: dict) {
let dynamicList = listInfo.dynamicList
let nextDynamicId = listInfo.nextDynamicId
completion(dynamicList, nextDynamicId)
} else {
// 序列化失败时返回空数据
completion([], "")
}
} else {
failure(Int(code), msg ?? YMLocalizedString("error.request_failed"))
}
}, dynamicId: nextID, pageSize: pageSize, types: types)
}
/// 发布动态
/// - Parameters:
/// - type: "0"=纯文本, "2"=图片
/// - content: 文本内容
/// - resList: 图片信息数组
/// - completion: 成功回调
/// - failure: 失败回调 (错误码, 错误信息)
@objc func publishMoment(
type: String,
content: String,
resList: [[String: Any]],
completion: @escaping () -> Void,
failure: @escaping (Int, String) -> Void
) {
guard let uid = AccountInfoStorage.instance().getUid() else {
failure(-1, YMLocalizedString("error.not_logged_in"))
return
}
// worldId 传空字符串(话题功能不实现)
// NOTE: 旧版本 XPMonentsPublishViewController 包含话题选择功能
// 但实际业务中话题功能使用率低,新版本暂不实现
// 如需实现参考: YuMi/Modules/YMMonents/View/XPMonentsPublishTopicView
Api.momentsPublish({ (data, code, msg) in
if code == 200 {
completion()
} else {
failure(Int(code), msg ?? YMLocalizedString("error.publish_failed"))
}
}, 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, YMLocalizedString("error.not_logged_in"))
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 ?? YMLocalizedString("error.like_failed"))
}
}, dynamicId: dynamicId, uid: uid, status: status, likedUid: likedUid, worldId: worldIdStr)
}
}