feat(drone-dock): 新增独立套餐收款页并接入收款接口
This commit is contained in:
@@ -0,0 +1,164 @@
|
||||
import Foundation
|
||||
|
||||
/// 独立机巢套餐收款状态;不使用普通景区收款链接,也不模拟单笔支付成功。
|
||||
final class DroneDockPaymentViewModel {
|
||||
/// 当前页面所属账号、景区和门店。
|
||||
private(set) var context: DroneDockHomeContext
|
||||
/// 服务端当前可售套餐。
|
||||
private(set) var projects: [DroneDockProject] = []
|
||||
/// 当前已确认报价的付款链接;改价、换套餐或失败时立即清除。
|
||||
private(set) var payCode: DroneDockPayCode?
|
||||
/// 是否正在加载套餐列表。
|
||||
private(set) var isLoadingProjects = false
|
||||
/// 是否正在向后端生成付款链接。
|
||||
private(set) var isGenerating = false
|
||||
/// 套餐读取错误,与成功的空列表分开表达。
|
||||
private(set) var projectError: String?
|
||||
/// 报价生成错误,不携带可继续使用的旧二维码。
|
||||
private(set) var payCodeError: String?
|
||||
|
||||
private let service: any DroneDockPaymentServing
|
||||
private let currentContext: @MainActor @Sendable () -> DroneDockHomeContext
|
||||
private var scopeGeneration = 0
|
||||
private var projectGeneration = 0
|
||||
private var codeGeneration = 0
|
||||
private var pendingCode: PendingCode?
|
||||
|
||||
/// 相同在途报价用于拦截重复点击;不同套餐或金额允许替换前一次请求。
|
||||
private struct PendingCode: Equatable {
|
||||
let projectID: Int
|
||||
let price: String?
|
||||
}
|
||||
|
||||
/// 注入真实收款服务与最新会话;调用方在完成后主动刷新 UIKit 界面。
|
||||
init(context: DroneDockHomeContext, service: any DroneDockPaymentServing,
|
||||
currentContext: @escaping @MainActor @Sendable () -> DroneDockHomeContext) {
|
||||
self.context = context
|
||||
self.service = service
|
||||
self.currentContext = currentContext
|
||||
}
|
||||
|
||||
/// 加载可售套餐;切换登录、景区或门店时清除之前的套餐和付款链接。
|
||||
func loadProjects() async {
|
||||
let scope = scopeGeneration
|
||||
let requested = await currentContext()
|
||||
guard scope == scopeGeneration, !Task.isCancelled else { return }
|
||||
if !context.hasSameIdentity(as: requested) {
|
||||
invalidate()
|
||||
context = requested
|
||||
}
|
||||
guard !isLoadingProjects else { return }
|
||||
projectGeneration &+= 1
|
||||
let request = projectGeneration
|
||||
isLoadingProjects = true
|
||||
projectError = nil
|
||||
defer { if projectGeneration == request { isLoadingProjects = false } }
|
||||
guard requested.canLoad else {
|
||||
projects = []
|
||||
payCode = nil
|
||||
projectError = "请先登录并选择景区和门店"
|
||||
return
|
||||
}
|
||||
do {
|
||||
let result = try await service.projects(scenicID: requested.scenicID)
|
||||
guard await accepts(request, isCode: false, context: requested) else { return }
|
||||
guard Set(result.map(\.id)).count == result.count else {
|
||||
projects = []
|
||||
projectError = "接口返回的套餐重复,请重试"
|
||||
return
|
||||
}
|
||||
projects = result
|
||||
} catch {
|
||||
guard await accepts(request, isCode: false, context: requested) else { return }
|
||||
projects = []
|
||||
if error is CancellationError { return }
|
||||
projectError = error.localizedDescription
|
||||
}
|
||||
}
|
||||
|
||||
/// 确认套餐或改价后重新生成;只接受所选套餐、当前门店及当前会话的响应。
|
||||
func generateCode(projectID: Int, price: String? = nil) async {
|
||||
let normalized = price.flatMap(DroneDockPriceValidator.normalizedPrice)
|
||||
let pending = PendingCode(projectID: projectID, price: normalized)
|
||||
if isGenerating, pendingCode == pending, price == nil || normalized != nil { return }
|
||||
codeGeneration &+= 1
|
||||
let request = codeGeneration
|
||||
payCode = nil
|
||||
payCodeError = nil
|
||||
pendingCode = pending
|
||||
isGenerating = true
|
||||
defer {
|
||||
if codeGeneration == request {
|
||||
isGenerating = false
|
||||
pendingCode = nil
|
||||
}
|
||||
}
|
||||
guard price == nil || normalized != nil else {
|
||||
payCodeError = DroneDockPriceValidator.validationMessage(for: price ?? "")
|
||||
return
|
||||
}
|
||||
let requested = await currentContext()
|
||||
guard codeGeneration == request, !Task.isCancelled else { return }
|
||||
guard context.hasSameIdentity(as: requested) else {
|
||||
rejectChangedContext(requested)
|
||||
return
|
||||
}
|
||||
guard requested.canLoad else {
|
||||
payCodeError = "请先登录并选择景区和门店"
|
||||
return
|
||||
}
|
||||
guard projectID > 0, projects.contains(where: { $0.id == projectID }) else {
|
||||
payCodeError = "请先选择当前可售的航拍套餐"
|
||||
return
|
||||
}
|
||||
do {
|
||||
let result = try await service.payCode(scenicID: requested.scenicID, projectID: projectID, price: normalized)
|
||||
guard await accepts(request, isCode: true, context: requested) else { return }
|
||||
guard result.projectID == projectID, result.storeID == requested.storeID else {
|
||||
payCodeError = "返回的套餐或收款门店与当前选择不一致,请重新生成"
|
||||
return
|
||||
}
|
||||
if let normalized, result.price != Decimal(string: normalized, locale: Locale(identifier: "en_US_POSIX")) {
|
||||
payCodeError = "返回的收款金额与本次报价不一致,请重新生成"
|
||||
return
|
||||
}
|
||||
payCode = result
|
||||
} catch {
|
||||
guard await accepts(request, isCode: true, context: requested) else { return }
|
||||
if error is CancellationError { return }
|
||||
payCodeError = error.localizedDescription
|
||||
}
|
||||
}
|
||||
|
||||
/// 页面退出或会话失效时清除全部状态,并阻止任何在途请求回填旧二维码。
|
||||
func invalidate() {
|
||||
scopeGeneration &+= 1
|
||||
projectGeneration &+= 1
|
||||
codeGeneration &+= 1
|
||||
projects = []
|
||||
payCode = nil
|
||||
isLoadingProjects = false
|
||||
isGenerating = false
|
||||
projectError = nil
|
||||
payCodeError = nil
|
||||
pendingCode = nil
|
||||
}
|
||||
|
||||
private func accepts(_ request: Int, isCode: Bool, context requested: DroneDockHomeContext) async -> Bool {
|
||||
guard request == (isCode ? codeGeneration : projectGeneration), !Task.isCancelled else { return false }
|
||||
let latest = await currentContext()
|
||||
guard request == (isCode ? codeGeneration : projectGeneration), !Task.isCancelled else { return false }
|
||||
guard requested.hasSameIdentity(as: latest) else {
|
||||
rejectChangedContext(latest)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
private func rejectChangedContext(_ latest: DroneDockHomeContext) {
|
||||
invalidate()
|
||||
context = latest
|
||||
projectError = "账号、景区或门店已变化,请重新加载套餐"
|
||||
payCodeError = "账号、景区或门店已变化,请重新选择套餐"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user