fix: 精简接口日志并补充已支付航拍订单退款入口
统一 HTTP 和 WebSocket 控制台 JSON 输出,保留完整入参、响应及本地错误。为多点位航拍订单增加已支付退款入口和回归测试,并同步联调文档、接口记录及截图。
This commit is contained in:
@@ -1,49 +1,28 @@
|
||||
import Foundation
|
||||
|
||||
/// 按模块生成可读的调试日志;只处理输出副本,不修改网络请求和业务响应。
|
||||
/// 沿用接口指定的正文脱敏策略生成简洁 JSON;只处理输出副本,不修改网络请求和响应。
|
||||
nonisolated enum APIRedactedLog {
|
||||
private static let urlPattern = try? NSRegularExpression(pattern: #"https?://[^\s"<>]+"#, options: [.caseInsensitive])
|
||||
|
||||
/// 输出方法、地址、查询参数及可选 JSON 请求体,不输出鉴权 Header。
|
||||
/// 保留接口原有脱敏策略,只修改日志展示结构。
|
||||
static func request(_ request: URLRequest, category: String, includeBody: Bool) -> String {
|
||||
var lines = [event("Request", for: request, category: category)]
|
||||
if let url = request.url,
|
||||
let items = URLComponents(url: url, resolvingAgainstBaseURL: false)?.queryItems,
|
||||
!items.isEmpty {
|
||||
lines.append("params: " + items.map {
|
||||
"\($0.name)=\(isSensitive($0.name) ? "<redacted>" : redactURLs(in: $0.value ?? ""))"
|
||||
}.joined(separator: "&"))
|
||||
}
|
||||
if includeBody, let data = request.httpBody {
|
||||
lines.append("body:\n" + body(data))
|
||||
}
|
||||
return lines.joined(separator: "\n")
|
||||
APIConsoleFormat.message(path: request.url?.path ?? "", params: parameters(request, includeBody: includeBody))
|
||||
}
|
||||
|
||||
/// 输出 HTTP 状态及格式化的脱敏 JSON,包括业务失败或非 2xx 响应。
|
||||
/// 请求与回包共享参数,响应关闭正文记录时继续遵守原设置。
|
||||
static func response(for request: URLRequest, response: URLResponse, data: Data,
|
||||
category: String, includeBody: Bool) -> String {
|
||||
let status = (response as? HTTPURLResponse).map { String($0.statusCode) } ?? "unknown"
|
||||
let payload = includeBody ? body(data) : "<sensitive payload omitted>"
|
||||
return event("Response", for: request, category: category) + " status=\(status)\n" + payload
|
||||
let payload: Any
|
||||
if !includeBody { payload = "<sensitive payload omitted>" }
|
||||
else if let object = try? JSONSerialization.jsonObject(with: data, options: .fragmentsAllowed) { payload = redact(object) }
|
||||
else { payload = "<non-JSON body omitted: \(data.count) bytes>" }
|
||||
return APIConsoleFormat.message(path: request.url?.path ?? "",
|
||||
params: parameters(request, includeBody: includeBody), response: payload)
|
||||
}
|
||||
|
||||
/// 生成请求、响应、失败和取消共用的模块标签与安全地址。
|
||||
static func event(_ event: String, for request: URLRequest, category: String) -> String {
|
||||
let url = request.url.map { redactedURL($0, keepsQuery: true) } ?? "<invalid url>"
|
||||
return "\(APIConsoleFormat.prefix(event, path: request.url?.path ?? "", category: category)) \(request.httpMethod ?? "REQUEST") \(url)"
|
||||
}
|
||||
|
||||
/// 非 JSON 正文可能包含凭证或服务端调试页面,不把原始文本写入控制台。
|
||||
private static func body(_ data: Data) -> String {
|
||||
guard !data.isEmpty else { return "<empty body>" }
|
||||
guard let object = try? JSONSerialization.jsonObject(with: data, options: [.fragmentsAllowed]),
|
||||
let formatted = try? JSONSerialization.data(withJSONObject: redact(object),
|
||||
options: [.prettyPrinted, .sortedKeys, .fragmentsAllowed, .withoutEscapingSlashes]),
|
||||
let text = String(data: formatted, encoding: .utf8) else {
|
||||
return "<non-JSON body omitted: \(data.count) bytes>"
|
||||
}
|
||||
return text
|
||||
/// 脱敏只处理日志副本,原请求不受影响。
|
||||
static func parameters(_ request: URLRequest, includeBody: Bool) -> Any {
|
||||
redact(APIConsoleFormat.parameters(request, includeBody: includeBody))
|
||||
}
|
||||
|
||||
/// 递归处理嵌套对象与数组,保留普通字段、空值及 JSON 类型。
|
||||
|
||||
Reference in New Issue
Block a user