feat: 接入拍摄任务实时订阅并完善日志与封面

This commit is contained in:
2026-11-03 09:06:38 +08:00
parent 687088326c
commit 980d16da9b
27 changed files with 1790 additions and 64 deletions
+4 -4
View File
@@ -430,7 +430,7 @@ final class APIClient {
}
let method = request.httpMethod ?? "REQUEST"
let url = request.url?.absoluteString ?? "<invalid url>"
var lines = ["[API][Request] \(method) \(url)"]
var lines = ["\(APIConsoleFormat.prefix("Request", path: request.url?.path ?? "")) \(method) \(url)"]
if let headers = request.allHTTPHeaderFields, !headers.isEmpty {
lines.append("headers: " + DroneDockDiagnostics.json(headers))
}
@@ -469,7 +469,7 @@ final class APIClient {
let headers = (response as? HTTPURLResponse)?.allHeaderFields.reduce(into: [String: String]()) {
$0[String(describing: $1.key)] = String(describing: $1.value)
} ?? [:]
debugLogSink("[API][Response] \(method) \(url) status=\(statusCode)\nheaders: \(DroneDockDiagnostics.json(headers))\n\(body)")
debugLogSink("\(APIConsoleFormat.prefix("Response", path: request.url?.path ?? "")) \(method) \(url) status=\(statusCode)\nheaders: \(DroneDockDiagnostics.json(headers))\n\(body)")
}
/// Debug/Release 均记录取消,取消不记作成功响应或网络失败。
@@ -480,7 +480,7 @@ final class APIClient {
}
let method = request.httpMethod ?? "REQUEST"
let url = request.url?.absoluteString ?? "<invalid url>"
debugLogSink("[API][Cancelled] \(method) \(url) reason=\(reason)")
debugLogSink("\(APIConsoleFormat.prefix("Cancelled", path: request.url?.path ?? "")) \(method) \(url) reason=\(reason)")
}
/// 所有业务模块均记录传输、业务拒绝或解码失败;保留显式模块日志策略。
@@ -490,7 +490,7 @@ final class APIClient {
debugLogSink(APIRedactedLog.event("Failure", for: request, category: category)
+ " error=\(error.domain)(\(error.code))")
} else {
debugLogSink("[API][Failure] \(request.httpMethod ?? "REQUEST") \(request.url?.absoluteString ?? "<invalid url>")"
debugLogSink("\(APIConsoleFormat.prefix("Failure", path: request.url?.path ?? "")) \(request.httpMethod ?? "REQUEST") \(request.url?.absoluteString ?? "<invalid url>")"
+ " error=\(error.domain)(\(error.code)) message=\(error.localizedDescription)")
}
}
@@ -9,9 +9,8 @@ enum APIConsoleDiagnostics {
/// 不依赖调试构建或 stdout;复用 UTF-8 分段规则,避免长 JSON 被系统截断。
static func write(_ message: String) {
let entry = String(UUID().uuidString.prefix(8))
let chunks = DroneDockDiagnostics.chunks(message)
for (index, chunk) in chunks.enumerated() {
let line = "[API] session=\(sessionID) entry=\(entry) part=\(index + 1)/\(chunks.count) \(chunk)"
for line in APIConsoleFormat.lines(message, sessionID: sessionID, entryID: entry,
fallbackModule: "通用接口") {
logger.notice("\(line, privacy: .public)")
}
}
@@ -0,0 +1,102 @@
import Foundation
/// 控制台统一使用中文业务模块标签;只格式化输出,不修改请求和响应内容。
enum APIConsoleFormat {
/// 根据接口路径匹配业务模块;共享接口按自身职责命名,未知接口保留通用标签。
static func module(for path: String) -> String {
let routes: [(String, String)] = [
("/api/app/airport", "机巢工作台"),
("/api/app/order-flight-task", "机巢工作台"),
("/api/yf-handset-app/photog/multi-plane", "机巢工作台"),
("/api/app/scenic-media", "景区空境库"),
("/api/app/config/get-sts-token", "文件上传"),
("/api/app/scenic-queue", "景区排队"),
("/api/app/socket-token", "消息连接"),
("/api/app/manual-live", "直播"),
("/api/app/view-album", "直播素材"),
("/api/app/media-album", "样片管理"),
("/api/app/msg", "消息中心"),
("/api/app/user/register-jpush-id", "消息推送"),
("/api/app/training", "培训"),
("/api/app/store/order", "订单"),
("/api/app/scenic-admin/order", "订单"),
("/api/app/store/analyse", "数据统计"),
("/api/app/scenic-admin/analyse", "数据统计"),
("/api/app/store", "门店"),
("/api/app/v9", "账号登录"),
("/api/yf-handset-app/account-deregister", "账号注销"),
("/api/yf-handset-app/photog/order", "订单"),
("/api/yf-handset-app/photog/task", "拍摄任务"),
("/api/yf-handset-app/photog/analyse", "数据统计"),
("/api/yf-handset-app/photog/cloud-driver", "云存储"),
("/api/yf-handset-app/photog/travel-album", "旅拍相册"),
("/api/yf-handset-app/photog/wallet", "钱包"),
("/api/yf-handset-app/photog/point", "积分"),
("/api/yf-handset-app/photog/scenic-spot", "打卡点"),
("/api/yf-handset-app/photog/scenic", "景区"),
("/api/yf-handset-app/photog/scenic-apply", "景区入驻"),
("/api/yf-handset-app/photog/role-apply", "角色申请"),
("/api/yf-handset-app/photog/loacation", "位置上报"),
("/api/yf-handset-app/photog/report", "摄影师举报"),
("/api/yf-handset-app/photog/sale-user", "销售员"),
("/api/yf-handset-app/photog/cooperative-salers", "销售员"),
("/api/yf-handset-app/photog/invite", "邀请"),
("/api/yf-handset-app/photog/invite-info", "邀请"),
("/api/yf-handset-app/photog/project", "套餐"),
("/api/yf-handset-app/photog/offline-pay-collect", "线下收款"),
("/api/yf-handset-app/photog/pay-code", "收款"),
("/api/yf-handset-app/photog/pay-page-config", "收款"),
("/api/yf-handset-app/photog/real-name", "实名认证"),
("/api/yf-handset-app/photog/profile", "个人资料"),
("/api/yf-handset-app/photog/schedule", "个人资料"),
("/api/yf-handset-app/photog/userinfo-update-avatar", "个人资料"),
("/api/yf-handset-app/userinfo", "个人资料"),
("/api/yf-handset-app/userinfo-update", "个人资料"),
("/api/yf-handset-app/userinfo-update-avatar-url", "个人资料"),
("/api/yf-handset-app/role-permission", "账号权限"),
("/api/app/config", "系统配置"),
("/api/app/latest-version", "版本更新")
]
return routes.first { path == $0.0 || path.hasPrefix($0.0 + "/") }?.1 ?? "通用接口"
}
/// 请求、响应以及明确失败/取消使用同一标签结构,模块可由调用方显式指定。
static func prefix(_ event: String, path: String, category: String? = nil) -> String {
"【\(category ?? module(for: path))-\(event)】"
}
/// 为每个完整分段重复添加模块标签,保留正文、会话、事件 ID 及分段顺序。
static func lines(_ message: String, sessionID: String, entryID: String,
fallbackModule: String) -> [String] {
let tag: String
let body: String
if message.hasPrefix("【"), let end = message.firstIndex(of: "】") {
tag = String(message[...end])
let remainder = message[message.index(after: end)...]
body = remainder.hasPrefix(" ") ? String(remainder.dropFirst()) : String(remainder)
} else if let fields = try? JSONSerialization.jsonObject(with: Data(message.utf8)) as? [String: Any] {
let event = fields["event"] as? String ?? ""
let module: String
if event.hasPrefix("file_download.") { module = "素材下载" }
else if event.hasPrefix("file_upload.") { module = "成片上传" }
else if let path = fields["path"] as? String { module = Self.module(for: path) }
else { module = fallbackModule }
let kind: String
if event.hasSuffix("request.started") { kind = "Request" }
else if event.hasSuffix("response.received") { kind = "Response" }
else if event.hasSuffix("request.cancelled") { kind = "Cancelled" }
else if event.hasSuffix("request.failed") || event.hasSuffix("response.rejected") { kind = "Failure" }
else if event.hasSuffix("request.blocked") { kind = "Blocked" }
else { kind = "Event" }
tag = "【\(module)-\(kind)】"
body = message
} else {
tag = "【\(fallbackModule)-Event】"
body = message
}
let chunks = DroneDockDiagnostics.chunks(body)
return chunks.enumerated().map { index, chunk in
"\(tag) session=\(sessionID) entry=\(entryID) part=\(index + 1)/\(chunks.count) \(chunk)"
}
}
}
@@ -31,7 +31,7 @@ nonisolated enum APIRedactedLog {
/// 生成请求、响应、失败和取消共用的模块标签与安全地址。
static func event(_ event: String, for request: URLRequest, category: String) -> String {
let url = request.url.map { redactedURL($0, keepsQuery: true) } ?? "<invalid url>"
return "[API][\(category)][\(event)] \(request.httpMethod ?? "REQUEST") \(url)"
return "\(APIConsoleFormat.prefix(event, path: request.url?.path ?? "", category: category)) \(request.httpMethod ?? "REQUEST") \(url)"
}
/// 非 JSON 正文可能包含凭证或服务端调试页面,不把原始文本写入控制台。
@@ -9,9 +9,8 @@ enum DroneDockDiagnostics {
/// 按 UTF-8 字节分段,避免系统日志截断大响应;每段携带同一事件 ID 和序号。
static func write(_ message: String) {
let entry = String(UUID().uuidString.prefix(8))
let chunks = chunks(message)
for (index, chunk) in chunks.enumerated() {
let line = "[DroneDock] session=\(sessionID) entry=\(entry) part=\(index + 1)/\(chunks.count) \(chunk)"
for line in APIConsoleFormat.lines(message, sessionID: sessionID, entryID: entry,
fallbackModule: "机巢工作台") {
logger.notice("\(line, privacy: .public)")
}
}