Files
suixinkan_uikit/suixinkan/Core/Push/PushModels.swift
汉秋 1b2637f0ee fix: 推送点击仅按 type 路由到收款页或消息中心。
对齐后端消息类型约定,去掉 route/uri 等兼容字段,并补充联调日志与单测。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-22 18:20:36 +08:00

87 lines
2.8 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import Foundation
///
enum PushDestination: Sendable, Equatable {
case paymentRecord
case paymentDetails
case messageCenter
}
/// /APNs payload
struct PushPayload: Sendable, Equatable {
private let type: String
/// userInfo
nonisolated init(userInfo: [AnyHashable: Any]) {
type = Self.extractType(from: userInfo)
}
/// payload
nonisolated init(values: [String: String]) {
type = Self.normalizedType(values["type"])
}
///
nonisolated var normalizedValues: [String: String] {
type.isEmpty ? [:] : ["type": type]
}
///
nonisolated var destination: PushDestination {
switch type {
case "1":
return .paymentRecord
case "6":
return .paymentDetails
default:
return .messageCenter
}
}
private nonisolated static func extractType(
from dictionary: [AnyHashable: Any],
depth: Int = 0
) -> String {
guard depth <= 4 else { return "" }
let directType = normalizedType(dictionary["type"])
guard directType.isEmpty else { return directType }
for key in nestedTypeContainerKeys {
guard let value = dictionary[key] else { continue }
if let nested = value as? [AnyHashable: Any] {
let nestedType = extractType(from: nested, depth: depth + 1)
if !nestedType.isEmpty { return nestedType }
continue
}
guard let text = value as? String,
let data = text.data(using: .utf8),
let object = try? JSONSerialization.jsonObject(with: data),
let nested = object as? [String: Any]
else { continue }
let bridged = Dictionary(uniqueKeysWithValues: nested.map {
(AnyHashable($0.key), $0.value)
})
let nestedType = extractType(from: bridged, depth: depth + 1)
if !nestedType.isEmpty { return nestedType }
}
return ""
}
private nonisolated static func normalizedType(_ value: Any?) -> String {
switch value {
case let string as String:
return string.trimmingCharacters(in: .whitespacesAndNewlines)
case let number as NSNumber:
return number.stringValue
default:
return ""
}
}
private nonisolated static let nestedTypeContainerKeys = [
"extras", "extra", "JMessageExtra", "n_extras",
]
}