feat(drone-dock): 新增独立套餐收款页并接入收款接口

This commit is contained in:
2026-10-27 16:15:04 +08:00
parent 40060cd883
commit 570b951280
15 changed files with 3052 additions and 8 deletions
@@ -0,0 +1,282 @@
import Foundation
/// 张工提供的当前景区可售航拍套餐;价格与次数来自服务器配置。
struct DroneDockProject: Decodable, Hashable, Sendable {
/// 套餐稳定 ID。
let id: Int
/// 套餐名称。
let name: String
/// 当前默认售价,单位元。
let price: Decimal
/// 整个套餐共享的拍摄次数。
let shootCount: Int
/// 套餐接口字段。
private enum CodingKeys: String, CodingKey {
case id, name, price
case shootCount = "shoot_count"
}
/// 严格读取可售套餐,不为缺失价格或次数生成默认值。
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
id = try container.paymentInteger(.id, minimum: 1)
name = try container.decode(String.self, forKey: .name)
price = try container.paymentMoney(.price)
shootCount = try container.paymentInteger(.shootCount, minimum: 1)
guard DroneDockPriceValidator.isValid(price) else {
throw DecodingError.dataCorruptedError(forKey: .price, in: container, debugDescription: "套餐价格超出报价范围")
}
}
}
/// 后端生成的可重复购买付款链接;不代表单笔交易或到账结果。
struct DroneDockPayCode: Decodable, Equatable, Sendable {
/// 报价所属套餐 ID。
let projectID: Int
/// 后端确认的套餐名称。
let projectName: String
/// 后端确认的成交报价,单位元。
let price: Decimal
/// 报价包含的拍摄次数。
let shootCount: Int
/// 收款归属的店铺工作人员 ID。
let storeUserID: Int
/// 收款归属的店铺 ID。
let storeID: Int
/// 用于绘制普通二维码的 HTTP(S) 付款链接。
let payURL: String
/// 付款链接接口字段。
private enum CodingKeys: String, CodingKey {
case price
case projectID = "project_id", projectName = "project_name", shootCount = "shoot_count"
case storeUserID = "store_user_id", storeID = "store_id", payURL = "pay_url"
}
/// 验证身份、报价和链接格式;不接受缺失或不可扫码的付款地址。
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
projectID = try container.paymentInteger(.projectID, minimum: 1)
projectName = try container.decode(String.self, forKey: .projectName)
price = try container.paymentMoney(.price)
shootCount = try container.paymentInteger(.shootCount, minimum: 1)
storeUserID = try container.paymentInteger(.storeUserID, minimum: 1)
storeID = try container.paymentInteger(.storeID, minimum: 1)
payURL = try container.decode(String.self, forKey: .payURL)
guard DroneDockPriceValidator.isValid(price) else {
throw DecodingError.dataCorruptedError(forKey: .price, in: container, debugDescription: "付款报价超出范围")
}
guard payURL.rangeOfCharacter(from: .whitespacesAndNewlines.union(.controlCharacters)) == nil,
let components = URLComponents(string: payURL),
let scheme = components.scheme?.lowercased(), ["https", "http"].contains(scheme),
let host = components.host, !host.isEmpty, components.url != nil,
components.user == nil, components.password == nil else {
throw DecodingError.dataCorruptedError(forKey: .payURL, in: container, debugDescription: "付款链接必须是有效 HTTP(S) 地址")
}
}
}
/// 收款记录分页与筛选范围汇总,不能据此推断某次二维码的交易状态。
struct DroneDockReceiptsResponse: Decodable, Equatable, Sendable {
/// Laravel 格式的收款记录分页。
let records: DroneDockReceiptPage
/// 整个查询范围的收款汇总,与当前分页无关。
let summary: DroneDockReceiptSummary
}
/// 后端 records 分页对象;列表字段实际为 data。
struct DroneDockReceiptPage: Decodable, Equatable, Sendable {
/// 当前页,从 1 开始。
let currentPage: Int
/// 每页条数。
let perPage: Int
/// 整个查询范围的记录数量。
let total: Int
/// 最后一页页码。
let lastPage: Int
/// 当前页实际记录。
let items: [DroneDockReceipt]
/// Laravel 分页字段。
private enum CodingKeys: String, CodingKey {
case currentPage = "current_page", perPage = "per_page", total, lastPage = "last_page"
case items = "data"
}
/// 保留真实分页,不把缺失列表当成正常空记录。
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
currentPage = try container.paymentInteger(.currentPage, minimum: 1)
perPage = try container.paymentInteger(.perPage, minimum: 1)
total = try container.paymentInteger(.total, minimum: 0)
lastPage = try container.paymentInteger(.lastPage, minimum: 0)
items = try container.decode([DroneDockReceipt].self, forKey: .items)
guard Set(items.map(\.orderNumber)).count == items.count else {
throw DecodingError.dataCorruptedError(forKey: .items, in: container, debugDescription: "收款记录订单号重复")
}
}
}
/// 已付款、已完成或已退款的真实收款记录,现金与积分分别展示。
struct DroneDockReceipt: Decodable, Hashable, Sendable {
/// 真实订单号。
let orderNumber: String
/// 套餐 ID。
let projectID: Int
/// 购买时套餐名称。
let projectName: String
/// 后端订单状态原值。
let orderStatus: Int
/// 支付时间,Unix 秒。
let payTime: TimeInterval
/// 服务端脱敏后的付款人昵称。
let payer: String
/// 现金实收,单位元。
let cashPaid: Decimal
/// 成功现金退款,单位元。
let cashRefund: Decimal
/// 现金净收,单位元。
let cashNet: Decimal
/// 消费积分数量。
let pointsPaid: Int
/// 已退回积分数量。
let pointsRefund: Int
/// 净消费积分数量。
let pointsNet: Int
/// 积分抵扣金额,单位元。
let pointAmount: Decimal
/// 收款记录接口字段。
private enum CodingKeys: String, CodingKey {
case orderNumber = "order_number", projectID = "project_id", projectName = "project_name"
case orderStatus = "order_status", payTime = "pay_time", payer
case cashPaid = "cash_paid", cashRefund = "cash_refund", cashNet = "cash_net"
case pointsPaid = "points_paid", pointsRefund = "points_refund", pointsNet = "points_net"
case pointAmount = "point_amount"
}
/// 解码实际支付与退款字段,零现金支付保留为真实零值。
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
orderNumber = try container.decode(String.self, forKey: .orderNumber)
guard !orderNumber.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else {
throw DecodingError.dataCorruptedError(forKey: .orderNumber, in: container, debugDescription: "订单号不能为空")
}
projectID = try container.paymentInteger(.projectID, minimum: 1)
projectName = try container.decode(String.self, forKey: .projectName)
orderStatus = try container.paymentInteger(.orderStatus)
payTime = TimeInterval(try container.paymentInteger(.payTime, minimum: 0))
payer = try container.decode(String.self, forKey: .payer)
cashPaid = try container.paymentMoney(.cashPaid)
cashRefund = try container.paymentMoney(.cashRefund)
cashNet = try container.paymentMoney(.cashNet)
pointsPaid = try container.paymentInteger(.pointsPaid, minimum: 0)
pointsRefund = try container.paymentInteger(.pointsRefund, minimum: 0)
pointsNet = try container.paymentInteger(.pointsNet)
pointAmount = try container.paymentMoney(.pointAmount)
}
}
/// 整个筛选区间的到账与退款汇总,不把现金、积分合并为一种支付金额。
struct DroneDockReceiptSummary: Decodable, Equatable, Sendable {
/// 收款记录总数量。
let count: Int
/// 现金实收总额。
let cashPaid: Decimal
/// 成功现金退款总额。
let cashRefund: Decimal
/// 现金净收总额。
let cashNet: Decimal
/// 消费积分总数。
let pointsPaid: Int
/// 退回积分总数。
let pointsRefund: Int
/// 净消费积分总数。
let pointsNet: Int
/// 积分抵扣总金额。
let pointAmount: Decimal
/// 收款汇总接口字段。
private enum CodingKeys: String, CodingKey {
case count
case cashPaid = "cash_paid", cashRefund = "cash_refund", cashNet = "cash_net"
case pointsPaid = "points_paid", pointsRefund = "points_refund", pointsNet = "points_net"
case pointAmount = "point_amount"
}
/// 严格读取汇总;金额缺失或 null 不以零替代。
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
count = try container.paymentInteger(.count, minimum: 0)
cashPaid = try container.paymentMoney(.cashPaid)
cashRefund = try container.paymentMoney(.cashRefund)
cashNet = try container.paymentMoney(.cashNet)
pointsPaid = try container.paymentInteger(.pointsPaid, minimum: 0)
pointsRefund = try container.paymentInteger(.pointsRefund, minimum: 0)
pointsNet = try container.paymentInteger(.pointsNet)
pointAmount = try container.paymentMoney(.pointAmount)
}
}
/// 套餐报价校验与规范化,使用十进制金额并遵循后端报价范围。
enum DroneDockPriceValidator {
/// 合法金额返回 nil,否则返回可展示的输入提示。
static func validationMessage(for text: String) -> String? {
normalizedPrice(text) == nil ? "请输入 0.01~99999.99 元的金额,最多保留两位小数" : nil
}
/// 将合法输入转换为固定两位小数的元字符串;拒绝指数、符号及不完整小数。
static func normalizedPrice(_ text: String) -> String? {
let value = text.trimmingCharacters(in: .whitespacesAndNewlines)
guard value.range(of: #"^[0-9]+(?:\.[0-9]{1,2})?$"#, options: .regularExpression) != nil,
let amount = Decimal(string: value, locale: Locale(identifier: "en_US_POSIX")), isValid(amount) else { return nil }
let string = NSDecimalNumber(decimal: amount).stringValue
let parts = string.split(separator: ".", omittingEmptySubsequences: false)
if parts.count == 1 { return string + ".00" }
return string + String(repeating: "0", count: max(0, 2 - parts[1].count))
}
/// 验证金额处于后端支持范围且不存在超过分的精度。
static func isValid(_ amount: Decimal) -> Bool {
guard !amount.isNaN, amount >= Decimal(string: "0.01")!, amount <= Decimal(string: "99999.99")! else { return false }
var value = amount
var rounded = Decimal()
NSDecimalRound(&rounded, &value, 2, .plain)
return amount == rounded
}
}
/// 机巢收款字段的局部解码规则,不改变其他业务的网络解码策略。
private extension KeyedDecodingContainer {
/// 兼容整数与整数字符串,拒绝小数、空值及越界数值。
func paymentInteger(_ key: Key, minimum: Int? = nil) throws -> Int {
let value: Int
if let number = try? decode(Int.self, forKey: key) { value = number }
else if let text = try? decode(String.self, forKey: key), let number = Int(text) { value = number }
else { throw DecodingError.dataCorruptedError(forKey: key, in: self, debugDescription: "需要整数或整数字符串") }
if let minimum, value < minimum {
throw DecodingError.dataCorruptedError(forKey: key, in: self, debugDescription: "整数低于允许范围")
}
return value
}
/// 兼容数字与金额字符串,拒绝非法文本和超过两位小数的金额。
func paymentMoney(_ key: Key) throws -> Decimal {
let value: Decimal
if let text = try? decode(String.self, forKey: key) {
guard text.range(of: #"^-?[0-9]+(?:\.[0-9]{1,2})?$"#, options: .regularExpression) != nil,
let number = Decimal(string: text, locale: Locale(identifier: "en_US_POSIX")) else {
throw DecodingError.dataCorruptedError(forKey: key, in: self, debugDescription: "需要最多两位小数的金额")
}
value = number
} else { value = try decode(Decimal.self, forKey: key) }
var source = value
var rounded = Decimal()
NSDecimalRound(&rounded, &source, 2, .plain)
guard !value.isNaN, value == rounded else {
throw DecodingError.dataCorruptedError(forKey: key, in: self, debugDescription: "金额精度或数值无效")
}
return value
}
}