IOS_Boss/IOS_study/Auth/SignInPhoneView.swift
2025-07-27 12:33:06 +08:00

150 lines
6.0 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// SignInPhoneView.swift
// IOS_study
//
// Created by CC-star on 2025/7/10.
//
import SwiftUI
import Combine
private let totalTime = 60
struct SignInPhoneView: View {
enum FocusedField { case phoneNum, verCode }//case
@Environment(SignInPhoneViewModel.self) var vm
@Environment(HUD.self) var hud
@State var showVerBtn = true
@State var timer: Publishers.Autoconnect<Timer.TimerPublisher>?// ?
@State var timeRemain = totalTime//
@State var isAgree = false //
@State var showAgreeSheet = false//
@State var showAgreeView = false//
@State var agreeType = 0//01
@FocusState var focusedField: FocusedField?//便nil
var enableLoginBtn: Bool { vm.phoneNum.isPhoneNum && vm.verCode.isVerCode }
var totalNumCount: Int { vm.phoneNum.count + vm.verCode.count }
var body: some View {
@Bindable var vm = vm
NavigationStack {
ScrollView {
VStack(spacing: 33) {
HStack {
NavigationLink {
SettginsView(naviPath: .constant(NavigationPath()), isLogin: false)
} label: {
Image(systemName: "gearshape")
}
Button {
focusedField = nil// ->
} label: {
Image(systemName: "xmark")
}
}.push(to: .trailing).font(.title3).secondary()
Text("手机号登陆").tc().font(.title).bold()
.kerning(1.2)//,1
VStack(spacing: 14) {
HStack {
TextField("请输入手机号", text: $vm.phoneNum).tc().kerning(1.2)
.keyboardType(.numberPad)//
.focused($focusedField, equals: .phoneNum)
if !vm.phoneNum.isEmpty {
Image(systemName: "xmark.circle.fill").font(.title2).secondary().onTapGesture { vm.phoneNum = "" }
}
}
exDivider()
HStack {
//797687
TextField("请输入验证码", text: $vm.verCode).tc().tc().kerning(1.2).keyboardType(.numberPad)
.focused($focusedField, equals: .verCode)
if vm.phoneNum.isPhoneNum || !showVerBtn {//
if showVerBtn {
Button("获取验证码") {
timeRemain = totalTime
focusedField = .verCode//caseverCode
timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()//,
showVerBtn = false
Task {
await getVerCode()
}
}.headline()
} else {
if let timer {
Text("重新发送(\(timeRemain)s").headline().secondary()
//
.onReceive(timer) { _ in
timeRemain -= 1
if timeRemain <= 0 {
stopTimer()
showVerBtn = true
}
}
}
}
}
}
exDivider()
Button {
preLogin()
} label: {
if vm.isLogining {
ProgressView().appleStyleP()
} else {
Text("登录").appleStyle(disabled: !enableLoginBtn)
}
}.disabled(!enableLoginBtn || vm.isLogining)
HStack(spacing: 3) {
Button { isAgree.toggle()} label: {
Image(systemName: isAgree ? "checkmark.circle.fill" : "circle")
}.tint(isAgree ? .accent : .secondary).size16()
agreeTextView.size14()
}
}.font(.title3)
}.padding(33)
}
.scrollDismissesKeyboard(.immediately).bg()//
.fullScreenCover(isPresented: $showAgreeView, content: { AgreeView(agreeType: $agreeType) })
.sheet(isPresented: $showAgreeSheet) {//
VStack(spacing: 18) {
agreeTextView.size15()
Button {
isAgree = true//
showAgreeSheet = false//
Task { await login() }//
} label: {
Text("同意并登录").font(.body).push(to: .center).padding(.vertical, 6)
}.buttonStyle(.borderedProminent)
}.padding(33)
.presentationDetents([.height(200)])//
// .presentationDetents([.medium, .large])//
}
}
.onChange(of: totalNumCount) {
if totalNumCount == 17 {
preLogin()
}
}
}
var agreeTextView: some View {
HStack(spacing: 3) {
Text("我已阅读并同意").secondary()
Button("用户协议") {
agreeType = 0
showAgreeView = true
}
Text("").secondary()
Button("隐私政策") {
agreeType = 1
showAgreeView = true
}
}
}
}