refactor: 更新 EPLoginTypesViewController 以简化表单验证和错误处理
主要变更: 1. 将 EPLoginTypesViewController 继承自 BaseViewController,提升代码结构。 2. 简化表单验证逻辑,仅检查输入是否为空,减少对 EPLoginValidator 的依赖。 3. 更新错误处理方式,使用 showErrorToast 替代 showAlert,提升用户体验。 4. 在 EPLoginService 中直接使用字符串常量替代 grantType 变量,简化代码。 此更新旨在提升代码可读性和用户交互体验,确保登录流程更加流畅。
This commit is contained in:
@@ -7,14 +7,13 @@
|
||||
|
||||
import UIKit
|
||||
|
||||
class EPLoginTypesViewController: UIViewController {
|
||||
class EPLoginTypesViewController: BaseViewController {
|
||||
|
||||
// MARK: - Properties
|
||||
|
||||
var displayType: EPLoginDisplayType = .id
|
||||
|
||||
private let loginService = EPLoginService()
|
||||
private let validator = EPLoginValidator()
|
||||
|
||||
private let backgroundImageView = UIImageView()
|
||||
private let titleLabel = UILabel()
|
||||
@@ -380,14 +379,14 @@ class EPLoginTypesViewController: UIViewController {
|
||||
let id = firstInputView.text.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
let password = secondInputView.text
|
||||
|
||||
// 表单验证
|
||||
// 表单验证(简化,仅检查空值)
|
||||
guard !id.isEmpty else {
|
||||
showAlert("请输入用户ID")
|
||||
showErrorToast(YMLocalizedString("LoginPresenter0"))
|
||||
return
|
||||
}
|
||||
|
||||
guard !password.isEmpty else {
|
||||
showAlert("请输入密码")
|
||||
showErrorToast(YMLocalizedString("LoginPresenter1"))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -397,13 +396,14 @@ class EPLoginTypesViewController: UIViewController {
|
||||
loginService.loginWithID(id: id, password: password) { [weak self] (accountModel: AccountModel) in
|
||||
DispatchQueue.main.async {
|
||||
self?.showLoading(false)
|
||||
print("[EPLogin] ID登录成功: \(accountModel.uid ?? "")")
|
||||
print("[EPLogin] ID登录成功: \(accountModel.uid)")
|
||||
self?.showSuccessToast(YMLocalizedString("XPLoginPhoneViewController1"))
|
||||
EPLoginManager.jumpToHome(from: self!)
|
||||
}
|
||||
} failure: { [weak self] (code: Int, msg: String) in
|
||||
DispatchQueue.main.async {
|
||||
self?.showLoading(false)
|
||||
self?.showAlert("登录失败: \(msg)")
|
||||
self?.showErrorToast(msg)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -412,14 +412,14 @@ class EPLoginTypesViewController: UIViewController {
|
||||
let email = firstInputView.text.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
let code = secondInputView.text
|
||||
|
||||
// 表单验证
|
||||
guard validator.validateEmail(email) else {
|
||||
showAlert("请输入正确的邮箱地址")
|
||||
// 表单验证(简化,仅检查空值)
|
||||
guard !email.isEmpty else {
|
||||
showErrorToast(YMLocalizedString("LoginPresenter0"))
|
||||
return
|
||||
}
|
||||
|
||||
guard validator.validateCode(code) else {
|
||||
showAlert("请输入6位数字验证码")
|
||||
guard !code.isEmpty else {
|
||||
showErrorToast(YMLocalizedString("LoginPresenter1"))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -428,13 +428,14 @@ class EPLoginTypesViewController: UIViewController {
|
||||
loginService.loginWithEmail(email: email, code: code) { [weak self] (accountModel: AccountModel) in
|
||||
DispatchQueue.main.async {
|
||||
self?.showLoading(false)
|
||||
print("[EPLogin] 邮箱登录成功: \(accountModel.uid ?? "")")
|
||||
print("[EPLogin] 邮箱登录成功: \(accountModel.uid)")
|
||||
self?.showSuccessToast(YMLocalizedString("XPLoginPhoneViewController1"))
|
||||
EPLoginManager.jumpToHome(from: self!)
|
||||
}
|
||||
} failure: { [weak self] (code: Int, msg: String) in
|
||||
DispatchQueue.main.async {
|
||||
self?.showLoading(false)
|
||||
self?.showAlert("登录失败: \(msg)")
|
||||
self?.showErrorToast(msg)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -443,14 +444,14 @@ class EPLoginTypesViewController: UIViewController {
|
||||
let phone = firstInputView.text.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
let code = secondInputView.text
|
||||
|
||||
// 表单验证
|
||||
guard validator.validatePhone(phone) else {
|
||||
showAlert("请输入正确的手机号")
|
||||
// 表单验证(简化,仅检查空值)
|
||||
guard !phone.isEmpty else {
|
||||
showErrorToast(YMLocalizedString("XPLoginPhoneViewController0"))
|
||||
return
|
||||
}
|
||||
|
||||
guard validator.validateCode(code) else {
|
||||
showAlert("请输入6位数字验证码")
|
||||
guard !code.isEmpty else {
|
||||
showErrorToast(YMLocalizedString("LoginPresenter1"))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -459,13 +460,14 @@ class EPLoginTypesViewController: UIViewController {
|
||||
loginService.loginWithPhone(phone: phone, code: code, areaCode: "+86") { [weak self] (accountModel: AccountModel) in
|
||||
DispatchQueue.main.async {
|
||||
self?.showLoading(false)
|
||||
print("[EPLogin] 手机登录成功: \(accountModel.uid ?? "")")
|
||||
print("[EPLogin] 手机登录成功: \(accountModel.uid)")
|
||||
self?.showSuccessToast(YMLocalizedString("XPLoginPhoneViewController1"))
|
||||
EPLoginManager.jumpToHome(from: self!)
|
||||
}
|
||||
} failure: { [weak self] (code: Int, msg: String) in
|
||||
DispatchQueue.main.async {
|
||||
self?.showLoading(false)
|
||||
self?.showAlert("登录失败: \(msg)")
|
||||
self?.showErrorToast(msg)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -477,19 +479,19 @@ class EPLoginTypesViewController: UIViewController {
|
||||
let code = secondInputView.text
|
||||
let newPassword = thirdInput.text
|
||||
|
||||
// 表单验证
|
||||
guard validator.validateEmail(email) else {
|
||||
showAlert("请输入正确的邮箱地址")
|
||||
// 表单验证(简化,仅检查空值)
|
||||
guard !email.isEmpty else {
|
||||
showErrorToast(YMLocalizedString("LoginPresenter0"))
|
||||
return
|
||||
}
|
||||
|
||||
guard validator.validateCode(code) else {
|
||||
showAlert("请输入6位数字验证码")
|
||||
guard !code.isEmpty else {
|
||||
showErrorToast(YMLocalizedString("LoginPresenter1"))
|
||||
return
|
||||
}
|
||||
|
||||
guard validator.validatePassword(newPassword) else {
|
||||
showAlert("密码需6-16位,包含字母和数字")
|
||||
guard !newPassword.isEmpty else {
|
||||
showErrorToast(YMLocalizedString("LoginPresenter1"))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -498,14 +500,13 @@ class EPLoginTypesViewController: UIViewController {
|
||||
loginService.resetEmailPassword(email: email, code: code, newPassword: newPassword) { [weak self] in
|
||||
DispatchQueue.main.async {
|
||||
self?.showLoading(false)
|
||||
self?.showAlert("密码重置成功", completion: {
|
||||
self?.navigationController?.popViewController(animated: true)
|
||||
})
|
||||
self?.showSuccessToast(YMLocalizedString("XPForgetPwdViewController1"))
|
||||
self?.navigationController?.popViewController(animated: true)
|
||||
}
|
||||
} failure: { [weak self] (code: Int, msg: String) in
|
||||
DispatchQueue.main.async {
|
||||
self?.showLoading(false)
|
||||
self?.showAlert("重置失败: \(msg)")
|
||||
self?.showErrorToast(msg)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -517,19 +518,19 @@ class EPLoginTypesViewController: UIViewController {
|
||||
let code = secondInputView.text
|
||||
let newPassword = thirdInput.text
|
||||
|
||||
// 表单验证
|
||||
guard validator.validatePhone(phone) else {
|
||||
showAlert("请输入正确的手机号")
|
||||
// 表单验证(简化,仅检查空值)
|
||||
guard !phone.isEmpty else {
|
||||
showErrorToast(YMLocalizedString("XPLoginPhoneViewController0"))
|
||||
return
|
||||
}
|
||||
|
||||
guard validator.validateCode(code) else {
|
||||
showAlert("请输入6位数字验证码")
|
||||
guard !code.isEmpty else {
|
||||
showErrorToast(YMLocalizedString("LoginPresenter1"))
|
||||
return
|
||||
}
|
||||
|
||||
guard validator.validatePassword(newPassword) else {
|
||||
showAlert("密码需6-16位,包含字母和数字")
|
||||
guard !newPassword.isEmpty else {
|
||||
showErrorToast(YMLocalizedString("LoginPresenter1"))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -538,14 +539,13 @@ class EPLoginTypesViewController: UIViewController {
|
||||
loginService.resetPhonePassword(phone: phone, code: code, areaCode: "+86", newPassword: newPassword) { [weak self] in
|
||||
DispatchQueue.main.async {
|
||||
self?.showLoading(false)
|
||||
self?.showAlert("密码重置成功", completion: {
|
||||
self?.navigationController?.popViewController(animated: true)
|
||||
})
|
||||
self?.showSuccessToast(YMLocalizedString("XPForgetPwdViewController1"))
|
||||
self?.navigationController?.popViewController(animated: true)
|
||||
}
|
||||
} failure: { [weak self] (code: Int, msg: String) in
|
||||
DispatchQueue.main.async {
|
||||
self?.showLoading(false)
|
||||
self?.showAlert("重置失败: \(msg)")
|
||||
self?.showErrorToast(msg)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -555,8 +555,9 @@ class EPLoginTypesViewController: UIViewController {
|
||||
private func sendEmailCode() {
|
||||
let email = firstInputView.text.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
|
||||
guard validator.validateEmail(email) else {
|
||||
showAlert("请输入正确的邮箱地址")
|
||||
// 简化验证,仅检查空值
|
||||
guard !email.isEmpty else {
|
||||
secondInputView.stopCountdown()
|
||||
return
|
||||
}
|
||||
|
||||
@@ -565,11 +566,13 @@ class EPLoginTypesViewController: UIViewController {
|
||||
loginService.sendEmailCode(email: email, type: type) { [weak self] in
|
||||
DispatchQueue.main.async {
|
||||
self?.secondInputView.startCountdown()
|
||||
self?.showAlert("验证码已发送")
|
||||
self?.secondInputView.displayKeyboard()
|
||||
self?.showSuccessToast(YMLocalizedString("XPLoginPhoneViewController2"))
|
||||
}
|
||||
} failure: { [weak self] (code: Int, msg: String) in
|
||||
DispatchQueue.main.async {
|
||||
self?.showAlert("发送失败: \(msg)")
|
||||
self?.secondInputView.stopCountdown()
|
||||
self?.showErrorToast(msg)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -577,8 +580,10 @@ class EPLoginTypesViewController: UIViewController {
|
||||
private func sendPhoneCode() {
|
||||
let phone = firstInputView.text.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
|
||||
guard validator.validatePhone(phone) else {
|
||||
showAlert("请输入正确的手机号")
|
||||
// 简化验证,仅检查空值
|
||||
guard !phone.isEmpty else {
|
||||
showErrorToast(YMLocalizedString("XPLoginPhoneViewController0"))
|
||||
secondInputView.stopCountdown()
|
||||
return
|
||||
}
|
||||
|
||||
@@ -591,11 +596,13 @@ class EPLoginTypesViewController: UIViewController {
|
||||
self.loginService.sendPhoneCode(phone: phone, areaCode: "+86", type: type) { [weak self] in
|
||||
DispatchQueue.main.async {
|
||||
self?.secondInputView.startCountdown()
|
||||
self?.showAlert("验证码已发送")
|
||||
self?.secondInputView.displayKeyboard()
|
||||
self?.showSuccessToast(YMLocalizedString("XPLoginPhoneViewController2"))
|
||||
}
|
||||
} failure: { [weak self] (code: Int, msg: String) in
|
||||
DispatchQueue.main.async {
|
||||
self?.showAlert("发送失败: \(msg)")
|
||||
self?.secondInputView.stopCountdown()
|
||||
self?.showErrorToast(msg)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -653,14 +660,6 @@ class EPLoginTypesViewController: UIViewController {
|
||||
actionButton.alpha = isEnabled ? 1.0 : 0.5
|
||||
}
|
||||
|
||||
private func showAlert(_ message: String, completion: (() -> Void)? = nil) {
|
||||
let alert = UIAlertController(title: nil, message: message, preferredStyle: .alert)
|
||||
alert.addAction(UIAlertAction(title: "确定", style: .default) { _ in
|
||||
completion?()
|
||||
})
|
||||
present(alert, animated: true)
|
||||
}
|
||||
|
||||
/// 加载人机验证 Captcha WebView
|
||||
/// - Parameter completion: 验证成功后的回调
|
||||
private func loadCaptchaWebView(completion: @escaping () -> Void) {
|
||||
|
@@ -15,7 +15,6 @@ import Foundation
|
||||
|
||||
private let clientSecret = EPLoginConfig.API.clientSecret
|
||||
private let clientId = EPLoginConfig.API.clientId
|
||||
private let grantType = EPLoginConfig.API.grantType
|
||||
private let version = EPLoginConfig.API.version
|
||||
|
||||
// MARK: - Private Helper Methods
|
||||
@@ -151,7 +150,7 @@ import Foundation
|
||||
client_secret: clientSecret,
|
||||
version: version,
|
||||
client_id: clientId,
|
||||
grant_type: grantType)
|
||||
grant_type: "password")
|
||||
}
|
||||
|
||||
/// 邮箱 + 验证码登录
|
||||
@@ -182,7 +181,7 @@ import Foundation
|
||||
client_secret: clientSecret,
|
||||
version: version,
|
||||
client_id: clientId,
|
||||
grant_type: grantType)
|
||||
grant_type: "email")
|
||||
}
|
||||
|
||||
/// 手机号 + 验证码登录
|
||||
@@ -215,7 +214,7 @@ import Foundation
|
||||
client_secret: clientSecret,
|
||||
version: version,
|
||||
client_id: clientId,
|
||||
grant_type: grantType,
|
||||
grant_type: "password",
|
||||
phoneAreaCode: areaCode)
|
||||
}
|
||||
|
||||
|
@@ -249,6 +249,11 @@ class EPLoginInputView: UIView {
|
||||
inputTextField.text = ""
|
||||
}
|
||||
|
||||
/// 弹出键盘(自动聚焦输入框)
|
||||
func displayKeyboard() {
|
||||
inputTextField.becomeFirstResponder()
|
||||
}
|
||||
|
||||
// MARK: - Actions
|
||||
|
||||
@objc private func handleAreaTap() {
|
||||
|
Reference in New Issue
Block a user