【腾讯IM前端使用】uni.app开发微信小程序直播间 - 登录进群和匿名进群(匿名进群不可发言)

引入依赖

import TIM from 'tim-wx-sdk' //小程序环境

初始化

async Initialisierung() {
				// 创建 SDK 实例,TIM.create() 方法对于同一个 SDKAppID 只会返回同一份实例
				let options = {
					SDKAppID: '' // 接入时需要将0替换为您的即时通信应用的 SDKAppID
				};
				let tim = TIM.create(options); // SDK 实例通常用 tim 表示
				this.tim = tim
				await this.Benutzeranmeldung()
			},

登录

// 登录
				async Benutzeranmeldung() {
				// 登录
				let userSig = genTestUserSig(userID).userSig;
				let promiselogin = this.tim.login({
					userID: userID,
					userSig: userSig
				});
				await promiselogin.then(function(imResponse) {
					if (imResponse.data.repeatLogin === true) {
						console.log(imResponse.data.errorInfo, '登录1');
					}
				}).catch(function(imError) {
					console.warn('login error:', imError); // 登录失败的相关信息
				});
				await this.addlivegroup()
			},

// 加群

	async addlivegroup() {
				let this_ = this
				// 加群
				let promisejoinGroup = this.tim.joinGroup({
					groupID: '',
					type: TIM.TYPES.GRP_AVCHATROOM
				});
				await promisejoinGroup.then(function(imResponse) {
					switch (imResponse.data.status) {
						case TIM.TYPES.JOIN_STATUS_WAIT_APPROVAL: // 等待管理员同意
							break;
						case TIM.TYPES.JOIN_STATUS_SUCCESS: // 加群成功
							console.log(imResponse.data.group); // 加入的群组资料
							break;
						case TIM.TYPES.JOIN_STATUS_ALREADY_IN_GROUP: // 已经在群中
							break;
						default:
							break;
					}
				}).then(function(resjoinGroup) { // 获取成功
					console.log('加入成功1')
					this_.listeningcallback()
				}).catch(function(imError) {
					console.warn('joinGroup error:', imError); // 申请加群失败的相关信息
				});
			},

//监听消息

async listeningcallback() {
				let this_ = this
				console.log('执行消息函数')
				let onMessageReceived = function(event) {
					console.log(event, '收到消息总数据')
					const messageList = event.data;
					messageList.forEach((message) => {
						console.log(message.type)
						if (message.type === TIM.TYPES.MSG_TEXT) {
							console.log(message, '文本消息message')
							this_.indexList.push({
								name: message.nick,
								content: message.payload.text
							})
							this_.inputValue = ''
							this_.scrollTop += 600
							// 文本消息 - https://web.sdk.qcloud.com/im/doc/en/Message.html#.TextPayload
						} else if (message.type === TIM.TYPES.MSG_IMAGE) {
							console.log(message, '图片消息message')
							// 图片消息 - https://web.sdk.qcloud.com/im/doc/en/Message.html#.ImagePayload
						} else if (message.type === TIM.TYPES.MSG_SOUND) {
							console.log(message, '音频消息message')
							// 音频消息 - https://web.sdk.qcloud.com/im/doc/en/Message.html#.AudioPayload
						} else if (message.type === TIM.TYPES.MSG_VIDEO) {
							console.log(message, '视频消息message')
							// 视频消息 - https://web.sdk.qcloud.com/im/doc/en/Message.html#.VideoPayload
						} else if (message.type === TIM.TYPES.MSG_FILE) {
							console.log(message, '文件消息message')
							// 文件消息 - https://web.sdk.qcloud.com/im/doc/en/Message.html#.FilePayload
						} else if (message.type === TIM.TYPES.MSG_CUSTOM) {
							console.log(message, '自定义消息message')
							// 自定义消息 - https://web.sdk.qcloud.com/im/doc/en/Message.html#.CustomPayload
						} else if (message.type === TIM.TYPES.MSG_MERGER) {
							console.log(message, '合并消息message')
							// 合并消息 - https://web.sdk.qcloud.com/im/doc/en/Message.html#.MergerPayload
						} else if (message.type === TIM.TYPES.MSG_LOCATION) {
							console.log(message, '地理位置消息message')
							// 地理位置消息 - https://web.sdk.qcloud.com/im/doc/en/Message.html#.LocationPayload
						} else if (message.type === TIM.TYPES.MSG_GRP_TIP) {
							console.log(message, '群提示消息message')
							// 群提示消息 - https://web.sdk.qcloud.com/im/doc/en/Message.html#.GroupTipPayload
						} else if (message.type === TIM.TYPES.MSG_GRP_SYS_NOTICE) {
							let typedata = JSON.parse(message.payload.userDefinedField)
							// 执行商品推荐操作
							if( typedata.type == 1){
								console.log(typedata,'执行商品推荐操作')
							}
							console.log(message, '群提示消息message')
							console.log(typedata, '群提示消息,自定义消息体')
							// 群系统通知 - https://web.sdk.qcloud.com/im/doc/en/Message.html#.GroupSystemNoticePayload
						}
					});
					// 收到推送的单聊、群聊、群提示、群系统通知的新消息,可通过遍历 event.data 获取消息列表数据并渲染到页面
					// event.name - TIM.EVENT.MESSAGE_RECEIVED
					// event.data - 存储 Message 对象的数组 - [Message]

				};
				this.tim.on(TIM.EVENT.MESSAGE_RECEIVED, onMessageReceived);
			},

发送消息

// 发送群消息
handleInput(value) {
					let message = this.tim.createTextMessage({
						to: '@TGS#aOGRW4RJA',
						conversationType: TIM.TYPES.CONV_GROUP,
						payload: {
							text: value
						},
						// v2.18.0起支持群消息已读回执功能,如果您发消息需要已读回执,需购买旗舰版套餐,并且创建消息时将 needReadReceipt 设置为 true
						needReadReceipt: true
					});
					// 发送消息
					let promise = this.tim.sendMessage(message);
					promise.then(function(imResponse) {
						// 发送成功
						console.log(imResponse, '发送成功');
						this_.indexList.push({
							name: imResponse.data.message.nick,
							content: imResponse.data.message.payload.text
						})
						this_.inputValue = ''
						this_.scrollTop += 600
					}).catch(function(imError) {
						// 发送失败
						console.warn('sendMessage error:', imError);
					});
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

淡忘_cx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值