diff --git a/punchnet/Views/Login/LoginView.swift b/punchnet/Views/Login/LoginView.swift index 8319446..24b943a 100644 --- a/punchnet/Views/Login/LoginView.swift +++ b/punchnet/Views/Login/LoginView.swift @@ -74,39 +74,46 @@ struct LoginView: View { struct LoginTokenView: View { @Environment(UserContext.self) var userContext: UserContext - @State private var token: String = "49974818809840025617726088179154" + @State private var token: String = "" @State private var showAlert = false @State private var errorMessage = "" var body: some View { - TextField("认证密钥", text: $token) - .multilineTextAlignment(.leading) - .textFieldStyle(PlainTextFieldStyle()) - .frame(width: 200, height: 25) - .background(Color.clear) - .foregroundColor(Color.black) - .overlay( - Rectangle() - .frame(height: 1) - .foregroundColor(.blue) - .padding(.top, 25) - , alignment: .topLeading) - - Rectangle() - .overlay { - Text("登陆") - .font(.system(size: 14, weight: .regular)) - .foregroundColor(.black) - } - .frame(width: 120, height: 35) - .foregroundColor(Color(red: 74 / 255, green: 207 / 255, blue: 154 / 255)) - .cornerRadius(5.0) - .onTapGesture { - Task { - await self.doLogin() + VStack { + TextField("认证密钥", text: $token) + .multilineTextAlignment(.leading) + .textFieldStyle(PlainTextFieldStyle()) + .frame(width: 200, height: 25) + .background(Color.clear) + .foregroundColor(Color.black) + .overlay( + Rectangle() + .frame(height: 1) + .foregroundColor(.blue) + .padding(.top, 25) + , alignment: .topLeading) + + Rectangle() + .overlay { + Text("登陆") + .font(.system(size: 14, weight: .regular)) + .foregroundColor(.black) } + .frame(width: 120, height: 35) + .foregroundColor(Color(red: 74 / 255, green: 207 / 255, blue: 154 / 255)) + .cornerRadius(5.0) + .onTapGesture { + Task { + await self.doLogin() + } + } + } + .onAppear { + if let cacheToken = self.userContext.loadCacheToken() { + self.token = cacheToken } + } } // 执行登陆操作 @@ -139,8 +146,8 @@ struct LoginTokenView: View { struct LoginAccountView: View { @Environment(UserContext.self) var userContext: UserContext - @State private var username: String = "test3" - @State private var password: String = "111111" + @State private var username: String = "" + @State private var password: String = "" @State private var showAlert = false @State private var errorMessage = "" @@ -208,6 +215,12 @@ struct LoginAccountView: View { .alert(isPresented: $showAlert) { Alert(title: Text("错误提示"), message: Text("账号密码为空")) } + .onAppear { + if let (cacheUsername, cachePassword) = self.userContext.loadCacheUsernameAndPassword() { + self.username = cacheUsername + self.password = cachePassword + } + } } // 执行登陆操作 diff --git a/punchnet/Views/UserContext.swift b/punchnet/Views/UserContext.swift index abb8fb0..dcf49e6 100644 --- a/punchnet/Views/UserContext.swift +++ b/punchnet/Views/UserContext.swift @@ -11,10 +11,10 @@ import Observation @Observable class UserContext { var isLogined: Bool = false - var loginCredit: LoginCredit? + var loginCredit: Credit? var networkSession: NetworkSession? - - enum LoginCredit { + + enum Credit { case token(token: String) case accountAndPasword(account: String, password: String) } @@ -68,6 +68,11 @@ class UserContext { self.networkSession = try await SDLAPIClient.doPost(path: "/auth/login", params: params, as: NetworkSession.self) self.loginCredit = .accountAndPasword(account: username, password: password) self.isLogined = true + + // 将数据缓存到keychain + if let data = "\(username):\(password)".data(using: .utf8) { + try KeychainStore.shared.save(data, account: "accountAndPasword") + } } @MainActor @@ -80,6 +85,29 @@ class UserContext { self.networkSession = try await SDLAPIClient.doPost(path: "/auth/token", params: params, as: NetworkSession.self) self.loginCredit = .token(token: token) self.isLogined = true + + // 将数据缓存到keychain + if let data = token.data(using: .utf8) { + try KeychainStore.shared.save(data, account: "token") + } + } + + func loadCacheToken() -> String? { + if let data = try? KeychainStore.shared.load(account: "token") { + return String(data: data, encoding: .utf8) + } + return nil + } + + func loadCacheUsernameAndPassword() -> (String, String)? { + if let data = try? KeychainStore.shared.load(account: "accountAndPasword"), + let str = String(data: data, encoding: .utf8) { + let parts = str.split(separator: ":") + if parts.count == 2 { + return (String(parts[0]), String(parts[1])) + } + } + return nil } }