66 lines
2.3 KiB
Swift
66 lines
2.3 KiB
Swift
import Foundation
|
||
|
||
class LearningService {
|
||
static let shared = LearningService()
|
||
private let apiClient = APIClient.shared
|
||
|
||
// 获取内容
|
||
func getNodeContent(nodeId: String) async throws -> LessonData {
|
||
// ✨ 游客模式下不传 Token(支持游客浏览)
|
||
// 直接检查 Token 是否存在,而不是依赖 isLoggedIn(可能有过期 Token)
|
||
let hasToken = TokenManager.shared.getToken() != nil
|
||
let response: LessonResponse = try await apiClient.request(
|
||
endpoint: "/api/lessons/\(nodeId)/content",
|
||
method: "GET",
|
||
requiresAuth: hasToken // 有 Token 才传,没有就不传(游客模式)
|
||
)
|
||
|
||
return response.data
|
||
}
|
||
|
||
// 开始学习
|
||
func startLesson(nodeId: String) async throws {
|
||
let _: StartLessonResponse = try await apiClient.request(
|
||
endpoint: "/api/lessons/\(nodeId)/start",
|
||
method: "POST",
|
||
requiresAuth: true
|
||
)
|
||
}
|
||
|
||
// 上报进度
|
||
func updateProgress(nodeId: String, currentSlide: Int, studyTime: Int) async {
|
||
do {
|
||
let _: SimpleResponse = try await apiClient.request(
|
||
endpoint: "/api/lessons/\(nodeId)/progress",
|
||
method: "POST",
|
||
body: ["current_slide": currentSlide, "study_time": studyTime],
|
||
requiresAuth: true
|
||
)
|
||
} catch {
|
||
print("Progress update failed: \(error)")
|
||
}
|
||
}
|
||
|
||
// 完成学习
|
||
func completeLesson(nodeId: String, totalStudyTime: Int, completedSlides: Int) async throws -> CompleteLessonData {
|
||
let response: CompleteLessonResponse = try await apiClient.request(
|
||
endpoint: "/api/lessons/\(nodeId)/complete",
|
||
method: "POST",
|
||
body: ["total_study_time": totalStudyTime, "completed_slides": completedSlides],
|
||
requiresAuth: true
|
||
)
|
||
return response.data
|
||
}
|
||
|
||
// 获取总结
|
||
func getLessonSummary(nodeId: String) async throws -> LessonSummaryData {
|
||
let response: LessonSummaryResponse = try await apiClient.request(
|
||
endpoint: "/api/lessons/\(nodeId)/summary",
|
||
method: "GET",
|
||
requiresAuth: true
|
||
)
|
||
return response.data
|
||
}
|
||
}
|
||
|