fix view
This commit is contained in:
parent
253492b481
commit
89bff2f97b
30
punchnet/Core/SDLUtil.swift
Normal file
30
punchnet/Core/SDLUtil.swift
Normal file
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -8,12 +8,6 @@
|
|||||||
import SwiftUI
|
import SwiftUI
|
||||||
import Observation
|
import Observation
|
||||||
|
|
||||||
enum ContactType {
|
|
||||||
case phone
|
|
||||||
case email
|
|
||||||
case invalid
|
|
||||||
}
|
|
||||||
|
|
||||||
enum AuthScreen {
|
enum AuthScreen {
|
||||||
case login
|
case login
|
||||||
case resetPassword
|
case resetPassword
|
||||||
@ -306,27 +300,27 @@ struct ResetPasswordView: View {
|
|||||||
@Environment(UserContext.self) var userContext: UserContext
|
@Environment(UserContext.self) var userContext: UserContext
|
||||||
|
|
||||||
@State private var username: String = ""
|
@State private var username: String = ""
|
||||||
|
|
||||||
@State private var showAlert = false
|
@State private var showAlert = false
|
||||||
@State private var errorMessage = ""
|
@State private var errorMessage = ""
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
VStack {
|
VStack(spacing: 30) {
|
||||||
Text("重置密码")
|
|
||||||
|
|
||||||
TextField("手机号/邮箱", text: $username)
|
// 标题
|
||||||
.multilineTextAlignment(.leading)
|
Text("重置密码")
|
||||||
.textFieldStyle(PlainTextFieldStyle())
|
.font(.system(size: 18, weight: .medium))
|
||||||
.frame(width: 200, height: 25)
|
|
||||||
.background(Color.clear)
|
VStack(alignment: .leading, spacing: 16) {
|
||||||
.foregroundColor(Color.black)
|
TextField("请输入手机号或邮箱", text: $username)
|
||||||
|
.textFieldStyle(.plain)
|
||||||
|
.frame(width: 260, height: 28)
|
||||||
.overlay(
|
.overlay(
|
||||||
Rectangle()
|
Rectangle()
|
||||||
.frame(height: 1)
|
.frame(height: 1)
|
||||||
.foregroundColor(.blue)
|
.foregroundColor(.blue),
|
||||||
.padding(.top, 25)
|
alignment: .bottom
|
||||||
, alignment: .topLeading)
|
)
|
||||||
|
}
|
||||||
|
|
||||||
Button {
|
Button {
|
||||||
Task { @MainActor in
|
Task { @MainActor in
|
||||||
@ -334,14 +328,18 @@ struct ResetPasswordView: View {
|
|||||||
}
|
}
|
||||||
} label: {
|
} label: {
|
||||||
Text("获取验证码")
|
Text("获取验证码")
|
||||||
.font(.system(size: 14, weight: .regular))
|
.font(.system(size: 14))
|
||||||
.foregroundColor(.black)
|
.foregroundColor(.black)
|
||||||
.frame(width: 200, height: 35)
|
.frame(width: 160, height: 36)
|
||||||
}
|
|
||||||
.frame(width: 200, height: 35)
|
|
||||||
.background(Color(red: 74/255, green: 207/255, blue: 154/255))
|
.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(isPresented: $showAlert) {
|
||||||
Alert(title: Text("提示"), message: Text(self.errorMessage))
|
Alert(title: Text("提示"), message: Text(self.errorMessage))
|
||||||
}
|
}
|
||||||
@ -354,38 +352,22 @@ struct ResetPasswordView: View {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
switch identifyContact(username) {
|
switch SDLUtil.identifyContact(username) {
|
||||||
case .email, .phone:
|
case .email, .phone:
|
||||||
do {
|
do {
|
||||||
let result = try await self.userContext.sendVerifyCode(username: username)
|
let result = try await self.userContext.sendVerifyCode(username: username)
|
||||||
print("send verify code result: \(result)")
|
print("send verify code result: \(result)")
|
||||||
} catch let err {
|
} catch {
|
||||||
self.showAlert = true
|
self.showAlert = true
|
||||||
self.errorMessage = err.localizedDescription
|
self.errorMessage = error.localizedDescription
|
||||||
}
|
}
|
||||||
|
|
||||||
case .invalid:
|
case .invalid:
|
||||||
self.showAlert = true
|
self.showAlert = true
|
||||||
self.errorMessage = "手机号/邮箱格式错误"
|
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 {
|
#Preview {
|
||||||
// ResetPasswordView()
|
// ResetPasswordView()
|
||||||
// .environment(UserContext())
|
// .environment(UserContext())
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user