IM聊天相关接口
- im 服务有关的 API
创建单聊会话(createSingleChat)
- 支持环境:web 插件、webview 页面
- 创建单聊会话
js
export function createSingleChat(params: {
user: {
id: number | string
name: string
}
isThirdUserId: boolean // userid 如果是第三方 id,需要设为 true
}): Promise<number | void>- 示例
js
const chatid = await ksxz.im.createSingleChat({
user: {
id: id,
name: 'tk01'
},
isThirdUserId: true
})创建群聊会话(createGroupChat)
- 支持环境:web 插件、webview 页面
- 创建群聊会话
js
export function createGroupChat(params: {
users: {
id: number | string
name: string
}[]
isThirdUserId: boolean // userid 如果是第三方 id,需要设为 true
}): Promise<number | void>- 示例
js
const chatid = await ksxz.im.createGroupChat({
users: [
{
id: 'tk01',
name: 'tk01'
},
{
id: 'tk02',
name: 'tk02'
}
],
isThirdUserId: true
})根据用户id获取用户信息(getUserById)
- 支持环境:web、webview、node
- 根据用户id获取用户信息
js
/**
* 根据用户id获取用户信息
* @param userid 用户id
* @param isThirdId 用户id是否为第三方id
*/
export function getUserById(
userid: number | string,
isThirdId?: boolean
): Promise<IUser | undefined>;- 示例
js
const user = await ksxz.im.getUserById('tk01', true)
console.log(user)获取消息信息(getMessage)
- 支持环境:web/node/webview
- 根据会话id与消息id查询消息的数据
js
interface IMessage<T = any> {
cid: string
msgid: number
msgType: number
chatid: number
sender: number
content: T
}
// 获取消息数据
export function getMessage(params: { chatid: number; msgid: number }): Promise<IMessage>- 示例
js
const message = await ksxz.im.getMessage({ msgid, chatid })发送自定义消息(sendMessage)
- 支持环境:web/node/webview
- sendMessage 用于发送自定义消息,结合 registerMessageDisplayer 接口渲染自定义消息。
js
// 消息强提醒
export interface INotice {
/**
* 强提醒类型
* kim-mention: @人、@所有人
*/
noticeType: string
/** 全员设置 */
isAll: boolean
/** 接收强提醒用户标记 */
uids: string[]
}
// 消息离线推送配置
export interface IPushConfig {
/**
* 离线推送显示标题
* 如果是内置消息类型,则无需填,默认填充
* 如果是自定义消息类型,必填,不然无法离线推送
*/
title?: string
/**
* 离线推送显示内容
* 如果是内置消息类型,则无需填,默认填充
* 如果是自定义消息类型,必填,不然无法离线推送
*/
content?: string
/**
* 离线推送操作类型
* 0: 默认值,群聊所有人离线推送
* 1: 所有人不离线推送
*/
pushType?: number
/**
* 离线推送权限级别
* 0: 默认值
* 1: 忽视会话免打扰
*/
pushLevel?: number
}
export interface IAppCustomMsgContent {
content: string
customizeType: string
msgDesc: string
}
export interface ISendMessageParams {
chatid: number
sendProps: {
content: IAppCustomMsgContent
}
sendOptions?: {
// 离线推送配置
pushConfig?: IPushConfig
// 消息强提醒
notices?: INotice[]
// 不记录未读数
unmarkUnread?: boolean
}
}
// 发送消息
export function sendMessage(params: ISendMessageParams): Promise<void>