feat(drone-dock): 新增独立套餐收款页并接入收款接口
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
import Foundation
|
||||
|
||||
/// 张工的机巢套餐收款数据源;付款链接与收款记录不提供单笔支付状态查询。
|
||||
protocol DroneDockPaymentServing: Sendable {
|
||||
/// 获取当前景区可售套餐,无分页。
|
||||
@MainActor func projects(scenicID: Int) async throws -> [DroneDockProject]
|
||||
/// 获取指定套餐的付款链接;省略 price 时使用后端当前售价。
|
||||
@MainActor func payCode(scenicID: Int, projectID: Int, price: String?) async throws -> DroneDockPayCode
|
||||
/// 获取本人当前景区的收款记录,默认日期与每页数量由后端决定。
|
||||
@MainActor func receipts(scenicID: Int, projectID: Int?, page: Int) async throws -> DroneDockReceiptsResponse
|
||||
}
|
||||
|
||||
/// 机巢套餐、付款链接及真实收款记录接口,复用统一登录凭证和业务错误处理。
|
||||
@MainActor
|
||||
final class DroneDockPaymentAPI: DroneDockPaymentServing {
|
||||
private let client: APIClient
|
||||
private let prefix = "/api/yf-handset-app/photog/multi-plane"
|
||||
|
||||
/// 注入统一客户端,支持正式服务和隔离的网络测试。
|
||||
init(client: APIClient) { self.client = client }
|
||||
|
||||
/// 请求当前景区可售套餐;空数组表示无可售套餐。
|
||||
func projects(scenicID: Int) async throws -> [DroneDockProject] {
|
||||
try validateScenic(scenicID)
|
||||
let projects: [DroneDockProject] = try await client.send(APIRequest(
|
||||
method: .get, path: prefix + "/projects",
|
||||
queryItems: [URLQueryItem(name: "scenic_id", value: String(scenicID))]
|
||||
))
|
||||
guard Set(projects.map(\.id)).count == projects.count else {
|
||||
throw APIError.decodeFailed("套餐 ID 重复")
|
||||
}
|
||||
return projects
|
||||
}
|
||||
|
||||
/// 由后端生成完整付款链接;客户端不自行拼接金额、备注或身份参数。
|
||||
func payCode(scenicID: Int, projectID: Int, price: String? = nil) async throws -> DroneDockPayCode {
|
||||
try validateScenic(scenicID)
|
||||
guard projectID > 0 else { throw APIError.networkFailed("请选择航拍套餐") }
|
||||
let normalized: String?
|
||||
if let price {
|
||||
guard let value = DroneDockPriceValidator.normalizedPrice(price) else {
|
||||
throw APIError.networkFailed(DroneDockPriceValidator.validationMessage(for: price)!)
|
||||
}
|
||||
normalized = value
|
||||
} else { normalized = nil }
|
||||
return try await client.send(APIRequest(
|
||||
method: .post, path: prefix + "/pay-code",
|
||||
body: PayCodeBody(scenicID: scenicID, projectID: projectID, price: normalized)
|
||||
))
|
||||
}
|
||||
|
||||
/// 按当前身份读取实际收款记录;不把查询结果解释成当前码已支付。
|
||||
func receipts(scenicID: Int, projectID: Int? = nil, page: Int = 1) async throws -> DroneDockReceiptsResponse {
|
||||
try validateScenic(scenicID)
|
||||
guard page > 0, projectID.map({ $0 > 0 }) ?? true else {
|
||||
throw APIError.networkFailed("收款记录查询参数无效")
|
||||
}
|
||||
var query = [URLQueryItem(name: "scenic_id", value: String(scenicID)),
|
||||
URLQueryItem(name: "page", value: String(page))]
|
||||
if let projectID { query.append(URLQueryItem(name: "project_id", value: String(projectID))) }
|
||||
return try await client.send(APIRequest(method: .get, path: prefix + "/receipts", queryItems: query))
|
||||
}
|
||||
|
||||
private func validateScenic(_ scenicID: Int) throws {
|
||||
guard scenicID > 0 else { throw APIError.networkFailed("请先选择景区") }
|
||||
}
|
||||
|
||||
/// 报价请求体;nil 价格通过 Encodable 省略,不发送空串或 null。
|
||||
private struct PayCodeBody: Encodable {
|
||||
let scenicID: Int
|
||||
let projectID: Int
|
||||
let price: String?
|
||||
|
||||
/// 业务接口请求字段。
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case scenicID = "scenic_id", projectID = "project_id", price
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user