Files
yingmeng-ios-switf/yinmeng-ios/Modules/Auth/VC/AuthLoginVC.swift
2024-02-25 12:53:56 +08:00

470 lines
13 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// AuthLoginVC.swift
// yinmeng-ios
//
// Created by MaiMang on 2024/2/21.
//
import UIKit
import SnapKit
import NSObject_Rx
class AuthLoginVC: BaseViewController, HiddenNavigationBarProtocol {
var countdownSeconds = 60
var timer: Timer?
var viewModel:AuthViewModel = AuthViewModel()
var phone:String = ""
var code:String = ""
override func viewDidLoad() {
super.viewDidLoad()
loadSubViews()
viewModel.data.subscribe(onNext: { [weak self] success in
if success {
HUDTool.show(with: "验证码已发送\n+86\(self?.phone ?? "")")
self?.startCountdown()
}
}).disposed(by: rx.disposeBag)
}
func loadSubViews() {
view.addSubview(backImgView)
view.addSubview(backBtn)
view.addSubview(phoneLoginBtn)
view.addSubview(pwdLoginBtn)
view.addSubview(segmentBtn)
view.addSubview(phoneView)
view.addSubview(idLoginView)
view.addSubview(confirmBtn)
phoneView.addSubview(phoneNumView)
phoneView.addSubview(codeView)
phoneNumView.addSubview(areaLb)
phoneNumView.addSubview(phonetextFiled)
codeView.addSubview(codetextFiled)
codeView.addSubview(sepView)
codeView.addSubview(getCodeBtn)
idLoginView.addSubview(idView)
idLoginView.addSubview(pwdView)
idLoginView.addSubview(forgetBtn)
idView.addSubview(idTextFiled)
pwdView.addSubview(pwdTextFiled)
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)
}
phoneLoginBtn.snp.makeConstraints { make in
make.height.equalTo(22)
make.top.equalTo(view).offset(StatusBarHeight + 88)
make.right.equalTo(view.snp.centerX).offset(-47)
}
pwdLoginBtn.snp.makeConstraints { make in
make.centerY.height.equalTo(phoneLoginBtn)
make.left.equalTo(view.snp.centerX).offset(47)
}
segmentBtn.snp.makeConstraints { make in
make.size.equalTo(CGSize(width: 43, height: 12))
make.centerX.equalTo(phoneLoginBtn)
make.top.equalTo(phoneLoginBtn.snp.bottom).offset(1)
}
phoneView.snp.makeConstraints { make in
make.left.right.equalTo(view).inset(0)
make.bottom.equalTo(codeView.snp.bottom).offset(0)
make.top.equalTo(phoneLoginBtn.snp.bottom).offset(52)
}
phoneNumView.snp.makeConstraints { make in
make.left.right.equalTo(phoneView).inset(36)
make.height.equalTo(52)
make.top.equalTo(phoneView).offset(0)
}
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)
}
idLoginView.snp.makeConstraints { make in
make.left.right.top.equalTo(phoneView)
make.bottom.equalTo(forgetBtn.snp.bottom).offset(0)
}
idView.snp.makeConstraints { make in
make.left.right.equalTo(idLoginView).inset(36)
make.height.equalTo(52)
make.top.equalTo(idLoginView).offset(0)
}
idTextFiled.snp.makeConstraints { make in
make.left.equalTo(idView).offset(20)
make.top.bottom.equalTo(idView)
}
pwdView.snp.makeConstraints { make in
make.left.right.equalTo(idLoginView).inset(36)
make.height.equalTo(52)
make.top.equalTo(idView.snp.bottom).offset(20)
}
pwdTextFiled.snp.makeConstraints { make in
make.left.equalTo(pwdView).offset(20)
make.top.bottom.equalTo(pwdView)
}
forgetBtn.snp.makeConstraints { make in
make.height.equalTo(20)
make.right.equalTo(idView)
make.top.equalTo(pwdView.snp.bottom).offset(8)
}
confirmBtn.snp.makeConstraints { make in
make.left.right.equalTo(view).inset(36)
make.height.equalTo(52)
make.top.equalTo(phoneView.snp.bottom).offset(44)
}
}
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 phoneLoginBtn: UIButton = {
let button = UIButton(type: .custom)
button.setTitle("手机号登录", for: .normal)
button.setTitleColor(UIColor.white, for: .normal)
button.titleLabel?.font = UIFont.systemFont(ofSize: 18, weight: .semibold)
button.addTarget(self, action: #selector(phoneLoginBtnAction), for: .touchUpInside)
button.isSelected = true
return button
}()
private lazy var pwdLoginBtn: UIButton = {
let button = UIButton(type: .custom)
button.setTitle("账号登录", for: .normal)
button.setTitleColor(UIColor.white, for: .normal)
button.titleLabel?.font = UIFont.systemFont(ofSize: 18, weight: .semibold)
button.addTarget(self, action: #selector(pwdLoginBtnAction), for: .touchUpInside)
return button
}()
private lazy var segmentBtn: UIButton = {
let button = UIButton(type: .custom)
button.setImage(UIImage(named: "auth_segment_bg"), for: .normal)
button.setImage(UIImage(named: "auth_segment_bg"), for: .selected)
return button
}()
private lazy var phoneView: UIView = {
let view = UIView()
view.backgroundColor = .clear
return view
}()
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.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .regular)
button.addTarget(self, action: #selector(getCodeBtnAction), for: .touchUpInside)
return button
}()
private lazy var idLoginView: UIView = {
let view = UIView()
view.backgroundColor = .clear
view.isHidden = true
return view
}()
private lazy var idView: UIView = {
let view = UIView()
view.backgroundColor = .white
view.layer.cornerRadius = 26
view.layer.masksToBounds = true
return view
}()
lazy var idTextFiled:UITextField = {
let view = UITextField()
view.textColor = .firstText
view.font = UIFont.systemFont(ofSize: 16)
let attribute = NSMutableAttributedString(string: "请输入账号/ID", 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(idTextFiledDidChange), for: .editingChanged)
return view
}()
private lazy var pwdView: UIView = {
let view = UIView()
view.backgroundColor = .white
view.layer.cornerRadius = 26
view.layer.masksToBounds = true
return view
}()
lazy var pwdTextFiled: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(pwdTextFiledDidChange), for: .editingChanged)
return view
}()
private lazy var forgetBtn: UIButton = {
let button = UIButton(type: .custom)
button.setTitle("忘记密码?", for: .normal)
button.setTitleColor(ThemeColor(hexStr: "#FFFFFF", alpha: 0.8), for: .normal)
button.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .medium)
button.addTarget(self, action: #selector(forgetBtnAction), 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 AuthLoginVC {
@objc func backBtnAction() {
self.navigationController?.popViewController(animated: true)
}
@objc func phoneLoginBtnAction() {
phoneLoginBtn.isSelected = true
pwdLoginBtn.isSelected = false
phoneView.isHidden = false
idLoginView.isHidden = true
phoneLoginBtn.titleLabel?.font = UIFont.systemFont(ofSize: 18, weight: .semibold)
pwdLoginBtn.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: .semibold)
segmentBtn.snp.remakeConstraints { make in
make.size.equalTo(CGSize(width: 43, height: 12))
make.centerX.equalTo(phoneLoginBtn)
make.top.equalTo(phoneLoginBtn.snp.bottom).offset(1)
}
}
@objc func pwdLoginBtnAction() {
pwdLoginBtn.isSelected = true
phoneLoginBtn.isSelected = false
phoneView.isHidden = true
idLoginView.isHidden = false
phoneLoginBtn.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: .semibold)
pwdLoginBtn.titleLabel?.font = UIFont.systemFont(ofSize: 18, weight: .semibold)
segmentBtn.snp.remakeConstraints { make in
make.size.equalTo(CGSize(width: 43, height: 12))
make.centerX.equalTo(pwdLoginBtn)
make.top.equalTo(pwdLoginBtn.snp.bottom).offset(1)
}
}
@objc func getCodeBtnAction() {
if phone.count > 0 {
viewModel.getSmsCode(phone: phone, type: 1)
} else {
HUDTool.show(with: "请输入正确的手机号码")
}
}
@objc func confirmBtnAction() {
if self.phoneLoginBtn.isSelected == true {
viewModel.authPhoneCode(phone: phone, code: code)
} else {
//TODO: id
}
}
@objc func forgetBtnAction() {
self.navigationController?.pushViewController(AuthFillDataVC(), animated: true)
}
@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 ?? ""
}
@objc func idTextFiledDidChange(_ textField: UITextField) {
}
@objc func pwdTextFiledDidChange(_ textField: UITextField) {
}
}
extension AuthLoginVC {
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
}
}