This commit is contained in:
zjx 2025-07-19 13:41:15 +08:00
parent a18432c64b
commit 33d6182dbf
3 changed files with 66 additions and 68 deletions

View File

@ -6,6 +6,7 @@
// //
import SmartCodable import SmartCodable
import StoreKit
class VPWalletAPI { class VPWalletAPI {
@ -21,15 +22,64 @@ class VPWalletAPI {
VPHUD.show() VPHUD.show()
} }
VPIAPManager.manager.requestProductData(isToast: isToast) { model in _requestPayTemplate(isToast: false) { model in
if isLoding { guard let model = model else {
VPHUD.dismiss() if isLoding {
VPHUD.dismiss()
}
completer?(nil)
return
} }
completer?(model) var productIdArr: [String] = []
model.list_sub_vip?.forEach { item in
productIdArr.append(VPIAPManager.manager.getProductId(templateId: item.ios_template_id) ?? "")
}
model.list_coins?.forEach { item in
productIdArr.append(VPIAPManager.manager.getProductId(templateId: item.ios_template_id) ?? "")
}
VPIAPManager.manager.requestProductList(productIdArr: productIdArr) { products in
if isLoding {
VPHUD.dismiss()
}
var newCoinList: [VPPayTemplateItem] = []
var newVipList: [VPPayTemplateItem] = []
model.list_coins?.forEach { item in
let productId = VPIAPManager.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
}
}
}
model.list_sub_vip?.forEach { item in
let productId = VPIAPManager.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
}
}
}
model.list_coins = newCoinList
model.list_sub_vip = newVipList
completer?(model)
}
} }
} }
static func _requestPayTemplate(isToast: Bool = true, completer: ((_ model: VPPayTemplateModel?) -> Void)?) { private static func _requestPayTemplate(isToast: Bool = true, completer: ((_ model: VPPayTemplateModel?) -> Void)?) {
var param = VPNetworkParameters(path: "/paySettingsV3") var param = VPNetworkParameters(path: "/paySettingsV3")
param.method = .get param.method = .get
param.isToast = isToast param.isToast = isToast

View File

@ -20,11 +20,8 @@ class VPIAPManager {
/// ///
private var completionHandler: CompletionHandler? private var completionHandler: CompletionHandler?
///
private var productDataHandler: ((_ model: VPPayTemplateModel?) -> Void)?
/// private var productListHandler: ((_ products: [SKProduct]) -> Void)?
private var oldPayTemplateModel: VPPayTemplateModel?
private lazy var iapManager: JXIAPManager = { private lazy var iapManager: JXIAPManager = {
let manager = JXIAPManager() let manager = JXIAPManager()
@ -142,31 +139,13 @@ class VPIAPManager {
} }
/// func requestProductList(productIdArr: [String], completer: ((_ products: [SKProduct]) -> Void)?) {
func requestProductData(isToast: Bool = false, completer: ((_ model: VPPayTemplateModel?) -> Void)?) { self.productListHandler = completer
VPWalletAPI._requestPayTemplate(isToast: isToast) { [weak self] model in self.iapManager.requestProductList(productIdArr: productIdArr)
guard let self = self else { return }
guard let model = model else {
completer?(nil)
return
}
self.productDataHandler = completer
self.oldPayTemplateModel = model
var productIdArr: [String] = []
model.list_sub_vip?.forEach { item in
productIdArr.append(self.getProductId(templateId: item.ios_template_id) ?? "")
}
model.list_coins?.forEach { item in
productIdArr.append(self.getProductId(templateId: item.ios_template_id) ?? "")
}
self.iapManager.requestProductList(productIdArr: productIdArr)
}
} }
private func getProductId(templateId: String?) -> String? {
func getProductId(templateId: String?) -> String? {
guard let templateId = templateId else { return nil } guard let templateId = templateId else { return nil }
return VPIAPManager.IAPPrefix + templateId return VPIAPManager.IAPPrefix + templateId
} }
@ -237,42 +216,11 @@ extension VPIAPManager: JXIAPManagerDelegate {
self.completionHandler?(false) self.completionHandler?(false)
} }
func jx_iapPayGotProducts(productIds: [SKProduct]) { func jx_iapPayGotProducts(products: [SKProduct]) {
guard let payTemplateModel = self.oldPayTemplateModel else { return }
var newCoinList: [VPPayTemplateItem] = [] self.productListHandler?(products)
var newVipList: [VPPayTemplateItem] = []
payTemplateModel.list_coins?.forEach { item in self.productListHandler = nil
let productId = getProductId(templateId: item.ios_template_id) ?? ""
for product in productIds {
if productId == product.productIdentifier {
item.price = product.price.stringValue
item.currency = product.priceLocale.currencySymbol
newCoinList.append(item)
break
}
}
}
payTemplateModel.list_sub_vip?.forEach { item in
let productId = getProductId(templateId: item.ios_template_id) ?? ""
for product in productIds {
if productId == product.productIdentifier {
item.price = product.price.stringValue
item.currency = product.priceLocale.currencySymbol
newVipList.append(item)
break
}
}
}
payTemplateModel.list_sub_vip = newVipList
payTemplateModel.list_coins = newCoinList
productDataHandler?(payTemplateModel)
self.productDataHandler = nil
self.oldPayTemplateModel = nil
} }
} }

View File

@ -10,7 +10,7 @@ import StoreKit
@objc protocol JXIAPManagerDelegate { @objc protocol JXIAPManagerDelegate {
/// ///
@objc optional func jx_iapPayGotProducts(productIds: [SKProduct]) @objc optional func jx_iapPayGotProducts(products: [SKProduct])
/// ///
@objc optional func jx_iapPaySuccess(productId: String, receipt: String, transactionIdentifier: String?) @objc optional func jx_iapPaySuccess(productId: String, receipt: String, transactionIdentifier: String?)
/// ///
@ -163,7 +163,7 @@ extension JXIAPManager: SKProductsRequestDelegate {
func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) { func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
if self.operationType == .request { if self.operationType == .request {
DispatchQueue.main.async { DispatchQueue.main.async {
self.delegate?.jx_iapPayGotProducts?(productIds: response.products) self.delegate?.jx_iapPayGotProducts?(products: response.products)
} }
self.operationType = .idle self.operationType = .idle
} else if self.operationType == .buy { } else if self.operationType == .buy {