CNLiveMineChangePhoneController.swift
12.5 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
//
// CNLiveMineChangePhoneController.swift
// metaCode
//
// Created by zx on 2024/4/28.
// 更换手机号
import Foundation
import APButton
import SnapKit
class CNLiveMineChangePhoneController:CNLiveBaseViewController{
lazy var countryView :UIView = {
let countryView = UIView()
countryView.backgroundColor = .white
countryView.layer.cornerRadius = 9
countryView.clipsToBounds = true
return countryView
}()
lazy var countryLabel: UILabel = {
let titleLabel = UILabel()
titleLabel.textColor = HexColor("#333333")
titleLabel.text = "中国"
titleLabel.font = Font_15
return titleLabel
}()
lazy var moreImgView :UIImageView = {
let moreImgView = UIImageView()
moreImgView.image = UIImage(named: "mine_set_band_detail")
return moreImgView
}()
lazy var areaButton : UIButton = {
let areaButton = UIButton(type: .custom)
areaButton.backgroundColor = .white
areaButton.setTitle("+86", for: .normal)
areaButton.setTitle("+86", for: .highlighted)
areaButton.titleLabel?.font = BoldFont_16
areaButton.setTitleColor(HexColor("#333333"), for: .normal)
areaButton.setTitleColor(HexColor("#333333"), for: .highlighted)
areaButton.addTarget(self, action: #selector(changeCityNumberMothod), for: .touchUpInside)
return areaButton
}()
lazy var phoneCodeView :UIView = {
let countryView = UIView()
countryView.backgroundColor = .white
countryView.layer.cornerRadius = 9
countryView.clipsToBounds = true
return countryView
}()
lazy var phoneLabel: UILabel = {
let titleLabel = UILabel()
titleLabel.textColor = HexColor("#333333")
titleLabel.text = "手机号"
titleLabel.font = Font_15
return titleLabel
}()
lazy var phoneText : UITextField = {
let phoneText = UITextField.init()
phoneText.backgroundColor = .white
phoneText.placeholder = "输入手机号"
phoneText.font = BoldFont_16_Fit
phoneText.keyboardType = .numberPad
phoneText.clearButtonMode = .whileEditing
return phoneText
}()
lazy var phoneLineView :UIView = {
let countryView = UIView()
countryView.backgroundColor = HexColor("#F3F3F3")
return countryView
}()
lazy var codeLabel: UILabel = {
let titleLabel = UILabel()
titleLabel.textColor = HexColor("#333333")
titleLabel.text = "验证码"
titleLabel.font = Font_15
return titleLabel
}()
lazy var veriText: UITextField = {
let veriText = UITextField.init()
veriText.backgroundColor = .white
veriText.placeholder = "输入验证码"
veriText.font = BoldFont_16_Fit
veriText.keyboardType = .numberPad
veriText.clearButtonMode = .whileEditing
return veriText
}()
lazy var loginButton: APButton = {
let loginButton = APButton()
loginButton.setTitle("更换手机号", for: .normal)
loginButton.setTitleColor(.white, for: .normal)
loginButton.titleLabel?.font = BoldFont_16_Fit
loginButton.layer.cornerRadius = 20
loginButton.activityIndicator.color = .white
loginButton.layer.masksToBounds = true
loginButton.addTarget(self, action: #selector(sendLoginActionMothod), for: .touchUpInside)
return loginButton
}()
var countryCode: String = "86"
var countryStr: String = "中国"
var timeOut = 30//多少秒计时器
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
setNeedsStatusBarAppearanceUpdate()
view.backgroundColor = HexColor(kCNDownSlideBackgroundColorHex)
navigationBarHidden = false
titleName = "更换手机号"
self.setupCountryView()
self.setupPhoneView()
self.setupVeriCodeView()
self.setupLoginButtonView()
}
deinit {
print("已经销毁了")
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
func setupCountryView() {
self.view.addSubview(self.countryView)
self.countryView.snp.makeConstraints { make in
make.left.equalToSuperview().offset(15)
make.right.equalToSuperview().offset(-15)
make.top.equalTo(KNavigationHeight+15)
make.height.equalTo(61)
}
self.countryView.addSubview(self.countryLabel)
self.countryLabel.snp.makeConstraints { make in
make.centerY.equalToSuperview()
make.left.equalToSuperview().offset(15)
}
self.countryView.addSubview(self.moreImgView)
self.moreImgView.snp.makeConstraints { make in
make.centerY.equalToSuperview()
make.right.equalToSuperview().offset(-15)
make.width.height.equalTo(13)
}
self.countryView.addSubview(self.areaButton)
self.areaButton.snp.makeConstraints { make in
make.centerY.equalToSuperview()
make.right.equalTo(self.moreImgView.snp.left)
make.width.lessThanOrEqualTo(50)
}
}
func setupPhoneView() {
self.view.addSubview(self.phoneCodeView)
self.phoneCodeView.snp.makeConstraints { make in
make.left.equalToSuperview().offset(15)
make.right.equalToSuperview().offset(-15)
make.top.equalTo(self.countryView.snp.bottom).offset(15)
make.height.equalTo(118)
}
self.phoneCodeView.addSubview(self.phoneLabel)
self.phoneLabel.snp.makeConstraints { make in
make.left.equalToSuperview().offset(15)
make.top.equalToSuperview().offset(20)
make.width.equalTo(50)
make.height.equalTo(21)
}
self.phoneCodeView.addSubview(self.phoneText)
self.phoneText.snp.makeConstraints { make in
make.centerY.equalTo(phoneLabel.snp.centerY)
make.left.equalTo(phoneLabel.snp.right).offset(19)
make.height.equalTo(21)
make.right.equalToSuperview().offset(-15)
}
self.phoneCodeView.addSubview(self.phoneLineView)
self.phoneLineView.snp.makeConstraints { make in
make.left.equalToSuperview().offset(15)
make.right.equalToSuperview().offset(-15)
make.centerY.equalToSuperview()
make.height.equalTo(0.5)
}
}
func setupVeriCodeView() {
self.phoneCodeView.addSubview(self.codeLabel)
self.codeLabel.snp.makeConstraints { make in
make.left.equalToSuperview().offset(15)
make.top.equalTo(self.phoneLineView.snp.bottom).offset(15)
make.width.equalTo(50)
make.height.equalTo(21)
}
let codeButton = UIButton(type: .custom)
codeButton.backgroundColor = .white
codeButton.setTitle("获取验证码", for: .normal)
codeButton.setTitle("获取验证码", for: .highlighted)
codeButton.titleLabel?.font = Font_13
codeButton.setTitleColor(HexColor("#FEFFFF"), for: .normal)
codeButton.setTitleColor(HexColor("#FEFFFF"), for: .highlighted)
codeButton.layer.cornerRadius = 15.5
codeButton.clipsToBounds = true
// fillCode
let bgLayer1 = CAGradientLayer()
bgLayer1.colors = [UIColor(red: 1, green: 0.84, blue: 0.38, alpha: 1).cgColor, UIColor(red: 1, green: 0.55, blue: 0.21, alpha: 1).cgColor]
bgLayer1.locations = [0, 1]
bgLayer1.frame = CGRect(x: 0, y: 0, width: 100, height: 31)
bgLayer1.startPoint = CGPoint(x: 0, y: 0.5)
bgLayer1.endPoint = CGPoint(x: 0.5, y: 0.5)
codeButton.layer.insertSublayer(bgLayer1, at: 0)
codeButton.addTarget(self, action: #selector(sendVerifiCodeMothod), for: .touchUpInside)
self.phoneCodeView.addSubview(codeButton)
codeButton.snp.makeConstraints { make in
make.centerY.equalTo(self.codeLabel.snp.centerY)
make.right.equalToSuperview().offset(-15)
make.width.lessThanOrEqualTo(100)
make.height.equalTo(31)
}
self.phoneCodeView.addSubview(veriText)
veriText.snp.makeConstraints { make in
make.centerY.equalTo(self.codeLabel.snp.centerY)
make.right.equalTo(codeButton.snp.left).offset(-20)
make.height.equalTo(codeButton.snp.height)
make.left.equalTo(self.codeLabel.snp.right).offset(19)
}
}
func setupLoginButtonView() {
view.addSubview(loginButton)
loginButton.snp.makeConstraints { make in
make.centerX.equalTo(view)
make.top.equalTo(veriText.snp.bottom).offset(adapt_length(length: 105))
make.left.equalTo(40)
make.right.equalTo(-40)
make.height.equalTo(40)
}
loginButton.setGradient(colors: [UIColor(red: 103/255.0, green: 197/255.0, blue: 255/255.0, alpha: 1),UIColor(red: 96/255.0, green: 154/255.0, blue: 255/255.0, alpha: 1)], startPoint: CGPoint(x: 0, y: 0.5), endPoint: CGPoint(x: 1, y: 0.5))
}
@objc func changeCityNumberMothod(sender : UIButton) {
CNLiveLoginPickerView.showSelectView { [weak self] codeStr,countryStr in
self!.countryCode = codeStr
self!.countryStr = countryStr
self!.areaButton.setTitle("+\(codeStr)", for: .normal)
self!.areaButton.setTitle("+\(codeStr)", for: .highlighted)
self?.countryLabel.text = countryStr
}
}
@objc func sendVerifiCodeMothod(sender : UIButton) {
view.endEditing(true)
CNLiveLoginViewModel.requestChangePhoneVerCodeAction(phone: phoneText.text ?? "", code: countryCode) { [weak self] in
self!.setupCountdownAction(sender: sender)
}
}
@objc func sendLoginActionMothod(sender : UIButton) {
view.endEditing(true)
if phoneText.text ?? "" == ""{
showMessage(message: "请输入手机号")
return
}
if veriText.text ?? "" == ""{
showMessage(message: "请输入验证码")
return
}
if veriText.text?.count != 6{
showMessage(message: "请输入正确的验证码")
return
}
showLoading(message: "")
// 绑定手机号请求登录
CNLiveMineViewModel.sendChangePhone(mobile: phoneText.text ?? "", countryCode: self.countryCode, verificationCode: veriText.text ?? "") { result, status in
if status == .success{
let userInfo = UserInfoManager.shared.userInfo
userInfo.mobile = self.phoneText.text ?? ""
userInfo.countryName = self.countryStr
userInfo.countryCode = self.countryCode
UserInfoManager.cacheUserInfo(userInfo: userInfo)
showMessage(message: "更改成功")
self.navigationController?.popViewController(animated: true)
}else{
}
}
}
/// 设置发送验证码按钮
/// - Parameter sender: 按钮
private func setupCountdownAction(sender : UIButton) {
var timeOut = self.timeOut
let queue: DispatchQueue = DispatchQueue.global(qos: DispatchQoS.QoSClass.default)
let _timer: DispatchSource = DispatchSource.makeTimerSource(flags: [], queue: queue) as! DispatchSource
_timer.schedule(wallDeadline: DispatchWallTime.now(), repeating: .seconds(1))
//一秒执行
_timer.setEventHandler(handler: { () -> Void in
if timeOut <= 0 {
_timer.cancel()
DispatchQueue.main.sync(execute: { () -> Void in
sender.setTitle("获取验证码", for: .normal)
sender.isEnabled = true
})
} else {
let seconds = timeOut
DispatchQueue.main.sync(execute: {() -> Void in
let str = String(describing: seconds)
sender.setTitle("\(str)s重新获取", for: .normal)
sender.isEnabled = false
})
timeOut -= 1
}
})
_timer.resume()
}
}