// // ResetPwdSecondStepViewController.swift // MTP2_iOS // // Created by Handy_Cao on 2020/3/23. // Copyright © 2020 Muchinfo. All rights reserved. // import UIKit import WHToast import NVActivityIndicatorView class ResetPwdSecondStepViewController: BaseViewController { /// 登录账号标签 @IBOutlet weak var loginIdLabel: UILabel! /// 密码输入框 @IBOutlet weak var pwdField: UITextField! { didSet { pwdField.attributedPlaceholder = "请输入6-20个字符新密码".withTextColor(Color_placeholder_text) } } /// 确认密码输入框 @IBOutlet weak var confirmPwdField: UITextField! { didSet { confirmPwdField.attributedPlaceholder = "请确认新密码".withTextColor(Color_placeholder_text) } } ///提交按钮 @IBOutlet weak var submitButton: UIButton! /// 数据集合 var datas: [(key: String, value: String)] = [] /// 区号 var areaCode: String = "" /// 定义block typealias block = (_ String: String) ->() /// 创建block变量 var popBlock: block? // MARK: - 生命周期相关 override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. /// UI界面初始化 buildView() } // MARK: - 初始化相关 /// UI界面初始化 func buildView() { /// 账号信息 loginIdLabel.text = "您的账号为" + datas[0].value } /// 数据初始化 func initData() {} // MARK: - 交互相关 /// onButtonPressed /// - Parameter sender: sender @IBAction private func onButtonPressed(_ sender: UIButton) { switch sender { case submitButton: /// 提交按钮 /// 提交 doSubmit() default: break } } /// 输入框合规性校验 private func checkInputValue() -> Bool { let pwd = pwdField.text ?? "", confirmPwd = confirmPwdField.text ?? "" /// 请输入6-20位的字符 guard pwd.count > 5, pwd.count < 21 else { WHToast.showError(withMessage: "请输入6-20位的字符", duration: 1.5) { self.pwdField.becomeFirstResponder() } return false } /// 请输入6-20位的字符 guard confirmPwd.count > 5, confirmPwd.count < 21 else { WHToast.showError(withMessage: "请输入6-20位的字符", duration: 1.5) { self.confirmPwdField.becomeFirstResponder() } return false } /// 两次密码不一样 guard pwd == confirmPwd else { WHToast.showError(withMessage: "两次输入的密码不一样", duration: 1.5) { self.confirmPwdField.becomeFirstResponder() } return false } return true } // MARK: - 接口相关 /// 提交 private func doSubmit() { /// 数据异常 guard let accountManager = MTP2BusinessCore.shared.accountManager, checkInputValue() else { return } /// 密码 let pwd = pwdField.text ?? "" /// 验证码 let vcode = datas[1].value /// 手机号码 let mobile = datas[2].value // 加密手机号码 guard let utf8Mobile = mobile.data(using: .utf8), let key = "Ub$kl^P3".data(using: .utf8), let iv = "Ub$kl^P3".data(using: .utf8), let encryptMobileData = MTP2Crypto.desForPhone(objective: utf8Mobile, encryptOrDecrypt: CCOperation(kCCEncrypt), key: key, iv: iv) else { return } /// 处理加密后的字符串数据 let replaceString = NSString(string: encryptMobileData.getReplaceMobileDesEncyptString("+86")) /// 处理加密的密码 let encryNewpwd = (datas[0].value + pwd).bytes.sha256().toHexString().lowercased() /// 开启Loading NVActivityIndicatorPresenter.sharedInstance.startAnimating(ActivityData(message: "提交中", type: .ballClipRotate, color: UIColorFromHex(rgbValue: 0xA8B6CC)), NVActivityIndicatorView.DEFAULT_FADE_IN_ANIMATION) /// 重置密码请求 accountManager.requestResetPwd("", vcode, encryNewpwd, replaceString as String, callback: { (isComplete, errorInfo) in DispatchQueue.main.async { /// stop loding..... NVActivityIndicatorPresenter.sharedInstance.stopAnimating(NVActivityIndicatorView.DEFAULT_FADE_OUT_ANIMATION) if isComplete { /// 请求成功 WHToast.showSuccess(withMessage: "重置成功,请重新登录!", duration: 1.0) { /// 返回根视图 self.navigationController?.popToRootViewController(animated: false) /// 执行回调 if self.popBlock != nil { self.popBlock!("") } } } 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. } */ }