98b7600797
对标安卓正式版、对接 app-server(8770)的 SwiftUI 客户端。iOS 无障碍受限、无比价核心,做自成一体:美团 CPS 导购 + 福利(签到/任务/金币)+ 查看(比价记录/省钱战绩)。 功能完整 M0-M5:脚手架、短信登录(JWT+Keychain+401 自动刷新)、首页券 feed 导购、比价记录+省钱战绩、福利、我的(资料/反馈/设置/注销)。 单 target、0 三方依赖、XcodeGen 生成工程;Debug/Release 双 plist 分流 ATS。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
71 lines
2.4 KiB
Swift
71 lines
2.4 KiB
Swift
import SwiftUI
|
|
|
|
/// 首页(导购)逻辑,对齐安卓 HomeViewModel:feed 分页 + 定位更新 + 换推广链接。
|
|
@MainActor
|
|
@Observable
|
|
final class HomeViewModel {
|
|
private(set) var coupons: [CouponCard] = []
|
|
private(set) var isLoading = false
|
|
private(set) var isLoadingMore = false
|
|
private(set) var hasNext = true
|
|
private(set) var errorMessage: String?
|
|
|
|
// 默认天安门,定位失败兜底(对齐安卓)
|
|
private var longitude = 116.404
|
|
private var latitude = 39.928
|
|
private var page = 1
|
|
private let service = MeituanService()
|
|
|
|
func loadFirstPage() async {
|
|
guard !isLoading else { return }
|
|
isLoading = true
|
|
errorMessage = nil
|
|
defer { isLoading = false }
|
|
do {
|
|
let resp = try await service.feed(longitude: longitude, latitude: latitude, page: 1)
|
|
coupons = resp.items
|
|
page = resp.page
|
|
hasNext = resp.hasNext
|
|
} catch {
|
|
errorMessage = (error as? LocalizedError)?.errorDescription ?? "加载失败,请下拉重试"
|
|
}
|
|
}
|
|
|
|
func loadNextPage() async {
|
|
guard hasNext, !isLoading, !isLoadingMore else { return }
|
|
isLoadingMore = true
|
|
defer { isLoadingMore = false }
|
|
do {
|
|
let resp = try await service.feed(longitude: longitude, latitude: latitude, page: page + 1)
|
|
coupons += resp.items
|
|
page = resp.page
|
|
hasNext = resp.hasNext
|
|
} catch {
|
|
// 加载更多失败静默,不打断已有列表
|
|
}
|
|
}
|
|
|
|
/// 拿到真实定位后刷新成附近的券。
|
|
func updateLocation(longitude: Double, latitude: Double) async {
|
|
self.longitude = longitude
|
|
self.latitude = latitude
|
|
await loadFirstPage()
|
|
}
|
|
|
|
/// 点券 → 换推广链接 → 返回(deeplink 优先唤起美团 App,H5 兜底)。
|
|
func referralLinks(for card: CouponCard) async -> (deeplink: URL?, h5: URL?) {
|
|
do {
|
|
let resp = try await service.referralLink(
|
|
productViewSign: card.productViewSign,
|
|
platform: card.platform,
|
|
bizLine: card.bizLine
|
|
)
|
|
let deeplink = resp.linkMap["3"].flatMap { URL(string: $0) }
|
|
let h5raw = resp.linkMap["1"] ?? resp.link
|
|
return (deeplink, URL(string: h5raw))
|
|
} catch {
|
|
return (nil, nil)
|
|
}
|
|
}
|
|
}
|