fix
This commit is contained in:
parent
33e20843de
commit
3cb42af573
@ -16,14 +16,25 @@ class AppContext {
|
|||||||
var networkContext: SDLAPIClient.NetworkContext?
|
var networkContext: SDLAPIClient.NetworkContext?
|
||||||
|
|
||||||
var loginCredit: Credit?
|
var loginCredit: Credit?
|
||||||
var networkSession: SDLAPIClient.NetworkSession?
|
|
||||||
|
|
||||||
// 当前app所处的场景
|
// 当前app所处的场景
|
||||||
var appScene: AppScene = .login(username: nil)
|
var appScene: AppScene = .login(username: nil)
|
||||||
|
|
||||||
enum Credit {
|
enum Credit {
|
||||||
case token(token: String, accessToken: String)
|
case token(token: String, session: SDLAPIClient.NetworkSession)
|
||||||
case accountAndPasword(account: String, password: String, accessToken: String)
|
case accountAndPasword(account: String, password: String, session: SDLAPIClient.NetworkSession)
|
||||||
|
}
|
||||||
|
|
||||||
|
var networkSession: SDLAPIClient.NetworkSession? {
|
||||||
|
guard let loginCredit = self.loginCredit else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
switch loginCredit {
|
||||||
|
case .token(_, let session):
|
||||||
|
return session
|
||||||
|
case .accountAndPasword(_, _, let session):
|
||||||
|
return session
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 当前的场景
|
// 当前的场景
|
||||||
@ -40,9 +51,7 @@ class AppContext {
|
|||||||
|
|
||||||
func loginWith(token: String) async throws {
|
func loginWith(token: String) async throws {
|
||||||
let networkSession = try await SDLAPIClient.loginWithToken(token: token)
|
let networkSession = try await SDLAPIClient.loginWithToken(token: token)
|
||||||
self.loginCredit = .token(token: token, accessToken: networkSession.accessToken)
|
self.loginCredit = .token(token: token, session: networkSession)
|
||||||
self.networkSession = networkSession
|
|
||||||
|
|
||||||
// 将数据缓存到keychain
|
// 将数据缓存到keychain
|
||||||
if let data = token.data(using: .utf8) {
|
if let data = token.data(using: .utf8) {
|
||||||
try KeychainStore.shared.save(data, account: "token")
|
try KeychainStore.shared.save(data, account: "token")
|
||||||
@ -51,7 +60,7 @@ class AppContext {
|
|||||||
|
|
||||||
func loginWith(username: String, password: String) async throws {
|
func loginWith(username: String, password: String) async throws {
|
||||||
let networkSession = try await SDLAPIClient.loginWithAccountAndPassword(username: username, password: password)
|
let networkSession = try await SDLAPIClient.loginWithAccountAndPassword(username: username, password: password)
|
||||||
self.loginCredit = .accountAndPasword(account: username, password: password, accessToken: networkSession.accessToken)
|
self.loginCredit = .accountAndPasword(account: username, password: password, session: networkSession)
|
||||||
// 将数据缓存到keychain
|
// 将数据缓存到keychain
|
||||||
if let data = "\(username):\(password)".data(using: .utf8) {
|
if let data = "\(username):\(password)".data(using: .utf8) {
|
||||||
try KeychainStore.shared.save(data, account: "accountAndPasword")
|
try KeychainStore.shared.save(data, account: "accountAndPasword")
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user