308 lines
8.8 KiB
Swift
308 lines
8.8 KiB
Swift
//
|
||
// AuthForgetVC.swift
|
||
// yinmeng-ios
|
||
//
|
||
// Created by MaiMang on 2024/2/21.
|
||
//
|
||
|
||
import UIKit
|
||
|
||
class AuthForgetVC: BaseViewController, HiddenNavigationBarProtocol {
|
||
var countdownSeconds = 60
|
||
var timer: Timer?
|
||
var viewModel:AuthViewModel = AuthViewModel()
|
||
var phone:String = ""
|
||
var code:String = ""
|
||
var password:String = ""
|
||
override func viewDidLoad() {
|
||
super.viewDidLoad()
|
||
loadSubViews()
|
||
viewModel.resetPwd.subscribe(onNext: { success in
|
||
if success {
|
||
HUDTool.show(with: "重置密码成功")
|
||
self.navigationController?.popViewController(animated: true)
|
||
}
|
||
}).disposed(by: rx.disposeBag)
|
||
|
||
viewModel.data.subscribe(onNext: { [weak self] success in
|
||
if success {
|
||
HUDTool.show(with: "验证码已发送\n+86\(self?.phone ?? "")")
|
||
self?.startCountdown()
|
||
}
|
||
}).disposed(by: rx.disposeBag)
|
||
}
|
||
|
||
private func loadSubViews() {
|
||
view.addSubview(backImgView)
|
||
view.addSubview(backBtn)
|
||
view.addSubview(titleLb)
|
||
view.addSubview(phoneTextFiled)
|
||
view.addSubview(codeView)
|
||
view.addSubview(pwdTextFiled)
|
||
view.addSubview(confirmBtn)
|
||
|
||
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.equalTo(view)
|
||
make.centerY.equalTo(backBtn)
|
||
}
|
||
|
||
phoneTextFiled.snp.makeConstraints { make in
|
||
make.left.right.equalTo(view).inset(36)
|
||
make.height.equalTo(52)
|
||
make.top.equalTo(view).offset(StatusBarHeight + 94)
|
||
}
|
||
|
||
codeView.snp.makeConstraints { make in
|
||
make.left.right.height.equalTo(phoneTextFiled)
|
||
make.top.equalTo(phoneTextFiled.snp.bottom).offset(20)
|
||
}
|
||
|
||
codeTextFiled.snp.makeConstraints { make in
|
||
make.left.top.bottom.equalTo(codeView)
|
||
make.right.equalTo(sepView.snp.left)
|
||
}
|
||
|
||
getCodeBtn.snp.makeConstraints { make in
|
||
make.right.equalTo(codeView).offset(-18)
|
||
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)
|
||
}
|
||
|
||
pwdTextFiled.snp.makeConstraints { make in
|
||
make.left.right.height.equalTo(phoneTextFiled)
|
||
make.top.equalTo(codeView.snp.bottom).offset(20)
|
||
}
|
||
|
||
confirmBtn.snp.makeConstraints { make in
|
||
make.left.right.equalTo(view).inset(36)
|
||
make.height.equalTo(52)
|
||
make.top.equalTo(pwdTextFiled.snp.bottom).offset(40)
|
||
}
|
||
}
|
||
|
||
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.textAlignment = .center
|
||
label.text = "忘记密码"
|
||
label.font = UIFont.systemFont(ofSize: 18, weight: .medium)
|
||
return label
|
||
}()
|
||
|
||
lazy var phoneTextFiled:UITextField = {
|
||
let view = UITextField()
|
||
view.textColor = .firstText
|
||
view.font = UIFont.systemFont(ofSize: 16)
|
||
view.leftViewMode = .always
|
||
let leftView = UIView()
|
||
leftView.frame = CGRect(x: 0, y: 0, width: 20, height: 52)
|
||
leftView.backgroundColor = .clear
|
||
view.leftView = leftView
|
||
let attribute = NSMutableAttributedString(string: "请输入手机号", attributes: [NSAttributedString.Key.font:UIFont.systemFont(ofSize: 16), NSAttributedString.Key.foregroundColor:UIColor.placeholderText])
|
||
view.attributedPlaceholder = attribute
|
||
view.keyboardType = .numberPad
|
||
view.backgroundColor = .white
|
||
view.layer.masksToBounds = true
|
||
view.layer.cornerRadius = 26
|
||
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)
|
||
view.leftViewMode = .always
|
||
let leftView = UIView()
|
||
leftView.frame = CGRect(x: 0, y: 0, width: 20, height: 52)
|
||
leftView.backgroundColor = .clear
|
||
view.leftView = leftView
|
||
let attribute = NSMutableAttributedString(string: "请输入验证码", attributes: [NSAttributedString.Key.font:UIFont.systemFont(ofSize: 16), NSAttributedString.Key.foregroundColor:UIColor.placeholderText])
|
||
view.attributedPlaceholder = attribute
|
||
|
||
view.addTarget(self, action: #selector(codeTextFiledDidChange), for: .editingChanged)
|
||
return view
|
||
}()
|
||
|
||
private lazy var sepView: UIView = {
|
||
let view = UIView()
|
||
view.backgroundColor = UIColor.sepLine
|
||
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
|
||
}()
|
||
|
||
|
||
lazy var pwdTextFiled:UITextField = {
|
||
let view = UITextField()
|
||
view.textColor = .firstText
|
||
view.font = UIFont.systemFont(ofSize: 16)
|
||
view.isSecureTextEntry = true
|
||
view.leftViewMode = .always
|
||
let leftView = UIView()
|
||
leftView.frame = CGRect(x: 0, y: 0, width: 20, height: 52)
|
||
leftView.backgroundColor = .clear
|
||
view.leftView = leftView
|
||
let attribute = NSMutableAttributedString(string: "请输入密码(6-16字符)", attributes: [NSAttributedString.Key.font:UIFont.systemFont(ofSize: 16), NSAttributedString.Key.foregroundColor:UIColor.placeholderText])
|
||
view.attributedPlaceholder = attribute
|
||
view.backgroundColor = .white
|
||
view.layer.masksToBounds = true
|
||
view.layer.cornerRadius = 26
|
||
view.addTarget(self, action: #selector(pwdTextFiledDidChange), for: .editingChanged)
|
||
return view
|
||
}()
|
||
|
||
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
|
||
}()
|
||
|
||
|
||
@objc func backBtnAction() {
|
||
self.navigationController?.popViewController(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 pwdTextFiledDidChange(_ textField: UITextField) {
|
||
if let text = textField.text {
|
||
if text.count > 16 {
|
||
textField.text = text.substring(start: 0, 16)
|
||
}
|
||
}
|
||
password = textField.text ?? ""
|
||
}
|
||
|
||
@objc func codeTextFiledDidChange(_ textField: UITextField) {
|
||
if let text = textField.text {
|
||
if text.count > 5 {
|
||
textField.text = text.substring(start: 0, 5)
|
||
}
|
||
}
|
||
code = textField.text ?? ""
|
||
}
|
||
|
||
@objc func getCodeBtnAction() {
|
||
if phone.count > 0 {
|
||
viewModel.getSmsCode(phone: phone, type: 3)
|
||
} else {
|
||
HUDTool.show(with: "请输入正确的手机号码")
|
||
}
|
||
}
|
||
|
||
@objc func confirmBtnAction() {
|
||
if phone.count > 0 {
|
||
if code.count > 0 {
|
||
if password.count > 0 {
|
||
viewModel.resetPassword(phone: phone, newPwd: password, smsCode: code)
|
||
} else {
|
||
HUDTool.show(with: "请设置密码")
|
||
}
|
||
} else {
|
||
HUDTool.show(with: "请输入验证码")
|
||
}
|
||
} else {
|
||
HUDTool.show(with: "请输入正确的手机号")
|
||
}
|
||
}
|
||
}
|
||
|
||
extension AuthForgetVC {
|
||
|
||
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
|
||
}
|
||
}
|
||
|
||
|