feat(drone-dock): 展示首页真实拍摄订单

This commit is contained in:
2026-10-27 17:58:30 +08:00
parent 2db4ad8e69
commit f7a2a19dee
6 changed files with 552 additions and 42 deletions
@@ -6,8 +6,8 @@ struct DroneDockHomeResponse: Decodable, Equatable, Sendable {
let scenicID: Int
/// 当前景区的机巢设备,按真实非空响应的字段读取。
let airports: [DroneDockHomeAirport]
/// 订单原始记录;不将未知字段猜测成演示订单。
let orders: [DroneDockHomeRecord]
/// 按真实响应字段读取的拍摄订单,保持后端返回顺序。
let orders: [DroneDockHomeOrder]
/// 今日数据缺失时保留未知,不生成零值统计。
let today: DroneDockHomeToday?
@@ -28,7 +28,10 @@ struct DroneDockHomeResponse: Decodable, Equatable, Sendable {
guard Set(airports.map(\.id)).count == airports.count else {
throw DecodingError.dataCorruptedError(forKey: .airports, in: container, debugDescription: "设备 ID 重复,无法确定设备身份")
}
orders = try container.decode([DroneDockHomeRecord].self, forKey: .orders)
orders = try container.decode([DroneDockHomeOrder].self, forKey: .orders)
guard Set(orders.map(\.orderID)).count == orders.count else {
throw DecodingError.dataCorruptedError(forKey: .orders, in: container, debugDescription: "订单 ID 重复,无法确定订单身份")
}
today = try container.decodeIfPresent(DroneDockHomeToday.self, forKey: .today)
}
}
@@ -153,14 +156,123 @@ struct DroneDockHomeToday: Decodable, Equatable, Sendable {
}
}
/// 尚未取得真实样本的订单行,保留对象字段供后续精确接入和诊断。
struct DroneDockHomeRecord: Decodable, Equatable, Sendable {
/// 原始对象键值,不猜测订单状态或拍摄次数的字段别名。
let fields: [String: DroneDockHomeJSONValue]
/// 首页拍摄订单的展示数据;状态及动作仅用于呈现,不代表飞行执行许可。
struct DroneDockHomeOrder: Decodable, Equatable, Sendable {
/// 后端订单稳定 ID,用于 Diffable 项身份。
let orderID: Int
/// 后端订单编号,保留字符串形式与前导零。
let orderNumber: String?
/// 后端游客展示名称。
let touristLabel: String?
/// 后端已脱敏的游客手机号。
let touristPhoneMask: String?
/// 后端套餐名称,不据此推断套餐类型或项目 ID。
let packageName: String?
/// 后端订单状态展示文案。
let statusLabel: String?
/// 后端拍摄总次数,未知时不默认成零。
let shotTotal: Int?
/// 后端已拍摄次数,不在客户端根据任务状态重新计算。
let shotUsed: Int?
/// 后端剩余拍摄次数,不用总次数减已拍摄次数推断。
let shotRemain: Int?
/// 后端格式化的拍摄进度文案。
let progressText: String?
/// 后端素材状态原始值,保留尚未验证的值。
let mediaStatus: String?
/// 后端素材状态展示文案;空文案不生成新的业务状态。
let mediaStatusLabel: String?
/// 后端主动作标识,仅用于展示入口,不作为执行权限。
let primaryAction: String?
/// 后端主动作按钮文案。
let primaryActionLabel: String?
/// 后端游客用户 ID。
let userID: Int?
/// 只接受 JSON 对象作为列表记录,拒绝 null、标量和嵌套数组。
/// 已实测的首页订单字段名。
private enum CodingKeys: String, CodingKey {
case orderID = "order_id"
case orderNumber = "order_number"
case touristLabel = "tourist_label"
case touristPhoneMask = "tourist_phone_mask"
case packageName = "package_name"
case statusLabel = "status_label"
case shotTotal = "shot_total"
case shotUsed = "shot_used"
case shotRemain = "shot_remain"
case progressText = "progress_text"
case mediaStatus = "media_status"
case mediaStatusLabel = "media_status_label"
case primaryAction = "primary_action"
case primaryActionLabel = "primary_action_label"
case userID = "user_id"
}
/// 严格校验列表身份和计数;缺失展示字段保留未知。
init(from decoder: Decoder) throws {
fields = try decoder.singleValueContainer().decode([String: DroneDockHomeJSONValue].self)
let container = try decoder.container(keyedBy: CodingKeys.self)
orderID = try container.decode(DroneDockHomeInteger.self, forKey: .orderID).value
guard orderID > 0 else {
throw DecodingError.dataCorruptedError(forKey: .orderID, in: container, debugDescription: "订单 ID 必须为正整数")
}
orderNumber = try container.decodeIfPresent(String.self, forKey: .orderNumber)
touristLabel = try container.decodeIfPresent(String.self, forKey: .touristLabel)
touristPhoneMask = try container.decodeIfPresent(String.self, forKey: .touristPhoneMask)
packageName = try container.decodeIfPresent(String.self, forKey: .packageName)
statusLabel = try container.decodeIfPresent(String.self, forKey: .statusLabel)
shotTotal = try container.decodeIfPresent(DroneDockHomeInteger.self, forKey: .shotTotal)?.value
shotUsed = try container.decodeIfPresent(DroneDockHomeInteger.self, forKey: .shotUsed)?.value
shotRemain = try container.decodeIfPresent(DroneDockHomeInteger.self, forKey: .shotRemain)?.value
for (key, value) in [(CodingKeys.shotTotal, shotTotal), (.shotUsed, shotUsed), (.shotRemain, shotRemain)] {
if let value, value < 0 {
throw DecodingError.dataCorruptedError(forKey: key, in: container, debugDescription: "拍摄次数不能为负数")
}
}
progressText = try container.decodeIfPresent(String.self, forKey: .progressText)
mediaStatus = try container.decodeIfPresent(String.self, forKey: .mediaStatus)
mediaStatusLabel = try container.decodeIfPresent(String.self, forKey: .mediaStatusLabel)
primaryAction = try container.decodeIfPresent(String.self, forKey: .primaryAction)
primaryActionLabel = try container.decodeIfPresent(String.self, forKey: .primaryActionLabel)
userID = try container.decodeIfPresent(DroneDockHomeInteger.self, forKey: .userID)?.value
}
/// 页面稳定身份,与后端订单 ID 一致。
var id: Int { orderID }
/// 展示后端订单编号;缺失时不以内部 ID 冒充订单编号。
var orderNumberText: String { DroneDockHomeMetric.usableText(orderNumber) ?? "—" }
/// 优先展示服务端游客文案,其次使用已脱敏手机号。
var touristTitle: String {
if let label = DroneDockHomeMetric.usableText(touristLabel) { return label }
if let phone = DroneDockHomeMetric.usableText(touristPhoneMask) { return "游客 \(phone)" }
return "游客信息待确认"
}
/// 展示后端套餐名称,缺失时不生成套餐或次数。
var packageTitle: String { DroneDockHomeMetric.usableText(packageName) ?? "套餐信息待确认" }
/// 展示后端状态,缺失时不默认成待拍摄。
var statusTitle: String { DroneDockHomeMetric.usableText(statusLabel) ?? "状态待确认" }
/// 优先展示后端进度文案;没有文案时逐项展示真实计数与未知占位。
var progressTitle: String {
if let text = DroneDockHomeMetric.usableText(progressText) { return text }
let used = shotUsed.map(String.init) ?? "—"
let total = shotTotal.map(String.init) ?? "—"
let remain = shotRemain.map(String.init) ?? "—"
return "已拍摄 \(used)/\(total) 次,剩余 \(remain) 次"
}
/// 只有后端同时提供动作标识和文案时才显示按钮,不猜测缺失动作。
var actionTitle: String? {
guard DroneDockHomeMetric.usableText(primaryAction) != nil else { return nil }
return DroneDockHomeMetric.usableText(primaryActionLabel)
}
/// 仅展示有效的后端素材状态文案,不将空值转成未上传等新状态。
var mediaStatusTitle: String? {
DroneDockHomeMetric.usableText(mediaStatusLabel)
}
}
@@ -10,6 +10,7 @@ final class DroneDockCompactOrderCell: UICollectionViewCell {
private let customerLabel = DroneDockStyle.label(size: 16, weight: .bold)
private let statusLabel = DroneDockStatusLabel()
private let orderNumberLabel = DroneDockStyle.label(size: 11, color: DroneDockStyle.secondary)
private let packageLabel = DroneDockStyle.label(size: 13, color: UIColor(hex: 0x666E7C))
private let countsLabel = DroneDockStyle.label(size: 11, color: DroneDockStyle.secondary)
private let actionButton = UIButton(type: .system)
@@ -29,19 +30,54 @@ final class DroneDockCompactOrderCell: UICollectionViewCell {
/// 应用数据,不展示具体点位或航线,也不在卡片自行扣次。
func apply(order: DroneShootingOrder) {
customerLabel.text = order.customerName
orderNumberLabel.isHidden = true
statusLabel.apply(title: order.statusTitle, tone: order.statusTone)
packageLabel.text = order.packageSummary
packageLabel.numberOfLines = 1
countsLabel.numberOfLines = 1
actionButton.titleLabel?.numberOfLines = 1
countsLabel.text = "已拍摄 \(order.successfulShootingCount)/\(order.totalShootingCount) 次 · 剩余 \(order.remainingShootingCount) 次"
actionButton.setTitle(order.primaryAction.title, for: .normal)
actionButton.isHidden = false
actionButton.isEnabled = order.primaryActionEnabled
actionButton.backgroundColor = DroneDockStyle.primary
actionButton.accessibilityIdentifier = "drone.home.\(order.id).primary"
footer.isHidden = !order.hasMaterials
applyMaterial(title: order.materialState.title, symbol: order.materialState.systemImageName,
tone: order.materialState.tone)
accessibilityIdentifier = "drone.home.\(order.id)"
}
/// 展示 home 的真实订单快照,缺失字段保持未知,按钮只向页面报告点击。
func apply(homeOrder order: DroneDockHomeOrder) {
customerLabel.text = order.touristTitle
orderNumberLabel.isHidden = false
orderNumberLabel.text = "订单号 \(order.orderNumberText)"
statusLabel.apply(title: order.statusTitle, tone: order.statusTitle == "待拍摄" ? .blue : .gray)
packageLabel.text = order.packageTitle
packageLabel.numberOfLines = 0
countsLabel.numberOfLines = 0
actionButton.titleLabel?.numberOfLines = 2
countsLabel.text = order.progressTitle
actionButton.setTitle(order.actionTitle, for: .normal)
actionButton.isHidden = order.actionTitle == nil
actionButton.isEnabled = order.actionTitle != nil && order.primaryAction != "blocked"
actionButton.backgroundColor = actionButton.isEnabled ? DroneDockStyle.primary : DroneDockStyle.secondary
actionButton.accessibilityIdentifier = "drone.home.order.\(order.orderID).primary"
let materialTitle = order.mediaStatusTitle ?? ""
let materialStatus = order.mediaStatus?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
footer.isHidden = materialStatus.isEmpty || materialStatus == "none" || materialTitle.isEmpty
applyMaterial(title: materialTitle, symbol: "photo.on.rectangle.angled", tone: .gray)
accessibilityIdentifier = "drone.home.order.\(order.orderID)"
}
private func applyMaterial(title: String, symbol: String, tone: DroneDockStatusTone) {
var configuration = UIButton.Configuration.plain()
configuration.image = UIImage(systemName: order.materialState.systemImageName)
configuration.image = UIImage(systemName: symbol)
configuration.preferredSymbolConfigurationForImage = UIImage.SymbolConfiguration(pointSize: 15)
configuration.imagePadding = 6
configuration.title = order.materialState.title
configuration.baseForegroundColor = DroneDockStyle.color(for: order.materialState.tone)
configuration.title = title
configuration.baseForegroundColor = DroneDockStyle.color(for: tone)
configuration.contentInsets = .zero
configuration.titleTextAttributesTransformer = UIConfigurationTextAttributesTransformer { attributes in
var attributes = attributes
@@ -49,8 +85,7 @@ final class DroneDockCompactOrderCell: UICollectionViewCell {
return attributes
}
materialButton.configuration = configuration
materialButton.accessibilityLabel = "查看素材,\(order.materialState.title)"
accessibilityIdentifier = "drone.home.\(order.id)"
materialButton.accessibilityLabel = "查看素材,\(title)"
}
override func prepareForReuse() {
@@ -66,6 +101,8 @@ final class DroneDockCompactOrderCell: UICollectionViewCell {
let titleRow = UIStackView(arrangedSubviews: [customerLabel, statusLabel])
titleRow.alignment = .center
titleRow.spacing = 8
orderNumberLabel.isHidden = true
orderNumberLabel.numberOfLines = 0
let labels = UIStackView(arrangedSubviews: [packageLabel, countsLabel])
labels.axis = .vertical
labels.spacing = 9
@@ -78,12 +115,18 @@ final class DroneDockCompactOrderCell: UICollectionViewCell {
actionButton.titleLabel?.font = .systemFont(ofSize: 13, weight: .medium)
actionButton.titleLabel?.adjustsFontSizeToFitWidth = true
actionButton.titleLabel?.minimumScaleFactor = 0.8
actionButton.titleLabel?.textAlignment = .center
actionButton.layer.cornerRadius = 8
actionButton.addTarget(self, action: #selector(shootingTapped), for: .touchUpInside)
actionButton.snp.makeConstraints { make in make.width.equalTo(106); make.height.equalTo(42) }
actionButton.snp.makeConstraints { make in
make.width.equalTo(106).priority(999)
make.height.equalTo(42)
}
let body = UIStackView(arrangedSubviews: [labels, actionButton])
body.spacing = 10
body.alignment = .center
// 让内容行紧贴文字;按钮的固有高度仍可撑高整行,避免自适应高度的舍入余量在两行间游移。
body.snp.makeConstraints { make in make.height.equalTo(labels).priority(249) }
let divider = UIView()
divider.backgroundColor = DroneDockStyle.border
@@ -106,7 +149,7 @@ final class DroneDockCompactOrderCell: UICollectionViewCell {
make.trailing.lessThanOrEqualTo(viewButton.snp.leading).offset(-8)
}
viewButton.snp.makeConstraints { make in make.centerY.equalTo(materialButton); make.trailing.equalToSuperview() }
let stack = UIStackView(arrangedSubviews: [titleRow, body, footer])
let stack = UIStackView(arrangedSubviews: [titleRow, orderNumberLabel, body, footer])
stack.axis = .vertical
stack.spacing = 12
contentView.addSubview(stack)
@@ -11,10 +11,10 @@ final class DroneDockHomeViewController: BaseViewController {
private var devicePage = 0
private let pageControl = UIPageControl()
/// 首页固定分区;设备与订单的数据结构以实际联调结果为准。
/// 首页按当前身份、设备、订单与统计展示服务端快照。
private enum Section: Int, CaseIterable { case context, devices, orders, statistics }
/// 稳定身份确保刷新使用 Diffable 更新。
private enum Item: Hashable { case context, devices, device(Int), orders, statistics }
private enum Item: Hashable { case context, devices, device(Int), orders, order(Int), statistics }
private lazy var collectionView = UICollectionView(frame: .zero, collectionViewLayout: makeLayout())
private var dataSource: UICollectionViewDiffableDataSource<Section, Item>!
@@ -56,6 +56,7 @@ final class DroneDockHomeViewController: BaseViewController {
collectionView.accessibilityIdentifier = "drone.home.remote.list"
collectionView.register(DroneDockHomeContentCell.self, forCellWithReuseIdentifier: "content")
collectionView.register(DroneDockDeviceCell.self, forCellWithReuseIdentifier: "device")
collectionView.register(DroneDockCompactOrderCell.self, forCellWithReuseIdentifier: "order")
collectionView.register(DroneDockHomeHeader.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader,
withReuseIdentifier: "header")
collectionView.register(DroneDockHomePageFooter.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter,
@@ -97,12 +98,17 @@ final class DroneDockHomeViewController: BaseViewController {
isRefreshing = true
refreshTask = Task { @MainActor [weak self] in
guard let self else { return }
let isInitialLoad = self.viewModel.response == nil
self.applySnapshot(animated: false)
await self.viewModel.refresh()
guard !Task.isCancelled else { return }
self.isRefreshing = false
self.collectionView.refreshControl?.endRefreshing()
self.applySnapshot(animated: true) { [weak self] in
if isInitialLoad, let self {
self.collectionView.layoutIfNeeded()
self.collectionView.setContentOffset(CGPoint(x: 0, y: -self.collectionView.adjustedContentInset.top), animated: false)
}
#if DEBUG
Task { @MainActor [weak self] in await self?.captureHomeIfRequested() }
#endif
@@ -120,6 +126,17 @@ final class DroneDockHomeViewController: BaseViewController {
}
return cell
}
if case .order(let id) = item {
let cell = collection.dequeueReusableCell(withReuseIdentifier: "order", for: path) as! DroneDockCompactOrderCell
if let order = self.viewModel.response?.orders.first(where: { $0.orderID == id }) {
cell.apply(homeOrder: order)
cell.onShootingTap = { [weak self] in
self?.showAlert(title: order.actionTitle ?? "拍摄订单", message: "拍摄流程暂未接入,请稍后再试")
}
cell.onMaterialsTap = { [weak self] in self?.showUnavailable("订单素材") }
}
return cell
}
let cell = collection.dequeueReusableCell(withReuseIdentifier: "content", for: path) as! DroneDockHomeContentCell
switch item {
case .context:
@@ -130,7 +147,7 @@ final class DroneDockHomeViewController: BaseViewController {
cell.applyStatistics(self.viewModel.response?.today)
case .devices, .orders:
self.applyDataState(to: cell, isDevice: item == .devices)
case .device: break
case .device, .order: break
}
return cell
}
@@ -158,20 +175,13 @@ final class DroneDockHomeViewController: BaseViewController {
cell.applyMessage(symbol: "wifi.exclamationmark", title: "首页加载失败", detail: error + ",请下拉重试")
return
}
guard let response = viewModel.response else {
guard viewModel.response != nil else {
cell.applyMessage(symbol: "arrow.triangle.2.circlepath", title: "正在加载", detail: "正在获取最新数据")
return
}
let recordCount = isDevice ? response.airports.count : response.orders.count
if recordCount == 0 {
cell.applyMessage(symbol: isDevice ? "airplane" : "doc.text",
title: isDevice ? "暂无无人机" : "暂无待拍摄订单",
detail: isDevice ? "当前景区暂无设备数据" : "当前景区暂无拍摄订单数据")
} else {
// 订单仍等待真实非空样例,不影响已接入的设备卡片。
cell.applyMessage(symbol: "exclamationmark.circle", title: "订单信息暂不可用",
detail: "已获取 \(recordCount) 条数据,暂时无法展示详情")
}
cell.applyMessage(symbol: isDevice ? "airplane" : "doc.text",
title: isDevice ? "暂无无人机" : "暂无待拍摄订单",
detail: isDevice ? "当前景区暂无设备数据" : "当前景区暂无拍摄订单数据")
}
private func applySnapshot(animated: Bool, completion: (() -> Void)? = nil) {
@@ -181,7 +191,8 @@ final class DroneDockHomeViewController: BaseViewController {
snapshot.appendItems([.context], toSection: .context)
let airports = viewModel.response?.airports ?? []
snapshot.appendItems(airports.isEmpty ? [.devices] : airports.map { .device($0.id) }, toSection: .devices)
snapshot.appendItems([.orders], toSection: .orders)
let orders = viewModel.response?.orders ?? []
snapshot.appendItems(orders.isEmpty ? [.orders] : orders.map { .order($0.orderID) }, toSection: .orders)
snapshot.appendItems([.statistics], toSection: .statistics)
let old = Set(dataSource.snapshot().itemIdentifiers)
let previousDeviceCount = old.filter { if case .device = $0 { return true }; return false }.count
@@ -207,6 +218,7 @@ final class DroneDockHomeViewController: BaseViewController {
let group = NSCollectionLayoutGroup.horizontal(layoutSize: NSCollectionLayoutSize(widthDimension: width, heightDimension: height), subitems: [item])
let layout = NSCollectionLayoutSection(group: group)
layout.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 16, bottom: section == .statistics ? 24 : 0, trailing: 16)
if section == .orders { layout.interGroupSpacing = 12 }
if multipleDevices {
layout.interGroupSpacing = 12
layout.orthogonalScrollingBehavior = .groupPaging