Files
yingmeng-ios-switf/yinmeng-ios/Modules/User/VC/BindMobileVC.swift
2024-03-01 20:37:00 +08:00

284 lines
7.8 KiB
Swift

//
// BindMobileVC.swift
// yinmeng-ios
//
// Created by MaiMang on 2024/3/1.
//
import UIKit
class BindMobileVC: BaseViewController, HiddenNavigationBarProtocol {
var phone:String = ""
var code:String = ""
var countdownSeconds = 60
var timer: Timer?
var vm = AuthViewModel.authVM
override func viewDidLoad() {
super.viewDidLoad()
title = "绑定手机号"
vm.data.subscribe(onNext: { [weak self] success in
if success {
HUDTool.show(with: "验证码已发送\n+86\(self?.phone ?? "")")
self?.startCountdown()
}
}).disposed(by: rx.disposeBag)
view.addSubview(backImgView)
view.addSubview(phoneNumView)
view.addSubview(backBtn)
view.addSubview(titleLb)
phoneNumView.addSubview(areaLb)
phoneNumView.addSubview(phonetextFiled)
codeView.addSubview(codetextFiled)
codeView.addSubview(sepView)
codeView.addSubview(getCodeBtn)
backImgView.snp.makeConstraints { make in
make.edges.equalTo(view)
}
backBtn.snp.makeConstraints { make in
make.size.equalTo(CGSize(width: 20, height: 20))
make.left.equalTo(view).offset(16)
make.top.equalTo(view).offset(StatusBarHeight + 12)
}
titleLb.snp.makeConstraints { make in
make.centerX.equalToSuperview()
make.centerY.equalTo(backBtn)
}
phoneNumView.snp.makeConstraints { make in
make.left.right.equalToSuperview().inset(36)
make.height.equalTo(52)
make.top.equalToSuperview().offset(60)
}
areaLb.snp.makeConstraints { make in
make.centerY.equalTo(phoneNumView)
make.left.equalTo(phoneNumView).offset(20)
}
phonetextFiled.snp.makeConstraints { make in
make.top.bottom.equalTo(phoneNumView)
make.left.equalTo(phoneNumView).offset(82)
}
codeView.snp.makeConstraints { make in
make.left.right.height.equalTo(phoneNumView)
make.top.equalTo(phoneNumView.snp.bottom).offset(20)
}
codetextFiled.snp.makeConstraints { make in
make.left.equalTo(codeView).offset(20)
make.top.bottom.equalTo(codeView)
}
sepView.snp.makeConstraints { make in
make.size.equalTo(CGSize(width: 1, height: 20))
make.centerY.equalTo(codeView)
make.right.equalTo(getCodeBtn.snp.left).offset(-16)
}
getCodeBtn.snp.makeConstraints { make in
make.top.bottom.equalTo(codeView)
make.right.equalTo(codeView).offset(-18)
}
confirmBtn.snp.makeConstraints { make in
make.left.right.equalToSuperview().inset(36)
make.top.equalTo(codeView.snp.bottom).offset(30)
make.height.equalTo(52)
}
}
@objc func backBtnAction() {
self.navigationController?.popViewController(animated: true)
}
@objc func confirmBtnAction() {
if phone.count > 0 {
if code.count > 0 {
if let phoneDes = phone.encrypt() {
let params = ["phone": phoneDes, "code":code, "ticket": AuthManager.ticket]
RequestPost(path: "withDraw/phone", parma: params) { _ in
HUDTool.show(with: "绑定成功")
self.navigationController?.popViewController(animated: true)
} fail: { _, msg in
HUDTool.show(with: msg)
}
}
} else {
HUDTool.show(with: "请输入正确的手机号")
}
} else {
HUDTool.show(with: "请输入正确的手机号")
}
}
@objc func getCodeBtnAction() {
if phone.count > 0 {
vm.getSmsCode(phone: phone, type: 4)
} else {
HUDTool.show(with: "请输入正确的手机号码")
}
}
@objc func phoneTextFiledDidChange(_ textField: UITextField) {
if let text = textField.text {
if text.count > 11 {
textField.text = text.substring(start: 0, 11)
}
}
phone = textField.text ?? ""
}
@objc func codeTextFiledDidChange(_ textField: UITextField) {
if let text = textField.text {
if text.count > 11 {
textField.text = text.substring(start: 0, 11)
}
}
code = textField.text ?? ""
}
private lazy var backImgView: UIImageView = {
let imageView = UIImageView()
imageView.image = UIImage(named: "auth_login_bg")
imageView.isUserInteractionEnabled = true
imageView.layer.masksToBounds = true
imageView.contentMode = .scaleAspectFill
return imageView
}()
private lazy var backBtn: UIButton = {
let button = UIButton(type: .custom)
button.setImage(UIImage(named: "public_back_white"), for: .normal)
button.setImage(UIImage(named: "public_back_white"), for: .selected)
button.addTarget(self, action: #selector(backBtnAction), for: .touchUpInside)
return button
}()
private lazy var titleLb: UILabel = {
let label = UILabel()
label.textColor = .white
label.text = "绑定手机号"
label.font = UIFont.systemFont(ofSize: 18, weight: .medium)
return label
}()
private lazy var phoneNumView: UIView = {
let view = UIView()
view.backgroundColor = .white
view.layer.masksToBounds = true
view.layer.cornerRadius = 26
return view
}()
private lazy var areaLb: UILabel = {
let label = UILabel()
label.textColor = .firstText
label.text = "+86"
label.font = UIFont.systemFont(ofSize: 16)
return label
}()
lazy var phonetextFiled:UITextField = {
let view = UITextField()
view.textColor = .firstText
view.font = UIFont.systemFont(ofSize: 16)
let attribute = NSMutableAttributedString(string: "请输入手机号码", attributes: [NSAttributedString.Key.font:UIFont.systemFont(ofSize: 16), NSAttributedString.Key.foregroundColor:UIColor.placeholderText])
view.attributedPlaceholder = attribute
view.keyboardType = .numberPad
view.tintColor = ThemeColor(hexStr: "#282828")
view.addTarget(self, action: #selector(phoneTextFiledDidChange), for: .editingChanged)
return view
}()
private lazy var codeView: UIView = {
let view = UIView()
view.backgroundColor = .white
view.layer.masksToBounds = true
view.layer.cornerRadius = 26
return view
}()
lazy var codetextFiled:UITextField = {
let view = UITextField()
view.textColor = .firstText
view.font = UIFont.systemFont(ofSize: 16)
let attribute = NSMutableAttributedString(string: "请输入验证码", attributes: [NSAttributedString.Key.font:UIFont.systemFont(ofSize: 16), NSAttributedString.Key.foregroundColor: UIColor.placeholderText])
view.attributedPlaceholder = attribute
view.keyboardType = .numberPad
view.tintColor = ThemeColor(hexStr: "#282828")
view.addTarget(self, action: #selector(codeTextFiledDidChange), for: .editingChanged)
return view
}()
private lazy var sepView: UIView = {
let view = UIView()
view.backgroundColor = .clear
return view
}()
private lazy var getCodeBtn: UIButton = {
let button = UIButton(type: .custom)
button.setTitle("获取验证码", for: .normal)
button.setTitleColor(ThemeColor(hexStr: "#9552FF"), for: .normal)
button.setTitleColor(ThemeColor(hexStr: "#878B9C"), for: .disabled)
button.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .regular)
button.addTarget(self, action: #selector(getCodeBtnAction), for: .touchUpInside)
return button
}()
private lazy var confirmBtn: UIButton = {
let button = UIButton(type: .custom)
button.setBackgroundImage(UIImage.gradient(hexsString: ["#FF60FD", "#8974FF", "#69EBFF"]), for: .normal)
button.setImage(UIImage(named: "auth_login_confirm"), for: .normal)
button.layer.masksToBounds = true
button.layer.cornerRadius = 26
button.addTarget(self, action: #selector(confirmBtnAction), for: .touchUpInside)
return button
}()
}
extension BindMobileVC {
func startCountdown() {
if timer != nil {
timer?.invalidate()
timer = nil
}
countdownSeconds = 60
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(updateCountdown), userInfo: nil, repeats: true)
timer!.fire()
}
@objc func updateCountdown() {
countdownSeconds -= 1
if countdownSeconds <= 0 {
getCodeBtn.setTitle("获取验证码", for: .normal)
getCodeBtn.isEnabled = true
stopCountdown()
return
}
getCodeBtn.isHidden = false
let seconds = countdownSeconds % 60
getCodeBtn.setTitle("\(seconds)s", for: .disabled)
getCodeBtn.isEnabled = false
}
func stopCountdown() {
timer?.invalidate()
timer = nil
}
}