在群组管理中,最常见的其中一种管理场景就是禁言管理了,今天我就带大家看看IM的禁言管理如何实现。
禁言功能就是对群里的某一个群员或者全部群员禁言,并且可以设置禁言时长,禁言期间,被禁言成员退群后重新加入该群禁言依旧有效,直至禁言时间结束或被取消禁言。
1.调用getGroupProfile接口查看所在群组类型,确认是否支持禁言/取消禁言操作。
try {
//获取群组资料
let resp = await this.tim.getGroupProfile({ groupID: this.groupID });
//好友群不支持禁言
const notAllowedMuteGroups = [TIM.TYPES.GRP_WORK];
let groupProfile = resp.data.group;
//判断是否是不支持禁言的群组类型
this.canSetMuteTime = !notAllowedMuteGroups.includes(groupProfile.type);
console.log('查询群组资料成功', this.canSetMuteTime)
} catch (error) {
console.log('获取群组资料失败', error);
}
2.调用getGroupMemberProfile接口查看指定的 userID 在当前群的成员角色,确认是否有权限进行禁言/取消禁言操作。
try {
//查询当前用户在群组中的资料
let resp = await this.tim.getGroupMemberProfile({
groupID: this.groupID,
userIDList: [ this.userId ]
});
let memberProfile = resp.data.memberList[0];
//群主和管理员允许禁言管理
let managerRoles = [ TIM.TYPES.GRP_MBR_ROLE_OWNER, TIM.TYPES.GRP_MBR_ROLE_ADMIN ];
this.isManageRole = managerRoles.includes(memberProfile.role);
console.log('查询群组成员资料成功', memberProfile);
console.log('是否有管理员权限', this.isManageRole);
} catch (err) {
console.error('查询群组资料失败', err);
}
App 管理员、群主或群管理员调用接口setGroupMemberMuteTime可禁言/取消禁言指定群组内的指定成员,单次调用仅支持禁言/取消禁言单个群成员。
try {
this.tim.setGroupMemberMuteTime({
userID: this.userID, //被禁言人id
muteTime: 300, //禁言市场单位秒
groupID: this.groupID //群组id
})
this.$message({
type: 'success',
message: '设置禁言成功'
})
this.dialogVisible = false;
} catch (error) {
this.$message.error('设置禁言失败');
console.error('设置禁言失败', error);
}
使用该功能需将 SDK 升级至2.6.2及以上版本。全体禁言或取消全体禁言,暂无相关的群提示消息下发。
App 管理员或群主调用updateGroupProfile接口可禁言/取消禁言指定群的全体管理员和普通成员。
let promise = this.tim.updateGroupProfile({
groupID: this.groupID, //群组id
muteAllMembers:true, // true 全体禁言,false 取消全体禁言
});
promise.then(function(imResponse) {
console.log(imResponse.data.group) // 修改成功后的群组详细资料
}).catch(function(imError) {
console.warn('updateGroupProfile error:', imError); // 修改群组资料失败的相关信息
});
目前web端没有开放批量禁言的方法,但是 REST API可以进行批量禁言,具体操作可以查看官方文档批量禁言和取消禁言。
禁言后,该群成员收到的被禁言群提示消息,可通过遍历 event.data 获取相关数据并渲染到页面。
监听操作是在实例化SDK的时候就绑定好的,这里只是为了方便理清逻辑,所以放到了这一步展示
this.tim.on(TIM.EVENT.MESSAGE_RECEIVED, evt => {
const msgList = evt.data;
console.log('有消息加进来了', msgList);
msgList.forEach(msg => {
switch (msg.type) {
case TIM.TYPE.MSG_GRP_TIP:
//处理群消息
this.handleGroupTip(msg);
break;
}
});
})
handleGroupTip(msg) {
const payload = msg.payload;
const type = payload.operationType;
switch (type) {
case TIM.TYPES.GRP_TIP_MBR_PROFILE_UPDATED: // 群成员资料变更,例如:群成员被禁言
console.log('有群成员资料变了')
break;
case TIM.TYPES.GRP_TIP_MBR_JOIN: // 有成员加群
console.log('有人加群了')
break;
case TIM.TYPES.GRP_TIP_MBR_QUIT: // 有群成员退群
console.log('有人退群了')
break;
case TIM.TYPES.GRP_TIP_MBR_KICKED_OUT: // 有群成员被踢出群
console.log('有群成员被踢出群')
break;
case TIM.TYPES.GRP_TIP_GRP_PROFILE_UPDATED: // 群组资料变更
console.log('群组资料变更')
break;
default:
break;
}
console.log(payload)
}
2.6.2及以上版本 SDK,调用getGroupMemberList接口可以拉取群成员禁言截止时间戳(muteUntil),您根据该值即可判断群成员是否被禁言,以及禁言的剩余时间。取消禁言后,该群成员的 GroupMember.muteUntil * 1000 <= Date.now()。
目前官网没有给出查询当个群组成员是否被禁言的方法,所以需要遍历所有群组成员的资料判断当前成员是否被禁言了
// 从v2.6.2 起,getGroupMemberList 接口支持拉取群成员禁言截止时间戳。
let promise = this.tim.getGroupMemberList({ groupID: this.groupID, count: 30, offset:0 }); // 从0开始拉取30个群成员
promise.then(function(imResponse) {
console.log(imResponse.data.memberList); // 群成员列表
for (let groupMember of imResponse.data.memberList) {
if (groupMember.muteUntil * 1000 > Date.now()) {
console.log(`${groupMember.userID} 禁言中`);
} else {
console.log(`${groupMember.userID} 未被禁言`);
}
}
}).catch(function(imError) {
console.warn('getGroupMemberProfile error:', imError);
});
禁言功能比较简单,做好监听的逻辑就可以了,我主要总结一下在写代码时可能会遇到的坑:
1.在实例化sdk后,一定要在SDK_READY监听事件里面做之后的操作逻辑,比如加入/创建房间、更新信息等。最好是所有的操作都这样,等sdk ready之后再进行。
this.tim = TIM.create({ SDKAppID: AppConfig.SDKAPPID });
this.tim.setLogLevel(2);
this.tim.on(TIM.EVENT.SDK_READY, async evt => {
console.log('TIM sdk 登录成功', evt);
let updateProfileResp = await this.tim.updateMyProfile({
nick: '新用户' + this.userId,
avatar: 'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg'
})
console.log('更新信息成功', updateProfileResp)
try {
if (this.isManager) {
let resp = await this.tim.createGroup({
type: TIM.TYPES.GRP_AVCHATROOM,
name: '直播群' + this.roomId,
groupID: this.groupID
})
console.log(this.userId + '创建群组成功', resp)
}
await this.tim.joinGroup({
groupID: this.groupID
})
console.log('加入群组成功')
} catch (err) {
console.log('加入/创建群组失败', err)
}
cb();
})
2.如果设置了同一个客户端可以登录多个账号的话,一定要注意在不使用当前账号时,调用logout方法,不然很容易出现bug,同一个客户端登录多个账号可以在控制台进行设置。