| | |
| | | private var productList = Set<String>() |
| | | private var products = [SKProduct]() |
| | | private var timeLoopDisposeBag = DisposeBag() |
| | | private var vipContentModel:VIPContentModel? |
| | | |
| | | private var isShowMore:Bool = false |
| | | private var orderId:String? //下单后的ID |
| | | |
| | | override func viewDidLoad() { |
| | | super.viewDidLoad() |
| | |
| | | productList.insert("com.XQmuse.Non.renewing.year.1") |
| | | productList.insert("com.XQmuse.Non.renewing.half.year.1") |
| | | productList.insert("com.XQmuse.Non.renewing.month.1") |
| | | |
| | | 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 |
| | | Services.getVipPrice().subscribe(onNext: { data in |
| | | if let m = data.data{ |
| | | weakSelf.vipContentModel = m |
| | | weakSelf.collectionView.reloadData() |
| | | } |
| | | }).disposed(by: weakSelf.disposeBag) |
| | | } |
| | | }).disposed(by: disposeBag) |
| | | |
| | | |
| | | collectionView.delegate = self |
| | | collectionView.dataSource = self |
| | |
| | | } |
| | | }).disposed(by: disposeBag) |
| | | |
| | | 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: "购买成功") |
| | | weakSelf.timeLoopDisposeBag = DisposeBag() |
| | | NotificationCenter.default.post(name: UpdateUserProfile_Noti, object: nil, userInfo: nil) |
| | | DispatchQueue.main.asyncAfter(delay: 1.0) { |
| | | weakSelf.navigationController?.popViewController() |
| | | } |
| | | case "failed": |
| | | alertError(msg: "购买失败") |
| | | case "closed": |
| | | alertError(msg: "交易已关闭") |
| | | case "refunded": |
| | | alertError(msg: "全额退款") |
| | | case "pending": |
| | | alertError(msg: "待支付") |
| | | default:break |
| | | } |
| | | } |
| | | }).disposed(by: weakSelf.disposeBag) |
| | | } |
| | | }).disposed(by: disposeBag) |
| | | |
| | | } |
| | | |
| | | @IBAction func completeAction(_ sender: UIButton) { |
| | |
| | | alert(msg: "请先阅读并同意《会员用户协议》");return |
| | | } |
| | | |
| | | showHUD("正在进行内购买") |
| | | guard products.count != 0 else {return} |
| | | let oldVipExpireTime = UserViewModel.getAvatarInfo().vipExpireTime |
| | | if isShowMore{ |
| | | guard let vipContentModel else { return } |
| | | |
| | | let product = products[selectIndex] |
| | | let userId = UserViewModel.getAvatarInfo().userId.string |
| | | InPurchaseManager.purchaseProduct(ID: product.productIdentifier, applicationUsername: userId) {[weak self]result in |
| | | guard let weakSelf = self else { return } |
| | | if result.status == 0{ |
| | | hiddenHUD() |
| | | let transactionId = result.receipt?.in_app.first?.original_transaction_id ?? "" |
| | | var amount:Double = 0 |
| | | switch selectIndex{ |
| | | case 0:amount = Double(vipContentModel.monthlyVipIos) |
| | | case 1:amount = Double(vipContentModel.quarterlyVipIos) |
| | | case 2:amount = Double(vipContentModel.annualVipIos) |
| | | default:break |
| | | } |
| | | |
| | | showHUD("正在查询购买结果") |
| | | Services.placeOrderApple(amount: product.price.doubleValue, balanceFlag: .no, orderFrom: 3, transactionIdentifier: transactionId, vipType: weakSelf.selectIndex + 1).subscribe(onNext: { data in |
| | | showHUD("正在查询购买结果") |
| | | let observable = Observable<Int>.interval(RxTimeInterval.seconds(5), scheduler: MainScheduler.instance) |
| | | observable.subscribe { event in |
| | | if event.element == 10{ |
| | | alertError(msg: "订单异常") |
| | | weakSelf.timeLoopDisposeBag = DisposeBag() |
| | | return |
| | | PurchaseMethodView.show {[weak self] type in |
| | | guard let weakSelf = self else { return } |
| | | Services.placeOrder(orderForm: .vip, payType: type == .t2 ? 2:1, amount: amount, balanceFlag: nil, orderId: nil, receiverId: nil, targetId: nil, vipType: weakSelf.selectIndex + 1).subscribe(onNext: { data in |
| | | if let m = data.data{ |
| | | weakSelf.orderId = m.orderId |
| | | |
| | | if type == .t2{ |
| | | UIApplication.shared.open(URL(string: m.qrcodeUrl!)!) |
| | | } |
| | | Services.getUserDetail(showProgress: false).subscribe(onNext: {data in |
| | | if data.data?.vipExpireTime != oldVipExpireTime{ |
| | | hiddenHUD() |
| | | alertSuccess(msg: "购买成功") |
| | | weakSelf.timeLoopDisposeBag = DisposeBag() |
| | | NotificationCenter.default.post(name: UpdateUserProfile_Noti, object: nil, userInfo: nil) |
| | | DispatchQueue.main.asyncAfter(delay: 1.0) { |
| | | weakSelf.navigationController?.popViewController() |
| | | |
| | | if type == .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: weakSelf.disposeBag) |
| | | }.disposed(by: weakSelf.timeLoopDisposeBag) |
| | | } |
| | | } |
| | | }).disposed(by: weakSelf.disposeBag) |
| | | } |
| | | } errorClouse: { error in |
| | | alertError(msg: "支付失败") |
| | | }else{ |
| | | showHUD("正在进行内购买") |
| | | guard products.count != 0 else {return} |
| | | let oldVipExpireTime = UserViewModel.getAvatarInfo().vipExpireTime |
| | | |
| | | let product = products[selectIndex] |
| | | let userId = UserViewModel.getAvatarInfo().userId.string |
| | | InPurchaseManager.purchaseProduct(ID: product.productIdentifier, applicationUsername: userId) {[weak self]result in |
| | | guard let weakSelf = self else { return } |
| | | if result.status == 0{ |
| | | hiddenHUD() |
| | | let transactionId = result.receipt?.in_app.first?.original_transaction_id ?? "" |
| | | |
| | | showHUD("正在查询购买结果") |
| | | Services.placeOrderApple(amount: product.price.doubleValue, balanceFlag: .no, orderFrom: 3, transactionIdentifier: transactionId, vipType: weakSelf.selectIndex + 1).subscribe(onNext: { data in |
| | | showHUD("正在查询购买结果") |
| | | let observable = Observable<Int>.interval(RxTimeInterval.seconds(5), scheduler: MainScheduler.instance) |
| | | observable.subscribe { event in |
| | | if event.element == 10{ |
| | | alertError(msg: "订单异常") |
| | | weakSelf.timeLoopDisposeBag = DisposeBag() |
| | | return |
| | | } |
| | | Services.getUserDetail(showProgress: false).subscribe(onNext: {data in |
| | | if data.data?.vipExpireTime != oldVipExpireTime{ |
| | | hiddenHUD() |
| | | alertSuccess(msg: "购买成功") |
| | | weakSelf.timeLoopDisposeBag = DisposeBag() |
| | | NotificationCenter.default.post(name: UpdateUserProfile_Noti, object: nil, userInfo: 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 readAction(_ sender: UIButton) { |
| | | btn_isRead.isSelected = !btn_isRead.isSelected |
| | | |
| | | } |
| | | |
| | | @IBAction func agreementAction(_ sender: UIButton) { |
| | |
| | | |
| | | func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { |
| | | let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! VipCenterCCell |
| | | let product = products[indexPath.row] |
| | | cell.isSelect(indexPath.row == selectIndex,product: product) |
| | | if vipContentModel != nil{ |
| | | cell.isSelect(indexPath.row == selectIndex, indexPath: indexPath, vipContentModel: vipContentModel!) |
| | | }else{ |
| | | let product = products[indexPath.row] |
| | | cell.isSelect(indexPath.row == selectIndex,product: product) |
| | | } |
| | | return cell |
| | | } |
| | | |
| | | func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { |
| | | |
| | | if vipContentModel != nil{ |
| | | return 3 |
| | | } |
| | | |
| | | return products.count |
| | | } |
| | | } |
| | |
| | | } |
| | | } |
| | | |
| | | func isSelect(_ state:Bool,indexPath:IndexPath,vipContentModel:VIPContentModel){ |
| | | |
| | | var price:Int = 0 |
| | | switch indexPath.row{ |
| | | case 0: |
| | | price = vipContentModel.monthlyVipIos |
| | | label_title.text = "月度会员" |
| | | label_price.text = "¥" + vipContentModel.monthlyVipIos.string |
| | | lable_priorDay.text = String(format: "¥%.2lf/天", vipContentModel.monthlyVipIos.double / 30.0) |
| | | case 1: |
| | | price = vipContentModel.quarterlyVipIos |
| | | label_title.text = "半年会员" |
| | | label_price.text = "¥" + vipContentModel.quarterlyVipIos.string |
| | | lable_priorDay.text = String(format: "¥%.2lf/天", vipContentModel.quarterlyVipIos.double / 30.0) |
| | | case 2: |
| | | price = vipContentModel.annualVipIos |
| | | label_title.text = "年度会员" |
| | | label_price.text = "¥" + vipContentModel.annualVipIos.string |
| | | lable_priorDay.text = String(format: "¥%.2lf/天", vipContentModel.annualVipIos.double / 30.0) |
| | | default:break |
| | | } |
| | | |
| | | if state{ |
| | | contentView.backgroundColor = UIColor(hexString: "#A6C586") |
| | | label_title.textColor = .white |
| | | lable_priorDay.textColor = .white |
| | | label_price.attributedText = AttributedStringbuilder.build().add(string: "¥", withFont: .systemFont(ofSize: 15, weight:.heavy), withColor: .white).add(string: "\(price)", withFont: .systemFont(ofSize: 20, weight:.heavy), withColor: .white).mutableAttributedString |
| | | }else{ |
| | | contentView.backgroundColor = UIColor(hexString: "#EFF2F2") |
| | | label_title.textColor = UIColor(hexString: "#353535") |
| | | lable_priorDay.textColor = UIColor(hexString: "#B7B7B7") |
| | | label_price.attributedText = AttributedStringbuilder.build().add(string: "¥", withFont: .systemFont(ofSize: 15, weight:.heavy), withColor: UIColor(hexString: "#353535")!).add(string: "\(price)", withFont: .systemFont(ofSize: 20, weight:.heavy), withColor: UIColor(hexString: "#353535")!).mutableAttributedString |
| | | } |
| | | } |
| | | |
| | | required init?(coder: NSCoder) { |
| | | fatalError("init(coder:) has not been implemented") |
| | | } |