新增线下收款记录和 ai 修图优化

This commit is contained in:
han xin
2026-08-21 15:51:52 +08:00
parent 6fe9928b49
commit 5cf4409bad
40 changed files with 5482 additions and 56 deletions
@@ -0,0 +1,683 @@
//
// OfflineCollectionDailyViewController.swift
// suixinkan
//
import SnapKit
import UIKit
/// 日清列表的分区。
enum OfflineDailyListSection: Hashable {
case records
case batches
case empty
}
/// 日清列表的条目数据。
enum OfflineDailyListItem: Hashable {
case record(OfflineCollectionRecord)
case batch(OfflineSettlementBatch)
case empty(isToday: Bool)
}
/// 线下收款日清页,展示单营业日汇总、明细与独立补缴流水。
final class OfflineCollectionDailyViewController: BaseViewController {
private let viewModel: OfflineCollectionDailyViewModel
private let headerStack = UIStackView()
private let dateControl = UIButton(type: .system)
private let summaryCard = UIView()
private let summaryStatusLabel = UILabel()
private let totalMetric = OfflineDailyMetricView(title: "线下收款总额")
private let countMetric = OfflineDailyMetricView(title: "登记笔数")
private let settledMetric = OfflineDailyMetricView(title: "已补缴")
private let pendingMetric = OfflineDailyMetricView(title: "待补缴")
private let overdueBanner = UILabel()
private let tableView = UITableView(frame: .zero, style: .plain)
private var dataSource: UITableViewDiffableDataSource<OfflineDailyListSection, OfflineDailyListItem>!
private var currentSections: [OfflineDailyListSection] = []
private let bottomContainer = UIView()
private let settlementButton = UIButton(type: .system)
private var isFeedbackPresented = false
/// 使用指定营业日、收款上下文与 Mock 服务创建日清页。
init(
businessDate: String,
context: OfflineCollectionContext = .current(),
service: OfflineCollectionMockService = .shared
) {
viewModel = OfflineCollectionDailyViewModel(
businessDate: businessDate,
context: context,
service: service
)
super.init(nibName: nil, bundle: nil)
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func setupNavigationBar() {
title = "线下收款日清"
#if DEBUG
navigationItem.rightBarButtonItem = UIBarButtonItem(
title: "模拟失败",
style: .plain,
target: self,
action: #selector(simulateFailureTapped)
)
#endif
}
override func setupUI() {
view.backgroundColor = AppColor.pageBackground
headerStack.axis = .vertical
headerStack.spacing = AppSpacing.sm
dateControl.contentHorizontalAlignment = .leading
dateControl.showsMenuAsPrimaryAction = true
var dateConfiguration = UIButton.Configuration.plain()
dateConfiguration.image = UIImage(systemName: "calendar")
dateConfiguration.imagePadding = AppSpacing.sm
dateConfiguration.baseForegroundColor = AppColor.textPrimary
dateConfiguration.background.backgroundColor = .white
dateConfiguration.background.cornerRadius = AppRadius.lg
dateConfiguration.contentInsets = NSDirectionalEdgeInsets(
top: 12,
leading: AppSpacing.md,
bottom: 12,
trailing: AppSpacing.md
)
dateControl.configuration = dateConfiguration
dateControl.accessibilityIdentifier = "offlineCollection.businessDate"
summaryCard.backgroundColor = .white
summaryCard.layer.cornerRadius = AppRadius.lg
summaryStatusLabel.font = .systemFont(ofSize: 13, weight: .semibold)
summaryStatusLabel.textAlignment = .center
summaryStatusLabel.textColor = AppColor.success
summaryStatusLabel.backgroundColor = UIColor(hex: 0xECFDF3)
summaryStatusLabel.layer.cornerRadius = 12
summaryStatusLabel.clipsToBounds = true
let firstMetricRow = UIStackView(arrangedSubviews: [totalMetric, countMetric])
firstMetricRow.axis = .horizontal
firstMetricRow.distribution = .fillEqually
let secondMetricRow = UIStackView(arrangedSubviews: [settledMetric, pendingMetric])
secondMetricRow.axis = .horizontal
secondMetricRow.distribution = .fillEqually
let metricStack = UIStackView(arrangedSubviews: [firstMetricRow, secondMetricRow])
metricStack.axis = .vertical
metricStack.spacing = AppSpacing.md
summaryCard.addSubview(metricStack)
summaryCard.addSubview(summaryStatusLabel)
metricStack.snp.makeConstraints { make in
make.top.leading.trailing.equalToSuperview().inset(AppSpacing.md)
make.bottom.equalToSuperview().inset(AppSpacing.md)
}
summaryStatusLabel.snp.makeConstraints { make in
make.top.trailing.equalToSuperview().inset(AppSpacing.md)
make.height.equalTo(24)
}
overdueBanner.text = "该营业日存在逾期未补缴,请尽快完成补缴"
overdueBanner.font = .systemFont(ofSize: 13, weight: .medium)
overdueBanner.textColor = AppColor.danger
overdueBanner.backgroundColor = UIColor(hex: 0xFFF1F0)
overdueBanner.layer.cornerRadius = AppRadius.md
overdueBanner.clipsToBounds = true
overdueBanner.textAlignment = .center
overdueBanner.numberOfLines = 0
tableView.backgroundColor = .clear
tableView.separatorStyle = .none
tableView.rowHeight = UITableView.automaticDimension
tableView.estimatedRowHeight = 92
tableView.delegate = self
tableView.register(OfflineCollectionRecordCell.self, forCellReuseIdentifier: OfflineCollectionRecordCell.reuseIdentifier)
tableView.register(OfflineSettlementBatchCell.self, forCellReuseIdentifier: OfflineSettlementBatchCell.reuseIdentifier)
tableView.register(OfflineCollectionEmptyCell.self, forCellReuseIdentifier: OfflineCollectionEmptyCell.reuseIdentifier)
dataSource = makeDataSource()
bottomContainer.backgroundColor = .white
bottomContainer.layer.shadowColor = UIColor.black.cgColor
bottomContainer.layer.shadowOpacity = 0.06
bottomContainer.layer.shadowRadius = 8
bottomContainer.layer.shadowOffset = CGSize(width: 0, height: -2)
var settlementConfiguration = UIButton.Configuration.filled()
settlementConfiguration.title = "补缴今日全部"
settlementConfiguration.baseBackgroundColor = AppColor.primary
settlementConfiguration.baseForegroundColor = .white
settlementConfiguration.cornerStyle = .medium
settlementConfiguration.titleTextAttributesTransformer = UIConfigurationTextAttributesTransformer { incoming in
var outgoing = incoming
outgoing.font = .systemFont(ofSize: 17, weight: .semibold)
return outgoing
}
settlementButton.configuration = settlementConfiguration
settlementButton.accessibilityIdentifier = "offlineCollection.settle"
view.addSubview(headerStack)
[dateControl, summaryCard, overdueBanner].forEach(headerStack.addArrangedSubview)
view.addSubview(tableView)
view.addSubview(bottomContainer)
bottomContainer.addSubview(settlementButton)
}
override func setupConstraints() {
headerStack.snp.makeConstraints { make in
make.top.equalTo(view.safeAreaLayoutGuide).offset(AppSpacing.sm)
make.leading.trailing.equalToSuperview().inset(AppSpacing.screenHorizontalInset)
}
dateControl.snp.makeConstraints { make in
make.height.equalTo(50)
}
summaryCard.snp.makeConstraints { make in
make.height.equalTo(172)
}
overdueBanner.snp.makeConstraints { make in
make.height.greaterThanOrEqualTo(48)
}
bottomContainer.snp.makeConstraints { make in
make.leading.trailing.bottom.equalToSuperview()
}
settlementButton.snp.makeConstraints { make in
make.top.equalToSuperview().inset(AppSpacing.md)
make.leading.trailing.equalToSuperview().inset(AppSpacing.screenHorizontalInset)
make.height.equalTo(50)
make.bottom.equalTo(view.safeAreaLayoutGuide).inset(AppSpacing.md)
}
tableView.snp.makeConstraints { make in
make.top.equalTo(headerStack.snp.bottom).offset(AppSpacing.sm)
make.leading.trailing.equalToSuperview()
make.bottom.equalTo(bottomContainer.snp.top)
}
}
override func bindActions() {
settlementButton.addTarget(self, action: #selector(settlementTapped), for: .touchUpInside)
viewModel.onStateChange = { [weak self] in
Task { @MainActor in self?.applyViewModel() }
}
}
override func viewDidLoad() {
super.viewDidLoad()
applyViewModel()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
viewModel.load()
}
@MainActor
private func applyViewModel() {
let datePrefix = viewModel.isToday ? "今日 · " : "营业日 · "
dateControl.configuration?.title = datePrefix + viewModel.businessDate
dateControl.menu = makeDateMenu()
totalMetric.setValue(OfflineCollectionMoney.display(viewModel.summary.totalAmountFen))
countMetric.setValue("\(viewModel.summary.totalCount) 笔")
settledMetric.setValue(OfflineCollectionMoney.display(viewModel.summary.settledAmountFen))
pendingMetric.setValue(OfflineCollectionMoney.display(viewModel.summary.pendingAmountFen), highlighted: viewModel.summary.pendingAmountFen > 0)
let isSettled = viewModel.summary.totalCount > 0 && viewModel.summary.pendingAmountFen == 0
summaryStatusLabel.text = " 本日已结清 "
summaryStatusLabel.isHidden = !isSettled
overdueBanner.isHidden = !viewModel.summary.hasOverdue
let isProcessing = viewModel.settlementState == .processing
settlementButton.isEnabled = viewModel.canSettle
var configuration = settlementButton.configuration
configuration?.showsActivityIndicator = isProcessing
if isProcessing {
configuration?.title = "补缴中"
} else if isSettled {
configuration?.title = "本日已全部补缴"
} else if viewModel.summary.pendingAmountFen == 0 {
configuration?.title = "当日暂无待补缴"
} else {
let prefix = viewModel.isToday ? "补缴今日全部" : "补缴本日全部"
configuration?.title = "\(prefix) \(OfflineCollectionMoney.display(viewModel.summary.pendingAmountFen))"
}
settlementButton.configuration = configuration
settlementButton.alpha = settlementButton.isEnabled || isProcessing ? 1 : 0.5
applySnapshot()
presentSettlementFeedbackIfNeeded()
}
@MainActor
private func applySnapshot() {
var snapshot = NSDiffableDataSourceSnapshot<OfflineDailyListSection, OfflineDailyListItem>()
currentSections = []
if viewModel.records.isEmpty {
currentSections.append(.empty)
snapshot.appendSections([.empty])
snapshot.appendItems([.empty(isToday: viewModel.isToday)], toSection: .empty)
} else {
currentSections.append(.records)
snapshot.appendSections([.records])
snapshot.appendItems(viewModel.records.map(OfflineDailyListItem.record), toSection: .records)
}
if !viewModel.batches.isEmpty {
currentSections.append(.batches)
snapshot.appendSections([.batches])
snapshot.appendItems(viewModel.batches.map(OfflineDailyListItem.batch), toSection: .batches)
}
dataSource.apply(snapshot, animatingDifferences: view.window != nil)
}
private func makeDataSource() -> UITableViewDiffableDataSource<OfflineDailyListSection, OfflineDailyListItem> {
UITableViewDiffableDataSource(tableView: tableView) { [weak self] tableView, indexPath, item in
switch item {
case let .record(record):
guard let cell = tableView.dequeueReusableCell(
withIdentifier: OfflineCollectionRecordCell.reuseIdentifier,
for: indexPath
) as? OfflineCollectionRecordCell else { return UITableViewCell() }
cell.apply(record: record)
return cell
case let .batch(batch):
guard let cell = tableView.dequeueReusableCell(
withIdentifier: OfflineSettlementBatchCell.reuseIdentifier,
for: indexPath
) as? OfflineSettlementBatchCell else { return UITableViewCell() }
cell.apply(batch: batch)
return cell
case let .empty(isToday):
guard let cell = tableView.dequeueReusableCell(
withIdentifier: OfflineCollectionEmptyCell.reuseIdentifier,
for: indexPath
) as? OfflineCollectionEmptyCell else { return UITableViewCell() }
cell.apply(isToday: isToday)
cell.onRegister = { [weak self] in self?.openRegistration() }
return cell
}
}
}
private func makeDateMenu() -> UIMenu {
let actions = viewModel.availableBusinessDates.map { [weak self] date in
let isCurrent = date == self?.viewModel.businessDate
let title = date == self?.viewModel.service.todayBusinessDate ? "今日 \(date)" : date
return UIAction(title: title, state: isCurrent ? .on : .off) { [weak self] _ in
self?.viewModel.selectBusinessDate(date)
}
}
return UIMenu(title: "选择营业日", children: actions)
}
@MainActor
private func presentSettlementFeedbackIfNeeded() {
guard !isFeedbackPresented else { return }
switch viewModel.settlementState {
case .idle, .processing:
return
case let .success(batch):
isFeedbackPresented = true
let message = """
已补缴 \(OfflineCollectionMoney.display(batch.amountFen))
共结清 \(batch.recordCount) 笔线下收款
补缴流水号:\(batch.id)
"""
let alert = UIAlertController(title: "补缴成功", message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "完成", style: .default) { [weak self] _ in
self?.isFeedbackPresented = false
self?.viewModel.clearSettlementFeedback()
})
present(alert, animated: true)
case let .failed(message):
isFeedbackPresented = true
let alert = UIAlertController(title: "补缴失败", message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "暂不处理", style: .cancel) { [weak self] _ in
self?.isFeedbackPresented = false
self?.viewModel.cancelPreparedSettlement()
self?.viewModel.clearSettlementFeedback()
})
alert.addAction(UIAlertAction(title: "重新补缴", style: .default) { [weak self] _ in
guard let self else { return }
self.isFeedbackPresented = false
self.viewModel.clearSettlementFeedback()
Task { await self.viewModel.confirmSettlement() }
})
present(alert, animated: true)
}
}
private func openRegistration() {
guard viewModel.isToday else { return }
navigationController?.pushViewController(
OfflineCollectionRegistrationViewController(
context: viewModel.context,
service: viewModel.service
),
animated: true
)
}
@objc private func settlementTapped() {
do {
let request = try viewModel.prepareSettlement()
let message = """
营业日:\(request.businessDate)
待补缴记录:\(request.recordIds.count) 笔
本次补缴金额:\(OfflineCollectionMoney.display(request.amountFen))
补缴成功后,本批次记录将统一变为“已补缴”。
"""
let alert = UIAlertController(title: "确认补缴", message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "取消", style: .cancel) { [weak self] _ in
self?.viewModel.cancelPreparedSettlement()
})
alert.addAction(UIAlertAction(
title: "确认补缴 \(OfflineCollectionMoney.display(request.amountFen))",
style: .default
) { [weak self] _ in
guard let self else { return }
Task { await self.viewModel.confirmSettlement() }
})
present(alert, animated: true)
} catch {
showToast(error.localizedDescription)
viewModel.load()
}
}
@objc private func simulateFailureTapped() {
viewModel.simulateNextSettlementFailure()
showToast("下一次补缴将模拟失败,可验收失败后重试")
}
}
extension OfflineCollectionDailyViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
currentSections[safe: section] == .empty ? 0 : 40
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
guard let listSection = currentSections[safe: section], listSection != .empty else { return nil }
let container = UIView()
container.backgroundColor = AppColor.pageBackground
let label = UILabel()
label.text = listSection == .records ? "线下收款明细" : "线下收款补缴流水"
label.font = .systemFont(ofSize: 15, weight: .semibold)
label.textColor = AppColor.textPrimary
container.addSubview(label)
label.snp.makeConstraints { make in
make.leading.trailing.equalToSuperview().inset(AppSpacing.screenHorizontalInset)
make.bottom.equalToSuperview().inset(AppSpacing.xs)
}
return container
}
}
/// 日清汇总卡片中的单项指标视图。
final class OfflineDailyMetricView: UIView {
private let titleLabel = UILabel()
private let valueLabel = UILabel()
/// 使用指标名称创建汇总视图。
init(title: String) {
super.init(frame: .zero)
titleLabel.text = title
setupUI()
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
/// 更新指标值及是否使用警示色强调。
func setValue(_ value: String, highlighted: Bool = false) {
valueLabel.text = value
valueLabel.textColor = highlighted ? UIColor(hex: 0xD97706) : AppColor.textPrimary
}
private func setupUI() {
titleLabel.font = .systemFont(ofSize: 12)
titleLabel.textColor = AppColor.textTertiary
valueLabel.font = .systemFont(ofSize: 19, weight: .bold)
valueLabel.textColor = AppColor.textPrimary
valueLabel.adjustsFontSizeToFitWidth = true
valueLabel.minimumScaleFactor = 0.75
let stack = UIStackView(arrangedSubviews: [titleLabel, valueLabel])
stack.axis = .vertical
stack.spacing = 4
addSubview(stack)
stack.snp.makeConstraints { make in
make.edges.equalToSuperview().inset(UIEdgeInsets(top: 2, left: 4, bottom: 2, right: 4))
}
}
}
/// 线下收款明细单元格。
final class OfflineCollectionRecordCell: UITableViewCell {
static let reuseIdentifier = "OfflineCollectionRecordCell"
private let cardView = UIView()
private let timeLabel = UILabel()
private let methodLabel = UILabel()
private let identifierLabel = UILabel()
private let amountLabel = UILabel()
private let statusLabel = UILabel()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupUI()
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
/// 用线下收款记录刷新单元格。
func apply(record: OfflineCollectionRecord) {
timeLabel.text = OfflineCollectionDate.timeText(record.registeredAt)
methodLabel.text = record.paymentMethod.displayName
identifierLabel.text = record.id
amountLabel.text = OfflineCollectionMoney.display(record.amountFen)
statusLabel.text = " \(record.status.displayName) "
let color: UIColor
let background: UIColor
switch record.status {
case .pending:
color = UIColor(hex: 0xD97706)
background = UIColor(hex: 0xFFF7E6)
case .settled:
color = AppColor.success
background = UIColor(hex: 0xECFDF3)
case .overdue:
color = AppColor.danger
background = UIColor(hex: 0xFFF1F0)
}
statusLabel.textColor = color
statusLabel.backgroundColor = background
}
private func setupUI() {
selectionStyle = .none
backgroundColor = .clear
contentView.backgroundColor = .clear
cardView.backgroundColor = .white
cardView.layer.cornerRadius = AppRadius.lg
timeLabel.font = .systemFont(ofSize: 15, weight: .semibold)
timeLabel.textColor = AppColor.textPrimary
methodLabel.font = .systemFont(ofSize: 14)
methodLabel.textColor = AppColor.textSecondary
identifierLabel.font = .systemFont(ofSize: 11)
identifierLabel.textColor = AppColor.textTertiary
amountLabel.font = .systemFont(ofSize: 19, weight: .bold)
amountLabel.textColor = AppColor.textPrimary
amountLabel.textAlignment = .right
statusLabel.font = .systemFont(ofSize: 12, weight: .semibold)
statusLabel.textAlignment = .center
statusLabel.layer.cornerRadius = 11
statusLabel.clipsToBounds = true
let leftStack = UIStackView(arrangedSubviews: [timeLabel, methodLabel, identifierLabel])
leftStack.axis = .vertical
leftStack.spacing = 4
let rightStack = UIStackView(arrangedSubviews: [amountLabel, statusLabel])
rightStack.axis = .vertical
rightStack.alignment = .trailing
rightStack.spacing = 8
cardView.addSubview(leftStack)
cardView.addSubview(rightStack)
contentView.addSubview(cardView)
cardView.snp.makeConstraints { make in
make.top.bottom.equalToSuperview().inset(5)
make.leading.trailing.equalToSuperview().inset(AppSpacing.screenHorizontalInset)
}
leftStack.snp.makeConstraints { make in
make.leading.top.bottom.equalToSuperview().inset(AppSpacing.md)
make.trailing.lessThanOrEqualTo(rightStack.snp.leading).offset(-AppSpacing.sm)
}
rightStack.snp.makeConstraints { make in
make.trailing.equalToSuperview().inset(AppSpacing.md)
make.centerY.equalToSuperview()
}
statusLabel.snp.makeConstraints { make in
make.height.equalTo(22)
}
}
}
/// 线下收款补缴流水单元格。
final class OfflineSettlementBatchCell: UITableViewCell {
static let reuseIdentifier = "OfflineSettlementBatchCell"
private let cardView = UIView()
private let identifierLabel = UILabel()
private let amountLabel = UILabel()
private let detailLabel = UILabel()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupUI()
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
/// 用成功补缴流水刷新单元格。
func apply(batch: OfflineSettlementBatch) {
identifierLabel.text = batch.id
amountLabel.text = OfflineCollectionMoney.display(batch.amountFen)
detailLabel.text = "\(OfflineCollectionDate.dateTimeText(batch.paidAt)) · \(batch.payerName) · 关联 \(batch.recordCount) 笔"
}
private func setupUI() {
selectionStyle = .none
backgroundColor = .clear
contentView.backgroundColor = .clear
cardView.backgroundColor = UIColor(hex: 0xF4FBF7)
cardView.layer.cornerRadius = AppRadius.lg
cardView.layer.borderColor = UIColor(hex: 0xCDEED8).cgColor
cardView.layer.borderWidth = 1
identifierLabel.font = .systemFont(ofSize: 13, weight: .semibold)
identifierLabel.textColor = AppColor.success
amountLabel.font = .systemFont(ofSize: 18, weight: .bold)
amountLabel.textColor = AppColor.textPrimary
amountLabel.textAlignment = .right
detailLabel.font = .systemFont(ofSize: 12)
detailLabel.textColor = AppColor.textSecondary
detailLabel.numberOfLines = 0
let topRow = UIStackView(arrangedSubviews: [identifierLabel, amountLabel])
topRow.axis = .horizontal
topRow.distribution = .fillEqually
let stack = UIStackView(arrangedSubviews: [topRow, detailLabel])
stack.axis = .vertical
stack.spacing = AppSpacing.sm
cardView.addSubview(stack)
contentView.addSubview(cardView)
cardView.snp.makeConstraints { make in
make.top.bottom.equalToSuperview().inset(5)
make.leading.trailing.equalToSuperview().inset(AppSpacing.screenHorizontalInset)
}
stack.snp.makeConstraints { make in
make.edges.equalToSuperview().inset(AppSpacing.md)
}
}
}
/// 日清页在指定营业日无记录时展示的空状态单元格。
final class OfflineCollectionEmptyCell: UITableViewCell {
static let reuseIdentifier = "OfflineCollectionEmptyCell"
var onRegister: (() -> Void)?
private let titleLabel = UILabel()
private let detailLabel = UILabel()
private let registerButton = UIButton(type: .system)
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupUI()
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
/// 更新空状态,仅今日允许前往登记。
func apply(isToday: Bool) {
registerButton.isHidden = !isToday
}
override func prepareForReuse() {
super.prepareForReuse()
onRegister = nil
}
private func setupUI() {
selectionStyle = .none
backgroundColor = .clear
contentView.backgroundColor = .clear
titleLabel.text = "当日暂无线下收款记录"
titleLabel.font = .systemFont(ofSize: 16, weight: .semibold)
titleLabel.textColor = AppColor.textPrimary
titleLabel.textAlignment = .center
detailLabel.text = "完成线下收款后,请及时登记"
detailLabel.font = .systemFont(ofSize: 13)
detailLabel.textColor = AppColor.textSecondary
detailLabel.textAlignment = .center
registerButton.setTitle("去登记", for: .normal)
registerButton.titleLabel?.font = .systemFont(ofSize: 14, weight: .semibold)
registerButton.addTarget(self, action: #selector(registerTapped), for: .touchUpInside)
let stack = UIStackView(arrangedSubviews: [titleLabel, detailLabel, registerButton])
stack.axis = .vertical
stack.spacing = AppSpacing.sm
stack.alignment = .center
contentView.addSubview(stack)
stack.snp.makeConstraints { make in
make.top.equalToSuperview().inset(48)
make.leading.trailing.equalToSuperview().inset(AppSpacing.screenHorizontalInset)
make.bottom.equalToSuperview().inset(32)
}
}
@objc private func registerTapped() {
onRegister?()
}
}
private extension Collection {
/// 安全按下标读取元素,超出范围时返回 nil。
subscript(safe index: Index) -> Element? {
indices.contains(index) ? self[index] : nil
}
}
@@ -0,0 +1,340 @@
//
// OfflineCollectionRegistrationViewController.swift
// suixinkan
//
import SnapKit
import UIKit
/// 线下收款登记页,仅收集金额和线下收款方式。
final class OfflineCollectionRegistrationViewController: BaseViewController {
private let viewModel: OfflineCollectionRegistrationViewModel
private let scrollView = UIScrollView()
private let contentStack = UIStackView()
private let amountCard = UIView()
private let amountField = UITextField()
private let methodCard = UIView()
private let methodStack = UIStackView()
private var methodButtons: [OfflineCollectionPaymentMethod: OfflinePaymentMethodButton] = [:]
private let explanationCard = UIView()
private let contextLabel = UILabel()
private let bottomContainer = UIView()
private let submitButton = UIButton(type: .system)
private var hasFocusedAmountField = false
/// 使用指定的收款上下文与 Mock 服务创建登记页。
init(
context: OfflineCollectionContext = .current(),
service: OfflineCollectionMockService = .shared
) {
viewModel = OfflineCollectionRegistrationViewModel(context: context, service: service)
super.init(nibName: nil, bundle: nil)
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func setupNavigationBar() {
title = "线下收款登记"
}
override func setupUI() {
view.backgroundColor = AppColor.pageBackground
scrollView.keyboardDismissMode = .interactive
scrollView.alwaysBounceVertical = true
contentStack.axis = .vertical
contentStack.spacing = AppSpacing.md
configureCard(amountCard)
let amountTitleLabel = makeTitleLabel("收款金额")
let currencyLabel = UILabel()
currencyLabel.text = "¥"
currencyLabel.font = .systemFont(ofSize: 34, weight: .semibold)
currencyLabel.textColor = AppColor.textPrimary
amountField.placeholder = "0.00"
amountField.font = .systemFont(ofSize: 38, weight: .bold)
amountField.textColor = AppColor.textPrimary
amountField.keyboardType = .decimalPad
amountField.clearButtonMode = .whileEditing
amountField.delegate = self
amountField.accessibilityLabel = "收款金额"
let amountHelperLabel = UILabel()
amountHelperLabel.text = "单笔金额 0.01~99,999.99 元,最多两位小数"
amountHelperLabel.font = .systemFont(ofSize: 12)
amountHelperLabel.textColor = AppColor.textTertiary
let amountRow = UIStackView(arrangedSubviews: [currencyLabel, amountField])
amountRow.axis = .horizontal
amountRow.alignment = .center
amountRow.spacing = AppSpacing.sm
let amountStack = UIStackView(arrangedSubviews: [amountTitleLabel, amountRow, amountHelperLabel])
amountStack.axis = .vertical
amountStack.spacing = AppSpacing.sm
amountCard.addSubview(amountStack)
amountStack.snp.makeConstraints { make in
make.edges.equalToSuperview().inset(AppSpacing.lg)
}
amountField.snp.makeConstraints { make in
make.height.equalTo(58)
}
configureCard(methodCard)
let methodTitleLabel = makeTitleLabel("收款方式")
methodStack.axis = .horizontal
methodStack.distribution = .fillEqually
methodStack.spacing = AppSpacing.sm
for method in OfflineCollectionPaymentMethod.allCases {
let button = OfflinePaymentMethodButton(method: method)
button.addTarget(self, action: #selector(methodTapped(_:)), for: .touchUpInside)
methodButtons[method] = button
methodStack.addArrangedSubview(button)
}
let methodContentStack = UIStackView(arrangedSubviews: [methodTitleLabel, methodStack])
methodContentStack.axis = .vertical
methodContentStack.spacing = AppSpacing.md
methodCard.addSubview(methodContentStack)
methodContentStack.snp.makeConstraints { make in
make.edges.equalToSuperview().inset(AppSpacing.lg)
}
methodStack.snp.makeConstraints { make in
make.height.equalTo(64)
}
configureCard(explanationCard)
let explanationIcon = UIImageView(image: UIImage(systemName: "info.circle.fill"))
explanationIcon.tintColor = AppColor.primary
explanationIcon.contentMode = .scaleAspectFit
let explanationLabel = UILabel()
explanationLabel.text = "登记后将计入今日待补缴,不生成订单。"
explanationLabel.font = .systemFont(ofSize: 14)
explanationLabel.textColor = AppColor.textSecondary
explanationLabel.numberOfLines = 0
explanationCard.addSubview(explanationIcon)
explanationCard.addSubview(explanationLabel)
explanationIcon.snp.makeConstraints { make in
make.leading.top.equalToSuperview().inset(AppSpacing.md)
make.width.height.equalTo(20)
}
explanationLabel.snp.makeConstraints { make in
make.leading.equalTo(explanationIcon.snp.trailing).offset(AppSpacing.sm)
make.trailing.bottom.equalToSuperview().inset(AppSpacing.md)
make.top.equalToSuperview().inset(AppSpacing.md)
}
contextLabel.text = "当前:\(viewModel.context.collectorName) · \(viewModel.context.storeName) · \(viewModel.context.scenicName)"
contextLabel.font = .systemFont(ofSize: 12)
contextLabel.textColor = AppColor.textTertiary
contextLabel.numberOfLines = 0
contextLabel.textAlignment = .center
bottomContainer.backgroundColor = .white
bottomContainer.layer.shadowColor = UIColor.black.cgColor
bottomContainer.layer.shadowOpacity = 0.06
bottomContainer.layer.shadowRadius = 8
bottomContainer.layer.shadowOffset = CGSize(width: 0, height: -2)
var submitConfiguration = UIButton.Configuration.filled()
submitConfiguration.title = "确认登记"
submitConfiguration.baseBackgroundColor = AppColor.primary
submitConfiguration.baseForegroundColor = .white
submitConfiguration.cornerStyle = .medium
submitConfiguration.titleTextAttributesTransformer = UIConfigurationTextAttributesTransformer { incoming in
var outgoing = incoming
outgoing.font = .systemFont(ofSize: 17, weight: .semibold)
return outgoing
}
submitButton.configuration = submitConfiguration
submitButton.accessibilityIdentifier = "offlineCollection.submit"
view.addSubview(scrollView)
scrollView.addSubview(contentStack)
[amountCard, methodCard, explanationCard, contextLabel].forEach(contentStack.addArrangedSubview)
view.addSubview(bottomContainer)
bottomContainer.addSubview(submitButton)
}
override func setupConstraints() {
bottomContainer.snp.makeConstraints { make in
make.leading.trailing.equalToSuperview()
make.bottom.equalTo(view.keyboardLayoutGuide.snp.top)
}
submitButton.snp.makeConstraints { make in
make.top.equalToSuperview().inset(AppSpacing.md)
make.leading.trailing.equalToSuperview().inset(AppSpacing.screenHorizontalInset)
make.height.equalTo(50)
make.bottom.equalToSuperview().inset(AppSpacing.md)
}
scrollView.snp.makeConstraints { make in
make.top.leading.trailing.equalTo(view.safeAreaLayoutGuide)
make.bottom.equalTo(bottomContainer.snp.top)
}
contentStack.snp.makeConstraints { make in
make.edges.equalToSuperview().inset(AppSpacing.screenHorizontalInset)
make.width.equalTo(scrollView.snp.width).offset(-AppSpacing.screenHorizontalInset * 2)
}
}
override func bindActions() {
amountField.addTarget(self, action: #selector(amountChanged), for: .editingChanged)
submitButton.addTarget(self, action: #selector(submitTapped), for: .touchUpInside)
viewModel.onStateChange = { [weak self] in
Task { @MainActor in self?.applyViewModel() }
}
viewModel.onShowMessage = { [weak self] message in
Task { @MainActor in self?.showToast(message) }
}
viewModel.onRegistrationSuccess = { [weak self] receipt in
Task { @MainActor in self?.showRegistrationSuccess(receipt) }
}
}
override func viewDidLoad() {
super.viewDidLoad()
applyViewModel()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
guard !hasFocusedAmountField else { return }
hasFocusedAmountField = true
amountField.becomeFirstResponder()
}
@MainActor
private func applyViewModel() {
if amountField.text != viewModel.amountText {
amountField.text = viewModel.amountText
}
methodButtons.forEach { method, button in
button.setSelected(method == viewModel.paymentMethod)
}
submitButton.isEnabled = viewModel.canSubmit
submitButton.configuration?.showsActivityIndicator = viewModel.isSubmitting
submitButton.configuration?.title = viewModel.isSubmitting ? "登记中" : "确认登记"
submitButton.alpha = submitButton.isEnabled || viewModel.isSubmitting ? 1 : 0.45
}
@MainActor
private func showRegistrationSuccess(_ receipt: OfflineCollectionRegistrationReceipt) {
amountField.resignFirstResponder()
let message = """
本次登记 \(OfflineCollectionMoney.display(receipt.record.amountFen))
今日累计线下收款 \(OfflineCollectionMoney.display(receipt.summary.totalAmountFen))
今日待补缴 \(OfflineCollectionMoney.display(receipt.summary.pendingAmountFen))
"""
let alert = UIAlertController(title: "登记成功", message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "继续登记", style: .default) { [weak self] _ in
self?.viewModel.startAnotherRegistration()
self?.amountField.becomeFirstResponder()
})
alert.addAction(UIAlertAction(title: "查看今日明细", style: .default) { [weak self] _ in
guard let self else { return }
let controller = OfflineCollectionDailyViewController(
businessDate: receipt.summary.businessDate,
context: self.viewModel.context,
service: self.viewModel.service
)
self.navigationController?.pushViewController(controller, animated: true)
})
present(alert, animated: true)
}
private func makeTitleLabel(_ text: String) -> UILabel {
let label = UILabel()
label.text = text
label.font = .systemFont(ofSize: 16, weight: .semibold)
label.textColor = AppColor.textPrimary
return label
}
private func configureCard(_ card: UIView) {
card.backgroundColor = .white
card.layer.cornerRadius = AppRadius.lg
}
@objc private func amountChanged() {
viewModel.updateAmount(amountField.text ?? "")
}
@objc private func methodTapped(_ sender: OfflinePaymentMethodButton) {
viewModel.selectPaymentMethod(sender.method)
}
@objc private func submitTapped() {
Task { await viewModel.submit() }
}
}
extension OfflineCollectionRegistrationViewController: UITextFieldDelegate {
func textField(
_ textField: UITextField,
shouldChangeCharactersIn range: NSRange,
replacementString string: String
) -> Bool {
guard let current = textField.text,
let textRange = Range(range, in: current) else { return false }
let next = current.replacingCharacters(in: textRange, with: string)
return OfflineCollectionMoney.acceptsEditingText(next)
}
}
/// 登记页的收款方式单选按钮。
final class OfflinePaymentMethodButton: UIControl {
let method: OfflineCollectionPaymentMethod
private let iconView = UIImageView()
private let titleLabel = UILabel()
/// 使用指定收款方式创建单选按钮。
init(method: OfflineCollectionPaymentMethod) {
self.method = method
super.init(frame: .zero)
setupUI()
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
/// 更新选中态视觉。
func setSelected(_ selected: Bool) {
isSelected = selected
backgroundColor = selected ? UIColor(hex: 0xEEF5FF) : AppColor.inputBackground
layer.borderColor = (selected ? AppColor.primary : UIColor.clear).cgColor
layer.borderWidth = selected ? 1.5 : 0
titleLabel.textColor = selected ? AppColor.primary : AppColor.textSecondary
}
private func setupUI() {
layer.cornerRadius = AppRadius.md
clipsToBounds = true
accessibilityLabel = method.displayName
iconView.image = UIImage(named: method.assetName)?.withRenderingMode(.alwaysOriginal)
iconView.contentMode = .scaleAspectFit
titleLabel.text = method.displayName
titleLabel.font = .systemFont(ofSize: 14, weight: .medium)
titleLabel.textAlignment = .center
let stack = UIStackView(arrangedSubviews: [iconView, titleLabel])
stack.axis = .vertical
stack.alignment = .center
stack.spacing = 5
stack.isUserInteractionEnabled = false
addSubview(stack)
stack.snp.makeConstraints { make in
make.center.equalToSuperview()
}
iconView.snp.makeConstraints { make in
make.width.height.equalTo(22)
}
setSelected(false)
}
}
@@ -0,0 +1,245 @@
//
// OfflineCollectionHomeView.swift
// suixinkan
//
import SnapKit
import UIKit
/// 收款首页中的线下收款入口、今日汇总和历史逾期提醒区域。
final class OfflineCollectionHomeView: UIView {
var onRegister: (() -> Void)?
var onOpenToday: (() -> Void)?
var onOpenOverdue: (() -> Void)?
private let contentStack = UIStackView()
private let sectionTitleLabel = UILabel()
private let overdueControl = UIControl()
private let overdueTitleLabel = UILabel()
private let overdueDetailLabel = UILabel()
private let overdueActionLabel = UILabel()
private let registerControl = UIControl()
private let registerIconView = UIImageView()
private let registerTitleLabel = UILabel()
private let registerSubtitleLabel = UILabel()
private let registerActionLabel = UILabel()
private let summaryControl = UIControl()
private let summaryTitleLabel = UILabel()
private let summaryAmountLabel = UILabel()
private let summaryCountLabel = UILabel()
private let summaryRegisteredLabel = UILabel()
private let summaryStatusLabel = UILabel()
private let summaryActionLabel = UILabel()
override init(frame: CGRect) {
super.init(frame: frame)
setupUI()
setupConstraints()
bindActions()
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
/// 根据今日与历史逾期汇总刷新页面内容。
func apply(
today: OfflineDailySummary,
overdueDayCount: Int,
overdueRecordCount: Int,
overdueAmountFen: Int
) {
let hasOverdue = overdueDayCount > 0 && overdueAmountFen > 0
overdueControl.isHidden = !hasOverdue
overdueDetailLabel.text = "\(overdueDayCount) 个营业日,共 \(overdueRecordCount) 笔,待补缴 \(OfflineCollectionMoney.display(overdueAmountFen))"
summaryAmountLabel.text = OfflineCollectionMoney.display(today.pendingAmountFen)
summaryCountLabel.text = "\(today.pendingCount) 笔线下收款"
summaryRegisteredLabel.text = "今日已登记 \(OfflineCollectionMoney.display(today.totalAmountFen))"
let isSettled = today.totalCount > 0 && today.pendingAmountFen == 0
summaryStatusLabel.text = isSettled ? "今日已结清" : ""
summaryStatusLabel.isHidden = !isSettled
summaryActionLabel.text = today.pendingAmountFen > 0 ? "去补缴" : "查看明细"
summaryActionLabel.backgroundColor = today.pendingAmountFen > 0
? AppColor.primary
: UIColor(hex: 0xEEF2F7)
summaryActionLabel.textColor = today.pendingAmountFen > 0
? .white
: AppColor.textSecondary
}
private func setupUI() {
backgroundColor = .clear
contentStack.axis = .vertical
contentStack.spacing = AppSpacing.md
sectionTitleLabel.text = "线下收款"
sectionTitleLabel.font = .systemFont(ofSize: 16, weight: .semibold)
sectionTitleLabel.textColor = AppColor.textPrimary
configureCard(overdueControl, backgroundColor: UIColor(hex: 0xFFF1F0))
overdueControl.layer.borderColor = UIColor(hex: 0xFFCCC7).cgColor
overdueControl.layer.borderWidth = 1
overdueTitleLabel.text = "存在逾期未补缴"
overdueTitleLabel.font = .systemFont(ofSize: 15, weight: .semibold)
overdueTitleLabel.textColor = AppColor.danger
overdueDetailLabel.font = .systemFont(ofSize: 13)
overdueDetailLabel.textColor = UIColor(hex: 0x8C2F27)
overdueDetailLabel.numberOfLines = 0
configureActionLabel(overdueActionLabel, title: "立即处理", background: AppColor.danger, foreground: .white)
configureCard(registerControl, backgroundColor: .white)
registerIconView.image = UIImage(systemName: "plus.circle.fill")
registerIconView.tintColor = AppColor.primary
registerIconView.contentMode = .scaleAspectFit
registerTitleLabel.text = "线下收款登记"
registerTitleLabel.font = .systemFont(ofSize: 16, weight: .semibold)
registerTitleLabel.textColor = AppColor.textPrimary
registerSubtitleLabel.text = "线下收款后,请及时登记并在当日完成补缴"
registerSubtitleLabel.font = .systemFont(ofSize: 13)
registerSubtitleLabel.textColor = AppColor.textSecondary
registerSubtitleLabel.numberOfLines = 0
configureActionLabel(registerActionLabel, title: "去登记", background: UIColor(hex: 0xEEF5FF), foreground: AppColor.primary)
configureCard(summaryControl, backgroundColor: .white)
summaryTitleLabel.text = "今日待补缴"
summaryTitleLabel.font = .systemFont(ofSize: 15, weight: .semibold)
summaryTitleLabel.textColor = AppColor.textPrimary
summaryAmountLabel.font = .systemFont(ofSize: 30, weight: .bold)
summaryAmountLabel.textColor = AppColor.textPrimary
summaryCountLabel.font = .systemFont(ofSize: 13)
summaryCountLabel.textColor = UIColor(hex: 0xD97706)
summaryRegisteredLabel.font = .systemFont(ofSize: 13)
summaryRegisteredLabel.textColor = AppColor.textSecondary
summaryStatusLabel.font = .systemFont(ofSize: 13, weight: .semibold)
summaryStatusLabel.textColor = AppColor.success
configureActionLabel(summaryActionLabel, title: "查看明细", background: UIColor(hex: 0xEEF2F7), foreground: AppColor.textSecondary)
addSubview(contentStack)
[sectionTitleLabel, overdueControl, registerControl, summaryControl].forEach(contentStack.addArrangedSubview)
let overdueTextStack = UIStackView(arrangedSubviews: [overdueTitleLabel, overdueDetailLabel])
overdueTextStack.axis = .vertical
overdueTextStack.spacing = 5
overdueControl.addSubview(overdueTextStack)
overdueControl.addSubview(overdueActionLabel)
let registerTextStack = UIStackView(arrangedSubviews: [registerTitleLabel, registerSubtitleLabel])
registerTextStack.axis = .vertical
registerTextStack.spacing = 5
registerControl.addSubview(registerIconView)
registerControl.addSubview(registerTextStack)
registerControl.addSubview(registerActionLabel)
let summaryTextStack = UIStackView(arrangedSubviews: [
summaryTitleLabel,
summaryAmountLabel,
summaryCountLabel,
summaryRegisteredLabel,
summaryStatusLabel,
])
summaryTextStack.axis = .vertical
summaryTextStack.spacing = 6
summaryControl.addSubview(summaryTextStack)
summaryControl.addSubview(summaryActionLabel)
}
private func setupConstraints() {
contentStack.snp.makeConstraints { make in
make.edges.equalToSuperview()
}
sectionTitleLabel.snp.makeConstraints { make in
make.height.equalTo(24)
}
overdueControl.snp.makeConstraints { make in
make.height.greaterThanOrEqualTo(84)
}
overdueControl.subviews.first?.snp.makeConstraints { make in
make.leading.top.bottom.equalToSuperview().inset(AppSpacing.md)
make.trailing.lessThanOrEqualTo(overdueActionLabel.snp.leading).offset(-AppSpacing.sm)
}
overdueActionLabel.snp.makeConstraints { make in
make.trailing.equalToSuperview().inset(AppSpacing.md)
make.centerY.equalToSuperview()
make.height.equalTo(32)
}
registerControl.snp.makeConstraints { make in
make.height.greaterThanOrEqualTo(96)
}
registerIconView.snp.makeConstraints { make in
make.leading.equalToSuperview().inset(AppSpacing.md)
make.centerY.equalToSuperview()
make.width.height.equalTo(34)
}
registerTitleLabel.superview?.snp.makeConstraints { make in
make.leading.equalTo(registerIconView.snp.trailing).offset(AppSpacing.sm)
make.centerY.equalToSuperview()
make.trailing.lessThanOrEqualTo(registerActionLabel.snp.leading).offset(-AppSpacing.sm)
}
registerActionLabel.snp.makeConstraints { make in
make.trailing.equalToSuperview().inset(AppSpacing.md)
make.centerY.equalToSuperview()
make.height.equalTo(32)
}
summaryControl.snp.makeConstraints { make in
make.height.greaterThanOrEqualTo(180)
}
summaryTitleLabel.superview?.snp.makeConstraints { make in
make.leading.top.bottom.equalToSuperview().inset(AppSpacing.md)
make.trailing.lessThanOrEqualTo(summaryActionLabel.snp.leading).offset(-AppSpacing.sm)
}
summaryActionLabel.snp.makeConstraints { make in
make.trailing.equalToSuperview().inset(AppSpacing.md)
make.bottom.equalToSuperview().inset(AppSpacing.md)
make.height.equalTo(34)
}
}
private func bindActions() {
overdueControl.addTarget(self, action: #selector(overdueTapped), for: .touchUpInside)
registerControl.addTarget(self, action: #selector(registerTapped), for: .touchUpInside)
summaryControl.addTarget(self, action: #selector(summaryTapped), for: .touchUpInside)
}
private func configureCard(_ control: UIControl, backgroundColor: UIColor) {
control.backgroundColor = backgroundColor
control.layer.cornerRadius = AppRadius.lg
control.clipsToBounds = true
}
private func configureActionLabel(
_ label: UILabel,
title: String,
background: UIColor,
foreground: UIColor
) {
label.text = " \(title) "
label.font = .systemFont(ofSize: 13, weight: .semibold)
label.textAlignment = .center
label.textColor = foreground
label.backgroundColor = background
label.layer.cornerRadius = 16
label.clipsToBounds = true
label.setContentCompressionResistancePriority(.required, for: .horizontal)
}
@objc private func overdueTapped() {
onOpenOverdue?()
}
@objc private func registerTapped() {
onRegister?()
}
@objc private func summaryTapped() {
onOpenToday?()
}
}