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