Files
suixinkan_ios_uikit/suixinkan_ios/Features/Orders/ViewControllers/DepositOrderViewControllers.swift
汉秋 d99a5b1bf8 Advance UIKit rewrite with AMap integration and core UI modules.
Integrate高德 SDK with simulator-safe build flags, add map views for operating area and punch points, refactor main tabs and key feature screens to UIKit with Diffable lists, and document Swift concurrency defaults in AGENTS.md.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-26 15:16:12 +08:00

508 lines
21 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// DepositOrderViewControllers.swift
// suixinkan
//
import SnapKit
import UIKit
// MARK: - Deposit List Diffable
private typealias DepositListSection = Int
private typealias DepositListItem = String
/// /
private enum DepositListItemID {
static let placeholder = "depositList:placeholder"
}
// MARK: - Deposit Detail Diffable
private typealias DepositDetailSection = Int
private typealias DepositDetailRow = String
///
private enum DepositDetailRowID {
static let placeholder = "depositDetail:placeholder"
static func field(_ index: Int) -> String { "depositDetail:field:\(index)" }
}
// MARK: - Deposit Shooting Diffable
private typealias DepositShootingSection = Int
private typealias DepositShootingRow = String
/// section
private enum DepositShootingSectionID {
static let summary = 0
static let material = 1
static let complete = 2
}
///
private enum DepositShootingRowID {
static let placeholder = "depositShooting:placeholder"
static let scenicSpot = "depositShooting:scenicSpot"
static let rating = "depositShooting:rating"
static func material(_ index: Int) -> String { "depositShooting:material:\(index)" }
static func complete(_ index: Int) -> String { "depositShooting:complete:\(index)" }
}
/// 退
final class DepositOrderListViewController: UIViewController, UITableViewDelegate {
private let viewModel = DepositOrderListViewModel()
private var orderNumberField = UITextField()
private lazy var tableView: UITableView = {
let table = UITableView(frame: .zero, style: .insetGrouped)
table.delegate = self
table.register(DepositOrderCell.self, forCellReuseIdentifier: DepositOrderCell.reuseID)
return table
}()
/// Diffable
private var tableDataSource: UITableViewDiffableDataSource<DepositListSection, DepositListItem>!
/// UI
override func viewDidLoad() {
super.viewDidLoad()
title = "押金订单"
view.backgroundColor = AppDesignUIKit.pageBackground
configureTableDataSource()
view.addSubview(tableView)
tableView.snp.makeConstraints { make in make.edges.equalToSuperview() }
viewModel.onChange = { [weak self] in self?.applyTableSnapshot(reconfigure: true) }
setupHeader()
Task { await reload(showLoading: true) }
}
/// Diffable
private func configureTableDataSource() {
tableDataSource = UITableViewDiffableDataSource<DepositListSection, DepositListItem>(
tableView: tableView
) { [weak self] (tableView: UITableView, indexPath: IndexPath, item: DepositListItem) -> UITableViewCell? in
guard let self else { return UITableViewCell() }
if item == DepositListItemID.placeholder {
let cell = UITableViewCell()
cell.textLabel?.text = self.viewModel.loading ? "加载中..." : "暂无押金订单"
cell.selectionStyle = .none
return cell
}
guard let orderItem = self.viewModel.orders.first(where: { $0.orderNumber == item }) else {
return UITableViewCell()
}
let cell = tableView.dequeueReusableCell(withIdentifier: DepositOrderCell.reuseID, for: indexPath) as! DepositOrderCell
cell.configure(item: orderItem, isOperating: self.viewModel.operatingOrderNumber == orderItem.orderNumber)
cell.onDetail = { [weak self] in
self.flatMap { HomeMenuRouting.pushOrders(.depositDetail(orderNumber: orderItem.orderNumber), from: $0) }
}
cell.onWriteOff = { [weak self] in self?.writeOff(orderItem) }
cell.onRefund = { [weak self] in self?.refund(orderItem) }
return cell
}
applyTableSnapshot(animated: false)
}
/// Diffable snapshot
private func buildTableSnapshot() -> NSDiffableDataSourceSnapshot<DepositListSection, DepositListItem> {
var snapshot = NSDiffableDataSourceSnapshot<DepositListSection, DepositListItem>()
snapshot.appendSections([0])
if viewModel.orders.isEmpty {
snapshot.appendItems([DepositListItemID.placeholder], toSection: 0)
} else {
snapshot.appendItems(viewModel.orders.map(\.orderNumber), toSection: 0)
}
return snapshot
}
/// snapshot
private func applyTableSnapshot(animated: Bool = true, reconfigure: Bool = false) {
var snapshot = buildTableSnapshot()
if reconfigure, !snapshot.itemIdentifiers.isEmpty {
snapshot.reconfigureItems(snapshot.itemIdentifiers)
}
tableDataSource.apply(snapshot, animatingDifferences: animated)
}
/// Header UI
private func setupHeader() {
let header = UIView(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: 120))
orderNumberField.placeholder = "请输入押金订单号"
orderNumberField.borderStyle = .roundedRect
orderNumberField.autocorrectionType = .no
orderNumberField.autocapitalizationType = .none
let detailButton = makePrimaryButton(title: "查看订单详情")
detailButton.addTarget(self, action: #selector(openDetail), for: .touchUpInside)
header.addSubview(orderNumberField)
header.addSubview(detailButton)
orderNumberField.snp.makeConstraints { make in
make.top.leading.trailing.equalToSuperview().inset(16)
make.height.equalTo(44)
}
detailButton.snp.makeConstraints { make in
make.top.equalTo(orderNumberField.snp.bottom).offset(12)
make.leading.trailing.equalToSuperview().inset(16)
}
tableView.tableHeaderView = header
}
/// open
@objc private func openDetail() {
let text = orderNumberField.text?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
guard !text.isEmpty else { return }
HomeMenuRouting.pushOrders(.depositDetail(orderNumber: text), from: self)
}
///
private func reload(showLoading: Bool) async {
if showLoading {
await appServices.globalLoading.withLoading {
await viewModel.reload(api: appServices.ordersAPI, scenicId: appServices.accountContext.currentScenic?.id, reset: true)
}
} else {
await viewModel.reload(api: appServices.ordersAPI, scenicId: appServices.accountContext.currentScenic?.id, reset: true)
}
if let message = viewModel.errorMessage { showToast(message) }
}
/// writeOff
private func writeOff(_ item: DepositOrderListItem) {
let alert = UIAlertController(title: "确认核销该押金订单?", message: item.orderNumber, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "取消", style: .cancel))
alert.addAction(UIAlertAction(title: "确认核销", style: .default) { [weak self] _ in
Task {
guard let self else { return }
let success = await self.viewModel.writeOff(api: self.appServices.ordersAPI, scenicId: self.appServices.accountContext.currentScenic?.id, orderNumber: item.orderNumber)
if success { self.showToast("核销成功") }
else if let message = self.viewModel.errorMessage { self.showToast(message) }
}
})
present(alert, animated: true)
}
/// refund
private func refund(_ item: DepositOrderListItem) {
let alert = UIAlertController(title: "申请退款", message: "请填写退款原因", preferredStyle: .alert)
alert.addTextField { $0.placeholder = "退款原因" }
alert.addAction(UIAlertAction(title: "取消", style: .cancel))
alert.addAction(UIAlertAction(title: "提交", style: .default) { [weak self] _ in
Task {
guard let self else { return }
let reason = alert.textFields?.first?.text ?? ""
let success = await self.viewModel.refund(api: self.appServices.ordersAPI, scenicId: self.appServices.accountContext.currentScenic?.id, orderNumber: item.orderNumber, reason: reason)
if success { self.showToast("退款申请已提交") }
else if let message = self.viewModel.errorMessage { self.showToast(message) }
}
})
present(alert, animated: true)
}
/// UITableView section
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
"押金订单列表 \(viewModel.orders.count)/\(viewModel.total)"
}
/// UITableView
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
guard let item = tableDataSource.itemIdentifier(for: indexPath),
item != DepositListItemID.placeholder,
item == viewModel.orders.last?.orderNumber,
viewModel.hasMore else { return }
Task {
await viewModel.reload(api: appServices.ordersAPI, scenicId: appServices.accountContext.currentScenic?.id, reset: false)
}
}
}
/// DepositOrder Cell
private final class DepositOrderCell: UITableViewCell {
static let reuseID = "DepositOrderCell"
var onDetail: (() -> Void)?
var onWriteOff: (() -> Void)?
var onRefund: (() -> Void)?
private let titleLabel = UILabel()
private let statusLabel = UILabel()
private let amountLabel = UILabel()
///
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
selectionStyle = .none
titleLabel.font = .systemFont(ofSize: AppMetrics.FontSize.subheadline, weight: .semibold)
statusLabel.font = .systemFont(ofSize: AppMetrics.FontSize.caption)
amountLabel.font = .systemFont(ofSize: AppMetrics.FontSize.body, weight: .semibold)
amountLabel.textColor = AppDesignUIKit.primary
let detailButton = UIButton(type: .system)
detailButton.setTitle("详情", for: .normal)
detailButton.addTarget(self, action: #selector(detailTapped), for: .touchUpInside)
let writeOffButton = UIButton(type: .system)
writeOffButton.setTitle("核销", for: .normal)
writeOffButton.addTarget(self, action: #selector(writeOffTapped), for: .touchUpInside)
let refundButton = UIButton(type: .system)
refundButton.setTitle("退款", for: .normal)
refundButton.addTarget(self, action: #selector(refundTapped), for: .touchUpInside)
let buttonRow = UIStackView(arrangedSubviews: [detailButton, writeOffButton, refundButton])
buttonRow.axis = .horizontal
buttonRow.distribution = .fillEqually
let stack = UIStackView(arrangedSubviews: [titleLabel, amountLabel, buttonRow])
stack.axis = .vertical
stack.spacing = 8
contentView.addSubview(stack)
contentView.addSubview(statusLabel)
stack.snp.makeConstraints { make in make.edges.equalToSuperview().inset(12) }
statusLabel.snp.makeConstraints { make in make.trailing.top.equalToSuperview().inset(12) }
}
@available(*, unavailable)
required init?(coder: NSCoder) { fatalError() }
///
func configure(item: DepositOrderListItem, isOperating: Bool) {
titleLabel.text = item.orderNumber
statusLabel.text = isOperating ? "处理中" : item.statusName
amountLabel.text = "¥\(item.amount.isEmpty ? "0.00" : item.amount)"
}
/// detail
@objc private func detailTapped() { onDetail?() }
/// writeOff
@objc private func writeOffTapped() { onWriteOff?() }
/// refund
@objc private func refundTapped() { onRefund?() }
}
///
final class DepositOrderDetailViewController: UIViewController {
private let orderNumber: String
private let viewModel = DepositOrderDetailViewModel()
private lazy var tableView: UITableView = {
let table = UITableView(frame: .zero, style: .insetGrouped)
table.backgroundColor = AppDesignUIKit.pageBackground
return table
}()
/// Diffable
private var tableDataSource: UITableViewDiffableDataSource<DepositDetailSection, DepositDetailRow>!
///
init(orderNumber: String) {
self.orderNumber = orderNumber
super.init(nibName: nil, bundle: nil)
}
@available(*, unavailable)
required init?(coder: NSCoder) { fatalError() }
/// UI
override func viewDidLoad() {
super.viewDidLoad()
title = "押金订单详情"
configureTableDataSource()
view.addSubview(tableView)
tableView.snp.makeConstraints { make in make.edges.equalToSuperview() }
Task { await loadDetail() }
}
/// Diffable
private func configureTableDataSource() {
tableDataSource = UITableViewDiffableDataSource<DepositDetailSection, DepositDetailRow>(
tableView: tableView
) { [weak self] (_: UITableView, _: IndexPath, row: DepositDetailRow) -> UITableViewCell? in
guard let self else { return UITableViewCell() }
let cell = UITableViewCell(style: .value1, reuseIdentifier: nil)
cell.selectionStyle = .none
guard let detail = self.viewModel.detail else {
cell.textLabel?.text = self.viewModel.errorMessage ?? (self.viewModel.loading ? "加载中..." : "暂无详情")
return cell
}
let rows: [(String, String)] = [
("订单号", detail.orderNumber),
("状态", detail.orderStatusName),
("类型", detail.orderTypeLabel),
("付款金额", detail.actualPayAmount),
("手机号", detail.phone),
("项目", detail.projectName),
("创建时间", detail.createdAt),
("备注", detail.remark)
]
let index = Int(row.split(separator: ":").last ?? "") ?? 0
if index < rows.count {
cell.textLabel?.text = rows[index].0
cell.detailTextLabel?.text = rows[index].1
}
return cell
}
applyTableSnapshot(animated: false)
}
/// Diffable snapshot
private func buildTableSnapshot() -> NSDiffableDataSourceSnapshot<DepositDetailSection, DepositDetailRow> {
var snapshot = NSDiffableDataSourceSnapshot<DepositDetailSection, DepositDetailRow>()
snapshot.appendSections([0])
if viewModel.detail == nil {
snapshot.appendItems([DepositDetailRowID.placeholder], toSection: 0)
} else {
snapshot.appendItems((0..<8).map { DepositDetailRowID.field($0) }, toSection: 0)
}
return snapshot
}
/// snapshot
private func applyTableSnapshot(animated: Bool = true) {
tableDataSource.apply(buildTableSnapshot(), animatingDifferences: animated)
}
/// Detail
private func loadDetail() async {
await appServices.globalLoading.withLoading {
await viewModel.load(
api: appServices.ordersAPI,
storeId: appServices.accountContext.currentStore?.id,
orderNumber: orderNumber
)
}
applyTableSnapshot()
if let message = viewModel.errorMessage { showToast(message) }
}
}
///
final class DepositOrderShootingInfoViewController: UIViewController, UITableViewDelegate {
private let orderNumber: String
private let scenicSpotId: Int
private let photogUid: Int
private let viewModel = DepositOrderShootingInfoViewModel()
private lazy var tableView: UITableView = {
let table = UITableView(frame: .zero, style: .insetGrouped)
table.delegate = self
return table
}()
/// Diffable
private var tableDataSource: UITableViewDiffableDataSource<DepositShootingSection, DepositShootingRow>!
///
init(orderNumber: String, scenicSpotId: Int, photogUid: Int) {
self.orderNumber = orderNumber
self.scenicSpotId = scenicSpotId
self.photogUid = photogUid
super.init(nibName: nil, bundle: nil)
}
@available(*, unavailable)
required init?(coder: NSCoder) { fatalError() }
/// UI
override func viewDidLoad() {
super.viewDidLoad()
title = "押金拍摄信息"
configureTableDataSource()
view.addSubview(tableView)
tableView.snp.makeConstraints { make in make.edges.equalToSuperview() }
Task { await loadDetail() }
}
/// Diffable
private func configureTableDataSource() {
tableDataSource = UITableViewDiffableDataSource<DepositShootingSection, DepositShootingRow>(
tableView: tableView
) { [weak self] (_: UITableView, indexPath: IndexPath, row: DepositShootingRow) -> UITableViewCell? in
guard let self else { return UITableViewCell() }
guard let detail = self.viewModel.detail else {
let cell = UITableViewCell()
cell.textLabel?.text = self.viewModel.errorMessage ?? "加载中..."
return cell
}
if row == DepositShootingRowID.scenicSpot || row == DepositShootingRowID.rating {
let cell = UITableViewCell(style: .value1, reuseIdentifier: nil)
if row == DepositShootingRowID.scenicSpot {
cell.textLabel?.text = "打卡点"
cell.detailTextLabel?.text = detail.scenicSpotName
} else {
cell.textLabel?.text = "拍摄评分"
cell.detailTextLabel?.text = detail.orderComment.map { "\($0.starShooting)" } ?? "--"
}
return cell
}
let files: [OrderMediaFile]
if row.hasPrefix("depositShooting:material:") {
files = detail.materialList
} else {
files = detail.completeList
}
let index = Int(row.split(separator: ":").last ?? "") ?? 0
let media = files[index]
let cell = UITableViewCell(style: .subtitle, reuseIdentifier: nil)
cell.textLabel?.text = media.fileName
cell.detailTextLabel?.text = media.fileUrl
return cell
}
applyTableSnapshot(animated: false)
}
/// Diffable snapshot
private func buildTableSnapshot() -> NSDiffableDataSourceSnapshot<DepositShootingSection, DepositShootingRow> {
var snapshot = NSDiffableDataSourceSnapshot<DepositShootingSection, DepositShootingRow>()
guard let detail = viewModel.detail else {
snapshot.appendSections([0])
snapshot.appendItems([DepositShootingRowID.placeholder], toSection: 0)
return snapshot
}
snapshot.appendSections([DepositShootingSectionID.summary, DepositShootingSectionID.material, DepositShootingSectionID.complete])
snapshot.appendItems([DepositShootingRowID.scenicSpot, DepositShootingRowID.rating], toSection: DepositShootingSectionID.summary)
snapshot.appendItems(detail.materialList.indices.map { DepositShootingRowID.material($0) }, toSection: DepositShootingSectionID.material)
snapshot.appendItems(detail.completeList.indices.map { DepositShootingRowID.complete($0) }, toSection: DepositShootingSectionID.complete)
return snapshot
}
/// snapshot
private func applyTableSnapshot(animated: Bool = true) {
tableDataSource.apply(buildTableSnapshot(), animatingDifferences: animated)
}
/// Detail
private func loadDetail() async {
await appServices.globalLoading.withLoading {
await viewModel.load(
api: appServices.ordersAPI,
storeId: appServices.accountContext.currentStore?.id,
orderNumber: orderNumber,
scenicSpotId: scenicSpotId,
photogUid: photogUid
)
}
applyTableSnapshot()
if let message = viewModel.errorMessage { showToast(message) }
}
/// UITableView section
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
guard viewModel.detail != nil,
let sectionID = tableDataSource.snapshot().sectionIdentifiers[safe: section] else { return nil }
switch sectionID {
case DepositShootingSectionID.material: return "素材"
case DepositShootingSectionID.complete: return "成片"
default: return nil
}
}
}
/// section
private extension Array {
subscript(safe index: Int) -> Element? {
indices.contains(index) ? self[index] : nil
}
}