选型(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>
106 lines
3.6 KiB
Swift
106 lines
3.6 KiB
Swift
//
|
||
// LocationService.swift
|
||
// ylgamehall
|
||
//
|
||
// 高德 AMapLocationManager 异步包装:一次性 / 持续两种模式。
|
||
// 契约 §3.2 [6] getlocationinfo 9 字段;msext gameController.m:2491-2557 等价。
|
||
//
|
||
// ⚠️ canImport(AMapLocationKit) 守卫:SDK 未链接时 LocationService 永远抛
|
||
// LocationError.sdkNotLinked,handler 兜底退化为 stub 行为。
|
||
//
|
||
|
||
import Foundation
|
||
import CoreLocation
|
||
|
||
#if canImport(AMapLocationKit)
|
||
import AMapLocationKit
|
||
#endif
|
||
#if canImport(AMapFoundationKit)
|
||
import AMapFoundationKit
|
||
#endif
|
||
|
||
/// 9 字段定位结果。字段名严格 1:1 H5 端契约(contract §3.2 表 [6]):
|
||
/// - latitude / longitude **是 string** (`String(format: "%f", ...)`)
|
||
/// - province **小写 p**(与 sharelogin.Province 大写不同)
|
||
public struct LocationPayload: Sendable {
|
||
public let address: String
|
||
public let city: String
|
||
public let cityCode: String
|
||
public let country: String
|
||
public let district: String
|
||
public let latitude: String
|
||
public let longitude: String
|
||
public let province: String
|
||
public let street: String
|
||
}
|
||
|
||
public enum LocationError: Error, Sendable {
|
||
case sdkNotLinked
|
||
case authorizationDenied
|
||
case timeout
|
||
case underlying(any Error)
|
||
}
|
||
|
||
@MainActor
|
||
public final class LocationService {
|
||
|
||
public static let shared = LocationService()
|
||
|
||
public init() {}
|
||
|
||
#if canImport(AMapLocationKit)
|
||
private let manager: AMapLocationManager = {
|
||
let m = AMapLocationManager()
|
||
m.desiredAccuracy = kCLLocationAccuracyHundredMeters
|
||
m.locationTimeout = 6
|
||
m.reGeocodeTimeout = 3
|
||
m.locatingWithReGeocode = true
|
||
return m
|
||
}()
|
||
#endif
|
||
|
||
/// 一次性定位 + 逆地理(msext locAction + completionBlock 等价)
|
||
public func requestOnce() async throws -> LocationPayload {
|
||
#if canImport(AMapLocationKit)
|
||
return try await withCheckedThrowingContinuation { (cont: CheckedContinuation<LocationPayload, Error>) in
|
||
manager.requestLocation(withReGeocode: true) { loc, regeo, err in
|
||
if let err = err as NSError? {
|
||
// msext 行为:权限相关错误归一到 12 errorCode(handler 转字段)
|
||
if err.code == AMapLocationErrorCode.locateFailed.rawValue {
|
||
cont.resume(throwing: LocationError.authorizationDenied)
|
||
} else {
|
||
cont.resume(throwing: LocationError.underlying(err))
|
||
}
|
||
return
|
||
}
|
||
guard let loc, let regeo else {
|
||
cont.resume(throwing: LocationError.timeout)
|
||
return
|
||
}
|
||
cont.resume(returning: LocationPayload(
|
||
address: regeo.formattedAddress ?? "",
|
||
city: regeo.city ?? "",
|
||
cityCode: regeo.citycode ?? "",
|
||
country: regeo.country ?? "",
|
||
district: regeo.district ?? "",
|
||
latitude: String(format: "%f", loc.coordinate.latitude),
|
||
longitude: String(format: "%f", loc.coordinate.longitude),
|
||
province: regeo.province ?? "",
|
||
street: regeo.street ?? ""
|
||
))
|
||
}
|
||
}
|
||
#else
|
||
throw LocationError.sdkNotLinked
|
||
#endif
|
||
}
|
||
|
||
/// 停止:msext gameController.m:2473-2478 cleanUpAction 等价
|
||
public func stop() {
|
||
#if canImport(AMapLocationKit)
|
||
manager.stopUpdatingLocation()
|
||
manager.delegate = nil
|
||
#endif
|
||
}
|
||
}
|