杨锴
2025-04-16 09a372bc45fde16fd42257ab6f78b8deeecf720b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
//
//  WalletRechargeVC.swift
//  XQMuse
//
//  Created by 无故事王国 on 2024/8/21.
//
 
import UIKit
import JQTools
import StoreKit
import RxSwift
import SwiftyStoreKit
 
class WalletRechargeVC: BaseVC {
 
    @IBOutlet weak var stackView: UIStackView!
    @IBOutlet weak var tap_other: TapBtn!
    @IBOutlet weak var btn_handle: UIButton!
    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var cons_collectionHei: NSLayoutConstraint!
    @IBOutlet weak var tap_other_title: UILabel!
 
    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]()
    private var oldPrice:Double = 0
    private var timeLoopDisposeBag = DisposeBag()
 
    private var isShowMore:Bool = false
    private var orderId:String? //下单后的ID
    private var tapType:TapType = .t2
 
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "疗愈币充值"
 
        if !isSimulator{
            Services.getTurn(progress: false).subscribe(onNext: {[weak self]data in
                guard let weakSelf = self else { return }
                if let m = data.data,m == true{
                    weakSelf.isShowMore = m
                    weakSelf.tap_other.isHidden = false
                    weakSelf.tap_other_title.text = "支".appending("付").appending("宝")
                }
            }).disposed(by: disposeBag)
        }
 
 
 
        btn_handle.isEnabled = false
        btn_handle.alpha = 0.6
 
        view.backgroundColor = .white
 
        for v in 0...3{
            productList.insert("com.XQmuse.coin.\(v + 1)")
        }
 
        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()
 
        Services.myWallet().subscribe(onNext: {data in
            if let m = data.data{
                self.oldPrice = m.recharge
            }
        }).disposed(by: disposeBag)
 
        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")
        stackView.jq_cornerRadius = 10
    }
 
    override func setRx() {
        NotificationCenter.default.rx.notification(UIApplication.didBecomeActiveNotification).take(until: self.rx.deallocated).subscribe(onNext: {[weak self] _ in
            guard let weakSelf = self else { return }
            if weakSelf.isShowMore && weakSelf.orderId != nil{
                Services.queryPayment(orderId: weakSelf.orderId!).subscribe(onNext: {data in
                    if let str = data.data{
                        switch str{
                            case "succeeded":
                                alertSuccess(msg: "购买成功")
                                NotificationCenter.default.post(name: WithDrawReply_Noti, object: nil)
                                DispatchQueue.main.asyncAfter(delay: 1.0) {
                                    weakSelf.navigationController?.popViewController()
                                }
                            case "failed":
                                alertError(msg: "购买失败")
                            case "closed":
                                break
//                                alertError(msg: "交易已关闭")
                            case "refunded":
                                break
//                                alertError(msg: "全额退款")
                            case "pending":
                                break
//                                alertError(msg: "待支付")
                            default:break
                        }
                    }
                }).disposed(by: weakSelf.disposeBag)
            }
        }).disposed(by: disposeBag)
    }
 
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        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}
 
        if isShowMore{
            let product = products[selectIndexPath.row]
            Services.placeOrder(orderForm: .recharge, payType: tapType == .t2 ? 2:1, amount: product.price.doubleValue, balanceFlag: nil, orderId: nil, receiverId: nil, targetId: nil, vipType: nil).subscribe(onNext: {[weak self] data in
                guard let weakSelf = self else { return }
 
                if let m = data.data{
                    weakSelf.orderId = m.orderId
 
                    if weakSelf.tapType == .t2{
                        UIApplication.shared.open(URL(string: m.qrcodeUrl!)!)
                    }
 
                    if weakSelf.tapType == .t3{
                        let miniProgram =  WXLaunchMiniProgramReq.object()
                        miniProgram.miniProgramType = .release
                        miniProgram.userName = m.orgId!
                        miniProgram.path = m.path!
                        WXApi.send(miniProgram){ s in
                            if !s{
                                alert(msg: "打开失败")
                            }
                        }
                    }
                }
            }).disposed(by: disposeBag)
        }else{
            showHUD("正在进行内购买")
            let product = products[selectIndexPath.row]
            InPurchaseManager.purchaseProduct(ID: product.productIdentifier, applicationUsername: userId.string) {[weak self] result in
                guard let weakSelf = self else { return }
                if result.status == 0{
                    let transactionId = result.receipt?.in_app.first?.transaction_id ?? ""
                    Services.placeOrderApple(amount: product.price.doubleValue, balanceFlag: .no, orderFrom: 4, transactionIdentifier: transactionId, vipType: nil).subscribe(onNext: { data in
                        showHUD("正在查询购买结果")
                        let observable = Observable<Int>.interval(RxTimeInterval.seconds(5), scheduler: MainScheduler.instance)
                        observable.subscribe { event in
                            Services.myWallet().subscribe(onNext: {data in
                                if let m = data.data{
                                    if m.recharge != weakSelf.oldPrice{
                                        hiddenHUD()
                                        alertSuccess(msg: "购买成功")
                                        weakSelf.timeLoopDisposeBag = DisposeBag()
                                        NotificationCenter.default.post(name: WithDrawReply_Noti, object: nil)
                                        DispatchQueue.main.asyncAfter(delay: 1.0) {
                                            weakSelf.navigationController?.popViewController()
                                        }
                                    }
                                }
                            }).disposed(by: weakSelf.disposeBag)
                        }.disposed(by: weakSelf.timeLoopDisposeBag)
                    }).disposed(by: weakSelf.disposeBag)
                }
            } errorClouse: { error in
                alertError(msg: "支付失败")
            }
        }
    }
 
    @IBAction func tapAction(_ sender: Any) {
        PurchaseMethodView.show {[weak self] type in
            guard let weakSelf = self else { return }
            weakSelf.tapType = type
 
            switch type {
                case .t2:
                    weakSelf.tap_other_title.text = "支".appending("付").appending("宝")
                case .t3:
                    weakSelf.tap_other_title.text = "微".appending("信")
                default:break
            }
        }
    }
}
 
extension WalletRechargeVC:UICollectionViewDelegate & UICollectionViewDataSource{
 
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        selectIndexPath = indexPath
        collectionView.reloadData()
    }
 
    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.price.stringValue
 
        btn_handle.isEnabled = true
        btn_handle.alpha = 1.0
 
        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, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 27
    }
 
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 27
    }
 
}
 
class WalletCCell:UICollectionViewCell{
 
    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
 
        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
    }
 
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}