ResetPwdFirstStepViewController.swift 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. //
  2. // ResetPwdFirstStepViewController.swift
  3. // MTP2_iOS
  4. //
  5. // Created by Handy_Cao on 2020/3/23.
  6. // Copyright © 2020 Muchinfo. All rights reserved.
  7. //
  8. import UIKit
  9. import WHToast
  10. import NVActivityIndicatorView
  11. /// 重置密码第一步视图容器控制类
  12. class ResetPwdFirstStepViewController: BaseViewController {
  13. /// 验证码按钮
  14. @IBOutlet weak var areaCodeButton: UIButton! {
  15. didSet {
  16. areaCodeButton.setTitle("+86", for: .normal)
  17. }
  18. }
  19. /// 手机号码输入框
  20. @IBOutlet weak var mobilePhoneField: UITextField! {
  21. didSet {
  22. mobilePhoneField.attributedPlaceholder = "请输入手机号码".withTextColor(Color_placeholder_text)
  23. }
  24. }
  25. /// 验证码输入框
  26. @IBOutlet weak var captchaCodeField: UITextField! {
  27. didSet {
  28. captchaCodeField.attributedPlaceholder = "请输入短信验证码".withTextColor(Color_placeholder_text)
  29. }
  30. }
  31. /// timerLabel
  32. @IBOutlet weak var timerLabel: UILabel!
  33. /// 更多按钮
  34. @IBOutlet weak var captchaButton: UIButton!
  35. /// 下一步按钮
  36. @IBOutlet weak var nextButton: UIButton!
  37. /// 区号
  38. var areaCode: String = "+86"
  39. /// Timer ID
  40. private let captchaTimerID = "captchaTimerID"
  41. /// 验证倒计时
  42. private var captchaCD = 60
  43. /// 短信验证码
  44. var captchaCode: String = ""
  45. /// 第二步
  46. lazy var secondStepViewController: ResetPwdSecondStepViewController = {
  47. $0.popBlock = { [weak self] (_ takeInfo: String) in
  48. self?.dismiss(animated: true, completion: nil)
  49. }
  50. return $0
  51. }(viewController(name: "ResetPwd", identifier: "ResetPwdSecondStepID") as! ResetPwdSecondStepViewController)
  52. // MARK: - 生命周期相关
  53. override func viewDidLoad() {
  54. super.viewDidLoad()
  55. // Do any additional setup after loading the view.
  56. }
  57. // MARK: - 交互相关
  58. @IBAction func onButtonPressed(_ sender: UIButton) {
  59. switch sender {
  60. case nextButton: /// 下一步
  61. doSubmit()
  62. case captchaButton: /// 发送验证码
  63. /// 合规性校验
  64. if !checkInputValue() { return }
  65. /// 图形验证码
  66. self.performSegue(withIdentifier: "ShowImageCode", sender: nil)
  67. case areaCodeButton:
  68. /// 区域号视图
  69. goToAreaCodeController()
  70. default:
  71. self.dismiss(animated: true, completion: {})
  72. }
  73. }
  74. /// 输入框合规性校验
  75. private func checkInputValue() -> Bool {
  76. /// 手机号码
  77. guard let mobilePhone = mobilePhoneField.text, mobilePhone != "" else {
  78. WHToast.showError(withMessage: "请输入手机号码", duration: 1.0, finishHandler: {})
  79. return false
  80. }
  81. /// 请输入正确的手机号码
  82. if areaCode == "+86" {
  83. guard mobilePhone.count == 11 else {
  84. WHToast.showError(withMessage: "请输入正确的手机号码", duration: 1.0, finishHandler: {})
  85. return false
  86. }
  87. }
  88. return true
  89. }
  90. /// 证件号码合规性校验
  91. private func checkCardNumInputValue() -> Bool {
  92. /// 证件号码
  93. guard let code = captchaCodeField.text, code != "" else {
  94. WHToast.showError(withMessage: "请输入短信验证码", duration: 1.0, finishHandler: {})
  95. return false
  96. }
  97. /// 短信验证码输入不正确
  98. guard code == captchaCode else {
  99. WHToast.showError(withMessage: "短信验证码输入不正确", duration: 1.0, finishHandler: {})
  100. return false
  101. }
  102. return true
  103. }
  104. /// 区域号视图
  105. private func goToAreaCodeController() {
  106. let mobileArearCodeViewController = viewController(name: "ResetPwd", identifier: "MobileAreaCodeID") as! MobileAreaCodeViewController
  107. /// 设置手机区号
  108. mobileArearCodeViewController.popBlock = { [weak self] (_ code: String) in
  109. DispatchQueue.main.async {
  110. self?.areaCode = code
  111. self?.areaCodeButton.setTitle(code, for: .normal)
  112. }
  113. }
  114. self.navigationController?.pushViewController(mobileArearCodeViewController, animated: true)
  115. }
  116. // MARK: - 接口相关
  117. /// 发送短信验证码的方法
  118. private func sendCaptcha() {
  119. /// 异常
  120. guard let accountManager = MTP2BusinessCore.shared.accountManager,
  121. checkInputValue() else {
  122. return
  123. }
  124. /// 手机号码
  125. let mobile = mobilePhoneField.text ?? ""
  126. accountManager.requestSendMobileCaptchaCode(mobile, areaCode, callback: { (isCompleted, errorInfo, ShowRefer, vcode) in
  127. DispatchQueue.main.async {
  128. if !isCompleted {
  129. self.showHintController(title: "提示", message: errorInfo?.retMsg ?? "发送失败,请稍后重试")
  130. return
  131. } else {
  132. /// 短信验证码
  133. self.captchaCode = vcode ?? ""
  134. debugPrint("短信验证码:\(vcode ?? "")")
  135. }
  136. self.setCaptcha(enable: false)
  137. }
  138. })
  139. }
  140. // MARK: - 定时器相关
  141. /// 设置验证码按钮状态的方法
  142. ///
  143. /// - Parameter enable: 是否可用,如为不可用则开始60s倒计时
  144. private func setCaptcha(enable: Bool) {
  145. if enable {
  146. MCGCDTimer.shared.cancleTimer(WithTimerName: self.captchaTimerID)
  147. DispatchQueue.main.async {
  148. self.captchaButton.isEnabled = true
  149. self.captchaButton.setTitle("获取验证码", for: .normal)
  150. self.timerLabel.isHidden = true
  151. }
  152. } else {
  153. DispatchQueue.main.async {
  154. self.captchaCD = 60
  155. self.captchaButton.isEnabled = false
  156. self.captchaButton.setTitle("", for: .disabled)
  157. self.timerLabel.text = "\(self.captchaCD)秒"
  158. self.timerLabel.isHidden = false
  159. self.captchaCodeField.becomeFirstResponder()
  160. MCGCDTimer.shared.cancleTimer(WithTimerName: self.captchaTimerID)
  161. MCGCDTimer.shared.scheduledDispatchTimer(WithTimerName: self.captchaTimerID, timeInterval: 1.0, queue: .main, repeats: true, isNowRun: false) {
  162. DispatchQueue.main.async {
  163. self.captchaCD -= 1
  164. self.timerLabel.text = "\(self.captchaCD)秒"
  165. if self.captchaCD <= 0 {
  166. self.setCaptcha(enable: true)
  167. }
  168. }
  169. }
  170. }
  171. }
  172. }
  173. /// 下一步操作
  174. private func doSubmit() {
  175. /// 异常
  176. guard let accountManager = MTP2BusinessCore.shared.accountManager,
  177. checkInputValue(),
  178. checkCardNumInputValue() else {
  179. return
  180. }
  181. /// 验证码
  182. let vcode = captchaCodeField.text ?? ""
  183. /// 手机号码
  184. let mobilePhone = mobilePhoneField.text ?? ""
  185. /// 开启Loading
  186. NVActivityIndicatorPresenter.sharedInstance.startAnimating(ActivityData(message: "查询数据中......", type: .ballClipRotate, color: UIColorFromHex(rgbValue: 0xA8B6CC)), NVActivityIndicatorView.DEFAULT_FADE_IN_ANIMATION)
  187. /// 查询登录账号信息
  188. accountManager.queryLoginId("", mobilePhone, areaCode, vcode, callback: { (isComplete, errorInfo, loginId) in
  189. DispatchQueue.main.async {
  190. /// stop loding.....
  191. NVActivityIndicatorPresenter.sharedInstance.stopAnimating(NVActivityIndicatorView.DEFAULT_FADE_OUT_ANIMATION)
  192. if isComplete,
  193. let id = loginId,
  194. id != "" { /// 成功
  195. /// 进入第二步视图校验
  196. self.navigationController?.pushViewController(self.secondStepViewController, animated: true)
  197. /// 登录账号
  198. self.secondStepViewController.datas.append(contentsOf: [(key: "loginId", value: id), (key: "vcode", value: vcode), (key: "mobilePhone", value: mobilePhone)])
  199. /// 区号
  200. self.secondStepViewController.areaCode = self.areaCode
  201. } else {
  202. WHToast.showError(withMessage: errorInfo?.retMsg ?? "查询登录账号失败,请稍候再试!", duration: 1.5, finishHandler: {})
  203. }
  204. }
  205. })
  206. }
  207. // MARK: - Navigation
  208. // In a storyboard-based application, you will often want to do a little preparation before navigation
  209. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  210. // Get the new view controller using segue.destination.
  211. // Pass the selected object to the new view controller.
  212. if segue.identifier == "ShowImageCode" {
  213. (segue.destination as? ImageCodeViewController)?.popBlock = { [weak self] (_ takeInfo: String?) in
  214. /// 发送验证码
  215. self?.sendCaptcha()
  216. }
  217. }
  218. }
  219. }