Files
youle_app_ios_v2/ylgamehall/Source/Bridge/BridgeProtocol.swift
T
joywayer b058990862 3.F 修复:mediaTypeAudio.user 字段兼容 number 与 string
## 问题

实测 H5 调 mediaTypeAudio 传入:
  {audiourl: "http://...amr", user: 0, type: 1}

user 是 number(0),不是 string。我之前用 obj["user"]?.asString 严格
要求 string,导致解析失败 → silent return,远端语音无法播放。

## 根因

daoqi 用 `[data objectForKey:@"user"]` 后 `[NSString stringWithFormat:
@"%@", userid]` 容忍 NSNumber(ObjC `%@` 对 NSNumber 调 description
自动转 string)。新外壳走 Swift 强类型解析,丢了这个隐式兼容。

## 修复

给 BridgeData 加 `asLooseString` 扩展:
  - string → 原样
  - number → 整数样 double 转 Int 字符串(0.0 → "0"),其它走 String(d)
  - bool   → "true"/"false"
  - null/array/object → nil

mediaTypeAudio handler 把 `obj["user"]?.asString` 改为
`obj["user"]?.asLooseString`。`audiourl` 保持 asString(URL 永远是
string)。

`asLooseString` 也可供其它 H5 字段类型不严格的 handler 复用。

## 验证

BuildProject 通过 0 错误。真机重试 mediaTypeAudio 应能正确解析
user=0 / user=12345 / user="abc" 等各种 H5 传入形态。
2026-06-24 02:12:21 +08:00

198 lines
6.9 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.
//
// BridgeProtocol.swift
// ylgamehall
//
// H5 ↔ Native 桥的核心抽象。详见 docs/H5-Native-Implementation-Design.md §3.2。
//
import Foundation
// MARK: - 桥接协议
/// 容器层(WebView VC)和 handler 注册层都依赖此抽象,便于单测注入 mock。
///
/// - 实现见 `BridgeBus`@MainActor,挂 WKWebView 上)
/// - H5 端协议见 `Resources/JS/WebViewJavascriptBridge.js`Phase 1.8 引入)
public protocol BridgeProtocol: AnyObject, Sendable {
/// 注册 H5 → Native handler。重复 name 后注册的会覆盖前一个。
func register(_ name: String, handler: @escaping BridgeHandler)
/// Native → H5 主动调用 H5 端 handler。`callback` 可选。
func call(_ name: String, data: BridgeData?, callback: BridgeCallback?)
}
/// H5 → Native handler 签名。
///
/// - 第 1 参:H5 端 `bridge.callHandler('name', data, ...)` 传过来的 data
/// - 第 2 参:响应回调,handler 调一次 `callback?(...)` 把结果回给 H5;可不调
/// - async:允许 handler 内部 await(如等待 SDK / 网络)
public typealias BridgeHandler = @Sendable (BridgeData?, BridgeCallback?) async -> Void
/// 桥回调签名(H5 → Native 的 responseCallback,或 Native → H5 的回包闭包)。
public typealias BridgeCallback = @Sendable (BridgeData?) -> Void
// MARK: - BridgeData
/// 桥消息载荷:与 JSON 值同构的代数数据类型。
///
/// 所有 H5 ↔ Native 之间流转的数据最终都序列化为此类型,方便:
/// - 类型化访问(`data["title"]?.asString` 比 `data["title"] as? String` 干净)
/// - Sendable 跨 actor 边界(避免 `[String: Any]` 这种 non-Sendable 类型扩散)
/// - 单测 fixture 构造(字面量语法 `["a": 1, "b": [true, nil]]` 等价 `.object([...])`
public enum BridgeData: Sendable {
case string(String)
case number(Double)
case bool(Bool)
case null
case array([BridgeData])
case object([String: BridgeData])
}
// MARK: - JSON 互转
extension BridgeData {
/// 从 `JSONSerialization` 输出(Any 树)构造,递归处理子节点。
/// 遇到未知类型返回 nil(不静默丢弃,便于上层检测异常 payload)。
public init?(jsonObject: Any) {
if jsonObject is NSNull { self = .null; return }
if let s = jsonObject as? String { self = .string(s); return }
if let n = jsonObject as? NSNumber {
// NSNumber 同时桥接 Bool/Int/Double。Bool 必须先用 CFTypeID 鉴别,
// 否则 NSNumber(value: true) 会被当 1.0 处理。
if CFGetTypeID(n) == CFBooleanGetTypeID() {
self = .bool(n.boolValue)
return
}
self = .number(n.doubleValue)
return
}
if let arr = jsonObject as? [Any] {
self = .array(arr.compactMap { BridgeData(jsonObject: $0) })
return
}
if let obj = jsonObject as? [String: Any] {
var out: [String: BridgeData] = [:]
out.reserveCapacity(obj.count)
for (k, v) in obj {
if let d = BridgeData(jsonObject: v) { out[k] = d }
}
self = .object(out)
return
}
return nil
}
/// 转为 `JSONSerialization` 可接受的 Any 树,供 evaluateJavaScript 拼 JSON 字符串。
public var jsonObject: Any {
switch self {
case .string(let s): return s
case .number(let d): return d
case .bool(let b): return b
case .null: return NSNull()
case .array(let arr): return arr.map { $0.jsonObject }
case .object(let d): return d.mapValues { $0.jsonObject }
}
}
}
// MARK: - 访问语法糖
// 注:项目默认 SWIFT_DEFAULT_ACTOR_ISOLATION = MainActor 让所有类型默认 main 隔离;
// BridgeData 是纯值类型、需要在 handler 闭包(@Sendable)内自由访问,
// 所有访问语法糖标 nonisolated 避免跨 actor 限制。
extension BridgeData {
nonisolated public var asString: String? {
if case .string(let s) = self { return s }
return nil
}
/// 宽松字符串:string 保持原样、number 自动转 String、bool 转 "true"/"false"。
///
/// 用于 H5 字段类型不严格的场景(如 mediaTypeAudio.user 实际可能传 number
/// 0/1/userId 或 string)。对齐 daoqi `[NSString stringWithFormat:@"%@",
/// data[@"user"]]` 的容忍行为—— ObjC `%@` 对 NSNumber 调用 description 自动
/// 转字符串。
nonisolated public var asLooseString: String? {
switch self {
case .string(let s): return s
case .number(let d):
// 整数样的 double 转 Int 字符串(如 0.0 → "0",避免无意义的 .0 尾巴)
if d == d.rounded(), d.isFinite, abs(d) < Double(Int.max) {
return String(Int(d))
}
return String(d)
case .bool(let b): return b ? "true" : "false"
case .null: return nil
case .array, .object: return nil
}
}
nonisolated public var asDouble: Double? {
if case .number(let d) = self { return d }
return nil
}
nonisolated public var asInt: Int? {
if case .number(let d) = self { return Int(d) }
return nil
}
nonisolated public var asBool: Bool? {
if case .bool(let b) = self { return b }
return nil
}
nonisolated public var asArray: [BridgeData]? {
if case .array(let a) = self { return a }
return nil
}
nonisolated public var asObject: [String: BridgeData]? {
if case .object(let o) = self { return o }
return nil
}
/// 对象字段访问:`data["title"]?.asString`
nonisolated public subscript(key: String) -> BridgeData? {
if case .object(let o) = self { return o[key] }
return nil
}
/// 数组索引访问:`data[0]?.asString`
nonisolated public subscript(index: Int) -> BridgeData? {
if case .array(let a) = self, a.indices.contains(index) { return a[index] }
return nil
}
}
// MARK: - 字面量构造
extension BridgeData: ExpressibleByStringLiteral {
public init(stringLiteral value: String) { self = .string(value) }
}
extension BridgeData: ExpressibleByIntegerLiteral {
public init(integerLiteral value: Int) { self = .number(Double(value)) }
}
extension BridgeData: ExpressibleByFloatLiteral {
public init(floatLiteral value: Double) { self = .number(value) }
}
extension BridgeData: ExpressibleByBooleanLiteral {
public init(booleanLiteral value: Bool) { self = .bool(value) }
}
extension BridgeData: ExpressibleByArrayLiteral {
public init(arrayLiteral elements: BridgeData...) { self = .array(elements) }
}
extension BridgeData: ExpressibleByDictionaryLiteral {
public init(dictionaryLiteral elements: (String, BridgeData)...) {
self = .object(Dictionary(uniqueKeysWithValues: elements))
}
}