Phase 4.E + Phase 5 + Phase 3.C 七牛上传:代码侧 SDK 接入(SPM 优先 / Vendor 兜底)

选型(CLAUDE.md ADR-006「SPM 优先 + Vendor xcframework 兜底」):
- 微信 → SPM(Tencent 官方 https://github.com/Tencent/WechatOpenSDK-XCFramework);
  项目方下载的 WechatOpenSDK-NoPay.xcframework 已撤出 Vendor,docs/res 内副本作离线备份
- 高德 → Vendor 手动(官方未提供 SPM;Vendor/AMap/{AMapFoundationKit,AMapLocationKit}.framework
  随仓库分发,fat .framework 同时含 x86_64 + arm64,Do Not Embed)
- 七牛 → SPM(https://github.com/qiniu/objc-sdk v8.9.x)

代码侧(全部 #if canImport 守卫,Xcode 加 SDK 前编译为 no-op):
- Source/SDK/WeChat/WeChatSDK.swift:WXApi.registerApp + handleOpenURL(沿用 msext AppID
  wx586a9b321e56efb7,universalLink 空字符串走非 ULAPI 路径)
- Source/SDK/WeChat/WeChatManager.swift:WXApiDelegate + state UUID 配对的 authorize +
  FIFO 串行 share async/await wrapper
- Source/Login/WeChatAuth.swift:客户端直拼 sns/oauth2/access_token + sns/userinfo →
  7 字段(Province 大写 P / city 经 danbian 去单引号),沿用 msext 同款路径
- Source/SDK/AMap/AMapWrapper.swift:iOS 14+ 隐私合规 3 步 + apiKey 注入
- Source/Location/LocationService.swift:actor + AMapLocationManager 异步包装 + 9 字段
- Source/Network/QiniuConfig.swift:4 项常量沿用 msext(AccessKey/SecretKey/Bucket/Domain)
- Source/Network/QiniuTokenSigner.swift:纯 Swift CryptoKit HMAC-SHA1 + Base64URL 自签
  token(与 msext QiniuManager.m:200-230 等价,不依赖 Qiniu SDK 工作)
- Source/Network/QiniuUploader.swift:actor 包 QNUploadManager async/await

Handler 升级:
- AccreditLoginHandler:拉起授权 → 反向 callback sharelogin 7 字段
- WechatShare:真实链接分享(type=2/3 截图待 Phase 4.F)
- StartLocationHandler:真实定位 → 9 字段(latitude/longitude string, province 小写 p)

生命周期:
- AppDelegate.didFinishLaunchingWithOptions:WeChatSDK.register + AMapWrapper.bootstrap
- SceneDelegate.openURLContexts:WeChatSDK.handleOpenURL 接入回调
- BackGameDataHandler:子游戏 backgameData pop 时 WXApi.delegate=nil + LocationService.stop()

Info.plist:CFBundleURLTypes 加 wx586a9b321e56efb7;LSApplicationQueriesSchemes 追加
weixin/weixinULAPI/weixinURLParamsAPI;NSLocationWhenInUseUsageDescription +
NSMicrophoneUsageDescription(gamehallname 中文文案)。

.gitignore:排除 docs/res/{AMap_iOS_Loc_ALL,objc-sdk-8.9.2}/(235MB 项目方下载副本,
已走 SPM/Vendor 接入不需要副本)。

文档:docs/SDK-Integration-Guide.md 重写 §A 用户手动 Xcode UI 三步(SPM × 2 +
Add Files × 2);Vendor/{AMap,WechatSDK}/README.md 记录各自接入路径选型。

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
joywayer
2026-06-22 22:22:23 +08:00
co-authored by Claude Opus 4.7
parent a2baf742b6
commit 1d7e258130
47 changed files with 3226 additions and 273 deletions
@@ -2,18 +2,17 @@
// AccreditLoginHandler.swift
// ylgamehall
//
// H5 → Native handler stub:accreditlogin — 微信授权登录
// H5 → Native handler:accreditlogin — 微信授权登录
// 契约:docs/H5-Native-Contract.md §3.1 [1] + §3.2 [10]sharelogin
//
// Phase 4.A 阶段为 stub(仅 cb 维持契约);Phase 4.B 等微信 SDK + AppID +
// Universal Links + 后台 /wechat/login 接口(或客户端 fallback)到位后升级。
// 完整链路(Phase 4.E):
// 1. WeChatAuth.authorize() → 拉起微信授权 + 客户端直拼 oauth2 + userinfo
// (msext NewRootVC.m:2415-2428 等价路径,沿用 AppID/AppSecret/scope)
// 2. bridge.call("sharelogin", data: 7 字段) 反向 callback
// 字段名严格 1:1 含历史包袱:`Province` 大写 P / `city` 经 danbian 去单引号
//
// 完整实现(Phase 4.B):
// 1. WeChatManager.shared.authorize() → 拿 WXAuthCode(state UUID 配对)
// 2. WeChatAuth.exchangeForUser(code:) → 后台 /wechat/login 或客户端直拼
// sns/oauth2/access_token + sns/userinfo
// 3. bridge.call("sharelogin", data: 7 字段) 反向 callback
// 字段名严格 1:1 含历史包袱:Province 大写 P、city 经 danbian: 去单引号
// ⚠️ canImport(WechatOpenSDK) 守卫:SDK 未链接前走 stub(与 Phase 4.A 一致),
// 仅响应 callback 不触发 sharelogin;项目方在 Xcode Embed & Sign 后真实路径激活。
//
import Foundation
@@ -21,11 +20,32 @@ import Foundation
public enum AccreditLoginHandler {
public static func register(on bridge: any BridgeProtocol) {
// 【1】accreditlogin
// 入参忽略;cb: "Response from accreditlogin"
// 完整实现需触发 sharelogin 反向 callback(Phase 4.B)
bridge.register("accreditlogin") { _, callback in
callback?(.string("Response from accreditlogin"))
#if canImport(WechatOpenSDK)
// SDK 已链接:拉起微信授权 → 拿 7 字段 → 反向 callback sharelogin
Task { @MainActor in
do {
let user = try await WeChatAuth.authorize()
bridge.call("sharelogin", data: .object([
// 字段名严格 1:1 contract §3.2 表 [10] / msext NewRootVC.m:2428
"openid": .string(user.openid),
"headimgurl": .string(user.headimgurl),
"nickname": .string(user.nickname),
"sex": .number(Double(user.sex)),
"city": .string(user.city),
"Province": .string(user.Province), // ← 大写 P
"unionid": .string(user.unionid)
]), callback: nil)
} catch WeChatAuthError.userCancelled {
// msext 行为:用户取消不弹 alert、不触发 sharelogin
} catch {
// 其它失败也吞下(msext 同款乐观策略:不反向通知 H5)
// 后续 Phase 9 监控接入后此处可发 Sentry breadcrumb
}
}
#endif
}
}
}
@@ -17,29 +17,24 @@
// msext 同款行为 — 这是契约边界,不在原生侧兜底。
//
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// ⚠️ 接入新 SDK 时必须回到此处补对应清理钩子(msext 在 backgameData / cleanUpAction
// 里逐项清理;我们当前是 stub,所以暂无需要清的状态。一旦真实接入下列任一 SDK,
// 在 MainActor.run 块里加对应清理一行):
// SDK 清理钩子状态(每接入新 SDK 都必须确认或新增):
//
// - Phase 4.E 微信 SDK:`WXApi.delegate = nil`
// (msext gameController.m:699;防止 pop 后微信回调指向已销毁的 SubGameVC)
//
// - Phase 5 高德定位:`LocationService.shared.stop()` + delegate nil
// (msext gameController.m:2473-2478 cleanUpAction;防止后台定位继续耗电 +
// 回调指向悬空对象)
//
// - Phase 3.B/C/D 录音 / 七牛上传:`Recorder.shared.cancel()` +
// `Uploader.shared.cancelInFlight()`
// (msext 通过 ChatVoiceRecorderVC 生命周期间接清理;我们的实现不会跟 VC 绑死,
// 必须显式 cancel 否则上传任务在 pop 后继续写网络)
//
// - Phase 8 Agora(若未来重启视频):`agoraKit.leaveChannel(...)`
// (目前 VideoRoomHandlers 是 stub 无需清理)
// ✅ Phase 4.E 微信:`WXApi.delegate = nil`(已加,canImport 守卫,Xcode Embed 后激活)
// ✅ Phase 5 高德定位:`LocationService.shared.stop()`(已加,canImport 守卫,
// Xcode Embed AMapLocationKit 后激活)
// ⏳ Phase 3.B/C/D 录音 / 七牛上传:`AudioRecorder.shared.cancel()` 待 opencore-amr
// 接入后取消注释;`QiniuUploader.cancelInFlight()` 依赖 Task.cancel 传播无需显式调
// ⏳ Phase 8 Agora(若未来重启视频):`agoraKit.leaveChannel(...)`
// (目前 VideoRoomHandlers 是 stub 无需清理)
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
//
import Foundation
#if canImport(WechatOpenSDK)
import WechatOpenSDK
#endif
public enum BackGameDataHandler {
public static func register(on bridge: any BridgeProtocol) {
@@ -52,7 +47,25 @@ public enum BackGameDataHandler {
// - 直接是 .string → 取字面
// - 其它类型 → JSON 序列化(保留对象结构传给大厅 H5)
let payload = serialize(data)
// 子游戏 push 期间挂载的 SDK 资源在 pop 前显式清理(避免 delegate 悬挂 /
// 后台任务残留),与 msext gameController.m:691-709 + cleanUpAction 等价。
AudioPlayer.shared.stopAllBackground()
#if canImport(WechatOpenSDK)
// msext gameController.m:699 等价:清 WXApi delegate 防止子游戏回调指向已销毁 VC
WXApi.delegate = nil
#endif
#if canImport(AMapLocationKit)
// msext gameController.m:2473-2478 cleanUpAction 等价
LocationService.shared.stop()
#endif
// Phase 3.C 录音 / Phase 3.B 七牛上传 清理(QiniuUploader.cancelInFlight 当前
// 依赖 Task.cancel 传播;AudioRecorder 待 opencore-amr 接入后激活清理)
// AudioRecorder.shared.cancel() // ← Phase 3.C 接入时取消注释
AppCoordinator.shared.popSubGame(returningData: payload)
}
callback?(.string("backgameData"))
@@ -2,15 +2,18 @@
// StartLocationHandler.swift
// ylgamehall
//
// H5 → Native handler:startlocation — 定位(Phase 5 完整实现,本期为 stub)
// H5 → Native handler:startlocation — 定位(Phase 5 接入高德 SDK 后激活真实路径)
// 契约:docs/H5-Native-Contract.md §3.1 [20] + §3.2 [6]
//
// Stub 行为:
// - 接收 startlocation 调用,cb "startlocation"(响应不为 undefined,H5 不卡)
// - **不触发** getlocationinfo 反向 callback(暂无高德 SDK,做不到真实定位)
// - H5 业务侧若强依赖 getlocationinfo,需等 Phase 5 完整接入 AMapLocationKit
// 完整链路:
// 1. LocationService.shared.requestOnce()(actor,包 AMapLocationManager 异步)
// 2. bridge.call("getlocationinfo", 9 字段) 反向 callback
// - latitude / longitude 是 **string**(`String(format: "%f")`)
// - province 小写 p(与 sharelogin.Province 大写不同)
// 3. 失败 → getlocationinfo({errorCode:12, errorMsg:"缺少定位权限"})
//
// 按 CLAUDE.md 原则 A 第一准则:H5 调啥就得有人接,即使没真实业务也要注册。
// ⚠️ canImport(AMapLocationKit) 守卫:SDK 未链接时 LocationService 抛 sdkNotLinked,
// 此处兜底走 stub(与 Phase 2 一致),仅响应 callback 不触发反向 getlocationinfo。
//
import Foundation
@@ -19,9 +22,41 @@ public enum StartLocationHandler {
public static func register(on bridge: any BridgeProtocol) {
bridge.register("startlocation") { _, callback in
// Phase 2 stub:仅响应 callback,不真实定位
// Phase 5 接入 LocationKit + 高德 AMapLocationKit.xcframework 时升级为完整实现
// 与 msext 行为一致:先回 cb 字面,不等定位完成
callback?(.string("startlocation"))
#if canImport(AMapLocationKit)
// SDK 已链接:拉起一次性定位 → 反向 callback 9 字段
// (持续定位 data == 1 暂未实现,业务实际只调一次性 — Phase 5.4 再扩展)
Task { @MainActor in
do {
let payload = try await LocationService.shared.requestOnce()
bridge.call("getlocationinfo", data: .object([
"address": .string(payload.address),
"city": .string(payload.city),
"cityCode": .string(payload.cityCode),
"country": .string(payload.country),
"district": .string(payload.district),
"latitude": .string(payload.latitude), // ← string
"longitude": .string(payload.longitude), // ← string
"province": .string(payload.province), // ← 小写 p
"street": .string(payload.street)
]), callback: nil)
} catch LocationError.authorizationDenied {
// 契约失败回包(msext gameController.m:2507 等价)
bridge.call("getlocationinfo", data: .object([
"errorCode": .number(12),
"errorMsg": .string("缺少定位权限")
]), callback: nil)
} catch {
// 其它失败也归一到 12(msext 行为)
bridge.call("getlocationinfo", data: .object([
"errorCode": .number(12),
"errorMsg": .string("缺少定位权限")
]), callback: nil)
}
}
#endif
}
}
}