102 lines
3.4 KiB
Swift
102 lines
3.4 KiB
Swift
//
|
|
// SPPayTemplateRequest.swift
|
|
// ThimraTV
|
|
//
|
|
// Created by 长沙佳儿 on 2025/7/19.
|
|
//
|
|
|
|
import UIKit
|
|
import StoreKit
|
|
|
|
class SPPayTemplateRequest: NSObject {
|
|
|
|
private var oldTemplateModel: SPPayTemplateModel?
|
|
|
|
private var completerBlock: ((_ model: SPPayTemplateModel?) -> Void)?
|
|
|
|
private var isLoding = false
|
|
private var isToast = false
|
|
|
|
|
|
func requestProducts(isLoding: Bool = false, isToast: Bool = true, completer: ((_ model: SPPayTemplateModel?) -> Void)?) {
|
|
self.completerBlock = completer
|
|
self.isLoding = isLoding
|
|
self.isToast = isToast
|
|
|
|
if isLoding {
|
|
SPHUD.show()
|
|
}
|
|
|
|
SPWalletAPI.requestPayTemplate(isToast: isToast) { [weak self] model in
|
|
guard let self = self else { return }
|
|
guard let model = model else {
|
|
if isLoding {
|
|
SPHUD.dismiss()
|
|
}
|
|
self.completerBlock?(nil)
|
|
return
|
|
}
|
|
self.oldTemplateModel = model
|
|
|
|
var productIdArr: [String] = []
|
|
model.list_sub_vip?.forEach { item in
|
|
productIdArr.append(SPIAPManager.manager.getProductId(templateId: item.ios_template_id) ?? "")
|
|
}
|
|
model.list_coins?.forEach { item in
|
|
productIdArr.append(SPIAPManager.manager.getProductId(templateId: item.ios_template_id) ?? "")
|
|
}
|
|
|
|
let set = Set(productIdArr)
|
|
let productsRequest = SKProductsRequest(productIdentifiers: set)
|
|
productsRequest.delegate = self
|
|
productsRequest.start()
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
//MARK: -------------- SKProductsRequestDelegate --------------
|
|
extension SPPayTemplateRequest: SKProductsRequestDelegate {
|
|
|
|
func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
|
|
if isLoding {
|
|
SPHUD.dismiss()
|
|
}
|
|
|
|
guard let templateModel = self.oldTemplateModel else { return }
|
|
let products = response.products
|
|
|
|
var newCoinList: [SPPayTemplateItem] = []
|
|
var newVipList: [SPPayTemplateItem] = []
|
|
|
|
templateModel.list_coins?.forEach { item in
|
|
let productId = SPIAPManager.manager.getProductId(templateId: item.ios_template_id) ?? ""
|
|
for product in products {
|
|
if productId == product.productIdentifier {
|
|
item.price = product.price.stringValue
|
|
item.currency = product.priceLocale.currencySymbol
|
|
newCoinList.append(item)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
templateModel.list_sub_vip?.forEach { item in
|
|
let productId = SPIAPManager.manager.getProductId(templateId: item.ios_template_id) ?? ""
|
|
for product in products {
|
|
if productId == product.productIdentifier {
|
|
item.price = product.price.stringValue
|
|
item.currency = product.priceLocale.currencySymbol
|
|
newVipList.append(item)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
templateModel.list_coins = newCoinList
|
|
templateModel.list_sub_vip = newVipList
|
|
|
|
DispatchQueue.main.async {
|
|
self.completerBlock?(templateModel)
|
|
}
|
|
}
|
|
}
|