35 行
937 B
Swift
35 行
937 B
Swift
|
|
import Foundation
|
||
|
|
import XuqmSDK
|
||
|
|
|
||
|
|
@MainActor
|
||
|
|
final class AuthViewModel: ObservableObject {
|
||
|
|
@Published var state: LoginState = .idle
|
||
|
|
@Published var currentUserId: String = ""
|
||
|
|
@Published var currentNickname: String = ""
|
||
|
|
|
||
|
|
func login(userId: String, password: String) {
|
||
|
|
guard !userId.isEmpty, !password.isEmpty else {
|
||
|
|
state = .error("请输入用户 ID 和密码")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
state = .loading
|
||
|
|
Task {
|
||
|
|
do {
|
||
|
|
try await ImSDK.shared.loginWithDemo(userId: userId, password: password)
|
||
|
|
currentUserId = userId
|
||
|
|
currentNickname = userId
|
||
|
|
state = .success
|
||
|
|
} catch {
|
||
|
|
state = .error(error.localizedDescription)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func logout() {
|
||
|
|
ImSDK.shared.disconnect()
|
||
|
|
currentUserId = ""
|
||
|
|
currentNickname = ""
|
||
|
|
state = .idle
|
||
|
|
}
|
||
|
|
}
|