diff --git a/Veloria/Base/Networking/API/VPWalletAPI.swift b/Veloria/Base/Networking/API/VPWalletAPI.swift index 69a93bc..97783ea 100644 --- a/Veloria/Base/Networking/API/VPWalletAPI.swift +++ b/Veloria/Base/Networking/API/VPWalletAPI.swift @@ -6,6 +6,7 @@ // import SmartCodable +import StoreKit class VPWalletAPI { @@ -21,15 +22,64 @@ class VPWalletAPI { VPHUD.show() } - VPIAPManager.manager.requestProductData(isToast: isToast) { model in - if isLoding { - VPHUD.dismiss() + _requestPayTemplate(isToast: false) { model in + guard let model = model else { + 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") param.method = .get param.isToast = isToast diff --git a/Veloria/Libs/VPIAPManager/VPIAPManager.swift b/Veloria/Libs/VPIAPManager/VPIAPManager.swift index c62f0de..14adf89 100644 --- a/Veloria/Libs/VPIAPManager/VPIAPManager.swift +++ b/Veloria/Libs/VPIAPManager/VPIAPManager.swift @@ -20,11 +20,8 @@ class VPIAPManager { ///成功回调 private var completionHandler: CompletionHandler? - ///获取支付模版回调 - private var productDataHandler: ((_ model: VPPayTemplateModel?) -> Void)? - ///原始支付模版数据 - private var oldPayTemplateModel: VPPayTemplateModel? + private var productListHandler: ((_ products: [SKProduct]) -> Void)? private lazy var iapManager: JXIAPManager = { let manager = JXIAPManager() @@ -142,31 +139,13 @@ class VPIAPManager { } - ///获取支付数据 - func requestProductData(isToast: Bool = false, completer: ((_ model: VPPayTemplateModel?) -> Void)?) { - VPWalletAPI._requestPayTemplate(isToast: isToast) { [weak self] model in - 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) - } - + func requestProductList(productIdArr: [String], completer: ((_ products: [SKProduct]) -> Void)?) { + self.productListHandler = completer + self.iapManager.requestProductList(productIdArr: productIdArr) } - private func getProductId(templateId: String?) -> String? { + + func getProductId(templateId: String?) -> String? { guard let templateId = templateId else { return nil } return VPIAPManager.IAPPrefix + templateId } @@ -237,42 +216,11 @@ extension VPIAPManager: JXIAPManagerDelegate { self.completionHandler?(false) } - func jx_iapPayGotProducts(productIds: [SKProduct]) { - guard let payTemplateModel = self.oldPayTemplateModel else { return } + func jx_iapPayGotProducts(products: [SKProduct]) { - var newCoinList: [VPPayTemplateItem] = [] - var newVipList: [VPPayTemplateItem] = [] + self.productListHandler?(products) - payTemplateModel.list_coins?.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 - 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 + self.productListHandler = nil } } diff --git a/Veloria/Thirdparty/JXIAPManager/JXIAPManager.swift b/Veloria/Thirdparty/JXIAPManager/JXIAPManager.swift index 89b3a05..a8ff67e 100644 --- a/Veloria/Thirdparty/JXIAPManager/JXIAPManager.swift +++ b/Veloria/Thirdparty/JXIAPManager/JXIAPManager.swift @@ -10,7 +10,7 @@ import StoreKit @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?) /// 购买失败 @@ -163,7 +163,7 @@ extension JXIAPManager: SKProductsRequestDelegate { func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) { if self.operationType == .request { DispatchQueue.main.async { - self.delegate?.jx_iapPayGotProducts?(productIds: response.products) + self.delegate?.jx_iapPayGotProducts?(products: response.products) } self.operationType = .idle } else if self.operationType == .buy {