2026-04-21 22:07:29 +08:00
|
|
|
import { getConfig } from '../core/config'
|
2026-04-24 10:42:11 +08:00
|
|
|
import { apiRequest, getToken, saveToken } from '../core/http'
|
2026-04-21 22:07:29 +08:00
|
|
|
import { ImClient } from './imClient'
|
2026-04-24 11:14:41 +08:00
|
|
|
import type { ChatType, ImEventListener, ImGroup, ImMessage, MsgType } from './types'
|
2026-04-21 22:07:29 +08:00
|
|
|
|
|
|
|
|
let client: ImClient | null = null
|
2026-04-29 15:46:40 +08:00
|
|
|
const DEFAULT_IM_WS_URL = 'wss://dev.xuqinmin.com/ws/im'
|
2026-04-21 22:07:29 +08:00
|
|
|
|
|
|
|
|
export const ImSDK = {
|
|
|
|
|
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',
|
|
|
|
|
params: {
|
2026-04-29 15:46:40 +08:00
|
|
|
appId: config.appKey,
|
2026-04-21 22:07:29 +08:00
|
|
|
userId,
|
|
|
|
|
...(nickname ? { nickname } : {}),
|
|
|
|
|
...(avatar ? { avatar } : {}),
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
await saveToken(res.token)
|
2026-04-29 15:46:40 +08:00
|
|
|
client = new ImClient(DEFAULT_IM_WS_URL, res.token, config.appKey)
|
2026-04-21 22:07:29 +08:00
|
|
|
client.connect()
|
|
|
|
|
},
|
|
|
|
|
|
2026-04-24 10:42:11 +08:00
|
|
|
async reconnect(): Promise<void> {
|
|
|
|
|
const config = getConfig()
|
|
|
|
|
const token = await getToken()
|
|
|
|
|
if (!token) throw new Error('ImSDK: token not found')
|
2026-04-29 15:46:40 +08:00
|
|
|
client = new ImClient(DEFAULT_IM_WS_URL, token, config.appKey)
|
2026-04-24 10:42:11 +08:00
|
|
|
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: {
|
2026-04-29 15:46:40 +08:00
|
|
|
appId: config.appKey,
|
2026-04-24 10:42:11 +08:00
|
|
|
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',
|
2026-04-29 15:46:40 +08:00
|
|
|
params: { appId: config.appKey },
|
2026-04-24 10:42:11 +08:00
|
|
|
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',
|
2026-04-29 15:46:40 +08:00
|
|
|
params: { appId: config.appKey },
|
2026-04-24 10:42:11 +08:00
|
|
|
})
|
2026-04-21 22:07:29 +08:00
|
|
|
},
|
|
|
|
|
|
2026-04-29 00:37:10 +08:00
|
|
|
async editMessage(messageId: string, content: string): Promise<ImMessage> {
|
|
|
|
|
const config = getConfig()
|
|
|
|
|
return apiRequest<ImMessage>(`/api/im/messages/${encodeURIComponent(messageId)}`, {
|
|
|
|
|
method: 'PUT',
|
2026-04-29 15:46:40 +08:00
|
|
|
params: { appId: config.appKey },
|
2026-04-29 00:37:10 +08:00
|
|
|
body: { content },
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
|
2026-04-21 22:07:29 +08:00
|
|
|
addListener(listener: ImEventListener) {
|
|
|
|
|
client?.addListener(listener)
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
removeListener(listener: ImEventListener) {
|
|
|
|
|
client?.removeListener(listener)
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
disconnect() {
|
|
|
|
|
client?.disconnect()
|
|
|
|
|
client = null
|
|
|
|
|
},
|
|
|
|
|
|
2026-04-24 10:42:11 +08:00
|
|
|
subscribeGroup(groupId: string) {
|
|
|
|
|
client?.subscribeGroup(groupId)
|
|
|
|
|
},
|
|
|
|
|
|
2026-04-21 22:07:29 +08:00
|
|
|
isConnected() {
|
|
|
|
|
return client?.isConnected() ?? false
|
|
|
|
|
},
|
2026-04-24 11:14:41 +08:00
|
|
|
|
|
|
|
|
async createGroup(name: string, memberIds: string[]): Promise<ImGroup> {
|
|
|
|
|
const config = getConfig()
|
|
|
|
|
return apiRequest<ImGroup>('/api/im/groups', {
|
|
|
|
|
method: 'POST',
|
2026-04-29 15:46:40 +08:00
|
|
|
params: { appId: config.appKey },
|
2026-04-24 11:14:41 +08:00
|
|
|
body: { name, memberIds },
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
async listGroups(): Promise<ImGroup[]> {
|
|
|
|
|
const config = getConfig()
|
|
|
|
|
const res = await apiRequest<ImGroup[] | { content?: ImGroup[] }>('/api/im/groups', {
|
2026-04-29 15:46:40 +08:00
|
|
|
params: { appId: config.appKey },
|
2026-04-24 11:14:41 +08:00
|
|
|
})
|
|
|
|
|
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: {
|
2026-04-29 15:46:40 +08:00
|
|
|
appId: config.appKey,
|
2026-04-24 11:14:41 +08:00
|
|
|
page: String(page),
|
|
|
|
|
size: String(size),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
return Array.isArray(res) ? res : (res.content ?? [])
|
|
|
|
|
},
|
2026-04-21 22:07:29 +08:00
|
|
|
}
|