104 行
3.5 KiB
TypeScript
104 行
3.5 KiB
TypeScript
|
|
import { apiRequest, _getToken, _saveToken, getConfig } from '@xuqm/rn-common'
|
||
|
|
import { ImClient } from './ImClient'
|
||
|
|
import type { ChatType, ImEventListener, ImGroup, ImMessage, MsgType } from './types'
|
||
|
|
|
||
|
|
let client: ImClient | null = null
|
||
|
|
|
||
|
|
export const ImSDK = {
|
||
|
|
/**
|
||
|
|
* Login to IM service. Fetches a token internally and opens the WebSocket connection.
|
||
|
|
*/
|
||
|
|
async login(userId: string, nickname?: string, avatar?: string): Promise<void> {
|
||
|
|
const config = getConfig()
|
||
|
|
const res = await apiRequest<{ token: string }>('/api/im/auth/login', {
|
||
|
|
method: 'POST',
|
||
|
|
skipAuth: true,
|
||
|
|
params: {
|
||
|
|
appId: config.appId,
|
||
|
|
userId,
|
||
|
|
...(nickname ? { nickname } : {}),
|
||
|
|
...(avatar ? { avatar } : {}),
|
||
|
|
},
|
||
|
|
})
|
||
|
|
await _saveToken(res.token)
|
||
|
|
client = new ImClient(config.imWsUrl, res.token, config.appId)
|
||
|
|
client.connect()
|
||
|
|
},
|
||
|
|
|
||
|
|
async reconnect(): Promise<void> {
|
||
|
|
const config = getConfig()
|
||
|
|
const token = await _getToken()
|
||
|
|
if (!token) throw new Error('[ImSDK] No active session — call login() first.')
|
||
|
|
client = new ImClient(config.imWsUrl, token, config.appId)
|
||
|
|
client.connect()
|
||
|
|
},
|
||
|
|
|
||
|
|
async fetchHistory(toId: string, page = 0, size = 20): Promise<ImMessage[]> {
|
||
|
|
const config = getConfig()
|
||
|
|
const res = await apiRequest<{ content?: ImMessage[] } | ImMessage[]>(
|
||
|
|
`/api/im/messages/history/${encodeURIComponent(toId)}`,
|
||
|
|
{ params: { appId: config.appId, page: String(page), size: String(size) } },
|
||
|
|
)
|
||
|
|
return Array.isArray(res) ? res : (res.content ?? [])
|
||
|
|
},
|
||
|
|
|
||
|
|
async sendMessage(
|
||
|
|
toId: string,
|
||
|
|
chatType: ChatType,
|
||
|
|
msgType: MsgType,
|
||
|
|
content: string,
|
||
|
|
mentionedUserIds?: string,
|
||
|
|
): Promise<ImMessage> {
|
||
|
|
const config = getConfig()
|
||
|
|
return apiRequest<ImMessage>('/api/im/messages/send', {
|
||
|
|
method: 'POST',
|
||
|
|
params: { appId: config.appId },
|
||
|
|
body: { toId, chatType, msgType, content, mentionedUserIds: mentionedUserIds ?? '' },
|
||
|
|
})
|
||
|
|
},
|
||
|
|
|
||
|
|
async revokeMessage(messageId: string): Promise<ImMessage> {
|
||
|
|
const config = getConfig()
|
||
|
|
return apiRequest<ImMessage>(`/api/im/messages/${encodeURIComponent(messageId)}/revoke`, {
|
||
|
|
method: 'POST',
|
||
|
|
params: { appId: config.appId },
|
||
|
|
})
|
||
|
|
},
|
||
|
|
|
||
|
|
async createGroup(name: string, memberIds: string[]): Promise<ImGroup> {
|
||
|
|
const config = getConfig()
|
||
|
|
return apiRequest<ImGroup>('/api/im/groups', {
|
||
|
|
method: 'POST',
|
||
|
|
params: { appId: config.appId },
|
||
|
|
body: { name, memberIds },
|
||
|
|
})
|
||
|
|
},
|
||
|
|
|
||
|
|
async listGroups(): Promise<ImGroup[]> {
|
||
|
|
const config = getConfig()
|
||
|
|
const res = await apiRequest<ImGroup[] | { content?: ImGroup[] }>('/api/im/groups', {
|
||
|
|
params: { appId: config.appId },
|
||
|
|
})
|
||
|
|
return Array.isArray(res) ? res : (res.content ?? [])
|
||
|
|
},
|
||
|
|
|
||
|
|
async fetchGroupHistory(groupId: string, page = 0, size = 50): Promise<ImMessage[]> {
|
||
|
|
const config = getConfig()
|
||
|
|
const res = await apiRequest<{ content?: ImMessage[] } | ImMessage[]>(
|
||
|
|
`/api/im/messages/history/${encodeURIComponent(groupId)}`,
|
||
|
|
{ params: { appId: config.appId, page: String(page), size: String(size) } },
|
||
|
|
)
|
||
|
|
return Array.isArray(res) ? res : (res.content ?? [])
|
||
|
|
},
|
||
|
|
|
||
|
|
addListener(listener: ImEventListener): void { client?.addListener(listener) },
|
||
|
|
removeListener(listener: ImEventListener): void { client?.removeListener(listener) },
|
||
|
|
subscribeGroup(groupId: string): void { client?.subscribeGroup(groupId) },
|
||
|
|
isConnected(): boolean { return client?.isConnected() ?? false },
|
||
|
|
|
||
|
|
disconnect(): void {
|
||
|
|
client?.disconnect()
|
||
|
|
client = null
|
||
|
|
},
|
||
|
|
}
|