fix
This commit is contained in:
parent
9f5ce7242c
commit
33e20843de
@ -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,25 +38,25 @@ 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)
|
||||
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
|
||||
|
||||
// 将数据缓存到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)
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
}
|
||||
self.loginCredit = credit
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func loadCacheToken() -> String? {
|
||||
if let data = try? KeychainStore.shared.load(account: "token") {
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
@ -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 {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user