import Foundation /// 正式培训数据源;各资源独立读取,不为页面拼接全量任务聚合。 protocol TrainingDataServing: Sendable { /// 获取当前学员全局统计。 func summary() async throws -> TrainingSummary /// 按状态和关键词读取任务摘要页。 func tasks(status: TrainingLearnerStatus?, keyword: String, cursor: String?, limit: Int) async throws -> TrainingCursorPage /// 获取任务规则和快照引用。 func task(id: String) async throws -> TrainingTaskDetail /// 获取课程元信息,附当前授权任务。 func course(id: String, taskID: String) async throws -> TrainingCourseInfo /// 获取小节目录页。 func sections(courseID: String, taskID: String, cursor: String?, limit: Int) async throws -> TrainingCursorPage /// 读取一个小节的正式正文。 func section(courseID: String, sectionID: String, taskID: String) async throws -> TrainingSectionContent /// 读取附件元信息页。 func attachments(courseID: String, taskID: String, cursor: String?, limit: Int) async throws -> TrainingCursorPage /// 获取短期附件授权链接。 func attachmentURL(courseID: String, attachmentID: String, taskID: String) async throws -> TrainingAttachmentLink /// 刷新服务端已保存进度与操作资格。 func progress(taskID: String) async throws -> TrainingProgressSnapshot /// 按幂等键建立阅读会话。 func startReading(taskID: String, requestID: String) async throws -> TrainingStudySession /// 按会话序号确认有效读时;重试保持相同请求内容。 func heartbeat(sessionID: String, sequence: Int, activeSecondsTotal: Int, final: Bool) async throws -> TrainingHeartbeatConfirmation /// 读取考试规则,不获取全部试题。 func paper(id: String, taskID: String) async throws -> TrainingPaperInfo /// 按固定题位读取试题页。 func questions(paperID: String, taskID: String, offset: Int, limit: Int) async throws -> TrainingQuestionPage /// 获取草稿元信息及当前题位答案;limit=0 只读取元信息。 func draft(taskID: String, offset: Int, limit: Int) async throws -> TrainingDraftPage /// 串行增量保存本人选择。 func saveDraft(taskID: String, request: TrainingDraftSaveRequest) async throws -> TrainingDraftConfirmation /// 用户确认重考后重置草稿;响应是当前实况而非恒定空草稿。 func resetDraft(taskID: String, request: TrainingDraftResetRequest) async throws -> TrainingDraftConfirmation /// 消费已保存草稿版本并幂等交卷。 func submit(taskID: String, request: TrainingSubmissionRequest) async throws -> TrainingSubmissionResult /// 分页读取成绩;taskID+submissionID 可恢复一次交卷结果。 func attempts(taskID: String?, submissionID: String?, cursor: String?, limit: Int) async throws -> TrainingCursorPage /// 读取一条成绩和最新重考资格。 func attempt(id: String) async throws -> TrainingAttemptDetail /// 用户展开时读取解析页。 func reviews(attemptID: String, cursor: String?, limit: Int) async throws -> TrainingReviewPage } /// 培训稳定业务码及身份切换;保留服务器文案,恢复逻辑只依据枚举或 code。 enum TrainingRemoteError: Error, LocalizedError, Equatable, Sendable { case accountInvalid(String), forbidden(String), notWritable(String), readingRequired(String) case sessionExpired(String), sessionConflict(String), draftRevisionConflict(String), idempotencyConflict(String) case answerInvalid(String), answerIncomplete(String), examNotAllowed(String), contentMissing(String) case attemptNotFound(String), paramInvalid(String), identityChanged /// 返回稳定业务码,身份切换属于本地拒绝,没有服务端 code。 var code: Int? { switch self { case .accountInvalid: 150100 case .forbidden: 150101 case .notWritable: 150102 case .readingRequired: 150103 case .sessionExpired: 150104 case .sessionConflict: 150105 case .draftRevisionConflict: 150106 case .idempotencyConflict: 150107 case .answerInvalid: 150108 case .answerIncomplete: 150109 case .examNotAllowed: 150110 case .contentMissing: 150111 case .attemptNotFound: 150112 case .paramInvalid: 150113 case .identityChanged: nil } } /// 用于页面展示的错误文案,不参与错误分类。 var errorDescription: String? { switch self { case let .accountInvalid(message), let .forbidden(message), let .notWritable(message), let .readingRequired(message), let .sessionExpired(message), let .sessionConflict(message), let .draftRevisionConflict(message), let .idempotencyConflict(message), let .answerInvalid(message), let .answerIncomplete(message), let .examNotAllowed(message), let .contentMissing(message), let .attemptNotFound(message), let .paramInvalid(message): message case .identityChanged: "学习账号或业务身份已切换,请重新进入培训中心" } } /// 转换培训业务错误;网络和真正登录失效错误仍保留原类型。 static func map(_ error: Error) -> Error { guard let apiError = error as? APIError, case let .serverCode(code, message) = apiError else { return error } switch code { case 150100: return Self.accountInvalid(message) case 150101: return Self.forbidden(message) case 150102: return Self.notWritable(message) case 150103: return Self.readingRequired(message) case 150104: return Self.sessionExpired(message) case 150105: return Self.sessionConflict(message) case 150106: return Self.draftRevisionConflict(message) case 150107: return Self.idempotencyConflict(message) case 150108: return Self.answerInvalid(message) case 150109: return Self.answerIncomplete(message) case 150110: return Self.examNotAllowed(message) case 150111: return Self.contentMissing(message) case 150112: return Self.attemptNotFound(message) case 150113: return Self.paramInvalid(message) default: return error } } } /// 生产数据源固定进入时的账号和业务上下文,拒绝切换身份后的迟到响应。 enum TrainingRemoteServiceFactory { /// 当前店铺身份才可接入;Token 与业务范围固定在本次页面会话。 @MainActor static func production() -> any TrainingDataServing { let session = AppStore.shared.session let token = session.token let scope = session.accountCachePrefix let scenicID = session.currentScenicId let storeID = session.currentStoreId let eligible = !token.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty && session.accountType == .storeUser && !scope.isEmpty return NetworkServices.shared.trainingAPI.scoped(token: token, eligible: eligible, validateContext: { let current = AppStore.shared.session return current.isLoggedIn && current.accountType == .storeUser && current.accountCachePrefix == scope && current.token == token && current.currentScenicId == scenicID && current.currentStoreId == storeID }) } }