diff --git a/Sources/XuqmSDK/Core/XuqmSDK.swift b/Sources/XuqmSDK/Core/XuqmSDK.swift
index ac07ddc..1d018f3 100644
--- a/Sources/XuqmSDK/Core/XuqmSDK.swift
+++ b/Sources/XuqmSDK/Core/XuqmSDK.swift
@@ -34,7 +34,7 @@ public final class XuqmSDK: NSObject {
self.userSig = userSig
do {
- try await ImSDK.shared.loginWithUserSig(userId, userSig)
+ try await ImSDK.shared.login(userId, userSig)
} catch {
// IM login failed; silently ignored per facade pattern
}
diff --git a/Sources/XuqmSDK/IM/ImSDK.swift b/Sources/XuqmSDK/IM/ImSDK.swift
index bb4efca..6651627 100644
--- a/Sources/XuqmSDK/IM/ImSDK.swift
+++ b/Sources/XuqmSDK/IM/ImSDK.swift
@@ -24,7 +24,7 @@ public final class ImSDK {
}
}
- public func loginWithUserSig(_ userId: String, _ userSig: String) async throws {
+ public func login(_ userId: String, _ userSig: String) async throws {
let config = XuqmSDK.shared.requireConfig()
currentUserId = userId
XuqmSDK.shared.tokenStore?.save(userSig)
@@ -36,57 +36,6 @@ public final class ImSDK {
client?.connect()
}
- public func login(userId: String) async throws {
- let config = XuqmSDK.shared.requireConfig()
-
- let items = [
- URLQueryItem(name: "appId", value: config.appId),
- URLQueryItem(name: "userId", value: userId),
- ]
-
- let res: ImLoginResponse = try await ApiClient.shared.request(
- path: "/api/im/auth/login",
- method: "POST",
- queryItems: items
- )
- currentUserId = userId
- XuqmSDK.shared.tokenStore?.save(res.token)
- client?.disconnect()
- client = ImClient(token: res.token, appId: config.appId)
- client?.setCurrentUserId(userId)
- client?.delegate = self
- updateConnectionState(.connecting)
- client?.connect()
- }
-
- public func loginWithDemo(userId: String, password: String = "123456") async throws {
- let config = XuqmSDK.shared.requireConfig()
- struct DemoLoginResponse: Decodable, Sendable {
- let profile: DemoProfile
- let imToken: String
- struct DemoProfile: Decodable, Sendable {
- let appId: String
- let userId: String
- let nickname: String?
- let avatar: String?
- }
- }
- let res: DemoLoginResponse = try await ApiClient.shared.request(
- path: "/api/demo/auth/login",
- method: "POST",
- queryItems: [URLQueryItem(name: "appId", value: config.appId)],
- body: ["appId": config.appId, "userId": userId, "password": password]
- )
- currentUserId = res.profile.userId
- XuqmSDK.shared.tokenStore?.save(res.imToken)
- client?.disconnect()
- client = ImClient(token: res.imToken, appId: config.appId)
- client?.setCurrentUserId(res.profile.userId)
- client?.delegate = self
- updateConnectionState(.connecting)
- client?.connect()
- }
-
public func setDelegate(_ delegate: ImEventDelegate) {
self.delegate = delegate
client?.delegate = self
diff --git a/TEST_REPORT.md b/TEST_REPORT.md
index d836230..5994f3c 100644
--- a/TEST_REPORT.md
+++ b/TEST_REPORT.md
@@ -37,7 +37,7 @@
| 字段 | 内容 |
|------|------|
| **测试目的** | 验证 UserSig 鉴权登录与登出流程 |
-| **测试步骤** | 1. 调用 `XuqmSDK.shared.login(userId: "user_001", userSig: "xxx")`
2. 观察 `ImSDK.shared.loginWithUserSig` 内部触发 WebSocket 连接
3. 监听 `ImEventDelegate.imClientDidConnect()`
4. 调用 `XuqmSDK.shared.logout()`
5. 确认 `ImSDK.shared.disconnect()` 执行,Push Token 解注册 |
+| **测试步骤** | 1. 调用 `XuqmSDK.shared.login(userId: "user_001", userSig: "xxx")`
2. 观察 `ImSDK.shared.login` 内部触发 WebSocket 连接
3. 监听 `ImEventDelegate.imClientDidConnect()`
4. 调用 `XuqmSDK.shared.logout()`
5. 确认 `ImSDK.shared.disconnect()` 执行,Push Token 解注册 |
| **预期结果** | 1. `currentUserId` 被赋值
2. WebSocket 连接成功,状态变为 `.connecting` → `.connected`
3. delegate `imClientDidConnect()` 触发
4. 登出后 `currentUserId` 置 nil
5. `PushSDK.shared.unregisterToken` 被调用 |
| **实际结果** | 待测试 |
| **通过状态** | ⬜ |
diff --git a/XuqmDemo/Sources/ViewModels/AuthViewModel.swift b/XuqmDemo/Sources/ViewModels/AuthViewModel.swift
index 36f2736..60e031a 100644
--- a/XuqmDemo/Sources/ViewModels/AuthViewModel.swift
+++ b/XuqmDemo/Sources/ViewModels/AuthViewModel.swift
@@ -7,6 +7,14 @@ final class AuthViewModel: ObservableObject {
@Published var currentUserId: String = ""
@Published var currentNickname: String = ""
+ private struct DemoLoginResponse: Decodable, Sendable {
+ let imToken: String
+ let profile: DemoProfile
+ struct DemoProfile: Decodable, Sendable {
+ let userId: String
+ }
+ }
+
func login(userId: String, password: String) {
guard !userId.isEmpty, !password.isEmpty else {
state = .error("请输入用户 ID 和密码")
@@ -15,9 +23,16 @@ final class AuthViewModel: ObservableObject {
state = .loading
Task {
do {
- try await ImSDK.shared.loginWithDemo(userId: userId, password: password)
- currentUserId = userId
- currentNickname = userId
+ let config = XuqmSDK.shared.requireConfig()
+ let res: DemoLoginResponse = try await ApiClient.shared.request(
+ path: "/api/demo/auth/login",
+ method: "POST",
+ queryItems: [URLQueryItem(name: "appId", value: config.appId)],
+ body: ["appId": config.appId, "userId": userId, "password": password]
+ )
+ await XuqmSDK.shared.login(userId: res.profile.userId, userSig: res.imToken)
+ currentUserId = res.profile.userId
+ currentNickname = res.profile.userId
state = .success
} catch {
state = .error(error.localizedDescription)
@@ -26,7 +41,7 @@ final class AuthViewModel: ObservableObject {
}
func logout() {
- ImSDK.shared.disconnect()
+ Task { await XuqmSDK.shared.logout() }
currentUserId = ""
currentNickname = ""
state = .idle