//
|
// CardCenterSheetView.swift
|
// WanPai
|
//
|
// Created by 无故事王国 on 2025/3/26.
|
//
|
|
import UIKit
|
import JQTools
|
import QMUIKit
|
import RxSwift
|
import RxSwift
|
import RxRelay
|
import Combine
|
|
let Noti_hiddenCardCenter_Add = Notification.Name.init("Noti_hiddenCardCenter_Add")
|
let Noti_showSheetView = Notification.Name.init("Noti_showSheetView")
|
|
class CardCenterSheetView: UIView,JQNibView{
|
|
@IBOutlet weak var cons_bottom: NSLayoutConstraint!
|
@IBOutlet weak var tableView: UITableView!
|
@IBOutlet weak var cons_table_height: NSLayoutConstraint!
|
@IBOutlet weak var view_container: UIView!
|
@IBOutlet weak var btn_buyment: UIButton!
|
@IBOutlet weak var btn_addUser: QMUIButton!
|
@IBOutlet weak var text_view: UITextView!
|
@IBOutlet weak var label_price: UILabel!
|
@IBOutlet weak var btn_agreement: UIButton!
|
@IBOutlet weak var text_view_height: NSLayoutConstraint!
|
@IBOutlet weak var label_rule: UILabel!
|
|
|
let disposeBag = DisposeBag()
|
private var subscriptions = Set<AnyCancellable>()
|
private var model:HuiminCardDetailModel?
|
private var selectStudents = [StudentProfile1Model]()
|
private var students = [StudentProfile1Model]()
|
|
|
override func awakeFromNib() {
|
super.awakeFromNib()
|
btn_addUser.imagePosition = .right
|
btn_addUser.spacingBetweenImageAndTitle = 3
|
cons_bottom.constant = -(JQ_ScreenW * 1.1)
|
text_view.isScrollEnabled = false
|
text_view.isEditable = false
|
tableView.delegate = self
|
tableView.dataSource = self
|
tableView.separatorStyle = .none
|
tableView.isScrollEnabled = false
|
tableView.register(UINib(nibName: "StudentInfo_6_TCell", bundle: nil), forCellReuseIdentifier: "_StudentInfo_6_TCell")
|
|
text_view.isHidden = true
|
btn_agreement.isHidden = true
|
label_rule.isHidden = true
|
|
text_view.automaticallyAdjustsScrollIndicatorInsets = false
|
text_view.contentInset = .init(top: -8, left: 0, bottom: 0, right: 0)
|
alpha = 0
|
layoutIfNeeded()
|
text_view.delegate = self
|
label_price.font = UIFont(name: "DIN Alternate Bold", size: 24)
|
|
tableView.publisher(for: \.contentSize).sink {[unowned self] size in
|
if size.height > 250{
|
self.tableView.isScrollEnabled = true
|
}else{
|
self.tableView.isScrollEnabled = false
|
}
|
self.cons_table_height.constant = min(250,size.height)
|
}.store(in: &subscriptions)
|
|
text_view.publisher(for: \.contentSize).sink {[unowned self] size in
|
self.text_view_height.constant = size.height
|
}.store(in: &subscriptions)
|
|
NotificationCenter.default.rx.notification(Noti_hiddenCardCenter_Add).take(until: self.rx.deallocated).subscribe(onNext: {[unowned self] noti in
|
guard let state = noti.object as? Bool else { return }
|
self.alpha = state ? 0:1
|
}).disposed(by: disposeBag)
|
|
|
NotificationCenter.default.rx.notification(StudentUpdate_Nofi, object: nil).subscribe(onNext: {value in
|
var needShow = true
|
if let v = value.object as? [String:Bool]{
|
needShow = v["needShow"] ?? true
|
}
|
self.alpha = 1
|
self.getStudentList(needShow)
|
}).disposed(by: disposeBag)
|
|
|
NotificationCenter.default.rx.notification(Noti_showSheetView, object: nil).subscribe(onNext: {value in
|
self.alpha = 1
|
}).disposed(by: disposeBag)
|
|
|
Services.myHuiminRuleInfo().subscribe(onNext: {[unowned self]data in
|
self.label_rule.text = data.data?.selectUserRemark.jq_filterFromHTML_1()
|
}).disposed(by: disposeBag)
|
}
|
|
static func show(_ model:HuiminCardDetailModel){
|
let studentChooseView = CardCenterSheetView.jq_loadNibView()
|
studentChooseView.model = model
|
studentChooseView.frame = sceneDelegate?.window?.frame ?? .zero
|
|
sceneDelegate?.window?.addSubview(studentChooseView)
|
studentChooseView.cons_bottom.constant = 0
|
|
let font:UIFont = .systemFont(ofSize: 12, weight: .medium)
|
let normalColor:UIColor = .black.withAlphaComponent(0.6)
|
let linkColor:UIColor = UIColor(hexString: "#0078CD")!.withAlphaComponent(0.7)
|
|
studentChooseView.label_price.text = 0.currency()
|
studentChooseView.label_price.font = UIFont.init(name: "DIN Alternate Bold", size: 24)
|
|
let attributedStringbuilder = AttributedStringbuilder.build()
|
attributedStringbuilder.add(string: "我已阅读并同意", withFont: font, withColor: normalColor)
|
for (index,v) in model.agreementSettings.enumerated(){
|
attributedStringbuilder.addLink(string: "《\(v.agreementName)》", url: URL(string: "wanpai://page?index=\(index)")!, withFont: font, linkColor: linkColor)
|
}
|
studentChooseView.text_view.attributedText = attributedStringbuilder.mutableAttributedString
|
|
UIView.animate(withDuration: 0.4) {
|
studentChooseView.alpha = 1
|
studentChooseView.layoutIfNeeded()
|
}
|
studentChooseView.getStudentList()
|
|
}
|
|
private func getStudentList(_ needShow:Bool = true){
|
Services.startCourceStudentList().subscribe(onNext: {data in
|
self.students = data.data ?? []
|
if (data.data?.count ?? 0) > 0 && needShow{
|
StudentChoose3View.show(defaultStu: Set(self.selectStudents)) { m in
|
self.selectStudents = m.shuffled()
|
self.tableView.reloadData()
|
self.updatePrice()
|
}
|
}
|
}).disposed(by: self.disposeBag)
|
}
|
|
private func updatePrice(){
|
let totalMoney = (model?.salesMoney ?? 0) * Double(selectStudents.count)
|
label_price.text = totalMoney.currency()
|
|
self.text_view.isHidden = self.selectStudents.count > 0 ? false:true
|
self.btn_agreement.isHidden = self.selectStudents.count > 0 ? false:true
|
self.label_rule.isHidden = self.selectStudents.count > 0 ? false:true
|
}
|
|
@IBAction func addPersionAction(_ sender: QMUIButton) {
|
if students.count == 0{
|
NotificationCenter.default.post(name: Noti_hiddenCardCenter_Add, object: true)
|
let vc = CardCenterAddUserVC()
|
JQ_currentNavigationController().pushViewController(vc)
|
}else{
|
StudentChoose3View.show(defaultStu: Set(selectStudents)) { m in
|
self.selectStudents = m.shuffled()
|
self.tableView.reloadData()
|
self.updatePrice()
|
}
|
}
|
}
|
|
@IBAction func closeAction(_ sender: UIButton) {
|
|
self.cons_bottom.constant = -(JQ_ScreenW * 1.1)
|
UIView.animate(withDuration: 0.4) {
|
self.alpha = 0
|
self.layoutIfNeeded()
|
} completion: { _ in
|
self.removeFromSuperview()
|
}
|
}
|
|
@IBAction func agreementAction(_ sender: UIButton) {
|
sender.isSelected = !sender.isSelected
|
}
|
|
@IBAction func buyNowAction(_ sender: UIButton) {
|
|
guard let m = model else{return}
|
|
guard btn_agreement.isSelected else {
|
let v = m.agreementSettings.map { m in
|
return "《\(m.agreementName)》"
|
}.joined(separator: " ")
|
alertError(msg: "请先阅读协议\(v)");return
|
}
|
|
guard selectStudents.count != 0 else{
|
alertError(msg: "请选择人员");return
|
}
|
|
let totalMoney = (model?.salesMoney ?? 0) * Double(selectStudents.count)
|
updatePrice()
|
|
let studentIds = selectStudents.map({"\($0.stuId)"}).joined(separator: ",")
|
|
PaymentView.show(enumType: .huiminCard, money: (ali: totalMoney, wx: totalMoney, coin: nil, course: nil, integral: nil)) {[unowned self] type in
|
Services.payHuiminCard(id: m.id, payType: type, studentIds: studentIds).subscribe(onNext: { data in
|
if let m = data.data{
|
|
self.cons_bottom.constant = -(JQ_ScreenW * 1.1)
|
UIView.animate(withDuration: 0.4) {
|
self.alpha = 0
|
self.layoutIfNeeded()
|
} completion: { _ in
|
self.removeFromSuperview()
|
switch type {
|
case .wechat:
|
YYPaymentManager.shared.sendPaymentRequest(YYWeChatPayRequest(partnerId: m.partnerid, prepayId: m.prepayid, package: m.package, nonceStr: m.noncestr, timeStamp: m.timestamp, sign: m.sign)) { result in
|
switch result {
|
case .success:
|
let vc = PaymentResultVC(result: .success, objType: .huimin)
|
JQ_currentViewController().jq_push(vc: vc)
|
case .cancel:
|
let vc = PaymentResultVC(result: .fail("支付失败",0), objType: .huimin) {[unowned self] in
|
self.buyNowAction(self.btn_buyment)
|
}
|
JQ_currentViewController().jq_push(vc: vc)
|
case .failure(_):
|
let vc = PaymentResultVC(result: .fail("支付失败",0), objType: .huimin){[unowned self] in
|
self.buyNowAction(self.btn_buyment)
|
}
|
JQ_currentViewController().jq_push(vc: vc)
|
}
|
}
|
|
case .aliPay:
|
YYPaymentManager.shared.sendPaymentRequest(YYAlipayRequest(orderString: data.data?.orderString ?? "")) { result in
|
switch result {
|
case .success:
|
let vc = PaymentResultVC(result: .success, objType: .huimin)
|
JQ_currentViewController().jq_push(vc: vc)
|
case .cancel:
|
let vc = PaymentResultVC(result: .fail("支付失败",0), objType: .huimin) {[unowned self] in
|
self.buyNowAction(self.btn_buyment)
|
}
|
JQ_currentViewController().jq_push(vc: vc)
|
case .failure(_):
|
let vc = PaymentResultVC(result: .fail("支付失败",0), objType: .huimin){[unowned self] in
|
self.buyNowAction(self.btn_buyment)
|
}
|
JQ_currentViewController().jq_push(vc: vc)
|
}
|
}
|
default:
|
let vc = PaymentResultVC(result: .success, objType: .huimin)
|
JQ_currentViewController().jq_push(vc: vc)
|
}
|
}
|
}
|
|
}).disposed(by: self.disposeBag)
|
}
|
}
|
|
override func layoutSubviews() {
|
super.layoutSubviews()
|
DispatchQueue.main.asyncAfter(wallDeadline: .now()+0.1) {
|
self.view_container.jq_addCorners(corner: [.topLeft,.topRight], radius: 20)
|
}
|
}
|
}
|
|
extension CardCenterSheetView:UITextViewDelegate{
|
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
|
// 处理链接点击事件
|
guard let index = Int(URL.jq_params?["index"] ?? "") else {
|
return false
|
}
|
|
self.alpha = 0
|
|
let sub = model!.agreementSettings[index]
|
|
let vc = CommonWebVC(type: .user, customTitle: sub.agreementName, content: sub.agreementContent, needBackBtn: true)
|
JQ_currentViewController().jq_push(vc: vc)
|
|
return false // 返回 false 表示不进一步处理链接(例如,不自动打开链接)
|
}
|
}
|
|
extension CardCenterSheetView:UITableViewDelegate{
|
|
}
|
|
extension CardCenterSheetView:UITableViewDataSource{
|
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
return selectStudents.count
|
}
|
|
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
let cell = tableView.dequeueReusableCell(withIdentifier: "_StudentInfo_6_TCell", for: indexPath) as! StudentInfo_6_TCell
|
cell.indexPath = indexPath
|
cell.activityDetailPartModel = selectStudents[indexPath.row]
|
cell.deleteAt = {[unowned self]index in
|
self.selectStudents.remove(at: index)
|
tableView.reloadData()
|
self.updatePrice()
|
}
|
return cell
|
}
|
}
|