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
@@ -0,0 +1,42 @@
//
// AMapWrapper.swift
// ylgamehall
//
// 高德定位 SDK 启动注册。iOS 14+ 隐私合规必走 3 步:
// updatePrivacyShow → updatePrivacyAgree → apiKey
// 少一步定位接口直接拒绝(errorCode 7 KEY 校验失败)。
//
// APIKey 沿用 msext APIKey.h:14(与 Bundle ID com.skyapp.ylgamehall 绑死)。
//
// ⚠️ canImport(AMapFoundationKit) 守卫:在 Vendor/AMap/*.framework 尚未在 Xcode
// 添加到 Target Frameworks 之前,本文件编译为 no-op。
//
import Foundation
#if canImport(AMapFoundationKit)
import AMapFoundationKit
#endif
@MainActor
public enum AMapWrapper {
/// 高德 APIKey(沿用 msext APIKey.h:14,绑死 Bundle ID com.skyapp.ylgamehall)
public static let apiKey = "b0d4a8e3fcbbcc0dd96283b7df6a4494"
/// 启动注册:在 AppDelegate.didFinishLaunchingWithOptions 调用一次。
/// 必须按 iOS 14+ 隐私合规顺序:updatePrivacyShow → updatePrivacyAgree → apiKey
public static func bootstrap() {
#if canImport(AMapFoundationKit)
AMapLocationServices.updatePrivacyShow(.didShow, privacyInfo: .didContain)
AMapLocationServices.updatePrivacyAgree(.didAgree)
AMapServices.shared().apiKey = apiKey
#else
// SDK 未链接:no-op
#endif
}
}
#if canImport(AMapFoundationKit)
private typealias AMapLocationServices = AMapServices
#endif
@@ -0,0 +1,160 @@
//
// WeChatManager.swift
// ylgamehall
//
// 微信 SDK 持久 delegate + authorize + share 异步包装。
// 把 WXApiDelegate 的回调 → async/await,state UUID 配对多发起方。
//
// Design §8.5;msext WXApiManager.m 等价。
//
import Foundation
import UIKit
#if canImport(WechatOpenSDK)
import WechatOpenSDK
#endif
@MainActor
public final class WeChatManager: NSObject {
public static let shared = WeChatManager()
/// 授权 scope(沿用 msext SGDefineInfo.h:104)
public static let authScope = "snsapi_message,snsapi_userinfo,snsapi_friend,snsapi_contact"
/// 待 resolve 的 authorize continuation(state UUID 配对)
private var authPending: [String: CheckedContinuation<WXAuthCodePayload, Error>] = [:]
/// 待 resolve 的 share continuation(FIFO 串行 — 微信回包不带配对 ID)
private var sharePending: [CheckedContinuation<Void, Error>] = []
public override init() {
super.init()
}
// MARK: - Authorize
/// 拉起微信授权(msext 行为:state = "wechat_sdk" + scope = 4 项)
/// 返回的 code 可用 WeChatAuth.exchangeForUser 拿 7 字段用户信息。
public func authorize() async throws -> WXAuthCodePayload {
#if canImport(WechatOpenSDK)
let state = UUID().uuidString
return try await withCheckedThrowingContinuation { cont in
authPending[state] = cont
let req = SendAuthReq()
req.scope = Self.authScope
req.state = state
WXApi.send(req)
}
#else
throw WeChatError.sdkNotLinked
#endif
}
// MARK: - Share
public enum ShareScene {
case session // 微信好友
case timeline // 朋友圈
}
public struct ShareLink: Sendable {
public let url: String
public let title: String
public let desc: String
public let thumbnailURL: String?
public init(url: String, title: String, desc: String, thumbnailURL: String?) {
self.url = url; self.title = title; self.desc = desc; self.thumbnailURL = thumbnailURL
}
}
/// 链接分享(FIFO 串行)。微信回包不携带配对 ID,连续调时按入队顺序消费。
public func shareLink(_ link: ShareLink, scene: ShareScene) async throws {
#if canImport(WechatOpenSDK)
try await withCheckedThrowingContinuation { (cont: CheckedContinuation<Void, Error>) in
sharePending.append(cont)
let media = WXWebpageObject()
media.webpageUrl = link.url
let message = WXMediaMessage()
message.title = link.title
message.description = link.desc
message.mediaObject = media
let req = SendMessageToWXReq()
req.bText = false
req.message = message
switch scene {
case .session: req.scene = Int32(WXSceneSession.rawValue)
case .timeline: req.scene = Int32(WXSceneTimeline.rawValue)
}
WXApi.send(req)
}
#else
throw WeChatError.sdkNotLinked
#endif
}
// MARK: - Internal: resolve callbacks
fileprivate func resolveAuth(state: String, code: String?, errCode: Int) {
guard let cont = authPending.removeValue(forKey: state) else { return }
if errCode == 0, let code {
cont.resume(returning: WXAuthCodePayload(code: code, state: state))
} else {
cont.resume(throwing: WeChatError.authFailed(errCode))
}
}
fileprivate func resolveShare(errCode: Int) {
guard !sharePending.isEmpty else { return }
let cont = sharePending.removeFirst()
if errCode == 0 {
cont.resume(returning: ())
} else {
cont.resume(throwing: WeChatError.shareFailed(errCode))
}
}
}
// MARK: - WXApiDelegate
#if canImport(WechatOpenSDK)
extension WeChatManager: WXApiDelegate {
public nonisolated func onReq(_ req: BaseReq) {
// 我们当前只发起请求,不接受外部 req(微信 → 本 App 的 req 用于公众号回复等场景)
}
public nonisolated func onResp(_ resp: BaseResp) {
// 主线程化:BaseResp 由 SDK 在主线程派发,但保留 MainActor.run 兜底
let errCode = Int(resp.errCode)
if let auth = resp as? SendAuthResp {
let code = auth.code
let state = auth.state ?? ""
Task { @MainActor in
Self.shared.resolveAuth(state: state, code: code, errCode: errCode)
}
} else if resp is SendMessageToWXResp {
Task { @MainActor in
Self.shared.resolveShare(errCode: errCode)
}
}
}
}
#endif
// MARK: - Public payload / error types(无 SDK 依赖,可在 canImport 之外使用)
public struct WXAuthCodePayload: Sendable {
public let code: String
public let state: String
}
public enum WeChatError: Error, Sendable {
case sdkNotLinked
case authFailed(Int)
case shareFailed(Int)
}
@@ -0,0 +1,43 @@
//
// WeChatSDK.swift
// ylgamehall
//
// 微信 OpenSDK 启动注册。沿用 msext AppID(参 docs/SDK-Integration-Guide.md §0 凭证总表)。
//
// ⚠️ canImport(WechatOpenSDK) 守卫:在 Vendor/WechatSDK/WechatOpenSDK.xcframework
// 尚未在 Xcode 中 "Frameworks, Libraries, and Embedded Content" 里 Embed & Sign 之前,
// 本文件编译为 no-op,不影响主工程编译。Embed 后自动激活真实路径。
//
import Foundation
#if canImport(WechatOpenSDK)
import WechatOpenSDK
#endif
@MainActor
public enum WeChatSDK {
/// 微信 AppID(沿用 msext SGDefineInfo.h:105 kAuthOpenID)
public static let appID = "wx586a9b321e56efb7"
/// 启动注册:在 AppDelegate.didFinishLaunchingWithOptions 调用一次。
/// universalLink 沿用 msext 路径不走 ULAPI,传空字符串即可(与 msext 行为等价)。
public static func register() {
#if canImport(WechatOpenSDK)
// 不走 Universal Links(msext 同款路径,企业签 / 超签分发场景 ULAPI 无意义)
WXApi.registerApp(appID, universalLink: "")
#else
// SDK 未链接:no-op。Xcode 加完 framework 后这条分支会被替换为上面 WXApi 调用。
#endif
}
/// SceneDelegate.openURLContexts 转发入口(QQ → WXApi 顺序,QQ 必须先判 — msext 硬约束)
public static func handleOpenURL(_ url: URL) -> Bool {
#if canImport(WechatOpenSDK)
return WXApi.handleOpen(url, delegate: WeChatManager.shared)
#else
return false
#endif
}
}