244 lines
9.9 KiB
Swift
244 lines
9.9 KiB
Swift
//
|
||
// PromotionalActivitiesVC.swift
|
||
// QuickLocation
|
||
//
|
||
// Created by 八条 on 2026/7/3.
|
||
//
|
||
|
||
import UIKit
|
||
import RxSwift
|
||
import RxCocoa
|
||
import RxDataSources
|
||
|
||
extension PromotionalActivitiesItem: Equatable {
|
||
static func == (lhs: Self, rhs: Self) -> Bool {
|
||
switch (lhs, rhs) {
|
||
case (.recommend(let a), .recommend(let b)): return a == b
|
||
case (.normal(let a), .normal(let b)): return a == b
|
||
default: return false
|
||
}
|
||
}
|
||
}
|
||
|
||
extension PromotionalActivitiesItem: IdentifiableType {
|
||
typealias Identity = String
|
||
var identity: String {
|
||
switch self {
|
||
case .recommend(let model): return model.goods_id
|
||
case .normal(let model): return model.goods_id
|
||
}
|
||
}
|
||
}
|
||
|
||
class PromotionalActivitiesVC: BaseViewController {
|
||
|
||
fileprivate var rootView: PromotionalActivitiesView!
|
||
var onDismiss: (() -> Void)?
|
||
private var selectedGoodsId: String = ""
|
||
private let itemsRelay = BehaviorRelay<[PromotionalActivitiesItem]>(value: [])
|
||
|
||
var items: [PromotionalActivitiesItem] {
|
||
get { itemsRelay.value }
|
||
set { itemsRelay.accept(newValue) }
|
||
}
|
||
|
||
override func loadView() {
|
||
rootView = PromotionalActivitiesView(frame: UIScreen.main.bounds)
|
||
view = rootView
|
||
}
|
||
|
||
override func viewDidLoad() {
|
||
super.viewDidLoad()
|
||
fd_interactivePopDisabled = true
|
||
setupTableView()
|
||
requestRechargeInfo()
|
||
|
||
rootView.confirmBtn.rx.tap.subscribe(onNext: { _ in
|
||
self.requestOrderPayParams()
|
||
}).disposed(by: disposeBag)
|
||
|
||
NotificationCenter.default.rx.notification(.RequestOrderPayStatusNotification)
|
||
.subscribe(onNext: { [weak self] _ in
|
||
self?.handlePaymentSuccess()
|
||
}).disposed(by: disposeBag)
|
||
}
|
||
|
||
private func selectedGoodsName() -> String {
|
||
for item in itemsRelay.value {
|
||
switch item {
|
||
case .recommend(let m): if m.goods_id == selectedGoodsId { return m.goods_name }
|
||
case .normal(let m): if m.goods_id == selectedGoodsId { return m.goods_name }
|
||
}
|
||
}
|
||
return ""
|
||
}
|
||
|
||
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?.selectedGoodsName() ?? "")",
|
||
confirmText: "确定", confirmBlock: {
|
||
AppRouter.shared.popOrDismiss()
|
||
}, cancelBlock: { })
|
||
}.disposed(by: disposeBag)
|
||
}
|
||
|
||
private func setupTableView() {
|
||
typealias Section = SectionModel<String, PromotionalActivitiesItem>
|
||
let dataSource = RxTableViewSectionedReloadDataSource<Section> { _, tableView, indexPath, item in
|
||
let isSelected = item.identity == self.selectedGoodsId
|
||
switch item {
|
||
case .recommend(let model):
|
||
let cell: PromotionalRecommendCell = tableView.dequeueReusableCell(for: indexPath)
|
||
cell.configure(model: model, isSelected: isSelected)
|
||
return cell
|
||
case .normal(let model):
|
||
let cell: PromotionalNormalCell = tableView.dequeueReusableCell(for: indexPath)
|
||
cell.configure(model: model, isSelected: isSelected)
|
||
return cell
|
||
}
|
||
}
|
||
|
||
itemsRelay
|
||
.map { [SectionModel(model: "", items: $0)] }
|
||
.bind(to: rootView.tableView.rx.items(dataSource: dataSource))
|
||
.disposed(by: disposeBag)
|
||
|
||
itemsRelay
|
||
.subscribe(onNext: { [weak self] items in
|
||
self?.rootView.tableView.layoutChain.height(CGFloat(items.count) * 106 + 5)
|
||
self?.rootView.scrollContentView.layoutIfNeeded()
|
||
})
|
||
.disposed(by: disposeBag)
|
||
|
||
rootView.tableView.rx.itemSelected
|
||
.subscribe(onNext: { [weak self] indexPath in
|
||
guard let self = self else { return }
|
||
let item = self.itemsRelay.value[indexPath.row]
|
||
self.selectedGoodsId = item.identity
|
||
self.rootView.tableView.reloadData()
|
||
})
|
||
.disposed(by: disposeBag)
|
||
}
|
||
|
||
override func viewDidDisappear(_ animated: Bool) {
|
||
super.viewDidDisappear(animated)
|
||
if isMovingFromParent {
|
||
onDismiss?()
|
||
}
|
||
}
|
||
|
||
override func viewWillDisappear(_ animated: Bool) {
|
||
super.viewWillDisappear(animated)
|
||
navigationController?.setNavigationBarHidden(true, animated: false)
|
||
}
|
||
|
||
// MARK: - API
|
||
private func requestRechargeInfo() {
|
||
DLToast.showLoading()
|
||
OrderService.rechargeInfo(type: "member").subscribe(onNext: { [weak self] response in
|
||
guard let self = self else { return }
|
||
if let checkedId = response.list.first(where: { $0.checked })?.goods_id {
|
||
self.selectedGoodsId = checkedId
|
||
}
|
||
let items = response.list.map { model in
|
||
model.goods_id == "20042"
|
||
? PromotionalActivitiesItem.recommend(model: model)
|
||
: PromotionalActivitiesItem.normal(model: model)
|
||
}
|
||
self.items = items
|
||
self.rootView.startCountdown()
|
||
}).disposed(by: disposeBag)
|
||
}
|
||
|
||
private func requestOrderPayParams() {
|
||
guard let systemConfig = AppContextManager.shared.systemConfig, let timeConfig = systemConfig.timeConfig else {
|
||
return
|
||
}
|
||
|
||
// if timeConfig.pay_pop_type == 3 {
|
||
// IAPManager.shared.purchase(goodsId: selectedGoodsId) { [weak self] success, msg in
|
||
// guard let self = self else { return }
|
||
// if success {
|
||
// self.showPayResultPop(showCloseBtn: false,
|
||
// status: true,
|
||
// title: "支付成功",
|
||
// message: "恭喜您!成功开通!",
|
||
// confirmText: "确定", confirmBlock: {
|
||
// AppRouter.shared.popOrDismiss()
|
||
// }, cancelBlock: { })
|
||
// } else {
|
||
// self.showPayResultPop(status: false,
|
||
// title: "支付失败",
|
||
// message: msg ?? "很抱歉,请您重新支付!",
|
||
// confirmText: "重新支付", confirmBlock: { },
|
||
// cancelText: "取消", cancelBlock: {
|
||
// AppRouter.shared.popOrDismiss()
|
||
// })
|
||
// }
|
||
// }
|
||
// return
|
||
// }
|
||
|
||
let payType = timeConfig.pay_pop_type == 1 ? "alipay" : "weixin"
|
||
dl.showLoading()
|
||
OrderService.orderPayParams(goodsId: selectedGoodsId,
|
||
payType: payType).subscribe(onNext: { response in
|
||
self.dl.dismiss()
|
||
guard let data = response.data else { return }
|
||
|
||
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)
|
||
}
|
||
}
|