CNLiveIntegralAlertTool.swift
14.4 KB
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
//
// CNLiveIntegralAlertTool.swift
// metaCode
//
// Created by zx on 2024/1/11.
// 积分支付弹窗
import Foundation
import QMUIKit
import SnapKit
///积分支付状态类型
enum CNLiveIntegralPayStatus {
///积分充足
case integralEnough
///积分不足
case integralNotEnough
///支付中
case paying
///支付成功
case paySuccess
///支付失败
case payFailed
}
class CNLiveIntegralAlertTool:NSObject{
static let shared = CNLiveIntegralAlertTool()
var homePageNeedReloadActionBlock:homePageNeedReloadBlockActionBlock?
///当前积分支付状态
var currentIntegralPayStatus: CNLiveIntegralPayStatus = .integralEnough
let delegate = UIApplication.shared.delegate as! AppDelegate
/// 专辑名称
var title:String = ""
/// 专辑积分值
var integralCount:String = "0"
/// 支付的商品id
var productId:String = ""
/// 用户积分值
var point:Int = 0
/// 用户积分model
var checkInModel:MinePointCheckInModel?
lazy var bgBtnView: UIButton = {
let bgBtnView = UIButton(frame: self.delegate.window!.bounds)
bgBtnView.backgroundColor = HexColor("#000000")
bgBtnView.alpha = 0.6
bgBtnView.addTarget(self, action: #selector(bgBtnViewAction), for: .touchUpInside)
return bgBtnView
}()
lazy var bottomBgView :UIView = {
let bottomBgView = UIView(frame: CGRect(x: 0, y: KSreenHeight-267, width: KSreenWidth, height: 267))
bottomBgView.backgroundColor = HexColor("#FFFFFF")
bottomBgView.layer.cornerRadius = 9
bottomBgView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
bottomBgView.tag = 10000
return bottomBgView
}()
///专辑标题
lazy var albumTitleLabel: UILabel = {
let titleLabel = UILabel.init(frame: CGRectMake(15, 15, KSreenWidth-15-60, 20))
titleLabel.textColor = HexColor("#333333")
titleLabel.textAlignment = .left
titleLabel.font = Font_16
titleLabel.text = "剧集名称45集"
return titleLabel
}()
lazy var closeBtn: UIButton = {
let closeBtn = UIButton(type: .custom)
closeBtn.frame = CGRect(x: self.albumTitleLabel.right, y: 0, width: 50, height: 50)
closeBtn.setImage(UIImage(named: "point_alert_close"), for: .normal)
closeBtn.addTarget(self, action: #selector(closeBtnAction), for: .touchUpInside)
return closeBtn
}()
lazy var bottomLineView :UIView = {
let bottomLineView = UIView(frame: CGRect(x: 0, y: self.closeBtn.bottom+2, width: KSreenWidth, height: 1))
bottomLineView.backgroundColor = HexColor("#E4E4E4")
return bottomLineView
}()
///积分数量
lazy var integralCountLabel: UILabel = {
let titleLabel = UILabel.init(frame: CGRectMake(0, self.bottomLineView.bottom+30, 100, 66))
titleLabel.textColor = HexColor("#333333")
titleLabel.textAlignment = .left
titleLabel.font = UIFont.boldSystemFont(ofSize: 55)
titleLabel.text = "8000"
return titleLabel
}()
///余额XXX积分
lazy var balanceIntegralLabel: UILabel = {
let titleLabel = UILabel.init(frame: CGRectMake(0, 0, 100, 20))
titleLabel.textColor = HexColor("#696969")
titleLabel.textAlignment = .left
titleLabel.font = Font_14
return titleLabel
}()
lazy var payBtn: UIButton = {
let payBtn = UIButton(type: .custom)
payBtn.frame = CGRect(x: 15, y: self.bottomBgView.height-15-44, width: KSreenWidth-30, height: 44)
payBtn.setTitleColor(HexColor("#FFFFFF"), for: .normal)
// fillCode
let bgLayer1 = CAGradientLayer()
bgLayer1.colors = [UIColor(red: 0.4, green: 0.77, blue: 1, alpha: 1).cgColor, UIColor(red: 0.38, green: 0.6, blue: 1, alpha: 1).cgColor]
bgLayer1.locations = [0, 1]
bgLayer1.frame = payBtn.bounds
bgLayer1.startPoint = CGPoint(x: 0, y: 0.5)
bgLayer1.endPoint = CGPoint(x: 0.5, y: 0.5)
payBtn.layer.addSublayer(bgLayer1)
payBtn.addTarget(self, action: #selector(payBtnAction), for: .touchUpInside)
return payBtn
}()
///支付中img
lazy var payingImgView :UIImageView = {
let payingImgView = UIImageView(frame: CGRect(x: (KSreenWidth-45)/2.0, y: self.bottomLineView.bottom+65, width: 45, height: 43))
payingImgView.image = UIImage(named: "jifen_paying")
return payingImgView
}()
///支付成功img
lazy var paySuccessImgView :UIImageView = {
let paySuccessImgView = UIImageView(frame: CGRect(x: (KSreenWidth-78)/2.0, y: 60, width: 78, height: 78))
paySuccessImgView.image = UIImage(named: "jifen_paySuccess")
return paySuccessImgView
}()
///支付中
lazy var payingLabel: UILabel = {
let titleLabel = UILabel.init(frame: CGRectMake(0, self.payingImgView.bottom+15, KSreenWidth-40, 20))
titleLabel.centerX = self.payingImgView.centerX
titleLabel.textColor = HexColor("#333333")
titleLabel.textAlignment = .center
titleLabel.font = Font_14
return titleLabel
}()
// MARK: - show
///弹出积分购买弹窗
func showIntegralAlert(title:String,integralCount:String,productId:String){
self.title = title
self.integralCount = integralCount
self.productId = productId
delegate.window?.addSubview(self.bgBtnView)
delegate.window?.addSubview(self.bottomBgView)
//专辑所需积分值
let intIntegralCount = Int(integralCount)!
//用户积分值
self.getCheckIn { result, status in
let checkInModel = result as! MinePointCheckInModel
self.checkInModel = checkInModel
let pointStr = checkInModel.integral ?? "0"
let intPoint = Int(pointStr)!
self.point = intPoint
if intPoint > intIntegralCount{
//可以支付积分
self.currentIntegralPayStatus = .integralEnough
}else{
//积分不足
self.currentIntegralPayStatus = .integralNotEnough
}
self.initUI()
}
}
//隐藏积分购买弹窗
func hiddenIntegralAlert(){
delegate.window?.subviews.forEach({ view in
if view.tag == 10000{
UIView.animate(withDuration: 0.25) {
view.removeFromSuperview()
self.bgBtnView.removeFromSuperview()
}
}
})
}
func initUI(){
self.bottomBgView.subviews.forEach { view in
view.removeFromSuperview()
}
if self.currentIntegralPayStatus == .integralEnough{
//积分充足
self.bottomBgView.addSubview(self.albumTitleLabel)
self.albumTitleLabel.text = self.title
self.bottomBgView.addSubview(self.closeBtn)
self.bottomBgView.addSubview(self.bottomLineView)
self.bottomBgView.addSubview(self.integralCountLabel)
let integralCountStr = "\(self.integralCount)积分"
let priceAttri = NSMutableAttributedString.init(string: integralCountStr)
priceAttri.addAttribute(.font, value: regular_adapt_font(pointSize: 55), range: NSMakeRange(0, integralCountStr.count-2))
priceAttri.addAttribute(.foregroundColor, value: HexColor("#333333")!, range: NSMakeRange(0, integralCountStr.count-2))
priceAttri.addAttribute(.font, value: regular_adapt_font(pointSize: 16), range: NSMakeRange(integralCountStr.count-2, 2))
priceAttri.addAttribute(.foregroundColor, value: HexColor("#333333")!, range: NSMakeRange(integralCountStr.count-2, 2))
self.integralCountLabel.attributedText = priceAttri
self.bottomBgView.addSubview(self.balanceIntegralLabel)
self.balanceIntegralLabel.text = String(format: "余额%d积分", self.point)
self.bottomBgView.addSubview(self.payBtn)
self.payBtn.setTitle("确认支付", for: .normal)
self.integralCountLabel.snp.makeConstraints { make in
make.centerX.equalTo(self.bottomBgView.snp.centerX)
make.top.equalTo(self.bottomLineView.snp.bottom).offset(30)
make.width.lessThanOrEqualToSuperview().offset(-10)
}
self.balanceIntegralLabel.snp.makeConstraints { make in
make.centerX.equalTo(self.bottomBgView.snp.centerX)
make.top.equalTo(self.integralCountLabel.snp.bottom).offset(5)
make.width.lessThanOrEqualToSuperview()
}
}else if self.currentIntegralPayStatus == .integralNotEnough{
//积分不足
self.bottomBgView.addSubview(self.albumTitleLabel)
self.albumTitleLabel.text = self.title
self.bottomBgView.addSubview(self.closeBtn)
self.bottomBgView.addSubview(self.bottomLineView)
self.bottomBgView.addSubview(self.integralCountLabel)
let integralCountStr = "\(self.integralCount)积分"
let priceAttri = NSMutableAttributedString.init(string: integralCountStr)
priceAttri.addAttribute(.font, value: regular_adapt_font(pointSize: 55), range: NSMakeRange(0, integralCountStr.count-2))
priceAttri.addAttribute(.foregroundColor, value: HexColor("#333333")!, range: NSMakeRange(0, integralCountStr.count-2))
priceAttri.addAttribute(.font, value: regular_adapt_font(pointSize: 16), range: NSMakeRange(integralCountStr.count-2, 2))
priceAttri.addAttribute(.foregroundColor, value: HexColor("#333333")!, range: NSMakeRange(integralCountStr.count-2, 2))
self.integralCountLabel.attributedText = priceAttri
self.bottomBgView.addSubview(self.balanceIntegralLabel)
self.balanceIntegralLabel.text = String(format: "余额%d积分 积分不足", self.point)
self.bottomBgView.addSubview(self.payBtn)
self.payBtn.setTitle("去赚积分", for: .normal)
self.integralCountLabel.snp.makeConstraints { make in
make.centerX.equalTo(self.bottomBgView.snp.centerX)
make.top.equalTo(self.bottomLineView.snp.bottom).offset(30)
make.width.lessThanOrEqualToSuperview().offset(-10)
}
self.balanceIntegralLabel.snp.makeConstraints { make in
make.centerX.equalTo(self.bottomBgView.snp.centerX)
make.top.equalTo(self.integralCountLabel.snp.bottom).offset(5)
make.width.lessThanOrEqualToSuperview()
}
}else if self.currentIntegralPayStatus == .paying{
//支付中
self.bottomBgView.addSubview(self.albumTitleLabel)
self.albumTitleLabel.text = self.title
self.bottomBgView.addSubview(self.closeBtn)
self.bottomBgView.addSubview(self.bottomLineView)
self.bottomBgView.addSubview(self.payingImgView)
self.payingImgView = UIImageView(frame: CGRect(x: (KSreenWidth-45)/2.0, y: self.bottomLineView.bottom+65, width: 45, height: 43))
self.payingImgView.image = UIImage(named: "jifen_paying")
self.bottomBgView.addSubview(self.payingLabel)
self.payingLabel.text = "支付中..."
}else if self.currentIntegralPayStatus == .paySuccess{
//支付成功
self.bottomBgView.addSubview(self.paySuccessImgView)
self.paySuccessImgView = UIImageView(frame: CGRect(x: (KSreenWidth-78)/2.0, y: 60, width: 78, height: 78))
self.paySuccessImgView.image = UIImage(named: "jifen_paySuccess")
self.bottomBgView.addSubview(self.payingLabel)
self.payingLabel.text = "支付成功\(self.integralCount)积分"
self.bottomBgView.addSubview(self.payBtn)
self.payBtn.setTitle("完成", for: .normal)
}else if self.currentIntegralPayStatus == .payFailed{
//支付失败
}else{
//其他
}
}
//获取用户积分
func getCheckIn(resultBlock: @escaping downSlideResultBlock){
CNLiveMineViewModel.requestWithCheckIn { result, respStatue in
if respStatue == .success {
let checkModel = result as! MinePointCheckInModel
resultBlock(checkModel, .success)
} else {
resultBlock(NSNull(), .failed)
}
}
}
// MARK: - BtnAction
@objc func bgBtnViewAction(){
self.hiddenIntegralAlert()
}
@objc func closeBtnAction(){
self.hiddenIntegralAlert()
}
@objc func payBtnAction(){
if self.currentIntegralPayStatus == .integralEnough{
self.postOrderVideoByPoint()
}else if self.currentIntegralPayStatus == .integralNotEnough{
self.hiddenIntegralAlert()
//跳转任务中心
navigationViewController().pushViewController(CNLiveMineQuestViewController(checkModel: self.checkInModel!), animated: pushAnimated)
}else if self.currentIntegralPayStatus == .paying{
}else if self.currentIntegralPayStatus == .paySuccess{
self.hiddenIntegralAlert()
}else if self.currentIntegralPayStatus == .payFailed{
self.currentIntegralPayStatus = .integralEnough
self.initUI()
}else{
}
}
// MARK: - 积分付费接口
func postOrderVideoByPoint(){
self.currentIntegralPayStatus = .paying
self.initUI()
//付费接口
CNLiveHomePageViewModel.orderVideoByPoint(prdId: self.productId) { result, status in
if status == .success{
//购买成功
self.currentIntegralPayStatus = .paySuccess
self.initUI()
//刷新短剧首页数据
self.homePageNeedReloadActionBlock!()
}else{
//购买失败
if self.point > Int(self.integralCount)!{
//可以支付积分
self.currentIntegralPayStatus = .integralEnough
}else{
//积分不足
self.currentIntegralPayStatus = .integralNotEnough
}
self.initUI()
let errMsg = result as! String
showMessage(message: errMsg)
}
}
}
}