feat: 完善机巢模拟调试与素材交付交互
增加禁飞期间的设备指令拦截及本地模拟拍摄流程;适配订单素材库和景区空境库,完善素材筛选、预览与下载;订单交付入口改为方式选择弹窗并复用云端上传。补充单元测试、接口记录和模拟器截图。
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
import Foundation
|
||||
|
||||
/// 为月、日两列滚轮提供保留期内的真实日期,月份保留年份以正确处理跨年。
|
||||
struct ScenicMaterialDatePickerOptions {
|
||||
/// 一个月中实际可选的日期;首月和末月只包含保留期覆盖的部分。
|
||||
struct Month {
|
||||
/// 日历中的年份,供跨年识别及辅助功能使用。
|
||||
let year: Int
|
||||
/// 日历中的月份。
|
||||
let number: Int
|
||||
/// 当前月份内按先后排列且允许选择的日期。
|
||||
let dates: [Date]
|
||||
}
|
||||
|
||||
/// 包含端点、按日归一化的最早日期。
|
||||
let minimumDate: Date
|
||||
/// 包含端点、按日归一化的最晚日期。
|
||||
let maximumDate: Date
|
||||
/// 从最早到最晚排列的可选月份。
|
||||
let months: [Month]
|
||||
private let calendar: Calendar
|
||||
|
||||
/// 将闭区间展开为有限选项,不依赖每月天数或一天固定为 24 小时的假设。
|
||||
init(minimumDate: Date, maximumDate: Date, calendar: Calendar) {
|
||||
self.calendar = calendar
|
||||
let minimum = calendar.startOfDay(for: minimumDate)
|
||||
let maximum = max(minimum, calendar.startOfDay(for: maximumDate))
|
||||
self.minimumDate = minimum
|
||||
self.maximumDate = maximum
|
||||
var months: [Month] = []
|
||||
var day = minimum
|
||||
while day <= maximum {
|
||||
let year = calendar.component(.year, from: day)
|
||||
let number = calendar.component(.month, from: day)
|
||||
if let last = months.last, last.year == year, last.number == number {
|
||||
months[months.count - 1] = Month(year: year, number: number, dates: last.dates + [day])
|
||||
} else {
|
||||
months.append(Month(year: year, number: number, dates: [day]))
|
||||
}
|
||||
guard let next = calendar.date(byAdding: .day, value: 1, to: day), next > day else { break }
|
||||
day = next
|
||||
}
|
||||
self.months = months
|
||||
}
|
||||
|
||||
/// 将外部值归一到允许范围内的日历日。
|
||||
func clamped(_ date: Date) -> Date {
|
||||
min(max(calendar.startOfDay(for: date), minimumDate), maximumDate)
|
||||
}
|
||||
|
||||
/// 找到日期对应的月份行,保留期外值先收敛到边界。
|
||||
func monthIndex(for date: Date) -> Int {
|
||||
let value = clamped(date)
|
||||
return months.firstIndex { month in
|
||||
month.dates.first.map { $0 <= value } == true && month.dates.last.map { value <= $0 } == true
|
||||
} ?? 0
|
||||
}
|
||||
|
||||
/// 切换月份时尽量保留原日号;月末或截断的保留期会收敛到最近可选日。
|
||||
func date(inMonthAt index: Int, preferredDay: Int) -> Date {
|
||||
let month = months[min(max(index, 0), months.count - 1)]
|
||||
return month.dates.min {
|
||||
abs(calendar.component(.day, from: $0) - preferredDay)
|
||||
< abs(calendar.component(.day, from: $1) - preferredDay)
|
||||
} ?? minimumDate
|
||||
}
|
||||
|
||||
/// 普通范围只显示月份;范围含同名月份时补年份,避免用户混淆。
|
||||
func monthTitle(at index: Int) -> String {
|
||||
let month = months[index]
|
||||
let repeats = months.filter { $0.number == month.number }.count > 1
|
||||
return repeats ? "\(month.year)年\(month.number)月" : "\(month.number)月"
|
||||
}
|
||||
}
|
||||
@@ -70,12 +70,21 @@ struct ScenicMaterialClip: Hashable, Sendable {
|
||||
let resolution: String
|
||||
/// 本地演示封面资源名。
|
||||
let coverAssetName: String
|
||||
/// 正式素材原片地址;预览素材为空。
|
||||
let remoteURL: URL?
|
||||
/// 正式素材封面地址。
|
||||
let coverURL: URL?
|
||||
/// 后端返回的片段名称。
|
||||
let fileLabel: String?
|
||||
/// 是否来自正式接口,缺失地址时禁止回退到演示视频。
|
||||
let isRemote: Bool
|
||||
/// 文件是否仍可预览和下载;不可用文件仍可展示其元数据。
|
||||
let isAvailable: Bool
|
||||
|
||||
/// 创建片段元数据,不读取本地相册或请求网络。
|
||||
init(id: String, groupID: String, ordinal: Int, durationSeconds: Int, fileSizeBytes: Int64,
|
||||
resolution: String, coverAssetName: String, isAvailable: Bool = true) {
|
||||
resolution: String, coverAssetName: String, isAvailable: Bool = true,
|
||||
remoteURL: URL? = nil, coverURL: URL? = nil, fileLabel: String? = nil, isRemote: Bool = false) {
|
||||
self.id = id
|
||||
self.groupID = groupID
|
||||
self.ordinal = ordinal
|
||||
@@ -84,16 +93,22 @@ struct ScenicMaterialClip: Hashable, Sendable {
|
||||
self.resolution = resolution
|
||||
self.coverAssetName = coverAssetName
|
||||
self.isAvailable = isAvailable
|
||||
self.remoteURL = remoteURL
|
||||
self.coverURL = coverURL
|
||||
self.fileLabel = fileLabel
|
||||
self.isRemote = isRemote
|
||||
}
|
||||
|
||||
/// 用于封面与预览信息的分秒时长。
|
||||
var durationText: String {
|
||||
guard durationSeconds >= 0 else { return "时长未知" }
|
||||
let value = max(durationSeconds, 0)
|
||||
return String(format: "%02d:%02d", value / 60, value % 60)
|
||||
}
|
||||
|
||||
/// 使用十进制 MB 展示片段大小,避免将原文件与本地示例文件混淆。
|
||||
var sizeText: String {
|
||||
guard fileSizeBytes >= 0 else { return "大小未知" }
|
||||
let megabytes = Double(max(fileSizeBytes, 0)) / 1_000_000
|
||||
return megabytes.rounded() == megabytes ? String(format: "%.0fMB", megabytes) : String(format: "%.1fMB", megabytes)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,113 @@
|
||||
import Foundation
|
||||
|
||||
/// 场次接口中已由部署响应确认的分页和保留策略;子项保留原始字段供合同映射使用。
|
||||
struct ScenicMaterialsSessionsResponse: Decodable, Equatable, Sendable {
|
||||
/// 当前页场次原始对象,不能把未确认的子项结构当成空列表。
|
||||
let list: [[String: ScenicMaterialsJSONValue]]
|
||||
/// 服务端航线分组原始对象。
|
||||
let groups: [[String: ScenicMaterialsJSONValue]]
|
||||
/// 当前筛选场次总数。
|
||||
let total: Int
|
||||
/// 服务端实际返回的页码。
|
||||
let page: Int
|
||||
/// 服务端实际返回的每页数量。
|
||||
let pageSize: Int
|
||||
/// 本次查询的素材保留窗口。
|
||||
let retentionDays: Int
|
||||
/// 服务端提供的保留期提示。
|
||||
let retentionTips: String
|
||||
/// 服务端实际应用的筛选。
|
||||
let filter: ScenicMaterialsFilter
|
||||
/// 当前景区可用的航线筛选对象。
|
||||
let airLines: [[String: ScenicMaterialsJSONValue]]
|
||||
|
||||
/// 部署接口的根字段。
|
||||
private enum CodingKeys: String, CodingKey {
|
||||
case list, groups, total, page, pageSize = "page_size", retentionDays = "retention_days"
|
||||
case retentionTips = "retention_tips", filter, filterOptions = "filter_options"
|
||||
}
|
||||
|
||||
/// 只接受真实容器和有效分页,避免缺字段响应被误显示成空态。
|
||||
init(from decoder: Decoder) throws {
|
||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||
list = try container.decode([[String: ScenicMaterialsJSONValue]].self, forKey: .list)
|
||||
groups = try container.decode([[String: ScenicMaterialsJSONValue]].self, forKey: .groups)
|
||||
total = try container.decode(Int.self, forKey: .total)
|
||||
page = try container.decode(Int.self, forKey: .page)
|
||||
pageSize = try container.decode(Int.self, forKey: .pageSize)
|
||||
retentionDays = try container.decode(Int.self, forKey: .retentionDays)
|
||||
retentionTips = try container.decode(String.self, forKey: .retentionTips)
|
||||
filter = try container.decode(ScenicMaterialsFilter.self, forKey: .filter)
|
||||
airLines = try container.decode(ScenicMaterialsFilterOptions.self, forKey: .filterOptions).airLines
|
||||
guard total >= 0, page > 0, (1...100).contains(pageSize), (1...365).contains(retentionDays),
|
||||
list.count <= pageSize, list.count <= total else {
|
||||
throw DecodingError.dataCorruptedError(forKey: .list, in: container, debugDescription: "空境库场次分页或保留期无效")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 后端实际应用的筛选,全部航线为 0、全部日期为空字符串。
|
||||
struct ScenicMaterialsFilter: Decodable, Equatable, Sendable {
|
||||
/// 当前航线筛选值。
|
||||
let airLineID: Int
|
||||
/// 当前起始日期筛选值。
|
||||
let dateFrom: String
|
||||
/// 当前结束日期筛选值。
|
||||
let dateTo: String
|
||||
|
||||
/// 部署响应中的筛选字段。
|
||||
private enum CodingKeys: String, CodingKey {
|
||||
case airLineID = "air_line_id", dateFrom = "date_from", dateTo = "date_to"
|
||||
}
|
||||
}
|
||||
|
||||
/// filter_options 下独立返回当前景区的航线集合。
|
||||
private struct ScenicMaterialsFilterOptions: Decodable {
|
||||
let airLines: [[String: ScenicMaterialsJSONValue]]
|
||||
/// 部署响应中的航线容器。
|
||||
private enum CodingKeys: String, CodingKey { case airLines = "air_lines" }
|
||||
}
|
||||
|
||||
/// 场次详情的已确认素材数组及完整场次信息,不借用订单素材 ID 模型。
|
||||
struct ScenicMaterialsSessionDetailResponse: Decodable, Equatable, Sendable {
|
||||
/// 文档明确返回的全部素材项,保留服务端所有字段。
|
||||
let materials: [[String: ScenicMaterialsJSONValue]]
|
||||
/// 场次详情完整对象,供依据实际合同解码归属与展示信息。
|
||||
let fields: [String: ScenicMaterialsJSONValue]
|
||||
|
||||
/// 缺少 materials 或容器错误时报错,不伪装成没有素材。
|
||||
init(from decoder: Decoder) throws {
|
||||
let container = try decoder.singleValueContainer()
|
||||
fields = try container.decode([String: ScenicMaterialsJSONValue].self)
|
||||
guard case let .array(values)? = fields["materials"] else {
|
||||
throw DecodingError.dataCorruptedError(in: container, debugDescription: "空境场次详情缺少 materials 数组")
|
||||
}
|
||||
materials = try values.map { value in
|
||||
guard case let .object(fields) = value else {
|
||||
throw DecodingError.dataCorruptedError(in: container, debugDescription: "空境素材必须为对象")
|
||||
}
|
||||
return fields
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 保留空境库尚未完整描述的子项 JSON,不猜测字段类型或单位。
|
||||
indirect enum ScenicMaterialsJSONValue: Decodable, Equatable, Sendable {
|
||||
case object([String: ScenicMaterialsJSONValue])
|
||||
case array([ScenicMaterialsJSONValue])
|
||||
case string(String)
|
||||
case number(Decimal)
|
||||
case bool(Bool)
|
||||
case null
|
||||
|
||||
/// 按真实 JSON 类型读取,未知字段不会静默丢失。
|
||||
init(from decoder: Decoder) throws {
|
||||
let container = try decoder.singleValueContainer()
|
||||
if container.decodeNil() { self = .null }
|
||||
else if let value = try? container.decode(Bool.self) { self = .bool(value) }
|
||||
else if let value = try? container.decode(String.self) { self = .string(value) }
|
||||
else if let value = try? container.decode(Decimal.self) { self = .number(value) }
|
||||
else if let value = try? container.decode([String: ScenicMaterialsJSONValue].self) { self = .object(value) }
|
||||
else { self = .array(try container.decode([ScenicMaterialsJSONValue].self)) }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
import Foundation
|
||||
|
||||
/// 朱工空境库场次查询范围,分页和筛选均由服务端执行。
|
||||
struct ScenicMaterialsQuery: Equatable, Sendable {
|
||||
/// 当前景区身份,不依赖订单、机巢或飞行任务。
|
||||
let scenicID: Int
|
||||
/// 从 1 开始的场次页码。
|
||||
let page: Int
|
||||
/// 每页场次数,接口允许 1 到 100。
|
||||
let pageSize: Int
|
||||
/// 指定航线;nil 表示全部航线。
|
||||
let airLineID: Int?
|
||||
/// 可选起始日期,格式为 yyyy-MM-dd。
|
||||
let dateFrom: String?
|
||||
/// 可选结束日期,格式为 yyyy-MM-dd。
|
||||
let dateTo: String?
|
||||
/// 保留期查询窗口,默认 90 天,部署接口允许 1 到 365。
|
||||
let retentionDays: Int
|
||||
|
||||
/// 创建完整查询快照,便于判断翻页和筛选请求是否过期。
|
||||
init(scenicID: Int, page: Int = 1, pageSize: Int = 20, airLineID: Int? = nil,
|
||||
dateFrom: String? = nil, dateTo: String? = nil, retentionDays: Int = 90) {
|
||||
self.scenicID = scenicID
|
||||
self.page = page
|
||||
self.pageSize = pageSize
|
||||
self.airLineID = airLineID
|
||||
self.dateFrom = dateFrom
|
||||
self.dateTo = dateTo
|
||||
self.retentionDays = retentionDays
|
||||
}
|
||||
|
||||
/// 校验范围后生成文档规定的 GET 参数,全部航线和全部日期不发送占位值。
|
||||
func queryItems() throws -> [URLQueryItem] {
|
||||
guard scenicID > 0 else { throw APIError.networkFailed("请先选择景区") }
|
||||
guard page > 0, (1...100).contains(pageSize), (1...365).contains(retentionDays),
|
||||
airLineID.map({ $0 > 0 }) ?? true else {
|
||||
throw APIError.networkFailed("景区空境库查询参数无效")
|
||||
}
|
||||
guard Self.isValidDate(dateFrom), Self.isValidDate(dateTo) else {
|
||||
throw APIError.networkFailed("请选择有效的日期范围")
|
||||
}
|
||||
if let dateFrom, let dateTo, dateFrom > dateTo {
|
||||
throw APIError.networkFailed("请选择有效的日期范围")
|
||||
}
|
||||
var result = [URLQueryItem(name: "scenic_id", value: String(scenicID)),
|
||||
URLQueryItem(name: "page", value: String(page)),
|
||||
URLQueryItem(name: "page_size", value: String(pageSize)),
|
||||
URLQueryItem(name: "retention_days", value: String(retentionDays))]
|
||||
if let airLineID { result.append(URLQueryItem(name: "air_line_id", value: String(airLineID))) }
|
||||
if let dateFrom { result.append(URLQueryItem(name: "date_from", value: dateFrom)) }
|
||||
if let dateTo { result.append(URLQueryItem(name: "date_to", value: dateTo)) }
|
||||
return result
|
||||
}
|
||||
|
||||
/// 严格校验公历日期,拒绝 DateFormatter 自动纠正的越界日期。
|
||||
private static func isValidDate(_ value: String?) -> Bool {
|
||||
guard let value else { return true }
|
||||
let formatter = DateFormatter()
|
||||
formatter.calendar = Calendar(identifier: .gregorian)
|
||||
formatter.locale = Locale(identifier: "en_US_POSIX")
|
||||
formatter.timeZone = TimeZone(secondsFromGMT: 0)
|
||||
formatter.dateFormat = "yyyy-MM-dd"
|
||||
formatter.isLenient = false
|
||||
guard let date = formatter.date(from: value) else { return false }
|
||||
return formatter.string(from: date) == value
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user