Files
suixinkan_ios_uikit/suixinkan_ios/Features/Wallet/ViewControllers/WalletViewControllers.swift
2026-06-26 14:33:31 +08:00

133 lines
4.7 KiB
Swift

//
// WalletViewControllers.swift
// suixinkan
//
// Created by Codex on 2026/6/26.
//
import UIKit
///
final class WalletViewController: ModuleTableViewController {
private let viewModel = WalletViewModel()
private let summaryLabel = UILabel()
override func viewDidLoad() {
title = "我的钱包"
navigationItem.rightBarButtonItem = UIBarButtonItem(
title: "提现",
style: .plain,
target: self,
action: #selector(openWithdraw)
)
super.viewDidLoad()
setupHeader()
wireViewModel(viewModel) { [weak self] in self?.updateSummary() }
}
private func setupHeader() {
summaryLabel.numberOfLines = 0
summaryLabel.font = .systemFont(ofSize: 14)
summaryLabel.textAlignment = .center
summaryLabel.textColor = AppDesign.textSecondary
summaryLabel.frame = CGRect(x: 0, y: 0, width: view.bounds.width, height: 72)
tableView.tableHeaderView = summaryLabel
}
override func tableRowCount() -> Int {
switch viewModel.selectedTab {
case .earnings:
return viewModel.earningsGroups.flatMap(\.items).count
case .withdraws:
return viewModel.withdrawRecords.count
}
}
override func configureCell(_ cell: TitleSubtitleTableViewCell, at indexPath: IndexPath) {
switch viewModel.selectedTab {
case .earnings:
let items = viewModel.earningsGroups.flatMap(\.items)
let item = items[indexPath.row]
cell.configure(title: item.typeLabel, subtitle: item.amount, detail: item.createdAt)
case .withdraws:
let record = viewModel.withdrawRecords[indexPath.row]
cell.configure(title: "¥\(record.amount)", subtitle: record.statusLabel, detail: record.createdAt)
}
}
override func reloadContent() async {
await viewModel.loadInitial(api: services.walletAPI, staffId: services.staffId)
updateSummary()
}
private func updateSummary() {
summaryLabel.text = "\(viewModel.withdrawableText) · \(viewModel.totalAmountText)"
navigationItem.rightBarButtonItem?.title = viewModel.selectedTab == .earnings ? "提现记录" : "收益明细"
}
@objc private func openWithdraw() {
navigationController?.pushViewController(WalletWithdrawViewController(), animated: true)
}
}
extension WalletViewModel: ViewModelBindable {}
///
final class WalletWithdrawViewController: ModuleTableViewController {
private let viewModel = WithdrawApplyViewModel()
private let amountField = UITextField()
private let smsField = UITextField()
override func viewDidLoad() {
title = "申请提现"
navigationItem.rightBarButtonItems = [
UIBarButtonItem(title: "提交", style: .done, target: self, action: #selector(submit)),
UIBarButtonItem(title: "验证码", style: .plain, target: self, action: #selector(sendSms))
]
super.viewDidLoad()
amountField.placeholder = "提现金额"
amountField.borderStyle = .roundedRect
amountField.keyboardType = .decimalPad
smsField.placeholder = "短信验证码"
smsField.borderStyle = .roundedRect
let stack = UIStackView(arrangedSubviews: [amountField, smsField])
stack.axis = .vertical
stack.spacing = 8
stack.frame = CGRect(x: 0, y: 0, width: view.bounds.width, height: 96)
stack.layoutMargins = UIEdgeInsets(top: 12, left: 16, bottom: 12, right: 16)
stack.isLayoutMarginsRelativeArrangement = true
tableView.tableHeaderView = stack
wireViewModel(viewModel) { }
}
override func tableRowCount() -> Int { 1 }
override func configureCell(_ cell: TitleSubtitleTableViewCell, at indexPath: IndexPath) {
let amount = viewModel.info?.amountWithdrawable ?? "0"
cell.configure(title: "可提现", subtitle: "¥ \(amount)")
}
override func reloadContent() async {
await viewModel.load(api: services.walletAPI)
}
@objc private func sendSms() {
Task { await viewModel.sendSms(api: services.walletAPI) }
}
@objc private func submit() {
viewModel.amountText = amountField.text ?? ""
viewModel.smsCode = smsField.text ?? ""
Task {
if await viewModel.submit(api: services.walletAPI) {
services.toastCenter.show("提现申请已提交")
navigationController?.popViewController(animated: true)
} else if let message = viewModel.errorMessage {
services.toastCenter.show(message)
}
}
}
}
extension WithdrawApplyViewModel: ViewModelBindable {}