fix
杨锴
2024-10-09 2e91ee1387ef545ecef49230f2024b89b2f82a58
XQMuse/Root/Me/VC/WalletRechargeVC.swift
@@ -7,100 +7,132 @@
import UIKit
import JQTools
import StoreKit
class WalletRechargeVC: BaseVC {
            @IBOutlet weak var btn_handle: UIButton!
            @IBOutlet weak var collectionView: UICollectionView!
            @IBOutlet weak var cons_collectionHei: NSLayoutConstraint!
    @IBOutlet weak var btn_handle: UIButton!
    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var cons_collectionHei: NSLayoutConstraint!
            private var selectIndexPath = IndexPath(row: 0, section: 0)
            private    let cellW = (JQ_ScreenW - (24 * 2) - 3 * 27) / 4
            private let cellH:Double = 35
    private var selectIndexPath = IndexPath(row: 0, section: 0)
    private    let cellW = (JQ_ScreenW - (24 * 2) - 3 * 27) / 4
    private let cellH:Double = 35
    private var productList = Set<String>()
    private var products = [SKProduct]()
            override func viewDidLoad() {
    override func viewDidLoad() {
        super.viewDidLoad()
                        title = "疗愈币充值"
                        collectionView.delegate = self
                        collectionView.dataSource = self
                        collectionView.contentInset = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10)
                        collectionView.register(WalletCCell.self, forCellWithReuseIdentifier: "_WalletCCell")
        title = "疗愈币充值"
                        let h = ceil(4.0 / 4.0) * cellH + floor((4.0 - 1) / 4.0) * 27.0
                        cons_collectionHei.constant = h
                        view.layoutIfNeeded()
        for v in 0...5{
            productList.insert("com.XQMuse.p.\(v)")
        }
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.contentInset = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10)
        collectionView.register(WalletCCell.self, forCellWithReuseIdentifier: "_WalletCCell")
        let h = ceil(4.0 / 4.0) * cellH + floor((4.0 - 1) / 4.0) * 27.0
        cons_collectionHei.constant = h
        view.layoutIfNeeded()
        InPurchaseManager.instance().setProductList(productList) { products in
            self.products = Array(products)
            self.products.sort { p1, p2 in
                return p1.price.doubleValue < p2.price.doubleValue
            }
            self.collectionView.reloadData()
        }
    }
            override func setUI() {
                        view.backgroundColor = UIColor(hexString: "f6f6f6")
                        btn_handle.jq_gradientNibColor(colorArr:  [UIColor(hexStr: "#8EA47A").cgColor,UIColor(hexStr: "#AFCA98").cgColor], cornerRadius: 20)
            }
    override func setUI() {
        view.backgroundColor = UIColor(hexString: "f6f6f6")
        btn_handle.jq_gradientNibColor(colorArr:  [UIColor(hexStr: "#8EA47A").cgColor,UIColor(hexStr: "#AFCA98").cgColor], cornerRadius: 20)
    }
    @IBAction func completeAction(_ sender: UIButton) {
        guard let userId = UserViewModel.getLoginInfo()?.userid else {
            sceneDelegate?.needLogin()
            return}
        let product = products[selectIndexPath.row]
        InPurchaseManager.purchaseProduct(ID: product.productIdentifier, applicationUsername: userId.string) { model in
        } errorClouse: { error in
            alertError(msg: error.localizedDescription)
        }
    }
}
extension WalletRechargeVC:UICollectionViewDelegate & UICollectionViewDataSource{
            func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
                        selectIndexPath = indexPath
                        collectionView.reloadData()
            }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        selectIndexPath = indexPath
        collectionView.reloadData()
    }
            func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
                        return 4
            }
            func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
                        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "_WalletCCell", for: indexPath) as! WalletCCell
                        cell.selectAt(indexPath == selectIndexPath)
                        return cell
            }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return products.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "_WalletCCell", for: indexPath) as! WalletCCell
        cell.selectAt(indexPath == selectIndexPath)
        let product = products[indexPath.row]
        cell.lable_title.text = product.localizedTitle
        return cell
    }
}
extension WalletRechargeVC:UICollectionViewDelegateFlowLayout{
            func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
                        return CGSize(width: cellW, height:cellH)
            }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: cellW, height:cellH)
    }
            func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
                        return 27
            }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 27
    }
            func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
                        return 27
            }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 27
    }
}
class WalletCCell:UICollectionViewCell{
            private lazy var lable_title:UILabel = {
                        let label = UILabel()
                        label.text = "疗愈币0"
                        label.textAlignment = .center
                        label.textColor = UIColor(hexString: "#A9C393")
                        label.font = UIFont.systemFont(ofSize: 16, weight: .medium)
                        return label
            }()
    private(set) lazy var lable_title:UILabel = {
        let label = UILabel()
        label.text = "0币"
        label.textAlignment = .center
        label.textColor = UIColor(hexString: "#A9C393")
        label.font = UIFont.systemFont(ofSize: 16, weight: .medium)
        return label
    }()
            override init(frame: CGRect) {
                        super.init(frame: frame)
                     jq_borderWidth = 1
                     jq_borderColor = UIColor(hexString: "#ABC695")
                     jq_cornerRadius = 8
    override init(frame: CGRect) {
        super.init(frame: frame)
        jq_borderWidth = 1
        jq_borderColor = UIColor(hexString: "#ABC695")
        jq_cornerRadius = 8
                        contentView.addSubview(lable_title)
                        lable_title.snp.makeConstraints { make in
                                    make.center.equalToSuperview()
                        }
            }
        contentView.addSubview(lable_title)
        lable_title.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
    }
            func selectAt(_ state:Bool){
                        backgroundColor = state ? UIColor(hexString: "#8EA47A") : .white
                        lable_title.textColor = state ? .white:UIColor(hexString: "#8EA47A")
                        jq_borderWidth = state ? 0:1
            }
    func selectAt(_ state:Bool){
        backgroundColor = state ? UIColor(hexString: "#8EA47A") : .white
        lable_title.textColor = state ? .white:UIColor(hexString: "#8EA47A")
        jq_borderWidth = state ? 0:1
    }
            required init?(coder: NSCoder) {
                        fatalError("init(coder:) has not been implemented")
            }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}