130 lines
3.9 KiB
Swift
130 lines
3.9 KiB
Swift
//
|
||
// VipRechargeVM.swift
|
||
// QuickLocation
|
||
//
|
||
// Created by 八条 on 2026/6/3.
|
||
//
|
||
|
||
import RxSwift
|
||
import RxCocoa
|
||
import RxDataSources
|
||
import SwiftyUserDefaults
|
||
|
||
typealias ExpenseListSectionModel = SectionModel<String, VipExpenseModel>
|
||
typealias VipRightsListSectionModel = SectionModel<String, VipRightsModel>
|
||
|
||
class VipRechargeVM {
|
||
struct Input {
|
||
|
||
}
|
||
struct Output {
|
||
var sectionedItems: Observable<[ExpenseListSectionModel]>
|
||
var vipRightItems: Observable<[VipRightsListSectionModel]>
|
||
}
|
||
|
||
let input: Input
|
||
let output: Output
|
||
var disposeBag = DisposeBag()
|
||
|
||
var createCount = BehaviorRelay<String>(value: "0")
|
||
var groupCount = BehaviorRelay<String>(value: "0")
|
||
var joinCount = BehaviorRelay<String>(value: "0")
|
||
|
||
private let sectionedItems = PublishSubject<[ExpenseListSectionModel]>()
|
||
private let vipRightsItems = PublishSubject<[VipRightsListSectionModel]>()
|
||
|
||
private let vipRightsList: [VipRightsModel] = [
|
||
VipRightsModel(icon: "VipRecharge/location", title: "定位查询"),
|
||
VipRightsModel(icon: "VipRecharge/map", title: "地图样式"),
|
||
VipRightsModel(icon: "VipRecharge/sos", title: "SOS求救"),
|
||
VipRightsModel(icon: "VipRecharge/bubble", title: "创建气泡"),
|
||
VipRightsModel(icon: "VipRecharge/ad", title: "无广告"),
|
||
VipRightsModel(icon: "VipRecharge/battery", title: "电池电量"),
|
||
VipRightsModel(icon: "VipRecharge/emoji", title: "趣味表情"),
|
||
VipRightsModel(icon: "VipRecharge/service", title: "专属客服")
|
||
]
|
||
|
||
var selectedIndex: Int = -1 {
|
||
didSet {
|
||
refreshData()
|
||
loadVipRightsData()
|
||
}
|
||
}
|
||
var list: [VipExpenseModel] = []
|
||
var payType: String {
|
||
guard list.count > 0 else { return "" }
|
||
return list[selectedIndex].pay_type
|
||
}
|
||
|
||
var price: String {
|
||
guard list.count > 0 else { return "" }
|
||
return list[selectedIndex].price
|
||
}
|
||
|
||
var discountPriceString: String {
|
||
guard list.count > 0 else { return "已优惠0元" }
|
||
|
||
let price = list[selectedIndex].price
|
||
let oPrice = list[selectedIndex].origin_price
|
||
|
||
guard oPrice.int >= price.int else { return "已优惠0元" }
|
||
|
||
let diff = oPrice.double - price.double
|
||
let diffStr = diff.truncatingRemainder(dividingBy: 1) == 0 ? String(format: "%.0f", diff) : String(diff)
|
||
return "已优惠\(diffStr)元"
|
||
}
|
||
|
||
// MARK: - 加载数据
|
||
func loadData(list: [VipExpenseModel]) {
|
||
self.list = list
|
||
selectedIndex = list.firstIndex(where: { $0.checked == true }) ?? 0
|
||
sectionedItems.onNext(list.mapSection())
|
||
}
|
||
|
||
func refreshData() {
|
||
sectionedItems.onNext(list.mapSection())
|
||
}
|
||
|
||
func loadVipRightsData() {
|
||
// 20040(没有专属客服): 5 10 5 20041: 10 20 10 20042: 15 30 15
|
||
let model = list[selectedIndex]
|
||
var tmpList = vipRightsList
|
||
|
||
switch model.goods_id {
|
||
case "20040":
|
||
createCount.accept("5")
|
||
groupCount.accept("10")
|
||
joinCount.accept("5")
|
||
let _ = tmpList.popLast()
|
||
case "20041":
|
||
createCount.accept("10")
|
||
groupCount.accept("20")
|
||
joinCount.accept("10")
|
||
case "20042":
|
||
createCount.accept("15")
|
||
groupCount.accept("30")
|
||
joinCount.accept("15")
|
||
default:
|
||
createCount.accept("5")
|
||
groupCount.accept("10")
|
||
joinCount.accept("5")
|
||
}
|
||
|
||
vipRightsItems.onNext(tmpList.mapSection())
|
||
}
|
||
|
||
// MARK: - init
|
||
init() {
|
||
input = Input()
|
||
output = Output(
|
||
sectionedItems: sectionedItems.asObservable(),
|
||
vipRightItems: vipRightsItems.asObservable()
|
||
)
|
||
}
|
||
}
|
||
|
||
struct VipRightsModel {
|
||
var icon: String
|
||
var title: String
|
||
}
|