杨锴
2024-11-06 63f7ed967433acee3ae8764c7a077e15c29c41f2
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
//
//  PaymentOrderVC.swift
//  XQMuse
//
//  Created by 无故事王国 on 2024/8/15.
//
 
import UIKit
import QMUIKit
 
let Refreh_PaymentWallet_Noti = Notification.Name.init("Refreh_PaymentWallet_Noti")
 
class PaymentOrderVC: BaseVC {
 
    enum PaymentOrderType {
        case course
        case muse
    }
 
                @IBOutlet weak var image_cover: UIImageView!
                @IBOutlet weak var label_price: UILabel!
                @IBOutlet weak var label_teacher: UILabel!
                @IBOutlet weak var label_paymentCount: UILabel!
                @IBOutlet weak var label_courseName: UILabel!
                @IBOutlet weak var label_orderPrice: UILabel!
                @IBOutlet weak var label_currentAccount: UILabel!
                @IBOutlet weak var label_totalPrice: UILabel!
                @IBOutlet weak var view_searchUserInput: UIView!
                @IBOutlet weak var view_searchUserResult: UIView!
 
                @IBOutlet weak var image_avatar: UIImageView!
                @IBOutlet weak var label_userName: UILabel!
                @IBOutlet weak var label_userPhone: UILabel!
                @IBOutlet weak var label_searchInfo: UILabel!
                @IBOutlet weak var btn_invate: UIButton!
                @IBOutlet weak var btn_isRead: UIButton!
 
                @IBOutlet weak var tf_phone: QMUITextField!
                @IBOutlet weak var label_walletBalance: UILabel!
                @IBOutlet weak var btn_isreadAgreement: UIButton!
                private var museItemModel:MeditationModel?
    private var courseItemModel:CourseModel?
                private var giftUserId:Int?
                private var giftToOther:Bool!
                private var courseModel:CourseModel?
    private var museModel:MeditationModel?
                private var balance:Double = 0
    private var type:PaymentOrderType!
 
    init(museItemModel:MeditationModel? = nil,courseItemModel:CourseModel? = nil,type:PaymentOrderType,giftToOther:Bool = false) {
                                super.init(nibName: nil, bundle: nil)
        self.museItemModel = museItemModel
        self.courseItemModel = courseItemModel
                                self.giftToOther = giftToOther
        self.type = type
                }
                
                required init?(coder: NSCoder) {
                                fatalError("init(coder:) has not been implemented")
                }
                
                override func viewDidLoad() {
        super.viewDidLoad()
                                title = "确认订单"
                                view_searchUserResult.isHidden = true
                                view_searchUserInput.isHidden = !giftToOther
 
                                getBalance()
 
        if type == .course{
            Services.getCourseDetail(courseId: courseItemModel!.id).subscribe(onNext: {data in
                if let m = data.data{
                    self.courseModel = m
                    self.image_cover.sd_setImage(with: URL(string: m.coverUrl.jq_urlEncoded()))
                    self.label_courseName.text = m.courseTitle
                    self.label_price.text = "愈疗币\(m.iosPrice.jq_formatFloat)"
                    self.label_teacher.text = "导师 \(m.tutor)"
                    self.label_paymentCount.text = "x\(m.count == 0 ? 1:m.count)"
                    self.label_orderPrice.text = "愈疗币\(m.iosPrice.jq_formatFloat)"
                    self.label_totalPrice.text = "愈疗币\(m.iosPrice.jq_formatFloat)"
                }
            }).disposed(by: disposeBag)
        }
 
        if type == .muse{
            Services.getMeditationDetail(id: museItemModel!.id).subscribe(onNext: {data in
                if let m = data.data{
                    self.museModel = m
                    self.image_cover.sd_setImage(with: URL(string: m.coverUrl.jq_urlEncoded()))
                    self.label_courseName.text = m.meditationTitle
                    self.label_price.text = "愈疗币\(m.iosPrice.jq_formatFloat)"
                    self.label_teacher.text = ""
                    self.label_paymentCount.text = "x1"
                    self.label_orderPrice.text = "愈疗币\(m.iosPrice.jq_formatFloat)"
                    self.label_totalPrice.text = "愈疗币\(m.iosPrice.jq_formatFloat)"
                }
            }).disposed(by: disposeBag)
        }
    }
 
                override func setUI() {
                                super.setUI()
                }
 
                override func setRx() {
                                NotificationCenter.default.rx.notification(Refreh_PaymentWallet_Noti).take(until: self.rx.deallocated).subscribe(onNext: {_ in
                                                self.getBalance()
                                }).disposed(by: disposeBag)
                }
 
                private func getBalance(){
                                Services.getUserBalance().subscribe(onNext: {data in
                                                self.balance = data.data ?? 0
                                                self.label_walletBalance.text = String(format: "当前可用 %@疗愈币", data.data?.jq_formatFloat ?? "0")
                                }).disposed(by: disposeBag)
                }
 
                @IBAction func invateRegisterAction(_ sender: UIButton) {
 
                }
 
                @IBAction func searchUserAction(_ sender: UIButton) {
                                guard !tf_phone.text!.isEmpty else {
                                                alertError(msg: tf_phone.placeholder ?? "请输入好友手机号");return
                                }
 
                                guard tf_phone.text!.jq_isPhone else {
                                                alertError(msg: "请输入正确的手机号");return
                                }
 
                                Services.searchUserByPhone(tf_phone.text!).subscribe(onNext: { data in
                                                self.view_searchUserResult.isHidden = false
                                                if let m = data.data,m.id != 0{
                                                                self.giftUserId = m.userId
                                                                self.image_avatar.sd_setImage(with: URL(string: m.avatar))
                                                                self.label_userName.text = m.nickname
                                                                self.label_userPhone.text = m.cellPhone
                                                                self.label_searchInfo.isHidden = true
                                                                self.btn_invate.isHidden = true
                                                }else{
                                                                self.image_avatar.isHidden = true
                                                                self.label_userName.isHidden = true
                                                                self.label_userPhone.isHidden = true
                                                                self.label_searchInfo.isHidden = false
                                                                self.btn_invate.isHidden = false
                                                }
                                }).disposed(by: disposeBag)
 
                }
 
                @IBAction func readAgreementAction(_ sender: UIButton) {
                                sender.isSelected = !sender.isSelected
                }
 
 
                @IBAction func webAgreementAction(_ sender: UIButton) {
                                let vc = WebVC(type: .course)
                                push(vc: vc)
                }
 
                @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 btn_isRead.isSelected else {
            alertError(msg: "请先阅读并同意《课程/疗愈音频购买协议》");return
        }
 
        if giftToOther {
            guard !tf_phone.text!.isEmpty else {
                alertError(msg: "请输入您要赠送人的手机号");return
            }
 
            guard giftUserId != nil else {
                alertError(msg: "请先查询赠送人是否存在");return
            }
        }
 
                                guard balance > money else{
                                                CommonAlertView.show(title: "提示", content: "当前余额不足,请先充值", cancelStr: "暂不充值", completeStr: "去充值", isSingle: false) { state in
                                                                if state{
 
                                                                }
                                                }
                                                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: money)
            self?.push(vc: vc)
        }).disposed(by: disposeBag)
                }
}