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
@@ -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,