import Foundation /// 调试请求边界:Debug 禁止真实设备操作,Release 按用户授权开放已对接的操作。 enum DeviceCommandDebugPolicy { /// 2026-11-02 用户授权 Release 真机联调;Debug 仍无运行时放行开关。 static var blocksFlightRequests: Bool { #if DEBUG true #else false #endif } /// 所有 Debug 构建禁止真实设备操作,没有启动参数或设置开关可临时放行。 static var blocksLiveRequests: Bool { #if DEBUG true #else false #endif } /// 页面与本地拦截共用的说明,不将未发送的请求表述为服务端执行失败。 static let blockedMessage = "当前禁止真实设备指令,请使用本地模拟调试" /// 在日志和传输之前阻断操作请求;Release 准备页可读实时电量,其余状态读取关闭实时采集。 static func prepare(_ request: URLRequest, isLocalSimulation: Bool) throws -> URLRequest { guard !isLocalSimulation, let url = request.url else { return request } let path = "/" + url.path.split(separator: "/").joined(separator: "/") // Debug 创建也必须拦截,避免调试残留真实待飞任务。 if path == "/api/yf-handset-app/photog/multi-plane/tasks/create" { guard !blocksFlightRequests, request.httpMethod == "POST" else { throw DeviceCommandDebugBlockedError() } return request } guard path == "/api/app/airport" || path.hasPrefix("/api/app/airport/") else { return request } let flightPaths: Set = ["/api/app/airport/shoot", "/api/app/airport/start-shoot", "/api/app/airport/command"] if !blocksFlightRequests, request.httpMethod == "POST", flightPaths.contains(path) { return request } // 仅允许已确认的图传和飞控接口;未知设备操作仍阻断。 if request.httpMethod == "GET", path == "/api/app/airport/live", !blocksLiveRequests { return request } let readablePaths: Set = [ "/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() } #if !DEBUG // 2026-11-03 用户要求准备页获取电量;保留调用方的 live_battery,任务页仍强制读缓存。 if path == "/api/app/airport/plane-status" { return request } #endif 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 } }