From 19e3fd5eecfa5fdf1b16b60e3462a5a61de1136e Mon Sep 17 00:00:00 2001 From: 杨锴 <841720330@qq.com> Date: 星期二, 26 十一月 2024 18:47:27 +0800 Subject: [PATCH] fix BUG --- XQMuse/Root/Home/VC/PaymentOrderVC.swift | 334 ++++++++++++++++++++++++++++++++++++------------------- 1 files changed, 217 insertions(+), 117 deletions(-) diff --git a/XQMuse/Root/Home/VC/PaymentOrderVC.swift b/XQMuse/Root/Home/VC/PaymentOrderVC.swift index d838b04..5d45939 100644 --- a/XQMuse/Root/Home/VC/PaymentOrderVC.swift +++ b/XQMuse/Root/Home/VC/PaymentOrderVC.swift @@ -16,6 +16,9 @@ case t1 case t2 //z__f__b case t3 //w__x + case t1t2//组合 + case t1t3//组合 + case none } class PaymentOrderVC: BaseVC { @@ -59,10 +62,41 @@ @IBOutlet weak var img_action_2: UIImageView! @IBOutlet weak var img_action_3: UIImageView! - private var tapType:TapType? + private var price:Double = 0 + + private var tapType:TapType = .none{ + didSet{ + switch tapType { + case .t1: + img_action_1.image = UIImage(named: "icon_choose_small_s") + img_action_2.image = UIImage(named: "icon_choose_small_1") + img_action_3.image = UIImage(named: "icon_choose_small_1") + case .t2: + img_action_1.image = UIImage(named: "icon_choose_small_1") + img_action_2.image = UIImage(named: "icon_choose_small_s") + img_action_3.image = UIImage(named: "icon_choose_small_1") + case .t3: + img_action_1.image = UIImage(named: "icon_choose_small_1") + img_action_2.image = UIImage(named: "icon_choose_small_1") + img_action_3.image = UIImage(named: "icon_choose_small_s") + case .t1t2: + img_action_1.image = UIImage(named: "icon_choose_small_s") + img_action_2.image = UIImage(named: "icon_choose_small_s") + img_action_3.image = UIImage(named: "icon_choose_small_1") + case .t1t3: + img_action_1.image = UIImage(named: "icon_choose_small_s") + img_action_2.image = UIImage(named: "icon_choose_small_1") + img_action_3.image = UIImage(named: "icon_choose_small_s") + case .none: + img_action_1.image = UIImage(named: "icon_choose_small_1") + img_action_2.image = UIImage(named: "icon_choose_small_1") + img_action_3.image = UIImage(named: "icon_choose_small_1") + } + } + } private var isShowMore:Bool = false - private var discountByCoin:Bool = false +// private var discountByCoin:Bool = true private var museItemModel:MeditationModel? private var courseItemModel:CourseModel? @@ -99,8 +133,6 @@ view_searchUserInput.isHidden = !giftToOther img_action_1.image = UIImage(named: "icon_choose_small_s") - getBalance() - let btn = UIButton(type: .custom) btn.setTitle("取消订单", for: .normal) btn.setTitleColor(UIColor(hexString: "#353535"), for: .normal) @@ -115,6 +147,7 @@ Services.getCourseDetail(courseId: courseItemModel!.id).subscribe(onNext: {data in if let m = data.data{ self.courseModel = m + self.price = m.iosPrice self.image_cover.sd_setImage(with: URL(string: m.coverUrl.components(separatedBy: ",").first?.jq_urlEncoded())) self.label_courseName.text = m.courseTitle @@ -130,6 +163,7 @@ self.label_orderPrice.text = "愈疗币\(m.iosPrice.jq_formatFloat)" self.label_totalPrice.text = "愈疗币\(m.iosPrice.jq_formatFloat)" } + self.getBalance() } }).disposed(by: disposeBag) } @@ -139,23 +173,26 @@ self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: btn) } - Services.getMeditationDetail(id: museItemModel!.id).subscribe(onNext: {data in + Services.getMeditationDetail(id: museItemModel!.id).subscribe(onNext: {[weak self] data in + guard let weakSelf = self else { return } if let m = data.data{ - self.museModel = m - self.image_cover.sd_setImage(with: URL(string: m.coverUrl.components(separatedBy: ",").first?.jq_urlEncoded())) - self.label_courseName.text = m.meditationTitle - self.label_teacher.text = "" - self.label_paymentCount.text = "x1" + weakSelf.museModel = m + weakSelf.price = m.iosPrice + weakSelf.image_cover.sd_setImage(with: URL(string: m.coverUrl.components(separatedBy: ",").first?.jq_urlEncoded())) + weakSelf.label_courseName.text = m.meditationTitle + weakSelf.label_teacher.text = "" + weakSelf.label_paymentCount.text = "x1" - if self.isShowMore{ - self.label_price.text = "¥\(m.iosPrice.jq_formatFloat)" - self.label_orderPrice.text = "¥\(m.iosPrice.jq_formatFloat)" - self.label_totalPrice.text = "¥\(m.iosPrice.jq_formatFloat)" + if weakSelf.isShowMore{ + weakSelf.label_price.text = "¥\(m.iosPrice.jq_formatFloat)" + weakSelf.label_orderPrice.text = "¥\(m.iosPrice.jq_formatFloat)" + weakSelf.label_totalPrice.text = "¥\(m.iosPrice.jq_formatFloat)" }else{ - self.label_price.text = "愈疗币\(m.iosPrice.jq_formatFloat)" - self.label_orderPrice.text = "愈疗币\(m.iosPrice.jq_formatFloat)" - self.label_totalPrice.text = "愈疗币\(m.iosPrice.jq_formatFloat)" + weakSelf.label_price.text = "愈疗币\(m.iosPrice.jq_formatFloat)" + weakSelf.label_orderPrice.text = "愈疗币\(m.iosPrice.jq_formatFloat)" + weakSelf.label_totalPrice.text = "愈疗币\(m.iosPrice.jq_formatFloat)" } + weakSelf.getBalance() } }).disposed(by: disposeBag) } @@ -193,45 +230,55 @@ } override func setRx() { - NotificationCenter.default.rx.notification(Refreh_PaymentWallet_Noti).take(until: self.rx.deallocated).subscribe(onNext: {_ in - self.getBalance() + NotificationCenter.default.rx.notification(Refreh_PaymentWallet_Noti).take(until: self.rx.deallocated).subscribe(onNext: {[weak self] _ in + self?.getBalance() }).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: "购买成功") - var id = 0 - var money:Double = 0 - switch weakSelf.type { - case .muse: - id = weakSelf.museModel!.id - money = weakSelf.museModel!.iosPrice - case .course: - id = weakSelf.courseModel!.id - money = weakSelf.courseModel!.iosPrice - default:break - } - let vc = PaymentOrderResultVC(type: weakSelf.type, id: id, price: money) - weakSelf.push(vc: vc) - case "failed": - alertError(msg: "购买失败") - case "closed": - alertError(msg: "交易已关闭") - case "refunded": - alertError(msg: "全额退款") - case "pending": - alertError(msg: "待支付") - default:break + showHUD("正在查询支付结果...") + DispatchQueue.main.asyncAfter(delay: 2.5) { + if weakSelf.isShowMore && weakSelf.orderId != nil{ + Services.queryPayment(orderId: weakSelf.orderId!).subscribe(onNext: {data in + if let str = data.data{ + switch str{ + case "succeeded": + hiddenHUD() + alertSuccess(msg: "购买成功") + var id = 0 + var money:Double = 0 + switch weakSelf.type { + case .muse: + id = weakSelf.museModel!.id + money = weakSelf.museModel!.iosPrice + case .course: + id = weakSelf.courseModel!.id + money = weakSelf.courseModel!.iosPrice + default:break + } + + let vc = PaymentOrderResultVC(type: weakSelf.type, id: id, price: money) + weakSelf.push(vc: vc) + case "failed": + hiddenHUD() + alertError(msg: "购买失败") + case "closed": + hiddenHUD() + alertError(msg: "交易已关闭") + case "refunded": + hiddenHUD() + alertError(msg: "全额退款") + case "pending": + showHUD("正在支付...") + default:break + } } - } - }).disposed(by: weakSelf.disposeBag) + }).disposed(by: weakSelf.disposeBag) + }else{ + hiddenHUD() + } } }).disposed(by: disposeBag) @@ -257,61 +304,100 @@ } } - private func getBalance(){ - Services.getUserBalance().subscribe(onNext: {data in - self.balance = data.data ?? 0 - if self.isShowMore{ - self.label_walletBalance.text = String(format: "当前可用 ¥%@", data.data?.jq_formatFloat ?? "0") - }else{ - self.label_walletBalance.text = String(format: "当前可用 %@疗愈币", data.data?.jq_formatFloat ?? "0") + private func getBalance() { + Services.getUserBalance().subscribe(onNext: { [weak self] data in + guard let weakSelf = self else { return } + + // 解包数据并更新余额 + if let balanceData = data.data { + weakSelf.balance = balanceData + let formattedBalance = balanceData.jq_formatFloat + + // 更新 UI + if weakSelf.isShowMore { + weakSelf.img_action_1.image = balanceData > 0 ? UIImage(named: "icon_choose_small_s") : UIImage(named: "icon_choose_small_1") + weakSelf.label_walletBalance.text = "当前可用 ¥\(formattedBalance)" + + if balanceData > weakSelf.price { + weakSelf.tapType = .t1 + } + } else { + weakSelf.label_walletBalance.text = "当前可用 \(formattedBalance)疗愈币" + weakSelf.tapType = .t1 + } + } else { + weakSelf.balance = 0 + weakSelf.label_walletBalance.text = "当前可用 0疗愈币" + weakSelf.tapType = .t1 } }).disposed(by: disposeBag) } @IBAction func discountByCoinAction(_ sender: TapBtn) { - discountByCoin.toggle() - if discountByCoin{ - img_action_1.image = UIImage(named: "icon_choose_small_s") + + guard balance > 0 else {return} + //是否能全部抵扣 + let allDiscount = balance > price + + if allDiscount{ + tapType = .t1 }else{ - img_action_1.image = UIImage(named: "icon_choose_small_1") + switch tapType { + case .t2:self.tapType = .t1t2 + case .t3:self.tapType = .t1t3 + case .t1t2: + self.tapType = .t2 + case .t1t3: + self.tapType = .t3 + default:break + } } + print(tapType) +// label_totalPrice.text = "¥" + price.jq_formatFloat + } + + @IBAction func chooseAction(_ sender: TapBtn) { var price:Double = 0 - switch type { case .course: - if discountByCoin{ - price = max((courseModel?.iosPrice ?? 0) - balance,0) - }else{ - price = courseModel?.iosPrice ?? 0 - } + price = courseModel?.iosPrice ?? 0 case .muse: - if discountByCoin{ - price = max((museModel?.iosPrice ?? 0) - balance,0) - }else{ - price = museModel?.iosPrice ?? 0 - } + price = courseModel?.iosPrice ?? 0 default:break } - label_totalPrice.text = "¥" + price.jq_formatFloat - } + //是否能全部抵扣 + let allDiscount = balance > price - @IBAction func chooseAction(_ sender: TapBtn) { switch sender.tag { case 2001: - tapType = .t2 - img_action_2.image = UIImage(named: "icon_choose_small_s") - img_action_3.image = UIImage(named: "icon_choose_small_1") + if allDiscount { + tapType = .t2 + }else{ + if img_action_1.image == UIImage(named: "icon_choose_small_s"){ + tapType = .t1t2 + }else{ + tapType = .t2 + } + } case 2002: - tapType = .t3 - img_action_2.image = UIImage(named: "icon_choose_small_1") - img_action_3.image = UIImage(named: "icon_choose_small_s") + if allDiscount{ + tapType = .t3 + }else{ + if img_action_1.image == UIImage(named: "icon_choose_small_s"){ + tapType = .t1t3 + }else{ + tapType = .t3 + } + + } default: break } + print(tapType) } @IBAction func invateRegisterAction(_ sender: UIButton) { @@ -377,17 +463,12 @@ @IBAction func completeAction(_ sender: UIButton) { - - var money:Double = 0 var id:Int = 0 - if let m = courseModel{ - money = m.iosPrice id = m.id } if let m = museModel{ - money = m.iosPrice id = m.id guard id != UserViewModel.getAvatarInfo().id else { @@ -410,33 +491,46 @@ } if isShowMore{ - guard tapType != nil else {alertError(msg: "请选择支付方式");return} - - var price:Double = 0 + guard tapType != .none else {alertError(msg: "请选择支付方式");return} var targetId:Int = 0 switch type { case .course: - price = courseModel!.iosPrice targetId = courseModel!.id case .muse: - price = museModel!.iosPrice targetId = museModel!.id default:break } - if discountByCoin && balance < price{ + if balance < price && tapType == .t1{ alertError(msg: "抵扣余额不足");return } - Services.placeOrder(orderForm: type, payType: tapType == .t2 ? 2:1, amount: nil, balanceFlag: discountByCoin ? 1:2, orderId: nil, receiverId: giftUserId, targetId: targetId, vipType: nil).subscribe(onNext: { [self] data in - if let m = data.data{ - self.orderId = m.orderId + //进行抵扣 + var discountByCoin = 2 + if tapType == .t1 || tapType == .t1t2 || tapType == .t1t3{ + discountByCoin = 1 + } - if self.tapType == .t2{ + var payType = 1 + if tapType == .t1t2 || tapType == .t2{ + payType = 2 + } + + if tapType == .t1{ + paymentByCoin(id: id);return + } + + Services.placeOrder(orderForm: type, payType: payType, amount: nil, balanceFlag: discountByCoin, orderId: nil, receiverId: giftUserId, targetId: targetId, 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 || weakSelf.tapType == .t1t2{ + guard m.qrcodeUrl?.isEmpty == false else{alertError(msg: "QRCode 为空");return} UIApplication.shared.open(URL(string: m.qrcodeUrl!)!) } - if self.tapType == .t3{ + if weakSelf.tapType == .t3 || weakSelf.tapType == .t1t3{ let miniProgram = WXLaunchMiniProgramReq.object() miniProgram.miniProgramType = .release miniProgram.userName = m.orgId! @@ -451,38 +545,44 @@ }).disposed(by: disposeBag) }else{ - guard balance > money else{ - CommonAlertView.show(title: "提示", content: "当前余额不足,请先充值", cancelStr: "暂不充值", completeStr: "去充值", isSingle: false) {[weak self] state in - if state{ - let vc = WalletRechargeVC() - self?.push(vc: vc) - } - } - return - } + paymentByCoin(id: id) + } + } - if businessId != nil{ - Services.gvieCourseAgain(orderForm: type, id: businessId!).subscribe(onNext: {[weak self]data in - guard let weakSelf = self else { return } - self?.museItemModel?.isBuy = .yes - self?.courseItemModel?.isBuy = .yes - self?.museModel?.isBuy = .yes - self?.courseModel?.isBuy = .yes - let vc = PaymentOrderResultVC(type: weakSelf.type, id: id, price: money) + private func paymentByCoin(id:Int){ + + + guard balance > price else{ + CommonAlertView.show(title: "提示", content: "当前余额不足,请先充值", cancelStr: "暂不充值", completeStr: "去充值", isSingle: false) {[weak self] state in + if state{ + let vc = WalletRechargeVC() self?.push(vc: vc) - }).disposed(by: disposeBag) - return + } } + return + } - Services.gvieCourse(orderForm: type, targetId: id,receiverId: giftUserId).subscribe(onNext: {[weak self]data in + if businessId != nil{ + Services.gvieCourseAgain(orderForm: type, id: businessId!).subscribe(onNext: {[weak self]data in guard let weakSelf = self else { return } self?.museItemModel?.isBuy = .yes self?.courseItemModel?.isBuy = .yes self?.museModel?.isBuy = .yes self?.courseModel?.isBuy = .yes - let vc = PaymentOrderResultVC(type: weakSelf.type, id: id, price: money) + let vc = PaymentOrderResultVC(type: weakSelf.type, id: id, price: weakSelf.price) self?.push(vc: vc) }).disposed(by: disposeBag) + return } + + Services.gvieCourse(orderForm: type, targetId: id,receiverId: giftUserId).subscribe(onNext: {[weak self]data in + guard let weakSelf = self else { return } + self?.museItemModel?.isBuy = .yes + self?.courseItemModel?.isBuy = .yes + self?.museModel?.isBuy = .yes + self?.courseModel?.isBuy = .yes + let vc = PaymentOrderResultVC(type: weakSelf.type, id: id, price: weakSelf.price) + self?.push(vc: vc) + }).disposed(by: disposeBag) } } -- Gitblit v1.7.1