feat: 接入景区空境库真实素材并统一交付入口
接通场次详情、分页、视频时长与大小,修复日期筛选越界并增加脱敏日志。统一订单与素材库交付方式弹窗,保留任务预选和上传批次,补充模拟器回归测试与接口记录。
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
import Foundation
|
||||
|
||||
/// 按模块生成可读的调试日志;只处理输出副本,不修改网络请求和业务响应。
|
||||
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")
|
||||
}
|
||||
|
||||
/// 输出 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
|
||||
}
|
||||
|
||||
/// 生成请求、响应、失败和取消共用的模块标签与安全地址。
|
||||
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)"
|
||||
}
|
||||
|
||||
/// 非 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
|
||||
}
|
||||
|
||||
/// 递归处理嵌套对象与数组,保留普通字段、空值及 JSON 类型。
|
||||
private static func redact(_ value: Any) -> Any {
|
||||
if let object = value as? [String: Any] {
|
||||
return Dictionary(uniqueKeysWithValues: object.map { key, value in
|
||||
(key, isSensitive(key) ? "<redacted>" : redact(value))
|
||||
})
|
||||
}
|
||||
if let array = value as? [Any] { return array.map(redact) }
|
||||
if let text = value as? String { return redactURLs(in: text) }
|
||||
return value
|
||||
}
|
||||
|
||||
/// 兼容 snake_case、大小写及 OSS/S3 常用签名字段。
|
||||
private static func isSensitive(_ key: String) -> Bool {
|
||||
let normalized = key.lowercased().filter { $0.isLetter || $0.isNumber }
|
||||
return ["token", "authorization", "signature", "credential", "accesskey", "secret", "password", "cookie"]
|
||||
.contains { normalized.contains($0) }
|
||||
}
|
||||
|
||||
/// JSON 字段或消息中的 URL 只保留资源路径,不记录有效期、签名及其他查询值。
|
||||
private static func redactURLs(in text: String) -> String {
|
||||
guard let pattern = urlPattern else { return text }
|
||||
var result = text
|
||||
for match in pattern.matches(in: text, range: NSRange(text.startIndex..., in: text)).reversed() {
|
||||
guard let range = Range(match.range, in: result) else { continue }
|
||||
let replacement = URL(string: String(result[range])).map { redactedURL($0, keepsQuery: false) }
|
||||
?? "<redacted URL>"
|
||||
result.replaceSubrange(range, with: replacement)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/// 请求地址保留业务查询值;正文中的资源地址删除完整查询,兼容未来签名参数。
|
||||
private static func redactedURL(_ url: URL, keepsQuery: Bool) -> String {
|
||||
guard var components = URLComponents(url: url, resolvingAgainstBaseURL: false) else { return "<redacted URL>" }
|
||||
components.user = nil
|
||||
components.password = nil
|
||||
components.fragment = nil
|
||||
if keepsQuery {
|
||||
components.queryItems = components.queryItems?.map {
|
||||
URLQueryItem(name: $0.name, value: isSensitive($0.name) ? "<redacted>" : $0.value.map(redactURLs))
|
||||
}
|
||||
} else {
|
||||
components.query = nil
|
||||
}
|
||||
return components.string ?? "<redacted URL>"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user