feat(drone-dock): 对接订单素材库与点位筛选
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
import Foundation
|
||||
|
||||
/// 订单素材库只读数据源,分别读取整单摘要和指定范围内的任务。
|
||||
protocol DroneDockMaterialsServing: Sendable {
|
||||
/// 获取整单摘要和购买点位;筛选任务不改变摘要口径。
|
||||
@MainActor
|
||||
func detail(scenicID: Int, orderNumber: String) async throws -> DroneDockMaterialDetail
|
||||
|
||||
/// 一次获取全部有效任务;spotID 为空时省略点位参数。
|
||||
@MainActor
|
||||
func tasks(scenicID: Int, orderNumber: String, spotID: Int?) async throws -> DroneDockMaterialTasks
|
||||
}
|
||||
|
||||
/// 张工订单素材库与点位筛选接口,复用统一登录凭证和业务错误处理。
|
||||
@MainActor
|
||||
final class DroneDockMaterialsAPI: DroneDockMaterialsServing {
|
||||
private let client: APIClient
|
||||
|
||||
/// 注入统一客户端,便于在测试中隔离网络。
|
||||
init(client: APIClient) { self.client = client }
|
||||
|
||||
/// 读取根级订单摘要及 spot,不使用 workbench 或其他订单的数据回填。
|
||||
func detail(scenicID: Int, orderNumber: String) async throws -> DroneDockMaterialDetail {
|
||||
let queryItems = try queryItems(scenicID: scenicID, orderNumber: orderNumber)
|
||||
let response: DroneDockMaterialDetail = try await client.send(APIRequest(
|
||||
method: .get, path: "/api/yf-handset-app/photog/multi-plane/orders/detail", queryItems: queryItems
|
||||
))
|
||||
guard response.order.orderNumber == orderNumber else {
|
||||
throw APIError.decodeFailed("素材库摘要与当前订单不一致,请刷新重试")
|
||||
}
|
||||
return response
|
||||
}
|
||||
|
||||
/// 点位仅筛选任务;响应必须属于请求订单和点位,不发送分页参数。
|
||||
func tasks(scenicID: Int, orderNumber: String, spotID: Int?) async throws -> DroneDockMaterialTasks {
|
||||
var queryItems = try queryItems(scenicID: scenicID, orderNumber: orderNumber)
|
||||
if let spotID {
|
||||
guard spotID > 0 else { throw APIError.networkFailed("缺少有效拍摄点,请重新选择") }
|
||||
queryItems.append(URLQueryItem(name: "scenic_spot_id", value: String(spotID)))
|
||||
}
|
||||
let response: DroneDockMaterialTasks = try await client.send(APIRequest(
|
||||
method: .get, path: "/api/yf-handset-app/photog/multi-plane/tasks", queryItems: queryItems
|
||||
))
|
||||
guard response.orderNumber == orderNumber,
|
||||
spotID == nil || response.list.allSatisfy({ $0.spotID == spotID }) else {
|
||||
throw APIError.decodeFailed("素材任务与当前订单或拍摄点不一致,请刷新重试")
|
||||
}
|
||||
return response
|
||||
}
|
||||
|
||||
/// 请求前验证页面范围,禁止用缺失身份请求其他订单。
|
||||
private func queryItems(scenicID: Int, orderNumber: String) throws -> [URLQueryItem] {
|
||||
guard scenicID > 0, !orderNumber.isEmpty, orderNumber.count <= 128,
|
||||
orderNumber == orderNumber.trimmingCharacters(in: .whitespacesAndNewlines) else {
|
||||
throw APIError.networkFailed("缺少有效景区或订单信息,请返回订单列表重新进入")
|
||||
}
|
||||
return [URLQueryItem(name: "scenic_id", value: String(scenicID)),
|
||||
URLQueryItem(name: "order_number", value: orderNumber)]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,284 @@
|
||||
import Foundation
|
||||
|
||||
/// 订单素材库的整单摘要和购买点位,不受当前素材筛选条件影响。
|
||||
struct DroneDockMaterialDetail: Decodable, Equatable, Sendable {
|
||||
/// 复用张工订单摘要中的次数、状态和操作权限。
|
||||
let order: DroneDockHomeOrder
|
||||
/// 按购买快照顺序返回的点位筛选项。
|
||||
let spots: [DroneDockMaterialSpot]
|
||||
|
||||
/// 订单摘要位于根级,购买点位使用单数 spot 字段。
|
||||
private enum CodingKeys: String, CodingKey { case spots = "spot" }
|
||||
|
||||
/// 显式空数组表示无点位;缺少配置或重复身份视为异常响应。
|
||||
init(from decoder: Decoder) throws {
|
||||
order = try DroneDockHomeOrder(from: decoder)
|
||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||
spots = try container.decode([DroneDockMaterialSpot].self, forKey: .spots)
|
||||
guard Set(spots.map(\.id)).count == spots.count else {
|
||||
throw DecodingError.dataCorruptedError(forKey: .spots, in: container, debugDescription: "素材库点位 ID 重复")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 订单购买快照中的点位筛选项。
|
||||
struct DroneDockMaterialSpot: Decodable, Equatable, Sendable {
|
||||
/// 点位稳定身份,向 tasks 传递为 scenic_spot_id。
|
||||
let id: Int
|
||||
/// 点位名称,缺失时由页面显示占位文本。
|
||||
let name: String
|
||||
|
||||
/// 张工点位快照字段。
|
||||
private enum CodingKeys: String, CodingKey {
|
||||
case id = "scenic_spot_id", name = "scenic_spot_name"
|
||||
}
|
||||
|
||||
/// 点位身份必须有效,缺失名称不影响查看该点位任务。
|
||||
init(from decoder: Decoder) throws {
|
||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||
id = try container.positiveMaterialID(forKey: .id)
|
||||
name = try container.decodeIfPresent(String.self, forKey: .name) ?? ""
|
||||
}
|
||||
}
|
||||
|
||||
/// 当前订单及点位范围内的全部有效任务;接口一次返回,无分页。
|
||||
struct DroneDockMaterialTasks: Decodable, Equatable, Sendable {
|
||||
/// 任务列表所属订单号。
|
||||
let orderNumber: String
|
||||
/// 当前范围内的任务记录数,不能用作成功拍摄次数。
|
||||
let total: Int
|
||||
/// 保留服务端顺序及待飞、失败等任务。
|
||||
let list: [DroneDockMaterialTask]
|
||||
|
||||
/// tasks 响应根字段。
|
||||
private enum CodingKeys: String, CodingKey { case orderNumber = "order_number", total, list }
|
||||
|
||||
/// 校验完整列表及订单归属,避免异常结构被展示成真实空态。
|
||||
init(from decoder: Decoder) throws {
|
||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||
orderNumber = try container.materialOrderNumber(forKey: .orderNumber)
|
||||
total = try container.decode(DroneDockMaterialInteger.self, forKey: .total).value
|
||||
list = try container.decode([DroneDockMaterialTask].self, forKey: .list)
|
||||
guard total >= 0, total == list.count else {
|
||||
throw DecodingError.dataCorruptedError(forKey: .total, in: container, debugDescription: "任务总数与完整列表不一致")
|
||||
}
|
||||
guard Set(list.map(\.id)).count == list.count else {
|
||||
throw DecodingError.dataCorruptedError(forKey: .list, in: container, debugDescription: "素材库任务 ID 重复")
|
||||
}
|
||||
guard list.allSatisfy({ $0.orderNumber == orderNumber }) else {
|
||||
throw DecodingError.dataCorruptedError(forKey: .list, in: container, debugDescription: "任务不属于当前订单")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 一次有效飞行任务及其原素材、当前成片,不以素材是否就绪隐藏任务。
|
||||
struct DroneDockMaterialTask: Decodable, Equatable, Sendable {
|
||||
/// 订单飞行任务身份,与实际飞行平台任务编号不同。
|
||||
let id: Int
|
||||
/// 任务所属订单号。
|
||||
let orderNumber: String
|
||||
/// 购买快照中的点位身份。
|
||||
let spotID: Int
|
||||
/// 购买快照中的点位名称,缺失时为空。
|
||||
let spotName: String
|
||||
/// 购买快照中的航线名称。
|
||||
let airLineName: String?
|
||||
/// 订单内任务序号,不代表成功拍摄次数。
|
||||
let sequence: Int?
|
||||
/// 后端飞行状态,未知状态原样保留。
|
||||
let flightStatus: Int?
|
||||
/// 本任务原素材聚合状态。
|
||||
let materialStatus: Int?
|
||||
/// 服务端记录的任务创建时间。
|
||||
let createdAt: String?
|
||||
/// 本次任务的原素材,显式空数组表示尚无素材。
|
||||
let materials: [DroneDockMaterialFile]
|
||||
/// 当前已交付的视频成片,未交付时为空。
|
||||
let film: DroneDockMaterialFilm?
|
||||
|
||||
/// 张工任务字段,id 与 task_id 表示同一任务。
|
||||
private enum CodingKeys: String, CodingKey {
|
||||
case id, taskID = "task_id", orderNumber = "order_number"
|
||||
case spotID = "scenic_spot_id", spotName = "scenic_spot_name", airLineName = "air_line_name"
|
||||
case sequence = "seq", flightStatus = "flight_status", materialStatus = "material_status"
|
||||
case createdAt = "created_at", materials, film
|
||||
}
|
||||
|
||||
/// 兼容两种任务 ID 字段,但拒绝冲突、重复素材及其他任务的成片。
|
||||
init(from decoder: Decoder) throws {
|
||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||
let identifier = try container.optionalPositiveMaterialID(forKey: .id)
|
||||
let taskIdentifier = try container.optionalPositiveMaterialID(forKey: .taskID)
|
||||
guard let resolvedID = identifier ?? taskIdentifier,
|
||||
identifier == nil || taskIdentifier == nil || identifier == taskIdentifier else {
|
||||
throw DecodingError.dataCorruptedError(forKey: .id, in: container, debugDescription: "任务 ID 缺失或不一致")
|
||||
}
|
||||
id = resolvedID
|
||||
orderNumber = try container.materialOrderNumber(forKey: .orderNumber)
|
||||
spotID = try container.positiveMaterialID(forKey: .spotID)
|
||||
spotName = try container.decodeIfPresent(String.self, forKey: .spotName) ?? ""
|
||||
airLineName = try container.decodeIfPresent(String.self, forKey: .airLineName)
|
||||
sequence = try container.optionalMaterialInteger(forKey: .sequence)
|
||||
flightStatus = try container.optionalMaterialInteger(forKey: .flightStatus)
|
||||
materialStatus = try container.optionalMaterialInteger(forKey: .materialStatus)
|
||||
createdAt = try container.decodeIfPresent(String.self, forKey: .createdAt)
|
||||
materials = try container.decode([DroneDockMaterialFile].self, forKey: .materials)
|
||||
film = try container.decodeIfPresent(DroneDockMaterialFilm.self, forKey: .film)
|
||||
guard Set(materials.map(\.id)).count == materials.count else {
|
||||
throw DecodingError.dataCorruptedError(forKey: .materials, in: container, debugDescription: "任务内素材 ID 重复")
|
||||
}
|
||||
if let filmTaskID = film?.taskID, filmTaskID != id {
|
||||
throw DecodingError.dataCorruptedError(forKey: .film, in: container, debugDescription: "成片不属于当前任务")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 飞行模块回写的原素材,上传未完成时保留其状态和失败说明。
|
||||
struct DroneDockMaterialFile: Decodable, Equatable, Sendable {
|
||||
/// 素材稳定身份,在所属任务内唯一。
|
||||
let id: Int
|
||||
/// 1 为图片、2 为视频,未知值保留且不开放预览。
|
||||
let fileType: Int?
|
||||
/// 原素材文件名。
|
||||
let fileName: String?
|
||||
/// 原素材字节数,缺失时不推测。
|
||||
let fileSize: Int?
|
||||
/// 后端提供的封面地址。
|
||||
let coverURL: String?
|
||||
/// 后端提供的文件地址,未上传完成时可能为空串。
|
||||
let fileURL: String?
|
||||
/// 0 未开始、1 已就绪、2 上传中、3 失败。
|
||||
let uploadStatus: Int?
|
||||
/// 服务端返回的上传失败原因。
|
||||
let failureReason: String?
|
||||
|
||||
/// 只有明确就绪且类型受支持的 HTTP(S) 文件才可预览。
|
||||
var previewURL: URL? {
|
||||
guard uploadStatus == 1, fileType == 1 || fileType == 2 else { return nil }
|
||||
return DroneDockMaterialURL.preview(fileURL)
|
||||
}
|
||||
|
||||
/// 张工原素材字段。
|
||||
private enum CodingKeys: String, CodingKey {
|
||||
case id, fileType = "file_type", fileName = "file_name", fileSize = "file_size"
|
||||
case coverURL = "cover_url", fileURL = "file_url", uploadStatus = "upload_status"
|
||||
case failureReason = "failure_reason"
|
||||
}
|
||||
|
||||
/// 解码可为空的回写信息,素材身份仍必须有效。
|
||||
init(from decoder: Decoder) throws {
|
||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||
id = try container.positiveMaterialID(forKey: .id)
|
||||
fileType = try container.optionalMaterialInteger(forKey: .fileType)
|
||||
fileName = try container.decodeIfPresent(String.self, forKey: .fileName)
|
||||
fileSize = try container.optionalMaterialSize(forKey: .fileSize)
|
||||
coverURL = try container.decodeIfPresent(String.self, forKey: .coverURL)
|
||||
fileURL = try container.decodeIfPresent(String.self, forKey: .fileURL)
|
||||
uploadStatus = try container.optionalMaterialInteger(forKey: .uploadStatus)
|
||||
failureReason = try container.decodeIfPresent(String.self, forKey: .failureReason)
|
||||
}
|
||||
}
|
||||
|
||||
/// 当前任务已交付的成片;缺少有效地址时保留记录但不提供预览。
|
||||
struct DroneDockMaterialFilm: Decodable, Equatable, Sendable {
|
||||
/// 成片稳定身份。
|
||||
let id: Int
|
||||
/// 所属任务身份,接口提供时必须与外层任务一致。
|
||||
let taskID: Int?
|
||||
/// 成片文件名。
|
||||
let fileName: String?
|
||||
/// 成片字节数。
|
||||
let fileSize: Int?
|
||||
/// 后端提供的成片封面地址。
|
||||
let coverURL: String?
|
||||
/// 后端提供的成片视频地址。
|
||||
let fileURL: String?
|
||||
|
||||
/// 已交付成片只接受有效 HTTP(S) 视频地址。
|
||||
var previewURL: URL? { DroneDockMaterialURL.preview(fileURL) }
|
||||
|
||||
/// 张工当前成片字段。
|
||||
private enum CodingKeys: String, CodingKey {
|
||||
case id, taskID = "task_id", fileName = "file_name", fileSize = "file_size"
|
||||
case coverURL = "cover_url", fileURL = "file_url"
|
||||
}
|
||||
|
||||
/// 容忍未回写的展示字段,保留可校验的任务归属。
|
||||
init(from decoder: Decoder) throws {
|
||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||
id = try container.positiveMaterialID(forKey: .id)
|
||||
taskID = try container.optionalPositiveMaterialID(forKey: .taskID)
|
||||
fileName = try container.decodeIfPresent(String.self, forKey: .fileName)
|
||||
fileSize = try container.optionalMaterialSize(forKey: .fileSize)
|
||||
coverURL = try container.decodeIfPresent(String.self, forKey: .coverURL)
|
||||
fileURL = try container.decodeIfPresent(String.self, forKey: .fileURL)
|
||||
}
|
||||
}
|
||||
|
||||
/// 素材库整数读取器,兼容整数字符串并拒绝布尔、分数和任意文本。
|
||||
private struct DroneDockMaterialInteger: Decodable {
|
||||
let value: Int
|
||||
|
||||
/// 数值范围由具体字段的读取方法校验。
|
||||
init(from decoder: Decoder) throws {
|
||||
let container = try decoder.singleValueContainer()
|
||||
if let number = try? container.decode(Int.self) { value = number }
|
||||
else if let text = try? container.decode(String.self), let number = Int(text) { value = number }
|
||||
else { throw DecodingError.dataCorruptedError(in: container, debugDescription: "需要整数或整数字符串") }
|
||||
}
|
||||
}
|
||||
|
||||
/// 素材和成片共用的远程预览地址规则。
|
||||
private enum DroneDockMaterialURL {
|
||||
/// 拒绝空值、空白字符、本地地址和无主机的 URL。
|
||||
static func preview(_ value: String?) -> URL? {
|
||||
guard let value, !value.isEmpty,
|
||||
value.rangeOfCharacter(from: .whitespacesAndNewlines) == nil,
|
||||
let url = URL(string: value), let scheme = url.scheme?.lowercased(),
|
||||
["https", "http"].contains(scheme), let host = url.host, !host.isEmpty else { return nil }
|
||||
return url
|
||||
}
|
||||
}
|
||||
|
||||
private extension KeyedDecodingContainer {
|
||||
/// 素材、任务、点位及成片必须有正整数身份。
|
||||
func positiveMaterialID(forKey key: Key) throws -> Int {
|
||||
let value = try decode(DroneDockMaterialInteger.self, forKey: key).value
|
||||
guard value > 0 else {
|
||||
throw DecodingError.dataCorruptedError(forKey: key, in: self, debugDescription: "素材库 ID 必须为正整数")
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
/// 可选关联身份在提供值时仍要求正整数。
|
||||
func optionalPositiveMaterialID(forKey key: Key) throws -> Int? {
|
||||
guard let value = try optionalMaterialInteger(forKey: key) else { return nil }
|
||||
guard value > 0 else {
|
||||
throw DecodingError.dataCorruptedError(forKey: key, in: self, debugDescription: "素材库关联 ID 必须为正整数")
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
/// 状态码不限定已知枚举,缺失或 null 保留未知。
|
||||
func optionalMaterialInteger(forKey key: Key) throws -> Int? {
|
||||
try decodeIfPresent(DroneDockMaterialInteger.self, forKey: key)?.value
|
||||
}
|
||||
|
||||
/// 文件大小可缺失,但不能出现负字节数。
|
||||
func optionalMaterialSize(forKey key: Key) throws -> Int? {
|
||||
let value = try optionalMaterialInteger(forKey: key)
|
||||
if let value, value < 0 {
|
||||
throw DecodingError.dataCorruptedError(forKey: key, in: self, debugDescription: "素材大小不能为负数")
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
/// 保留订单号前导零,禁止缺失或带首尾空白的身份。
|
||||
func materialOrderNumber(forKey key: Key) throws -> String {
|
||||
let value = try decode(String.self, forKey: key)
|
||||
guard !value.isEmpty, value == value.trimmingCharacters(in: .whitespacesAndNewlines) else {
|
||||
throw DecodingError.dataCorruptedError(forKey: key, in: self, debugDescription: "订单号必须为有效非空字符串")
|
||||
}
|
||||
return value
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
import Foundation
|
||||
|
||||
/// 订单素材库的远程状态,整单摘要与点位筛选后的任务分别维护。
|
||||
final class DroneDockMaterialsViewModel {
|
||||
/// 进入页面时冻结的账号、景区和门店范围。
|
||||
let context: DroneDockHomeContext
|
||||
/// 入口订单仅提供身份和首次读取前的摘要。
|
||||
let order: DroneDockHomeOrder
|
||||
/// orders/detail 返回的整单摘要与购买点位。
|
||||
private(set) var detail: DroneDockMaterialDetail?
|
||||
/// 当前点位下的全部有效任务,保持服务端顺序。
|
||||
private(set) var tasks: [DroneDockMaterialTask] = []
|
||||
/// 当前筛选下的任务数,未知时不以零代替。
|
||||
private(set) var total: Int?
|
||||
/// 当前点位,nil 表示全部点位。
|
||||
private(set) var selectedSpotID: Int?
|
||||
/// 是否正在读取整单摘要和点位标签。
|
||||
private(set) var isLoading = false
|
||||
/// 是否正在读取全部或指定点位的任务。
|
||||
private(set) var isLoadingTasks = false
|
||||
/// 整单摘要、入口参数或会话范围错误。
|
||||
private(set) var detailErrorMessage: String?
|
||||
/// 当前任务列表读取错误,不改变已读取的整单摘要。
|
||||
private(set) var tasksErrorMessage: String?
|
||||
/// 点位标签始终来自整单购买快照,不从当前任务推导。
|
||||
var spots: [DroneDockMaterialSpot] { detail?.spots ?? [] }
|
||||
|
||||
private let service: any DroneDockMaterialsServing
|
||||
private let currentContext: @MainActor @Sendable () -> DroneDockHomeContext
|
||||
private var generation = 0
|
||||
|
||||
/// 注入只读素材服务和会话校验器,不依赖应用全局登录态。
|
||||
init(context: DroneDockHomeContext, order: DroneDockHomeOrder,
|
||||
service: any DroneDockMaterialsServing,
|
||||
currentContext: @escaping @MainActor @Sendable () -> DroneDockHomeContext) {
|
||||
self.context = context
|
||||
self.order = order
|
||||
self.service = service
|
||||
self.currentContext = currentContext
|
||||
}
|
||||
|
||||
/// 先刷新整单摘要,再读取任务;保留仍在购买快照中的点位筛选。
|
||||
@MainActor
|
||||
func refresh() async {
|
||||
generation &+= 1
|
||||
let requestGeneration = generation
|
||||
detail = nil
|
||||
clearTasks()
|
||||
isLoading = true
|
||||
isLoadingTasks = false
|
||||
detailErrorMessage = nil
|
||||
defer { if generation == requestGeneration { isLoading = false } }
|
||||
guard await accepts(requestGeneration) else { return }
|
||||
guard context.canLoad, let number = orderNumber else {
|
||||
selectedSpotID = nil
|
||||
detailErrorMessage = "缺少当前景区、门店或订单信息,请返回工作台重新进入"
|
||||
return
|
||||
}
|
||||
do {
|
||||
let value = try await service.detail(scenicID: context.scenicID, orderNumber: number)
|
||||
guard await accepts(requestGeneration) else { return }
|
||||
guard value.order.orderNumber == number else {
|
||||
selectedSpotID = nil
|
||||
detailErrorMessage = "素材库摘要与当前订单不一致,请刷新重试"
|
||||
return
|
||||
}
|
||||
detail = value
|
||||
if let selectedSpotID, !value.spots.contains(where: { $0.id == selectedSpotID }) {
|
||||
self.selectedSpotID = nil
|
||||
}
|
||||
isLoading = false
|
||||
await loadTasks(requestGeneration: requestGeneration, orderNumber: number)
|
||||
} catch {
|
||||
guard await accepts(requestGeneration) else { return }
|
||||
selectedSpotID = nil
|
||||
detailErrorMessage = Self.message(for: error)
|
||||
}
|
||||
}
|
||||
|
||||
/// 切换购买点位时只刷新 tasks,整单次数、状态和标签保持不变。
|
||||
@MainActor
|
||||
func selectSpot(id: Int?) async {
|
||||
guard !isLoading, detail != nil, id != selectedSpotID,
|
||||
id == nil || spots.contains(where: { $0.id == id }) else { return }
|
||||
generation &+= 1
|
||||
selectedSpotID = id
|
||||
clearTasks()
|
||||
guard let number = orderNumber else { return }
|
||||
await loadTasks(requestGeneration: generation, orderNumber: number)
|
||||
}
|
||||
|
||||
/// 任务读取失败后重试当前点位,不重复请求整单摘要。
|
||||
@MainActor
|
||||
func retryTasks() async {
|
||||
guard !isLoading, !isLoadingTasks, detail != nil, let number = orderNumber else { return }
|
||||
generation &+= 1
|
||||
clearTasks()
|
||||
await loadTasks(requestGeneration: generation, orderNumber: number)
|
||||
}
|
||||
|
||||
/// 离开页面时清除敏感内容,并使所有在途响应失效。
|
||||
@MainActor
|
||||
func invalidate() {
|
||||
generation &+= 1
|
||||
detail = nil
|
||||
selectedSpotID = nil
|
||||
clearTasks()
|
||||
isLoading = false
|
||||
isLoadingTasks = false
|
||||
detailErrorMessage = nil
|
||||
}
|
||||
|
||||
private var orderNumber: String? {
|
||||
guard let number = order.orderNumber, !number.isEmpty,
|
||||
number == number.trimmingCharacters(in: .whitespacesAndNewlines) else { return nil }
|
||||
return number
|
||||
}
|
||||
|
||||
@MainActor
|
||||
private func loadTasks(requestGeneration: Int, orderNumber: String) async {
|
||||
let requestedSpotID = selectedSpotID
|
||||
isLoadingTasks = true
|
||||
tasksErrorMessage = nil
|
||||
defer { if generation == requestGeneration { isLoadingTasks = false } }
|
||||
guard await accepts(requestGeneration) else { return }
|
||||
do {
|
||||
let value = try await service.tasks(scenicID: context.scenicID, orderNumber: orderNumber, spotID: requestedSpotID)
|
||||
guard await accepts(requestGeneration) else { return }
|
||||
guard value.orderNumber == orderNumber, value.total == value.list.count,
|
||||
value.list.allSatisfy({ $0.orderNumber == orderNumber && (requestedSpotID == nil || $0.spotID == requestedSpotID) }) else {
|
||||
clearTasks()
|
||||
tasksErrorMessage = "素材任务与当前订单或拍摄点不一致,请刷新重试"
|
||||
return
|
||||
}
|
||||
tasks = value.list
|
||||
total = value.total
|
||||
} catch {
|
||||
guard await accepts(requestGeneration) else { return }
|
||||
clearTasks()
|
||||
tasksErrorMessage = Self.message(for: error)
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
private func accepts(_ requestGeneration: Int) async -> Bool {
|
||||
guard generation == requestGeneration, !Task.isCancelled else { return false }
|
||||
let latest = currentContext()
|
||||
guard context.hasSameIdentity(as: latest) else {
|
||||
invalidate()
|
||||
detailErrorMessage = "账号或景区已变化,请返回工作台重新进入"
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
@MainActor
|
||||
private func clearTasks() {
|
||||
tasks = []
|
||||
total = nil
|
||||
tasksErrorMessage = nil
|
||||
}
|
||||
|
||||
private static func message(for error: Error) -> String? {
|
||||
error is CancellationError ? nil : error.localizedDescription
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user