feat: 完善机巢模拟调试与素材交付交互

增加禁飞期间的设备指令拦截及本地模拟拍摄流程;适配订单素材库和景区空境库,完善素材筛选、预览与下载;订单交付入口改为方式选择弹窗并复用云端上传。补充单元测试、接口记录和模拟器截图。
This commit is contained in:
2026-10-30 09:59:11 +08:00
parent e827bf8725
commit b95b251de1
100 changed files with 7256 additions and 500 deletions
@@ -24,6 +24,25 @@ final class DroneDockMaterialsViewModel {
private(set) var tasksErrorMessage: String?
/// 点位标签始终来自整单购买快照,不从当前任务推导。
var spots: [DroneDockMaterialSpot] { detail?.spots ?? [] }
/// 当前范围内已选择的就绪原素材,不包含独立交付成片。
private(set) var selectedMaterialIDs: Set<DroneDockMaterialSelectionID> = []
/// 只有完整读取成功且具备有效文件地址的原素材允许选择。
var selectableMaterialIDs: Set<DroneDockMaterialSelectionID> {
guard detail != nil, !isLoading, !isLoadingTasks,
detailErrorMessage == nil, tasksErrorMessage == nil else { return [] }
return Set(tasks.flatMap { task in
task.materials.filter { $0.previewURL != nil }.map {
DroneDockMaterialSelectionID(taskID: task.id, fileID: $0.id)
}
})
}
/// 当前选择数量仅用于界面展示,不表示已经下载或保存。
var selectedCount: Int { selectedMaterialIDs.count }
/// 空列表不展示全选;全部当前可用原素材被选中时才标记全选。
var allDownloadableSelected: Bool {
let selectable = selectableMaterialIDs
return !selectable.isEmpty && selectedMaterialIDs == selectable
}
private let service: any DroneDockMaterialsServing
private let currentContext: @MainActor @Sendable () -> DroneDockHomeContext
@@ -39,6 +58,38 @@ final class DroneDockMaterialsViewModel {
self.currentContext = currentContext
}
/// 勾选只修改本地状态;无效、未就绪或其他任务的素材不会进入选择集合。
@MainActor
func toggleSelection(taskID: Int, fileID: Int) {
guard acceptsSelectionContext() else { return }
let identifier = DroneDockMaterialSelectionID(taskID: taskID, fileID: fileID)
guard selectableMaterialIDs.contains(identifier) else { return }
if selectedMaterialIDs.contains(identifier) {
selectedMaterialIDs.remove(identifier)
} else {
selectedMaterialIDs.insert(identifier)
}
}
/// 全选仅覆盖当前点位可用的原素材,不跨点位、订单或成片扩大范围。
@MainActor
func toggleSelectAll() {
guard acceptsSelectionContext() else { return }
let selectable = selectableMaterialIDs
guard !selectable.isEmpty else { return }
selectedMaterialIDs = selectedMaterialIDs == selectable ? [] : selectable
}
@MainActor
private func acceptsSelectionContext() -> Bool {
guard context.hasSameIdentity(as: currentContext()) else {
invalidate()
detailErrorMessage = "账号或景区已变化,请返回工作台重新进入"
return false
}
return true
}
/// 先刷新整单摘要,再读取任务;保留仍在购买快照中的点位筛选。
@MainActor
func refresh() async {
@@ -156,6 +207,7 @@ final class DroneDockMaterialsViewModel {
@MainActor
private func clearTasks() {
tasks = []
selectedMaterialIDs = []
total = nil
tasksErrorMessage = nil
}
@@ -43,6 +43,7 @@ final class DroneDockPreparationViewModel {
private let planeStatusService: any DroneDockPlaneStatusServing
private let taskCreationService: any DroneDockTaskCreating
private let shootService: any DroneDockShootServing
private let allowsTaskSubmission: Bool
private let currentContext: @MainActor @Sendable () -> DroneDockHomeContext
private var generation = 0
private var planeStatusGeneration = 0
@@ -62,6 +63,7 @@ final class DroneDockPreparationViewModel {
planeStatusService: any DroneDockPlaneStatusServing,
taskCreationService: any DroneDockTaskCreating,
shootService: any DroneDockShootServing,
allowsTaskSubmission: Bool = true,
currentContext: @escaping @MainActor @Sendable () -> DroneDockHomeContext) {
self.context = context
self.order = order
@@ -69,12 +71,13 @@ final class DroneDockPreparationViewModel {
self.planeStatusService = planeStatusService
self.taskCreationService = taskCreationService
self.shootService = shootService
self.allowsTaskSubmission = allowsTaskSubmission
self.currentContext = currentContext
}
/// 当前阶段只创建待飞任务,不以设备电量或位置作为创建任务条件。
var canCreateTask: Bool {
guard context.canLoad, !creationContextInvalidated, !isLoading, !isCreatingTask,
guard allowsTaskSubmission, context.canLoad, !creationContextInvalidated, !isLoading, !isCreatingTask,
!hasCreatedTask, !taskCreationOutcomeUnknown, errorMessage == nil,
let info = projectInfo, info.order.canCreateTask == true,
let total = info.order.shotTotal, total > 0,
@@ -86,6 +89,9 @@ final class DroneDockPreparationViewModel {
return info.order.orderNumber == number && info.order.projectID == projectID
}
/// 当前入口是否被禁飞限制禁止提交,用于提供独立模拟入口而不放开真实写入。
var isTaskSubmissionBlocked: Bool { !allowsTaskSubmission }
/// 提交中和已有回执/不确定结果时冻结点位航线,避免显示与任务归属不一致。
var isTaskSelectionLocked: Bool {
isCreatingTask || isSummoning || isPerformingPrimaryAction || hasCreatedTask || taskCreationOutcomeUnknown
@@ -107,7 +113,7 @@ final class DroneDockPreparationViewModel {
/// 首次允许创建再召唤;明确业务拒绝后可对同任务重试,成功和未知结果锁定。
var isPrimaryActionEnabled: Bool {
guard context.canLoad, !creationContextInvalidated, !isLoading, !isCreatingTask,
guard allowsTaskSubmission, context.canLoad, !creationContextInvalidated, !isLoading, !isCreatingTask,
!isSummoning, !isPerformingPrimaryAction, !taskCreationOutcomeUnknown,
!hasSubmittedSummon, !summonOutcomeUnknown else { return false }
if let createdTask { return createdTask.taskID > 0 }
@@ -117,7 +123,8 @@ final class DroneDockPreparationViewModel {
/// 主按钮串接创建与召唤;已有任务跳过创建,成功后的导航由页面处理。
@MainActor
func prepareAndSummon(onStateChange: @escaping @MainActor () -> Void = {}) async {
guard isPrimaryActionEnabled else { return }
// 禁飞调试在整条链开始前阻断,避免先残留真实待飞任务再拦截召唤。
guard allowsTaskSubmission, isPrimaryActionEnabled else { return }
primaryActionGeneration &+= 1
let actionGeneration = primaryActionGeneration
isPerformingPrimaryAction = true
@@ -136,7 +143,7 @@ final class DroneDockPreparationViewModel {
/// 对明确创建成功的任务发送一次召唤;只有明确业务拒绝允许之后再次提交。
@MainActor
private func summonCreatedTask(onStateChange: @escaping @MainActor () -> Void) async {
guard !isCreatingTask, !isSummoning, !taskCreationOutcomeUnknown,
guard allowsTaskSubmission, !isCreatingTask, !isSummoning, !taskCreationOutcomeUnknown,
!hasSubmittedSummon, !summonOutcomeUnknown,
let taskID = createdTask?.taskID, taskID > 0 else { return }
summonGeneration &+= 1
@@ -162,8 +169,13 @@ final class DroneDockPreparationViewModel {
hasSubmittedSummon = true
summonOutcomeUnknown = false
} catch {
if error is DeviceCommandDebugBlockedError, summonGeneration == requestGeneration {
didSendSummon = false
}
guard acceptsSummon(requestGeneration, taskID: taskID) else { return }
if case APIError.serverCode = error {
if error is DeviceCommandDebugBlockedError {
shootErrorMessage = DeviceCommandDebugPolicy.blockedMessage
} else if case APIError.serverCode = error {
shootErrorMessage = error.localizedDescription
} else {
markSummonUnknown(reason: error.localizedDescription)
@@ -189,7 +201,7 @@ final class DroneDockPreparationViewModel {
/// 点击后先校验最新购买配置,再发送一次创建请求;成功留在准备页供核对。
@MainActor
func createTask(onStateChange: @escaping @MainActor () -> Void = {}) async {
guard canCreateTask, let spotID = selectedSpotID, let routeID = selectedRouteID,
guard allowsTaskSubmission, canCreateTask, let spotID = selectedSpotID, let routeID = selectedRouteID,
let number = order.orderNumber, let projectID = order.projectID else { return }
taskCreationGeneration &+= 1
let requestGeneration = taskCreationGeneration
@@ -238,8 +250,13 @@ final class DroneDockPreparationViewModel {
hasCreatedTask = true
taskCreationOutcomeUnknown = false
} catch {
if error is DeviceCommandDebugBlockedError, taskCreationGeneration == requestGeneration {
didSendTaskCreation = false
}
guard acceptsTaskCreation(requestGeneration) else { return }
if didSendTaskCreation {
if error is DeviceCommandDebugBlockedError {
taskCreationErrorMessage = DeviceCommandDebugPolicy.blockedMessage
} else if didSendTaskCreation {
if case APIError.serverCode = error {
taskCreationErrorMessage = error.localizedDescription
} else {
@@ -414,6 +431,7 @@ final class DroneDockPreparationViewModel {
if isCreatingTask { return "正在创建任务…" }
if isSummoning { return "正在召唤无人机…" }
if hasSubmittedSummon { return "查看拍摄任务" }
if !allowsTaskSubmission { return "仅限模拟调试" }
if summonOutcomeUnknown { return "召唤结果待核对" }
if hasCreatedTask { return shootErrorMessage == nil ? "确认并召唤" : "重试召唤" }
if taskCreationOutcomeUnknown { return "结果待核对" }
@@ -426,6 +444,7 @@ final class DroneDockPreparationViewModel {
if isCreatingTask { return "正在创建待飞任务,请勿重复点击" }
if isSummoning { return "任务已创建,正在提交召唤请求" }
if creationContextInvalidated, let errorMessage { return errorMessage }
if !allowsTaskSubmission && !hasSubmittedSummon { return DeviceCommandDebugPolicy.blockedMessage }
if let createdTask {
if hasSubmittedSummon { return "任务 \(createdTask.taskID) 的召唤请求已提交,请等待任务状态更新" }
if let shootErrorMessage { return shootErrorMessage }
@@ -1,6 +1,6 @@
import Foundation
/// 真实拍摄任务页只读状态,只按准备页传入的既有任务 ID 读取 shooting。
/// 拍摄任务状态及可选开拍流程,始终复用同一任务;禁飞调试仅为本地模拟注入开拍服务。
final class DroneDockShootingTaskViewModel {
/// 页面绑定的账号、景区和门店身份。
let context: DroneDockHomeContext
@@ -14,27 +14,47 @@ final class DroneDockShootingTaskViewModel {
private(set) var isLoading = false
/// 读取失败或身份变化时的提示。
private(set) var errorMessage: String?
/// 开拍提交及其后单次状态核对期间禁用重复操作,保留之前的显示快照。
private(set) var isStartingShooting = false
/// 开拍业务拒绝、本地拦截或结果待核对的说明。
private(set) var startErrorMessage: String?
/// 已发送但无法确认的开拍请求,不因后续读取仍允许开始而自动解除。
private(set) var startOutcomeUnknown = false
/// 当前页面是否已收到明确开拍受理回执,收到后不会再次提交。
private(set) var hasSubmittedStart = false
/// 同任务开拍回执;受理成功不直接替代 shooting 的完整状态快照。
private(set) var startResponse: DroneDockStartShootResponse?
private let service: any DroneDockShootingServing
private let startService: (any DroneDockStartShooting)?
private let currentContext: @MainActor @Sendable () -> DroneDockHomeContext
private var generation = 0
private var identityInvalidated = false
private var startGeneration = 0
private var didSendStart = false
/// 只注入读取服务,不依赖创建、召唤、开始拍摄或控制服务。
/// 默认只读;只有显式注入开拍服务才提供开拍动作,当前正式入口不注入该服务。
init(context: DroneDockHomeContext, taskID: Int, order: DroneDockHomeOrder,
service: any DroneDockShootingServing,
startService: (any DroneDockStartShooting)? = nil,
currentContext: @escaping @MainActor @Sendable () -> DroneDockHomeContext) {
self.context = context
self.taskID = taskID
self.order = order
self.service = service
self.startService = startService
self.currentContext = currentContext
}
/// 页面可见时读取同一任务;刷新及普通失败保留旧快照,身份失效立即清除。
@MainActor
func refresh() async {
guard !isLoading, !identityInvalidated else { return }
await refreshSnapshot(allowWhileStarting: false)
}
@MainActor
private func refreshSnapshot(allowWhileStarting: Bool) async {
guard !isLoading, !identityInvalidated, !isStartingShooting || allowWhileStarting else { return }
generation &+= 1
let requestGeneration = generation
isLoading = true
@@ -68,11 +88,14 @@ final class DroneDockShootingTaskViewModel {
/// 使旧请求失效;同一身份离页或进入后台可保留快照,账号变化始终清除并停止旧任务读取。
@MainActor
func invalidate(preservingSnapshot: Bool = false) {
invalidateStartShooting()
generation &+= 1
isLoading = false
if !context.hasSameIdentity(as: currentContext()) { identityInvalidated = true }
if identityInvalidated {
response = nil
startResponse = nil
startErrorMessage = nil
errorMessage = "账号或景区已变化,请返回工作台重新进入"
} else {
if !preservingSnapshot { response = nil }
@@ -80,6 +103,83 @@ final class DroneDockShootingTaskViewModel {
}
}
/// 只在有效读取明确到达、锁定飞机并允许 start_shoot 后开放;默认真实页面保持禁用。
var canStartShooting: Bool {
startService != nil && context.canLoad && taskID > 0 && !identityInvalidated
&& !isLoading && !isStartingShooting && errorMessage == nil
&& !hasSubmittedStart && !startOutcomeUnknown
&& resolvedDisplayPhase().0 == .ready
&& response?.executeGate?.canExecute == true
&& response?.executeGate?.primaryAction == "start_shoot"
&& (response?.currentPlane?.id ?? 0) > 0
}
/// 提交一次开拍并单次读取同任务;失败保留原快照,不轮询、不重建、不自动重发。
@MainActor
func startShooting(onStateChange: @escaping @MainActor () -> Void = {}) async {
guard canStartShooting, let startService else { return }
guard context.hasSameIdentity(as: currentContext()) else {
identityInvalidated = true
invalidate()
onStateChange()
return
}
guard !Task.isCancelled else { return }
let planeID = response?.currentPlane?.id
startGeneration &+= 1
let requestGeneration = startGeneration
isStartingShooting = true
didSendStart = false
startErrorMessage = nil
onStateChange()
defer {
if startGeneration == requestGeneration {
if Task.isCancelled && didSendStart && !hasSubmittedStart { markStartUnknown() }
isStartingShooting = false
didSendStart = false
onStateChange()
}
}
guard acceptsStart(requestGeneration) else { return }
do {
didSendStart = true
let value = try await startService.startShooting(taskID: taskID)
guard acceptsStart(requestGeneration) else { return }
guard value.orderFlightTaskID == taskID,
value.orderFlightTask?.id == nil || value.orderFlightTask?.id == taskID,
value.orderFlightTask?.orderNumber == nil || value.orderFlightTask?.orderNumber == order.orderNumber,
value.orderFlightTask?.scenicID == nil || value.orderFlightTask?.scenicID == context.scenicID,
value.planeID == nil || value.planeID == planeID else {
throw APIError.decodeFailed("开始拍摄回执与当前任务或飞机不一致,请先核对任务状态")
}
startResponse = value
hasSubmittedStart = true
startOutcomeUnknown = false
onStateChange()
guard acceptsStart(requestGeneration) else { return }
await refreshSnapshot(allowWhileStarting: true)
} catch {
if error is DeviceCommandDebugBlockedError, startGeneration == requestGeneration { didSendStart = false }
guard acceptsStart(requestGeneration) else { return }
if error is DeviceCommandDebugBlockedError {
startErrorMessage = DeviceCommandDebugPolicy.blockedMessage
} else if case APIError.serverCode = error {
startErrorMessage = error.localizedDescription
} else {
markStartUnknown(reason: error.localizedDescription)
}
}
}
/// 离页或进入后台后拒绝迟到回执;已发而未确认的请求保留待核对锁。
@MainActor
func invalidateStartShooting() {
if isStartingShooting && didSendStart && !hasSubmittedStart { markStartUnknown() }
startGeneration &+= 1
isStartingShooting = false
didSendStart = false
}
/// 优先使用当前接口标题,缺失时展示当前接口的点位和航线名称。
var title: String {
if let title = usable(response?.header?.title) { return title }
@@ -111,13 +211,18 @@ final class DroneDockShootingTaskViewModel {
?? usable(response?.orderFlightTask?.statusLabel) ?? "状态待确认"
}
/// 保留服务端阶段说明与执行门禁原因;本模块不执行门禁给出的动作。
/// 保留服务端阶段说明;未确认的开拍结果明确提示核对,不伪装为正在拍摄。
var statusMessage: String {
if response == nil {
if let errorMessage { return errorMessage }
if isLoading { return "正在读取当前拍摄任务" }
}
if resolvedDisplayPhase().0 == .failed { return failureMessage }
if startOutcomeUnknown, let startErrorMessage { return startErrorMessage }
if resolvedDisplayPhase().0 == .ready {
if let startErrorMessage { return startErrorMessage }
if hasSubmittedStart { return "开始拍摄请求已受理,请刷新确认任务状态" }
}
return usable(response?.ui?.message) ?? usable(response?.executeGate?.message) ?? "—"
}
@@ -138,10 +243,15 @@ final class DroneDockShootingTaskViewModel {
let actionTitle: String?
let enabled: Bool
switch phase {
case .ready where hasSubmittedStart || startOutcomeUnknown:
action = .refresh
actionTitle = "刷新状态"
enabled = !identityInvalidated
case .ready:
action = .start
actionTitle = usable(response?.executeGate?.primaryActionLabel) ?? "开始拍摄"
enabled = false
actionTitle = isStartingShooting ? "正在提交开拍…"
: startErrorMessage == nil ? (usable(response?.executeGate?.primaryActionLabel) ?? "开始拍摄") : "重试开始拍摄"
enabled = canStartShooting
case .paused where response?.executeGate?.canExecute == true:
action = .resume
actionTitle = usable(response?.executeGate?.primaryActionLabel) ?? "继续拍摄"
@@ -161,7 +271,7 @@ final class DroneDockShootingTaskViewModel {
}
return DroneDockTaskPresentation(phase: phase, title: title, message: message,
deviceStatus: deviceStatusText, action: action, actionTitle: actionTitle,
isActionEnabled: enabled && !isLoading && !identityInvalidated && (errorMessage == nil || action == .refresh),
isActionEnabled: enabled && !isLoading && !isStartingShooting && !identityInvalidated && (errorMessage == nil || action == .refresh),
actionNote: phase == .failed ? retryUnavailableNote : nil)
}
@@ -189,6 +299,22 @@ final class DroneDockShootingTaskViewModel {
return !Task.isCancelled
}
@MainActor
private func acceptsStart(_ requestGeneration: Int) -> Bool {
guard startGeneration == requestGeneration else { return false }
guard context.hasSameIdentity(as: currentContext()), !identityInvalidated else {
identityInvalidated = true
invalidate()
return false
}
return !Task.isCancelled
}
private func markStartUnknown(reason: String? = nil) {
startOutcomeUnknown = true
startErrorMessage = (reason.map { "\($0)。" } ?? "") + "开始拍摄结果待核对,请刷新任务状态,勿重复提交"
}
private func matchesIdentity(_ value: DroneDockShootingResponse) -> Bool {
let taskIDs = [value.orderFlightTask?.id, value.task?.id].compactMap { $0 }
let orderNumbers = [value.orderNumber, value.orderFlightTask?.orderNumber,