- 删除旧 ViewModel/Repository/Storage 层,简化架构 - 新增网络层:APIClient、APIConfig、APIError - 新增数据模型层:APIModels(含 20+ DTO) - 新增服务层:APIService(含 8 个业务服务类) - 各页面内联组件定义,消除跨文件依赖 - 修复 ZXQuickAction、ZXAIInteractionRow、ZXWeakRow 缺失 - 修复 ZXTabBtn 参数标签缺失 - 配置 ATS 例外允许 HTTP 请求 - API 地址指向线上服务器 81.70.187.179:3001
173 lines
5.4 KiB
Swift
173 lines
5.4 KiB
Swift
//
|
|
// APIService.swift - 各模块 API 调用封装
|
|
//
|
|
|
|
import Foundation
|
|
|
|
// MARK: - Waitlist
|
|
|
|
@MainActor
|
|
class WaitlistService {
|
|
static let shared = WaitlistService()
|
|
private let client = APIClient.shared
|
|
|
|
func join(email: String, nickname: String?, devices: [String]?,
|
|
interests: [String]?, painpoint: String?, willingBeta: Bool = true) async throws -> WaitlistResponse {
|
|
let body = WaitlistCreateRequest(email: email, nickname: nickname, devices: devices,
|
|
interests: interests, painpoint: painpoint, willingBeta: willingBeta)
|
|
return try await client.request("/waitlist", method: "POST", body: body)
|
|
}
|
|
|
|
func stats() async throws -> WaitlistStats {
|
|
return try await client.request("/waitlist/stats")
|
|
}
|
|
}
|
|
|
|
// MARK: - Auth
|
|
|
|
@MainActor
|
|
class AuthService {
|
|
static let shared = AuthService()
|
|
private let client = APIClient.shared
|
|
|
|
func appleLogin(identityToken: String, givenName: String?, familyName: String?) async throws -> AuthResponse {
|
|
let body = AppleAuthRequest(
|
|
identityToken: identityToken,
|
|
fullName: givenName != nil
|
|
? AppleAuthRequest.AppleFullName(givenName: givenName, familyName: familyName)
|
|
: nil
|
|
)
|
|
let resp: AuthResponse = try await client.request("/auth/apple", method: "POST", body: body)
|
|
if let token = resp.data?.accessToken {
|
|
await client.setToken(token)
|
|
}
|
|
return resp
|
|
}
|
|
|
|
func logout() async throws {
|
|
let _: GenericSuccessResponse = try await client.request("/auth/logout", method: "POST")
|
|
await client.setToken(nil)
|
|
}
|
|
}
|
|
|
|
// MARK: - User
|
|
|
|
@MainActor
|
|
class UserService {
|
|
static let shared = UserService()
|
|
private let client = APIClient.shared
|
|
|
|
func myProfile() async throws -> UserProfileResponse {
|
|
return try await client.request("/users/me")
|
|
}
|
|
|
|
func updateProfile(nickname: String?, preferences: UserPreferences?) async throws -> UserProfileResponse {
|
|
let body = UpdateUserRequest(nickname: nickname, preferences: preferences)
|
|
return try await client.request("/users/me", method: "PATCH", body: body)
|
|
}
|
|
}
|
|
|
|
// MARK: - Knowledge Base
|
|
|
|
@MainActor
|
|
class KnowledgeBaseService {
|
|
static let shared = KnowledgeBaseService()
|
|
private let client = APIClient.shared
|
|
|
|
func list() async throws -> KnowledgeBaseListResponse {
|
|
return try await client.request("/knowledge-bases")
|
|
}
|
|
|
|
func create(name: String, description: String?, icon: String?) async throws -> KnowledgeBaseListResponse {
|
|
let body = CreateKnowledgeBaseRequest(name: name, description: description, icon: icon)
|
|
return try await client.request("/knowledge-bases", method: "POST", body: body)
|
|
}
|
|
|
|
func detail(id: String) async throws -> KnowledgeBaseListResponse {
|
|
return try await client.request("/knowledge-bases/\(id)")
|
|
}
|
|
}
|
|
|
|
// MARK: - Knowledge Items
|
|
|
|
@MainActor
|
|
class KnowledgeItemService {
|
|
static let shared = KnowledgeItemService()
|
|
private let client = APIClient.shared
|
|
|
|
func list(baseId: String) async throws -> KnowledgeItemListResponse {
|
|
return try await client.request("/knowledge-items", queryItems: [URLQueryItem(name: "baseId", value: baseId)])
|
|
}
|
|
|
|
func detail(id: String) async throws -> KnowledgeItemListResponse {
|
|
return try await client.request("/knowledge-items/\(id)")
|
|
}
|
|
|
|
func create(baseId: String, title: String, content: String?, tags: [String]?) async throws -> KnowledgeItemListResponse {
|
|
let body = CreateKnowledgeItemRequest(title: title, content: content, baseId: baseId, tags: tags)
|
|
return try await client.request("/knowledge-items", method: "POST", body: body)
|
|
}
|
|
}
|
|
|
|
// MARK: - AI Analysis
|
|
|
|
@MainActor
|
|
class AIAnalysisService {
|
|
static let shared = AIAnalysisService()
|
|
private let client = APIClient.shared
|
|
|
|
func analyze(text: String, type: String = "weakness", context: AIAnalysisRequest.AIAnalysisContext? = nil) async throws -> AIAnalysisResponse {
|
|
let body = AIAnalysisRequest(text: text, type: type, context: context)
|
|
return try await client.request("/ai-analysis", method: "POST", body: body)
|
|
}
|
|
}
|
|
|
|
// MARK: - Activity & Stats
|
|
|
|
@MainActor
|
|
class ActivityService {
|
|
static let shared = ActivityService()
|
|
private let client = APIClient.shared
|
|
|
|
func summary() async throws -> ActivitySummaryResponse {
|
|
return try await client.request("/activity/summary")
|
|
}
|
|
}
|
|
|
|
// MARK: - Reviews
|
|
|
|
@MainActor
|
|
class ReviewService {
|
|
static let shared = ReviewService()
|
|
private let client = APIClient.shared
|
|
|
|
func due() async throws -> ReviewListResponse {
|
|
return try await client.request("/reviews/due")
|
|
}
|
|
}
|
|
|
|
// MARK: - Focus Items / Weak Points
|
|
|
|
@MainActor
|
|
class FocusItemService {
|
|
static let shared = FocusItemService()
|
|
private let client = APIClient.shared
|
|
|
|
func list() async throws -> FocusItemListResponse {
|
|
return try await client.request("/focus-items")
|
|
}
|
|
}
|
|
|
|
// MARK: - Feedback
|
|
|
|
@MainActor
|
|
class FeedbackService {
|
|
static let shared = FeedbackService()
|
|
private let client = APIClient.shared
|
|
|
|
func submit(type: String = "general", content: String, contact: String? = nil) async throws -> FeedbackResponse {
|
|
let body = FeedbackCreateRequest(type: type, content: content, contact: contact)
|
|
return try await client.request("/feedback", method: "POST", body: body)
|
|
}
|
|
}
|