feat(drone-dock): 接入已购套餐快照并固定首页无人机占位

准备页改用 project-info 购买快照,校验订单与项目身份,并按可用性选择已购点位和航线。首页在加载、空数据及失败时保留等高无人机卡片。同步相关测试与接口联调记录、响应和截图。

验证:iPhone 18 Pro 模拟器上 81 项相关测试通过。
This commit is contained in:
2026-10-28 16:50:35 +08:00
parent d01d18d5b3
commit 2793f6b113
58 changed files with 3890 additions and 717 deletions
@@ -1,187 +1,154 @@
import CoreLocation
import Foundation
/// 真实拍摄准备状态:读取订单、定位和点位航线,选择过程不创建或执行飞行任务。
/// 拍摄准备状态:只读取张工的已购配置,不创建飞行任务。
final class DroneDockPreparationViewModel {
/// 页面进入时固定的账号、景区及门店范围。
let context: DroneDockHomeContext
/// 首页选中的真实订单,仅用于身份及进入时的摘要展示。
/// 首页订单仅提供身份和初次加载时的摘要。
let order: DroneDockHomeOrder
/// 最新通过身份校验的准备信息。
private(set) var response: DroneDockPreparationResponse?
/// 是否正在定位或读取准备信息。
/// 张工返回的订单购买范围及最新次数。
private(set) var projectInfo: DroneDockProjectInfoResponse?
/// 购买配置是否尚未读取完成。
private(set) var isLoading = false
/// 加载失败或身份改变时的提示。
/// 购买配置或请求身份错误。
private(set) var errorMessage: String?
/// 没有取得有效位置时的说明,不使用后端无坐标时返回的距离。
private(set) var locationMessage: String?
/// 当前响应是否基于本次成功定位的坐标。
private(set) var hasLocation = false
/// 当前点位与航线的稳定身份。
/// 已购且可用的当前点位。
private(set) var selectedSpotID: Int?
/// 航线只属于当前选中的点位。
/// 当前点位下已购且可用的航线。
private(set) var selectedRouteID: Int?
private let service: any DroneDockPreparationServing
private let locationProvider: any LocationProviding
private let currentContext: @MainActor @Sendable () -> DroneDockHomeContext
private var generation = 0
/// 注入真实只读接口、可替换定位服务和最新登录范围。
/// 注入只读接口及最新会话,不绑定默认 MainActor。
init(context: DroneDockHomeContext, order: DroneDockHomeOrder,
service: any DroneDockPreparationServing, locationProvider: any LocationProviding,
service: any DroneDockPreparationServing,
currentContext: @escaping @MainActor @Sendable () -> DroneDockHomeContext) {
self.context = context
self.order = order
self.service = service
self.locationProvider = locationProvider
self.currentContext = currentContext
}
/// 当前订单的点位,保持服务端顺序。
var spots: [DroneDockPreparationSpot] { response?.spots ?? [] }
/// 当前选中点位,刷新后不再存在时会重新选择。
var selectedSpot: DroneDockPreparationSpot? { spots.first { $0.id == selectedSpotID } }
/// 只展示当前点位所属的航线。
var routes: [DroneDockPreparationAirLine] { selectedSpot?.airLines ?? [] }
/// 当前选中航线,不跨点位保留旧选择。
var selectedRoute: DroneDockPreparationAirLine? { routes.first { $0.id == selectedRouteID } }
/// 获取一次位置和准备快照;定位失败仍可浏览点位航线,但范围保持未知。
/// 只展示购买快照中的点位,保持张工返回顺序。
var spots: [DroneDockPurchasedSpot] { projectInfo?.spots ?? [] }
/// 当前有效点位。
var selectedSpot: DroneDockPurchasedSpot? { spots.first { $0.id == selectedSpotID } }
/// 只展示当前已购点位中的航线。
var routes: [DroneDockPurchasedAirLine] { selectedSpot?.airLines ?? [] }
/// 当前有效航线。
var selectedRoute: DroneDockPurchasedAirLine? { routes.first { $0.id == selectedRouteID } }
/// 进入页面后直接请求购买快照,不等待定位或设备接口。
func refresh() async {
generation &+= 1
let requestGeneration = generation
isLoading = true
errorMessage = nil
locationMessage = nil
hasLocation = false
defer { if generation == requestGeneration { isLoading = false } }
guard await accepts(requestGeneration) else { return }
guard context.canLoad, let number = DroneDockHomeMetric.usableText(order.orderNumber) else {
clearResponse()
errorMessage = "缺少当前景区或订单信息,请返回工作台重新进入"
guard context.canLoad, let number = DroneDockHomeMetric.usableText(order.orderNumber),
let projectID = order.projectID, projectID > 0 else {
clearProject()
errorMessage = "缺少当前景区、订单或套餐信息,请返回工作台重新进入"
return
}
var coordinate: DroneDockPreparationCoordinate?
let projectResult: Result<DroneDockProjectInfoResponse, Error>
do {
let result = try await locationProvider.requestCoordinate(desiredAccuracy: kCLLocationAccuracyBest)
let value = DroneDockPreparationCoordinate(latitude: result.latitude, longitude: result.longitude)
guard value.isValid else { throw LocationProviderError.locationFailed }
coordinate = value
projectResult = .success(try await service.projectInfo(scenicID: context.scenicID, projectID: projectID, orderNumber: number))
} catch {
guard await accepts(requestGeneration) else { return }
locationMessage = "\(error.localizedDescription),距离和拍摄范围待确认"
projectResult = .failure(error)
}
guard await accepts(requestGeneration) else { return }
do {
let result = try await service.workbench(scenicID: context.scenicID, orderNumber: number, coordinate: coordinate)
guard await accepts(requestGeneration) else { return }
guard result.scenicID == context.scenicID, result.orderNumber == number,
result.orderProgress?.orderNumber == nil || result.orderProgress?.orderNumber == number else {
clearResponse()
errorMessage = "接口返回的景区或订单与当前订单不一致,请刷新重试"
return
}
response = result
hasLocation = coordinate != nil
if !spots.contains(where: { $0.id == selectedSpotID }) { selectedSpotID = spots.first?.id }
if !routes.contains(where: { $0.id == selectedRouteID }) { selectedRouteID = routes.first?.id }
} catch {
guard await accepts(requestGeneration) else { return }
clearResponse()
errorMessage = error.localizedDescription
switch projectResult {
case .success(let value) where value.order.orderNumber == number && value.order.projectID == projectID:
projectInfo = value
restoreValidSelection()
case .success:
clearProject()
errorMessage = "接口返回的订单或套餐与当前订单不一致,请刷新重试"
case .failure(let error):
clearProject()
errorMessage = Self.message(for: error)
}
}
/// 选择现有点位,切点后航线重置为该点位的第一条,不复用另一点位的航线。
/// 不可用或可用性未知的点位不能选中,切点后选择首条明确可用的已购航线。
func selectSpot(id: Int) {
guard !isLoading, spots.contains(where: { $0.id == id }), selectedSpotID != id else { return }
guard !isLoading, spots.contains(where: { $0.id == id && $0.available == true }), selectedSpotID != id else { return }
selectedSpotID = id
selectedRouteID = routes.first?.id
selectedRouteID = routes.first(where: { $0.available == true })?.id
}
/// 选择当前点位中的航线,忽略过期或不属于该点位的点击。
/// 忽略其他点位、不可用及身份不明的航线选择。
func selectRoute(id: Int) {
guard !isLoading, routes.contains(where: { $0.id == id }) else { return }
guard !isLoading, selectedSpot?.available == true,
routes.contains(where: { $0.id == id && $0.available == true }) else { return }
selectedRouteID = id
}
/// 页面离开后使定位和网络的迟到结果失效。
/// 离开后使购买快照请求失效,避免旧响应回填。
func invalidate() {
generation &+= 1
isLoading = false
errorMessage = nil
locationMessage = nil
clearResponse()
clearProject()
}
/// 根据实际机巢摘要展示连接状态,不将 can_shoot 当作设备连接证明。
var connectionTitle: String {
guard let airports = response?.airports, !airports.isEmpty else { return "机巢连接待确认" }
if airports.contains(where: { $0.onlineStatus == 1 }) { return "有机巢在线" }
if airports.allSatisfy({ $0.onlineStatus == 2 }) { return "机巢未连接" }
return "机巢连接待确认"
}
/// 只为明确在线的设备摘要显示绿色状态。
var connectionTone: DroneDockStatusTone {
response?.airports.contains(where: { $0.onlineStatus == 1 }) == true ? .green : .gray
}
/// 只有请求携带有效定位时才采用服务端范围结论。
var rangeTitle: String {
guard hasLocation else { return "位置待确认" }
guard let selectedSpot else { return "请选择拍摄点" }
switch selectedSpot.inRange {
case true: return "在拍摄范围内"
case false: return "不在拍摄范围内"
default: return "拍摄范围待确认"
}
}
/// 未知位置或范围不显示绿色。
var rangeTone: DroneDockStatusTone { hasLocation && selectedSpot?.inRange == true ? .green : .gray }
/// 当前航线选择的展示摘要。
/// 当前接口没有机巢连接信息,不能由购买权限推导设备状态。
var connectionTitle: String { "机巢连接待确认" }
/// 设备连接未知时使用中性色。
var connectionTone: DroneDockStatusTone { .gray }
/// 当前未读取位置与范围数据。
var rangeTitle: String { "位置待确认" }
/// 范围未知时使用中性色。
var rangeTone: DroneDockStatusTone { .gray }
/// 以购买快照中的名称展示当前选择。
var selectionSummary: String {
guard let selectedSpot else { return "请选择拍摄点" }
guard let selectedRoute else { return "\(selectedSpot.name) · 未选航线" }
return "\(selectedSpot.name) · \(selectedRoute.name)"
}
/// 多机状态一致时展示该状态,不假定某台机巢已被选派。
var deviceStatusText: String {
let labels = Set(response?.airports.map(\.statusTitle) ?? [])
return labels.count == 1 ? labels.first! : "状态待确认"
}
/// 准备阶段尚未派机,状态标签使用中性色。
/// 当前未读取设备状态,也尚未派机。
var deviceStatusText: String { "状态待确认" }
/// 准备阶段尚未派机。
var deviceTone: DroneDockStatusTone { .gray }
/// 保留后端明确的阻止原因,允许准备时展示下一步按钮标题。
/// 文案反映购买权限,不开放实际召唤操作。
var primaryButtonTitle: String {
if let gate = response?.shootGate, gate.canShoot == false {
return DroneDockHomeMetric.usableText(gate.primaryActionLabel) ?? "暂不可召唤"
}
if projectInfo?.order.shotRemain == 0 { return "拍摄次数已用完" }
if let info = projectInfo, info.order.canCreateTask != true { return "暂不可召唤" }
return "确认并召唤"
}
/// 展示当前阻止原因;本阶段仅对接准备信息,不发送召唤指令。
/// 检查购买范围与额度;条件有效后仍提示召唤尚未开放。
var admissionMessage: String {
if isLoading { return "正在获取位置和拍摄准备信息" }
if isLoading { return "正在获取拍摄准备信息" }
if let errorMessage { return errorMessage }
guard let response else { return "正在加载拍摄准备信息" }
if let gate = response.shootGate, gate.canShoot == false {
return DroneDockHomeMetric.usableText(gate.message)
?? DroneDockHomeMetric.usableText(gate.primaryActionLabel) ?? "当前暂不可召唤,请刷新状态"
}
if let locationMessage { return locationMessage }
guard let info = projectInfo else { return "正在加载拍摄准备信息" }
if info.order.shotRemain == 0 { return "当前订单拍摄次数已用完" }
guard let remaining = info.order.shotRemain, remaining > 0, info.order.shotTotal != nil else { return "拍摄次数待确认,请刷新状态" }
guard info.order.canCreateTask == true else { return "当前订单暂不可新增拍摄任务,请刷新状态" }
if spots.isEmpty { return "当前暂无可用拍摄点" }
guard let spot = selectedSpot, spot.available == true else { return "请选择可用拍摄点" }
if routes.isEmpty { return "当前拍摄点暂无可用航线" }
if !hasLocation || selectedSpot?.inRange == nil { return "拍摄范围待确认,请重新定位" }
if selectedSpot?.inRange == false { return "请前往所选拍摄点的拍摄范围内" }
guard response.shootGate?.canShoot == true else { return "拍摄条件待确认,请刷新状态" }
guard selectedRoute?.available == true else { return "请选择可用航线" }
return "召唤功能暂未开放"
}
private func clearResponse() {
response = nil
private func restoreValidSelection() {
if !spots.contains(where: { $0.id == selectedSpotID && $0.available == true }) {
selectedSpotID = spots.first(where: { $0.available == true })?.id
selectedRouteID = nil
}
if !routes.contains(where: { $0.id == selectedRouteID && $0.available == true }) {
selectedRouteID = routes.first(where: { $0.available == true })?.id
}
}
private func clearProject() {
projectInfo = nil
selectedSpotID = nil
selectedRouteID = nil
hasLocation = false
}
private func accepts(_ requestGeneration: Int) async -> Bool {
@@ -189,12 +156,15 @@ final class DroneDockPreparationViewModel {
let latest = await currentContext()
guard generation == requestGeneration, !Task.isCancelled else { return false }
guard context.hasSameIdentity(as: latest) else {
generation &+= 1
isLoading = false
clearResponse()
invalidate()
errorMessage = "账号或景区已变化,请返回工作台重新进入"
return false
}
return true
}
private static func message(for error: Error) -> String? {
error is CancellationError ? nil : error.localizedDescription
}
}