0.0.25 • Published 3 years ago

utils-tim v0.0.25

Weekly downloads
5
License
ISC
Repository
-
Last release
3 years ago

Tim-Util

基于腾讯即时通信IM的工具类封装。

更新日志

点击前往

API

init

初始化 Tim SDK。

参数:

Object object
属性类型默认值必填说明
SDKAPPIDstring/numbertim sdk的 appid,在腾讯云控制台获取
nickNamestring默认昵称用于聊天列表展示的昵称
TIM_SDKobjectTIM SDK
COSobject发送图片、文件等消息需要的 COS SDK
groupIdstring发送消息时需要用到的目标群组id
userIdstring用户id
userSigstring用户签名
onReadyFunctionSDK 处于 READY 状态的回调,可在此回调中处理加群等操作
onConversationListUpdatedFunction会话列表更新时的回调
onKickedOutFunction被踢掉线回调
onLoginErrorFunction登录失败回调

用法:

import TimUtil from 'tim-util'  // 引入node_modules中的tim-util
import TIM_SDK from './../tencent-webim/tim-wx'  // 引入tim wx sdk
// 发送图片、文件等消息需要的 COS SDK
import COS from 'cos-wx-sdk-v5'

const onTimReady = () => {
	console.log('初始化成功')
	TimUtil.joinGroup()  // 加入群组
}

const onConversationListUpdated = (res) => {
	console.log('conversation list', res)  // 会话列表数组
}

const onKickedOut = (e) => {
	console.log('被踢掉线啦')  // 这里可以做一些处理,如重新连接
}

const onLoginError = () => {
	console.log('login error')  // 这里可以做一些处理,如重新登录
}

// 调用init方法
TimUtil.init({
	SDKAPPID: 'tim appid',
	nickName: 'im test nickname',
	TIM_SDK,
	COS,
	groupId: 'chatroom groupid',
	userId: 'user id',
	userSig: 'user sign',
	onReady: onTimReady,
	onConversationListUpdated: onConversationListUpdated,
	onKickedOut: onKickedOut,
	onLoginError: onLoginError,
}

login

tim 登录。

参数

Object Object
属性类型默认值必填说明
userIdstring用户id
userSigstring用户签名
onReadyFunctionTim SDK 处于 Ready 状态的回调
onLoginErrorFunction登录错误回调

用法

const onTimReady = () => {
	console.log('初始化成功')
	TimUtil.joinGroup()  // 加入群组
}

const onLoginError = () => {
	console.log('login error')  // 这里可以做一些处理,如重新登录
}

TimUtil.login({
	userId: 'user id',
	userSig: 'user sign',
	onReady: onTimReady,
	onLoginError: onLoginError,
}

joinGroup

加入群组,只有在 sdk 处于 ready 状态时才能调用。

createCustomMsg

创建自定义消息。

参数

Object Object
属性类型默认值必填说明
payloadobject消息内容
payload.datastringbuying-购买, guiding-讲解, coming-进入直播间消息, like-点赞, posterShowStatus-海报展示状态(即将废弃,请使用eleVisibleeleVisible), eleVisible-元素显隐状态, text-文本消息
payload.extensionobject自定义扩展字段

使用示例

const customMsg = TimUtil.createCustomMsg({
	payload: {
		data: 'buying',
	},
})
TimUtil.sendMsg(customMsg).then(() => {
	console.log('消息发送成功')
})

createTextMsg

创建文本消息。

参数

Object Object
属性类型默认值必填说明
textstring文本内容

like

发送点赞消息。

sendMsg

发送消息,可发送通过 createTextMsg 和 createCustomMsg 方法创建的文本和自定义消息,返回一个封装了发送结果的 Promise。

参数

msgBody

通过 createTextMsg 和 createCustomMsg 创建的消息。

用法
TimUtil.sendMsg(msg)
.then(res => {
	console.log('消息发送成功')
	resolve(res)
})
.catch(err => {
	this.onSendMsgError(err)
})

sendC2CCustomMsg

发送端对端自定义消息。

参数

Object Object
属性类型默认值必填说明
toUserIdstring目标用户IM userId
msgobject object消息体
TimUtil.sendC2CCustomMsg({
	toUserId: '',  // 目标userId
	msg: {
		data: 'linkmic',  // 消息类型
		extension: {      // 自定义扩展字段
			type: 'respond',
			action: 'reject',
			msgDirection: 'c2a', 
			userNickName: '',
			userId: '',
		},
	},
})

sendGroupCustomMsg

发送群自定义消息。

参数

Object Object
属性类型默认值必填说明
toGroupIdstringIM groupId
msgobject object消息体
TimUtil.sendC2CCustomMsg({
	toGroupId: '',    // 群id
	msg: {
		data: 'linkmic',  // 自定义消息类型
		extension: {      // 自定义扩展字段
			type: 'respond',
			action: 'reject',
			msgDirection: 'c2a',
			userNickName: '',
			userId: '',
		},
	},
})
0.0.25

3 years ago

0.0.24

3 years ago