This commit is contained in:
anlicheng 2026-03-24 13:29:29 +08:00
parent 9f5ce7242c
commit 33e20843de
3 changed files with 23 additions and 23 deletions

View File

@ -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? {

View File

@ -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
}

View File

@ -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 {