diff --git a/punchnet/Views/AppContext.swift b/punchnet/Views/AppContext.swift index 5af0e1a..e05f256 100644 --- a/punchnet/Views/AppContext.swift +++ b/punchnet/Views/AppContext.swift @@ -22,8 +22,8 @@ class AppContext { var appScene: AppScene = .login(username: nil) enum Credit { - case token(token: String) - case accountAndPasword(account: String, password: String) + case token(token: String, accessToken: String) + case accountAndPasword(account: String, password: String, accessToken: String) } // 当前的场景 @@ -38,24 +38,24 @@ class AppContext { self.noticePort = noticePort } - func loginWith(credit: Credit) async throws -> Bool { - switch credit { - case .token(let token): - self.networkSession = try await SDLAPIClient.loginWithToken(token: token) - // 将数据缓存到keychain - if let data = token.data(using: .utf8) { - try KeychainStore.shared.save(data, account: "token") - } - case .accountAndPasword(let username, let password): - self.networkSession = try await SDLAPIClient.loginWithAccountAndPassword(username: username, password: password) - // 将数据缓存到keychain - if let data = "\(username):\(password)".data(using: .utf8) { - try KeychainStore.shared.save(data, account: "accountAndPasword") - } - } - self.loginCredit = credit + func loginWith(token: String) async throws { + let networkSession = try await SDLAPIClient.loginWithToken(token: token) + self.loginCredit = .token(token: token, accessToken: networkSession.accessToken) + self.networkSession = networkSession - return true + // 将数据缓存到keychain + if let data = token.data(using: .utf8) { + try KeychainStore.shared.save(data, account: "token") + } + } + + func loginWith(username: String, password: String) async throws { + let networkSession = try await SDLAPIClient.loginWithAccountAndPassword(username: username, password: password) + self.loginCredit = .accountAndPasword(account: username, password: password, accessToken: networkSession.accessToken) + // 将数据缓存到keychain + if let data = "\(username):\(password)".data(using: .utf8) { + try KeychainStore.shared.save(data, account: "accountAndPasword") + } } func loadCacheToken() -> String? { diff --git a/punchnet/Views/Login/LoginView.swift b/punchnet/Views/Login/LoginView.swift index 1f1daa4..ca6ff40 100644 --- a/punchnet/Views/Login/LoginView.swift +++ b/punchnet/Views/Login/LoginView.swift @@ -170,7 +170,7 @@ struct LoginAccountView: View { } do { - _ = try await appContext.loginWith(credit: .accountAndPasword(account: username, password: password)) + _ = try await appContext.loginWith(username: username, password: password) withAnimation(.spring(duration: 0.6, bounce: 0.2)) { self.appContext.appScene = .logined } @@ -231,7 +231,7 @@ struct LoginTokenView: View { } do { - _ = try await appContext.loginWith(credit: .token(token: token)) + _ = try await appContext.loginWith(token: token) withAnimation(.spring(duration: 0.6, bounce: 0.2)) { self.appContext.appScene = .logined } diff --git a/punchnet/Views/Settings/SettingsAccountView.swift b/punchnet/Views/Settings/SettingsAccountView.swift index 5093d90..8c7313b 100644 --- a/punchnet/Views/Settings/SettingsAccountView.swift +++ b/punchnet/Views/Settings/SettingsAccountView.swift @@ -20,9 +20,9 @@ struct SettingsAccountView: View { VStack(spacing: 0) { if let loginCredit = appContext.loginCredit { switch loginCredit { - case .token(let token): + case .token(let token, _): TokenCreditView(token: token) - case .accountAndPasword(let account, _): + case .accountAndPasword(let account, _, _): AccountCreditView(username: account) } } else {