2026-05-01 21:27:39 +08:00
|
|
|
import { _clearToken, setUserId as setCommonUserId, getUserId as getCommonUserId, XuqmSDK as CommonXuqmSDK } from '@xuqm/rn-common'
|
|
|
|
|
import { ImSDK } from '@xuqm/rn-im'
|
|
|
|
|
import { PushSDK } from '@xuqm/rn-push'
|
|
|
|
|
|
|
|
|
|
export interface UnifiedLoginOptions {
|
|
|
|
|
userId: string
|
|
|
|
|
userSig: string
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-01 22:40:10 +08:00
|
|
|
let currentSession: UnifiedLoginOptions | null = null
|
2026-05-01 21:27:39 +08:00
|
|
|
|
2026-05-01 22:40:10 +08:00
|
|
|
async function applyLoginSession(session: UnifiedLoginOptions): Promise<void> {
|
2026-05-01 21:27:39 +08:00
|
|
|
setCommonUserId(session.userId)
|
|
|
|
|
try {
|
|
|
|
|
await ImSDK.loginWithUserSig(session.userId, session.userSig)
|
|
|
|
|
} catch (error) {
|
|
|
|
|
setCommonUserId(null)
|
|
|
|
|
currentSession = null
|
|
|
|
|
throw error
|
|
|
|
|
}
|
|
|
|
|
currentSession = session
|
|
|
|
|
try {
|
|
|
|
|
await PushSDK.initialize(session.userId)
|
|
|
|
|
} catch (error) {
|
|
|
|
|
void error
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function login(options: UnifiedLoginOptions): Promise<void> {
|
|
|
|
|
if (currentSession) {
|
|
|
|
|
await logout()
|
|
|
|
|
}
|
|
|
|
|
await applyLoginSession(options)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function logout(): Promise<void> {
|
|
|
|
|
const userId = currentSession?.userId ?? getCommonUserId()
|
|
|
|
|
currentSession = null
|
|
|
|
|
setCommonUserId(null)
|
|
|
|
|
if (userId) {
|
|
|
|
|
try {
|
|
|
|
|
await PushSDK.logout(userId)
|
|
|
|
|
} catch (error) {
|
|
|
|
|
void error
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ImSDK.disconnect()
|
|
|
|
|
await _clearToken()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const XuqmSDK = {
|
|
|
|
|
...CommonXuqmSDK,
|
|
|
|
|
login,
|
|
|
|
|
logout,
|
|
|
|
|
}
|