209 lines
9.0 KiB
Swift
209 lines
9.0 KiB
Swift
//
|
||
// VipRechargeVC.swift
|
||
// QuickLocation
|
||
//
|
||
// Created by 八条 on 2026/6/3.
|
||
//
|
||
|
||
import UIKit
|
||
import RxSwift
|
||
import RxCocoa
|
||
import RxDataSources
|
||
|
||
class VipRechargeVC: BaseViewController {
|
||
|
||
fileprivate var rootView: VipRechargeView!
|
||
|
||
override func loadView() {
|
||
rootView = VipRechargeView(frame: UIScreen.main.bounds)
|
||
view = rootView
|
||
}
|
||
|
||
private var viewModel = VipRechargeVM()
|
||
|
||
override func viewDidLoad() {
|
||
super.viewDidLoad()
|
||
fd_interactivePopDisabled = true
|
||
bindViewModel()
|
||
reactiveAction()
|
||
requestRechargeInfo()
|
||
|
||
NotificationCenter.default.rx.notification(.RequestOrderPayStatusNotification)
|
||
.subscribe(onNext: { [weak self] _ in
|
||
self?.handlePaymentSuccess()
|
||
}).disposed(by: disposeBag)
|
||
}
|
||
|
||
private func handlePaymentSuccess() {
|
||
UserService.userInfo().subscribe { [weak self] response in
|
||
if let model = response.model {
|
||
AppContextManager.shared.saveAccount(model)
|
||
}
|
||
self?.showPayResultPop(showCloseBtn: false,
|
||
status: true,
|
||
title: "支付成功",
|
||
message: "恭喜您!成功开通\(self?.viewModel.goodsName ?? "")",
|
||
confirmText: "确定", confirmBlock: {
|
||
AppRouter.shared.popOrDismiss()
|
||
}, cancelBlock: { })
|
||
}.disposed(by: disposeBag)
|
||
}
|
||
|
||
override func viewWillDisappear(_ animated: Bool) {
|
||
super.viewWillDisappear(animated)
|
||
navigationController?.setNavigationBarHidden(true, animated: false)
|
||
}
|
||
|
||
private func bindViewModel() {
|
||
viewModel.output.sectionedItems
|
||
.bind(to: rootView.expenseCollectionView.rx.items(dataSource: dataSource))
|
||
.disposed(by: disposeBag)
|
||
|
||
viewModel.output.vipRightItems
|
||
.bind(to: rootView.vipRightsCollectionView.rx.items(dataSource: vipRightsDataSource))
|
||
.disposed(by: disposeBag)
|
||
|
||
viewModel.createCount.asObservable()
|
||
.bind(to: rootView.createCountLab.rx.text)
|
||
.disposed(by: disposeBag)
|
||
|
||
viewModel.groupCount.asObservable()
|
||
.bind(to: rootView.groupCountLab.rx.text)
|
||
.disposed(by: disposeBag)
|
||
|
||
viewModel.joinCount.asObservable()
|
||
.bind(to: rootView.joinCountLab.rx.text)
|
||
.disposed(by: disposeBag)
|
||
}
|
||
|
||
private func reactiveAction() {
|
||
rootView.payBtnView.rx.tapGesture.subscribe(onNext: { _ in
|
||
self.requestOrderPayParams()
|
||
}).disposed(by: disposeBag)
|
||
|
||
Observable.zip(
|
||
rootView.expenseCollectionView.rx.itemSelected,
|
||
rootView.expenseCollectionView.rx.modelSelected(VipExpenseModel.self)
|
||
).subscribe(onNext: { indexPath, model in
|
||
self.viewModel.selectedIndex = indexPath.row
|
||
self.rootView.setupPayTypes(model.pay_type)
|
||
self.rootView.animatePrice(to: model.price)
|
||
self.rootView.discountLab.text = self.viewModel.discountPriceString
|
||
})
|
||
.disposed(by: disposeBag)
|
||
}
|
||
|
||
// MARK: - dataSource
|
||
private lazy var dataSource: RxCollectionViewSectionedReloadDataSource<ExpenseListSectionModel> = {
|
||
RxCollectionViewSectionedReloadDataSource<ExpenseListSectionModel> { datasource, collectionView, indexPath, model in
|
||
let cell: ExpenseCell = collectionView.dequeueReusableCell(for: indexPath)
|
||
cell.configure(model: model, isSelected: self.viewModel.selectedIndex == indexPath.row)
|
||
return cell
|
||
}
|
||
}()
|
||
|
||
private lazy var vipRightsDataSource: RxCollectionViewSectionedReloadDataSource<VipRightsListSectionModel> = {
|
||
RxCollectionViewSectionedReloadDataSource<VipRightsListSectionModel> { datasource, collectionView, indexPath, model in
|
||
let cell: VipRightsCell = collectionView.dequeueReusableCell(for: indexPath)
|
||
cell.configure(model)
|
||
return cell
|
||
}
|
||
}()
|
||
|
||
// MARK: - API
|
||
private func requestRechargeInfo() {
|
||
DLToast.showLoading()
|
||
OrderService.rechargeInfo(type: "member").subscribe(onNext: { [weak self] response in
|
||
guard let self = self else { return }
|
||
self.viewModel.loadData(list: response.list)
|
||
self.rootView.setupPayTypes(self.viewModel.payType)
|
||
self.rootView.animatePrice(to: self.viewModel.price)
|
||
self.rootView.discountLab.text = self.viewModel.discountPriceString
|
||
}).disposed(by: disposeBag)
|
||
}
|
||
|
||
// MARK: - API支付参数
|
||
private func requestOrderPayParams() {
|
||
let types = viewModel.payType.components(separatedBy: ",").map { $0.trimmingCharacters(in: .whitespaces) }
|
||
let payType = types[rootView.selectedPayTypeTag]
|
||
|
||
// if payType == "apple" {
|
||
// IAPManager.shared.purchase(goodsId: viewModel.goodsId) { [weak self] success, msg in
|
||
// guard let self = self else { return }
|
||
// if success {
|
||
// self.showPayResultPop(showCloseBtn: false,
|
||
// status: true,
|
||
// title: "支付成功",
|
||
// message: "恭喜您!成功开通\(self.viewModel.goodsName)",
|
||
// confirmText: "确定", confirmBlock: {
|
||
// AppRouter.shared.popOrDismiss()
|
||
// }, cancelBlock: { })
|
||
// } else {
|
||
// self.showPayResultPop(status: false,
|
||
// title: "支付失败",
|
||
// message: msg ?? "很抱歉,请您重新支付!",
|
||
// confirmText: "重新支付", confirmBlock: { },
|
||
// cancelText: "取消", cancelBlock: {
|
||
// AppRouter.shared.popOrDismiss()
|
||
// })
|
||
// }
|
||
// }
|
||
// return
|
||
// }
|
||
|
||
DLToast.showLoading()
|
||
OrderService.orderPayParams(goodsId: viewModel.goodsId,
|
||
payType: types[rootView.selectedPayTypeTag]).subscribe(onNext: { response in
|
||
guard let data = response.data else { return }
|
||
|
||
let payType = types[self.rootView.selectedPayTypeTag]
|
||
if payType == "weixin" { // 微信
|
||
guard let partnerId = data["partnerId"] as? String,
|
||
let prepayId = data["prepayId"] as? String,
|
||
let package = data["package"] as? String,
|
||
let nonceStr = data["nonceStr"] as? String,
|
||
let timeStamp = data["timeStamp"] as? String,
|
||
let sign = data["sign"] as? String else { return }
|
||
let request: PayReq = PayReq()
|
||
request.partnerId = partnerId
|
||
request.prepayId = prepayId
|
||
request.package = package
|
||
request.nonceStr = nonceStr
|
||
request.timeStamp = UInt32(timeStamp) ?? 0
|
||
request.sign = sign
|
||
WXApi.send(request)
|
||
}
|
||
else if payType == "alipay" { // 支付宝
|
||
if let payParam = data["payParam"] as? String, let appId = data["appId"] as? String {
|
||
AlipaySDK.defaultService().payOrder(payParam, fromScheme: "Alipay\(appId)") { resultDic in
|
||
// print("支付宝callback -> \(resultDic)")
|
||
guard let result = resultDic,
|
||
let resultStatus = result["resultStatus"] as? String else { return }
|
||
/**
|
||
9000 订单支付成功。
|
||
8000 正在处理中,支付结果未知(有可能已经支付成功),请查询商家订单列表中订单的支付状态。
|
||
4000 订单支付失败。
|
||
5000 重复请求。
|
||
6001 用户中途取消。
|
||
6002 网络连接出错。
|
||
6004 支付结果未知(有可能已经支付成功),请查询商家订单列表中订单的支付状态。
|
||
其它 其它支付错误。
|
||
*/
|
||
if resultStatus == "9000" {
|
||
self.handlePaymentSuccess()
|
||
}
|
||
else {
|
||
self.showPayResultPop(status: false,
|
||
title: "支付失败",
|
||
message: "很抱歉,请您重新支付!",
|
||
confirmText: "重新支付", confirmBlock: { }, cancelText: "取消", cancelBlock: {
|
||
AppRouter.shared.popOrDismiss()
|
||
})
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}, onError: { _ in }).disposed(by: disposeBag)
|
||
}
|
||
}
|