feat: 添加中文注释

This commit is contained in:
jinyaqiu 2025-08-18 16:56:49 +08:00
parent fc5735964f
commit ace3e3dd14
2 changed files with 60 additions and 57 deletions

View File

@ -1,31 +1,31 @@
import SwiftUI
import AuthenticationServices
import Alamofire
import CryptoKit
import AuthenticationServices //
import Alamofire //
import CryptoKit //
/// Main login view that handles Apple Sign In
/// -
struct LoginView: View {
// MARK: - Properties
@Environment(\.dismiss) private var dismiss
@State private var isLoading = false
@State private var showError = false
@State private var errorMessage = ""
@State private var currentNonce: String?
// MARK: -
@Environment(\.dismiss) private var dismiss //
@State private var isLoading = false //
@State private var showError = false //
@State private var errorMessage = "" //
@State private var currentNonce: String? //
// MARK: - Body
// MARK: -
var body: some View {
ZStack {
// Background
//
Color(.systemBackground)
.edgesIgnoringSafeArea(.all)
// Main content
//
VStack(spacing: 24) {
Spacer()
appHeaderView()
signInButton()
appHeaderView() //
signInButton() //
Spacer()
termsAndPrivacyView()
termsAndPrivacyView() //
}
.padding()
.alert(isPresented: $showError) {
@ -36,7 +36,7 @@ struct LoginView: View {
)
}
// Loading indicator
//
if isLoading {
loadingView()
}
@ -44,9 +44,9 @@ struct LoginView: View {
.navigationBarHidden(true)
}
// MARK: - View Components
// MARK: -
/// App header with icon and welcome text
///
private func appHeaderView() -> some View {
VStack(spacing: 16) {
Image(systemName: "person.circle.fill")
@ -66,27 +66,27 @@ struct LoginView: View {
.padding(.bottom, 40)
}
/// Apple Sign In button
///
private func signInButton() -> some View {
SignInWithAppleButton(
onRequest: { request in
// Generate nonce for security
//
let nonce = String.randomURLSafeString(length: 32)
self.currentNonce = nonce
// Configure the request
request.requestedScopes = [.fullName, .email]
request.nonce = self.sha256(nonce)
//
request.requestedScopes = [.fullName, .email] //
request.nonce = self.sha256(nonce) // nonce
},
onCompletion: handleAppleSignIn
onCompletion: handleAppleSignIn //
)
.signInWithAppleButtonStyle(.black)
.signInWithAppleButtonStyle(.black) //
.frame(height: 50)
.padding(.horizontal, 40)
.cornerRadius(10)
}
/// Terms and Privacy policy links
///
private func termsAndPrivacyView() -> some View {
VStack(spacing: 8) {
Text("By continuing, you agree to our")
@ -113,7 +113,7 @@ struct LoginView: View {
.padding(.bottom, 24)
}
/// Loading overlay view
///
private func loadingView() -> some View {
return ZStack {
Color.black.opacity(0.4)
@ -125,9 +125,9 @@ struct LoginView: View {
}
}
// MARK: - Apple Sign In Handlers
// MARK: -
/// Handles the result of Apple Sign In
///
private func handleAppleSignIn(result: Result<ASAuthorization, Error>) {
switch result {
case .success(let authResults):
@ -137,14 +137,14 @@ struct LoginView: View {
}
}
/// Processes the Apple ID credential
/// ID
private func processAppleIDCredential(_ credential: ASAuthorizationCredential) {
guard let appleIDCredential = credential as? ASAuthorizationAppleIDCredential else {
showError(message: "Unable to process Apple ID credentials")
showError(message: "无法处理Apple ID凭证")
return
}
// Get user data
//
let userId = appleIDCredential.user
let email = appleIDCredential.email ?? ""
let fullName = [
@ -154,20 +154,20 @@ struct LoginView: View {
.compactMap { $0 }
.joined(separator: " ")
// Get the identity token
//
guard let identityTokenData = appleIDCredential.identityToken,
let identityToken = String(data: identityTokenData, encoding: .utf8) else {
showError(message: "Unable to fetch identity token")
showError(message: "无法获取身份令牌")
return
}
// Get the authorization code (optional)
//
var authCode: String? = nil
if let authCodeData = appleIDCredential.authorizationCode {
authCode = String(data: authCodeData, encoding: .utf8)
}
// Proceed with backend authentication
//
authenticateWithBackend(
userId: userId,
email: email,
@ -177,9 +177,9 @@ struct LoginView: View {
)
}
// MARK: - Network Operations
// MARK: -
/// Authenticates the user with the backend server
///
private func authenticateWithBackend(
userId: String,
email: String,
@ -189,6 +189,7 @@ struct LoginView: View {
) {
isLoading = true
// TODO: API
let url = "https://your-api-endpoint.com/api/auth/apple"
var parameters: [String: Any] = [
"appleUserId": userId,
@ -197,11 +198,12 @@ struct LoginView: View {
"identityToken": identityToken
]
// Add authorization code if available
//
if let authCode = authCode {
parameters["authorizationCode"] = authCode
}
//
AF.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default)
.validate()
.responseJSON { response in
@ -209,7 +211,7 @@ struct LoginView: View {
switch response.result {
case .success(let value):
print("Authentication successful: \(value)")
print("认证成功: \(value)")
self.handleSuccessfulAuthentication()
case .failure(let error):
self.handleAuthenticationError(error)
@ -217,30 +219,30 @@ struct LoginView: View {
}
}
// MARK: - Helper Methods
// MARK: -
/// Handles successful authentication
///
private func handleSuccessfulAuthentication() {
DispatchQueue.main.async {
self.dismiss()
self.dismiss() //
}
}
/// Handles sign in errors
///
private func handleSignInError(_ error: Error) {
let errorMessage = (error as NSError).localizedDescription
print("Apple Sign In failed: \(errorMessage)")
showError(message: "Sign in failed: \(error.localizedDescription)")
print("苹果登录失败: \(errorMessage)")
showError(message: "登录失败: \(error.localizedDescription)")
}
/// Handles authentication errors
///
private func handleAuthenticationError(_ error: AFError) {
let errorMessage = error.localizedDescription
print("API Error: \(errorMessage)")
showError(message: "Failed to sign in: \(errorMessage)")
print("API错误: \(errorMessage)")
showError(message: "登录失败: \(errorMessage)")
}
/// Shows error message to the user
///
private func showError(message: String) {
DispatchQueue.main.async {
self.errorMessage = message
@ -248,12 +250,13 @@ struct LoginView: View {
}
}
/// Opens a URL in Safari
/// SafariURL
private func openURL(_ string: String) {
guard let url = URL(string: string) else { return }
UIApplication.shared.open(url)
}
/// SHA256
private func sha256(_ input: String) -> String {
let inputData = Data(input.utf8)
let hashedData = SHA256.hash(data: inputData)
@ -262,12 +265,12 @@ struct LoginView: View {
}
}
// MARK: - String Extension for Random String Generation
// MARK: -
extension String {
/// Generates a random string of the specified length using URL-safe characters
/// - Parameter length: The length of the random string to generate
/// - Returns: A random string containing URL-safe characters
/// URL
/// - Parameter length:
/// - Returns:
static func randomURLSafeString(length: Int) -> String {
let characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~"
var randomString = ""
@ -282,7 +285,7 @@ extension String {
}
}
// MARK: - Preview
// MARK: -
struct LoginView_Previews: PreviewProvider {
static var previews: some View {
LoginView()