// // ResetPwdFirstStepViewController.swift // MTP2_iOS // // Created by Handy_Cao on 2020/3/23. // Copyright © 2020 Muchinfo. All rights reserved. // import UIKit import WHToast import NVActivityIndicatorView /// 重置密码第一步视图容器控制类 class ResetPwdFirstStepViewController: BaseViewController { /// 验证码按钮 @IBOutlet weak var areaCodeButton: UIButton! { didSet { areaCodeButton.setTitle("+86", for: .normal) } } /// 手机号码输入框 @IBOutlet weak var mobilePhoneField: UITextField! { didSet { mobilePhoneField.attributedPlaceholder = "请输入手机号码".withTextColor(Color_placeholder_text) } } /// 验证码输入框 @IBOutlet weak var captchaCodeField: UITextField! { didSet { captchaCodeField.attributedPlaceholder = "请输入短信验证码".withTextColor(Color_placeholder_text) } } /// timerLabel @IBOutlet weak var timerLabel: UILabel! /// 更多按钮 @IBOutlet weak var captchaButton: UIButton! /// 下一步按钮 @IBOutlet weak var nextButton: UIButton! /// 区号 var areaCode: String = "+86" /// Timer ID private let captchaTimerID = "captchaTimerID" /// 验证倒计时 private var captchaCD = 60 /// 短信验证码 var captchaCode: String = "" /// 第二步 lazy var secondStepViewController: ResetPwdSecondStepViewController = { $0.popBlock = { [weak self] (_ takeInfo: String) in self?.dismiss(animated: true, completion: nil) } return $0 }(viewController(name: "ResetPwd", identifier: "ResetPwdSecondStepID") as! ResetPwdSecondStepViewController) // MARK: - 生命周期相关 override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } // MARK: - 交互相关 @IBAction func onButtonPressed(_ sender: UIButton) { switch sender { case nextButton: /// 下一步 doSubmit() case captchaButton: /// 发送验证码 /// 合规性校验 if !checkInputValue() { return } /// 图形验证码 self.performSegue(withIdentifier: "ShowImageCode", sender: nil) case areaCodeButton: /// 区域号视图 goToAreaCodeController() default: self.dismiss(animated: true, completion: {}) } } /// 输入框合规性校验 private func checkInputValue() -> Bool { /// 手机号码 guard let mobilePhone = mobilePhoneField.text, mobilePhone != "" else { WHToast.showError(withMessage: "请输入手机号码", duration: 1.0, finishHandler: {}) return false } /// 请输入正确的手机号码 if areaCode == "+86" { guard mobilePhone.count == 11 else { WHToast.showError(withMessage: "请输入正确的手机号码", duration: 1.0, finishHandler: {}) return false } } return true } /// 证件号码合规性校验 private func checkCardNumInputValue() -> Bool { /// 证件号码 guard let code = captchaCodeField.text, code != "" else { WHToast.showError(withMessage: "请输入短信验证码", duration: 1.0, finishHandler: {}) return false } /// 短信验证码输入不正确 guard code == captchaCode else { WHToast.showError(withMessage: "短信验证码输入不正确", duration: 1.0, finishHandler: {}) return false } return true } /// 区域号视图 private func goToAreaCodeController() { let mobileArearCodeViewController = viewController(name: "ResetPwd", identifier: "MobileAreaCodeID") as! MobileAreaCodeViewController /// 设置手机区号 mobileArearCodeViewController.popBlock = { [weak self] (_ code: String) in DispatchQueue.main.async { self?.areaCode = code self?.areaCodeButton.setTitle(code, for: .normal) } } self.navigationController?.pushViewController(mobileArearCodeViewController, animated: true) } // MARK: - 接口相关 /// 发送短信验证码的方法 private func sendCaptcha() { /// 异常 guard let accountManager = MTP2BusinessCore.shared.accountManager, checkInputValue() else { return } /// 手机号码 let mobile = mobilePhoneField.text ?? "" accountManager.requestSendMobileCaptchaCode(mobile, areaCode, callback: { (isCompleted, errorInfo, ShowRefer, vcode) in DispatchQueue.main.async { if !isCompleted { self.showHintController(title: "提示", message: errorInfo?.retMsg ?? "发送失败,请稍后重试") return } else { /// 短信验证码 self.captchaCode = vcode ?? "" debugPrint("短信验证码:\(vcode ?? "")") } self.setCaptcha(enable: false) } }) } // MARK: - 定时器相关 /// 设置验证码按钮状态的方法 /// /// - Parameter enable: 是否可用,如为不可用则开始60s倒计时 private func setCaptcha(enable: Bool) { if enable { MCGCDTimer.shared.cancleTimer(WithTimerName: self.captchaTimerID) DispatchQueue.main.async { self.captchaButton.isEnabled = true self.captchaButton.setTitle("获取验证码", for: .normal) self.timerLabel.isHidden = true } } else { DispatchQueue.main.async { self.captchaCD = 60 self.captchaButton.isEnabled = false self.captchaButton.setTitle("", for: .disabled) self.timerLabel.text = "\(self.captchaCD)秒" self.timerLabel.isHidden = false self.captchaCodeField.becomeFirstResponder() MCGCDTimer.shared.cancleTimer(WithTimerName: self.captchaTimerID) MCGCDTimer.shared.scheduledDispatchTimer(WithTimerName: self.captchaTimerID, timeInterval: 1.0, queue: .main, repeats: true, isNowRun: false) { DispatchQueue.main.async { self.captchaCD -= 1 self.timerLabel.text = "\(self.captchaCD)秒" if self.captchaCD <= 0 { self.setCaptcha(enable: true) } } } } } } /// 下一步操作 private func doSubmit() { /// 异常 guard let accountManager = MTP2BusinessCore.shared.accountManager, checkInputValue(), checkCardNumInputValue() else { return } /// 验证码 let vcode = captchaCodeField.text ?? "" /// 手机号码 let mobilePhone = mobilePhoneField.text ?? "" /// 开启Loading NVActivityIndicatorPresenter.sharedInstance.startAnimating(ActivityData(message: "查询数据中......", type: .ballClipRotate, color: UIColorFromHex(rgbValue: 0xA8B6CC)), NVActivityIndicatorView.DEFAULT_FADE_IN_ANIMATION) /// 查询登录账号信息 accountManager.queryLoginId("", mobilePhone, areaCode, vcode, callback: { (isComplete, errorInfo, loginId) in DispatchQueue.main.async { /// stop loding..... NVActivityIndicatorPresenter.sharedInstance.stopAnimating(NVActivityIndicatorView.DEFAULT_FADE_OUT_ANIMATION) if isComplete, let id = loginId, id != "" { /// 成功 /// 进入第二步视图校验 self.navigationController?.pushViewController(self.secondStepViewController, animated: true) /// 登录账号 self.secondStepViewController.datas.append(contentsOf: [(key: "loginId", value: id), (key: "vcode", value: vcode), (key: "mobilePhone", value: mobilePhone)]) /// 区号 self.secondStepViewController.areaCode = self.areaCode } else { WHToast.showError(withMessage: errorInfo?.retMsg ?? "查询登录账号失败,请稍候再试!", duration: 1.5, finishHandler: {}) } } }) } // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepare(for segue: UIStoryboardSegue, sender: Any?) { // Get the new view controller using segue.destination. // Pass the selected object to the new view controller. if segue.identifier == "ShowImageCode" { (segue.destination as? ImageCodeViewController)?.popBlock = { [weak self] (_ takeInfo: String?) in /// 发送验证码 self?.sendCaptcha() } } } }