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>
70 lines
2.4 KiB
Swift
70 lines
2.4 KiB
Swift
import SwiftUI
|
|
|
|
/// 首页(导购)。不设防(未登录可浏览)。进入时先用默认坐标出券,定位回来再刷新成附近的。
|
|
struct HomeView: View {
|
|
@State private var vm = HomeViewModel()
|
|
@State private var locationManager = LocationManager()
|
|
@Environment(\.openURL) private var openURL
|
|
|
|
var body: some View {
|
|
NavigationStack {
|
|
content
|
|
.navigationTitle("傻瓜比价")
|
|
}
|
|
.task {
|
|
await vm.loadFirstPage()
|
|
// 首屏已出券;定位回来若成功,刷新成附近的(失败则保持默认坐标结果)
|
|
if let coord = await locationManager.requestOnce() {
|
|
await vm.updateLocation(longitude: coord.longitude, latitude: coord.latitude)
|
|
}
|
|
}
|
|
}
|
|
|
|
@ViewBuilder
|
|
private var content: some View {
|
|
if vm.isLoading && vm.coupons.isEmpty {
|
|
ProgressView("加载中…")
|
|
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
|
} else if vm.coupons.isEmpty {
|
|
ContentUnavailableView {
|
|
Label("暂时没有优惠", systemImage: "ticket")
|
|
} description: {
|
|
Text(vm.errorMessage ?? "下拉刷新试试")
|
|
}
|
|
} else {
|
|
List {
|
|
ForEach(vm.coupons) { card in
|
|
CouponCardView(card: card) {
|
|
Task { await openCard(card) }
|
|
}
|
|
.listRowSeparator(.hidden)
|
|
}
|
|
|
|
if vm.hasNext {
|
|
HStack {
|
|
Spacer()
|
|
ProgressView()
|
|
Spacer()
|
|
}
|
|
.listRowSeparator(.hidden)
|
|
.onAppear { Task { await vm.loadNextPage() } }
|
|
}
|
|
}
|
|
.listStyle(.plain)
|
|
.refreshable { await vm.loadFirstPage() }
|
|
}
|
|
}
|
|
|
|
/// 点券:换推广链接 → deeplink 优先唤起美团 App,唤不起用 H5 兜底。
|
|
private func openCard(_ card: CouponCard) async {
|
|
let links = await vm.referralLinks(for: card)
|
|
if let deeplink = links.deeplink {
|
|
openURL(deeplink) { accepted in
|
|
if !accepted, let h5 = links.h5 { openURL(h5) }
|
|
}
|
|
} else if let h5 = links.h5 {
|
|
openURL(h5)
|
|
}
|
|
}
|
|
}
|