feat: 完善机巢模拟调试与素材交付交互
增加禁飞期间的设备指令拦截及本地模拟拍摄流程;适配订单素材库和景区空境库,完善素材筛选、预览与下载;订单交付入口改为方式选择弹窗并复用云端上传。补充单元测试、接口记录和模拟器截图。
This commit is contained in:
@@ -11,6 +11,9 @@ protocol URLSessionProtocol {
|
||||
func data(for request: URLRequest) async throws -> (Data, URLResponse)
|
||||
}
|
||||
|
||||
/// 仅供完全在内存中返回预设响应的传输实现,不得转发至 URLSession 或外部服务。
|
||||
protocol LocalSimulationURLSession: URLSessionProtocol {}
|
||||
|
||||
extension URLSession: URLSessionProtocol {}
|
||||
|
||||
@MainActor
|
||||
@@ -63,7 +66,9 @@ final class APIClient {
|
||||
_ apiRequest: APIRequest<Response>,
|
||||
tokenOverride: String? = nil
|
||||
) async throws -> Response {
|
||||
let request = try makeURLRequest(apiRequest, tokenOverride: tokenOverride)
|
||||
let request = try DeviceCommandDebugPolicy.prepare(
|
||||
makeURLRequest(apiRequest, tokenOverride: tokenOverride),
|
||||
isLocalSimulation: session is any LocalSimulationURLSession)
|
||||
logRequest(request, includeBody: apiRequest.logsPayload)
|
||||
|
||||
let data: Data
|
||||
@@ -120,6 +125,7 @@ final class APIClient {
|
||||
}
|
||||
|
||||
request.httpBody = jsonBody.data(using: .utf8)
|
||||
request = try DeviceCommandDebugPolicy.prepare(request, isLocalSimulation: session is any LocalSimulationURLSession)
|
||||
logRequest(request)
|
||||
|
||||
let data: Data
|
||||
@@ -191,6 +197,7 @@ final class APIClient {
|
||||
request.setValue(token, forHTTPHeaderField: "token")
|
||||
}
|
||||
request.httpBody = body
|
||||
request = try DeviceCommandDebugPolicy.prepare(request, isLocalSimulation: session is any LocalSimulationURLSession)
|
||||
logRequest(request)
|
||||
|
||||
let responseData: Data
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
import Foundation
|
||||
|
||||
/// 禁飞期间的 Debug 请求边界;模拟器和测试环境均不能视为没有真实设备副作用。
|
||||
enum DeviceCommandDebugPolicy {
|
||||
/// 所有 Debug 构建禁止真实设备操作,没有启动参数或设置开关可临时放行。
|
||||
static var blocksLiveRequests: Bool {
|
||||
#if DEBUG
|
||||
true
|
||||
#else
|
||||
false
|
||||
#endif
|
||||
}
|
||||
|
||||
/// 页面与本地拦截共用的说明,不将未发送的请求表述为服务端执行失败。
|
||||
static let blockedMessage = "当前禁止真实设备指令,请使用本地模拟调试"
|
||||
|
||||
/// 在日志和传输之前阻断操作请求;允许的状态读取关闭实时设备采集。
|
||||
static func prepare(_ request: URLRequest, isLocalSimulation: Bool) throws -> URLRequest {
|
||||
guard blocksLiveRequests, !isLocalSimulation, let url = request.url else { return request }
|
||||
let path = "/" + url.path.split(separator: "/").joined(separator: "/")
|
||||
// 创建虽不直接起飞,但会改变真实订单状态;禁飞调试使用本地待飞任务。
|
||||
if path == "/api/yf-handset-app/photog/multi-plane/tasks/create" {
|
||||
throw DeviceCommandDebugBlockedError()
|
||||
}
|
||||
guard path == "/api/app/airport" || path.hasPrefix("/api/app/airport/") else { return request }
|
||||
let readablePaths: Set<String> = [
|
||||
"/api/app/airport/home", "/api/app/airport/plane-status",
|
||||
"/api/app/airport/shooting", "/api/app/airport/status"
|
||||
]
|
||||
guard request.httpMethod == "GET", readablePaths.contains(path) else {
|
||||
throw DeviceCommandDebugBlockedError()
|
||||
}
|
||||
let liveKey: String
|
||||
switch path {
|
||||
case "/api/app/airport/plane-status", "/api/app/airport/shooting": liveKey = "live_battery"
|
||||
case "/api/app/airport/status": liveKey = "live_metrics"
|
||||
default: return request
|
||||
}
|
||||
guard var components = URLComponents(url: url, resolvingAgainstBaseURL: false) else { throw APIError.invalidURL }
|
||||
var items = components.queryItems ?? []
|
||||
items.removeAll { $0.name == liveKey }
|
||||
items.append(URLQueryItem(name: liveKey, value: "0"))
|
||||
components.queryItems = items
|
||||
guard let safeURL = components.url else { throw APIError.invalidURL }
|
||||
var safeRequest = request
|
||||
safeRequest.url = safeURL
|
||||
return safeRequest
|
||||
}
|
||||
}
|
||||
|
||||
/// 明确表示请求在本机被阻断,任何设备指令或任务创建都未发送。
|
||||
struct DeviceCommandDebugBlockedError: Error, LocalizedError {
|
||||
/// 提示使用本地模拟,不伪造后端错误码或成功回执。
|
||||
var errorDescription: String? { DeviceCommandDebugPolicy.blockedMessage }
|
||||
}
|
||||
Reference in New Issue
Block a user