diff --git a/punchnet/Core/SDLUtil.swift b/punchnet/Core/SDLUtil.swift new file mode 100644 index 0000000..4eea6d3 --- /dev/null +++ b/punchnet/Core/SDLUtil.swift @@ -0,0 +1,30 @@ +// +// SDLUtil.swift +// punchnet +// +// Created by 安礼成 on 2026/3/9. +// + +struct SDLUtil { + enum ContactType { + case phone + case email + case invalid + } + + static func identifyContact(_ input: String) -> ContactType { + let trimmed = input.trimmingCharacters(in: .whitespacesAndNewlines) + // 手机号正则(中国手机号为例,以 1 开头,11 位数字) + let phoneRegex = /^1[3-9][0-9]{9}$/ + // 邮箱正则 + let emailRegex = /^[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$/ + if trimmed.wholeMatch(of: phoneRegex) != nil { + return .phone + } else if trimmed.wholeMatch(of: emailRegex) != nil { + return .email + } else { + return .invalid + } + } + +} diff --git a/punchnet/Views/Login/LoginView.swift b/punchnet/Views/Login/LoginView.swift index 48d6fa9..15b980a 100644 --- a/punchnet/Views/Login/LoginView.swift +++ b/punchnet/Views/Login/LoginView.swift @@ -8,12 +8,6 @@ import SwiftUI import Observation -enum ContactType { - case phone - case email - case invalid -} - enum AuthScreen { case login case resetPassword @@ -306,27 +300,27 @@ struct ResetPasswordView: View { @Environment(UserContext.self) var userContext: UserContext @State private var username: String = "" - @State private var showAlert = false @State private var errorMessage = "" - + var body: some View { - VStack { + VStack(spacing: 30) { + + // 标题 Text("重置密码") + .font(.system(size: 18, weight: .medium)) - TextField("手机号/邮箱", text: $username) - .multilineTextAlignment(.leading) - .textFieldStyle(PlainTextFieldStyle()) - .frame(width: 200, height: 25) - .background(Color.clear) - .foregroundColor(Color.black) - .overlay( - Rectangle() - .frame(height: 1) - .foregroundColor(.blue) - .padding(.top, 25) - , alignment: .topLeading) - + VStack(alignment: .leading, spacing: 16) { + TextField("请输入手机号或邮箱", text: $username) + .textFieldStyle(.plain) + .frame(width: 260, height: 28) + .overlay( + Rectangle() + .frame(height: 1) + .foregroundColor(.blue), + alignment: .bottom + ) + } Button { Task { @MainActor in @@ -334,58 +328,46 @@ struct ResetPasswordView: View { } } label: { Text("获取验证码") - .font(.system(size: 14, weight: .regular)) + .font(.system(size: 14)) .foregroundColor(.black) - .frame(width: 200, height: 35) + .frame(width: 160, height: 36) + .background(Color(red: 74/255, green: 207/255, blue: 154/255)) } - .frame(width: 200, height: 35) - .background(Color(red: 74 / 255, green: 207 / 255, blue: 154 / 255)) - .cornerRadius(5.0) + .frame(width: 160, height: 36) + punchnet/Core/SDLUtil.swift.cornerRadius(6) + + Spacer() } + .padding(.top, 40) + .frame(width: 400, height: 400) .alert(isPresented: $showAlert) { Alert(title: Text("提示"), message: Text(self.errorMessage)) } } - + private func sendVerifyCode() async { if username.isEmpty { self.showAlert = true self.errorMessage = "手机号/邮箱为空" return } - - switch identifyContact(username) { + + switch SDLUtil.identifyContact(username) { case .email, .phone: do { let result = try await self.userContext.sendVerifyCode(username: username) print("send verify code result: \(result)") - } catch let err { + } catch { self.showAlert = true - self.errorMessage = err.localizedDescription + self.errorMessage = error.localizedDescription } + case .invalid: self.showAlert = true self.errorMessage = "手机号/邮箱格式错误" } } - - private func identifyContact(_ input: String) -> ContactType { - let trimmed = input.trimmingCharacters(in: .whitespacesAndNewlines) - // 手机号正则(中国手机号为例,以 1 开头,11 位数字) - let phoneRegex = /^1[3-9][0-9]{9}$/ - // 邮箱正则 - let emailRegex = /^[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$/ - if trimmed.wholeMatch(of: phoneRegex) != nil { - return .phone - } else if trimmed.wholeMatch(of: emailRegex) != nil { - return .email - } else { - return .invalid - } - } - } - #Preview { // ResetPasswordView() // .environment(UserContext())