ResetPwdSecondStepViewController.swift 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //
  2. // ResetPwdSecondStepViewController.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. class ResetPwdSecondStepViewController: BaseViewController {
  12. /// 登录账号标签
  13. @IBOutlet weak var loginIdLabel: UILabel!
  14. /// 密码输入框
  15. @IBOutlet weak var pwdField: UITextField! {
  16. didSet {
  17. pwdField.attributedPlaceholder = "请输入6-20个字符新密码".withTextColor(Color_placeholder_text)
  18. }
  19. }
  20. /// 确认密码输入框
  21. @IBOutlet weak var confirmPwdField: UITextField! {
  22. didSet {
  23. confirmPwdField.attributedPlaceholder = "请确认新密码".withTextColor(Color_placeholder_text)
  24. }
  25. }
  26. ///提交按钮
  27. @IBOutlet weak var submitButton: UIButton!
  28. /// 数据集合
  29. var datas: [(key: String, value: String)] = []
  30. /// 区号
  31. var areaCode: String = ""
  32. /// 定义block
  33. typealias block = (_ String: String) ->()
  34. /// 创建block变量
  35. var popBlock: block?
  36. // MARK: - 生命周期相关
  37. override func viewDidLoad() {
  38. super.viewDidLoad()
  39. // Do any additional setup after loading the view.
  40. /// UI界面初始化
  41. buildView()
  42. }
  43. // MARK: - 初始化相关
  44. /// UI界面初始化
  45. func buildView() {
  46. /// 账号信息
  47. loginIdLabel.text = "您的账号为" + datas[0].value
  48. }
  49. /// 数据初始化
  50. func initData() {}
  51. // MARK: - 交互相关
  52. /// onButtonPressed
  53. /// - Parameter sender: sender
  54. @IBAction private func onButtonPressed(_ sender: UIButton) {
  55. switch sender {
  56. case submitButton: /// 提交按钮
  57. /// 提交
  58. doSubmit()
  59. default:
  60. break
  61. }
  62. }
  63. /// 输入框合规性校验
  64. private func checkInputValue() -> Bool {
  65. let pwd = pwdField.text ?? "", confirmPwd = confirmPwdField.text ?? ""
  66. /// 请输入6-20位的字符
  67. guard pwd.count > 5, pwd.count < 21 else {
  68. WHToast.showError(withMessage: "请输入6-20位的字符", duration: 1.5) {
  69. self.pwdField.becomeFirstResponder()
  70. }
  71. return false
  72. }
  73. /// 请输入6-20位的字符
  74. guard confirmPwd.count > 5, confirmPwd.count < 21 else {
  75. WHToast.showError(withMessage: "请输入6-20位的字符", duration: 1.5) {
  76. self.confirmPwdField.becomeFirstResponder()
  77. }
  78. return false
  79. }
  80. /// 两次密码不一样
  81. guard pwd == confirmPwd else {
  82. WHToast.showError(withMessage: "两次输入的密码不一样", duration: 1.5) {
  83. self.confirmPwdField.becomeFirstResponder()
  84. }
  85. return false
  86. }
  87. return true
  88. }
  89. // MARK: - 接口相关
  90. /// 提交
  91. private func doSubmit() {
  92. /// 数据异常
  93. guard let accountManager = MTP2BusinessCore.shared.accountManager,
  94. checkInputValue() else { return }
  95. /// 密码
  96. let pwd = pwdField.text ?? ""
  97. /// 验证码
  98. let vcode = datas[1].value
  99. /// 手机号码
  100. let mobile = datas[2].value
  101. // 加密手机号码
  102. guard let utf8Mobile = mobile.data(using: .utf8),
  103. let key = "Ub$kl^P3".data(using: .utf8),
  104. let iv = "Ub$kl^P3".data(using: .utf8),
  105. let encryptMobileData = MTP2Crypto.desForPhone(objective: utf8Mobile, encryptOrDecrypt: CCOperation(kCCEncrypt), key: key, iv: iv)
  106. else { return }
  107. /// 处理加密后的字符串数据
  108. let replaceString = NSString(string: encryptMobileData.getReplaceMobileDesEncyptString("+86"))
  109. /// 处理加密的密码
  110. let encryNewpwd = (datas[0].value + pwd).bytes.sha256().toHexString().lowercased()
  111. /// 开启Loading
  112. NVActivityIndicatorPresenter.sharedInstance.startAnimating(ActivityData(message: "提交中", type: .ballClipRotate, color: UIColorFromHex(rgbValue: 0xA8B6CC)), NVActivityIndicatorView.DEFAULT_FADE_IN_ANIMATION)
  113. /// 重置密码请求
  114. accountManager.requestResetPwd("", vcode, encryNewpwd, replaceString as String, callback: { (isComplete, errorInfo) in
  115. DispatchQueue.main.async {
  116. /// stop loding.....
  117. NVActivityIndicatorPresenter.sharedInstance.stopAnimating(NVActivityIndicatorView.DEFAULT_FADE_OUT_ANIMATION)
  118. if isComplete { /// 请求成功
  119. WHToast.showSuccess(withMessage: "重置成功,请重新登录!", duration: 1.0) {
  120. /// 返回根视图
  121. self.navigationController?.popToRootViewController(animated: false)
  122. /// 执行回调
  123. if self.popBlock != nil { self.popBlock!("") }
  124. }
  125. } else { /// 请求失败
  126. WHToast.showError(withMessage: "请求失败,原因:\(errorInfo?.retMsg ?? "响应超时")!", duration: 1.5, finishHandler: {})
  127. }
  128. }
  129. })
  130. }
  131. /*
  132. // MARK: - Navigation
  133. // In a storyboard-based application, you will often want to do a little preparation before navigation
  134. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  135. // Get the new view controller using segue.destination.
  136. // Pass the selected object to the new view controller.
  137. }
  138. */
  139. }