群聊相关im交互
目录
一、创建群聊
选择人员后改变其选择状态(初始为false),点击创建群聊时根据选择对象插入用户列表,调用相应接口创建群聊。
selectFriend:function(e){
this.friendList[e].selected=!this.friendList[e].selected;
if(this.friendList[e].selected){
this.selectedNumber++;
}
else{
this.selectedNumber--;
}
},
createGroup:function(){
if(this.selectedNumber > 0){
let addMe = {
userID: this.info.userID
}
this.selectFriends.push(addMe);
for(let i=0;i<this.friendList.length;i++){
if(this.friendList[i].selected){
let addFriend = {
userID: this.friendList[i].profile.userID
}
this.selectFriends.push(addFriend);
}
}
let promise = tim.createGroup({
type: TIM.TYPES.GRP_MEETING,
name: this.groupname,
memberList: this.selectFriends
});
promise.then((imResponse)=> {
uni.showModal({
title: '提示',
content: '创建群组成功',
showCancel:false,
success: (res)=> {
if (res.confirm) {
uni.switchTab({
url:"../index/index"
})
}
}
});
}).catch((imError)=> {
console.warn('createGroup error:', imError); // 创建群组失败的相关信息
});
}
}
二、搜索并申请群聊
申请加入群聊,点击加入便调用相应接口发送请求:
joinGroup:function(){
let promise = tim.joinGroup({ groupID: this.groupname, type: TIM.TYPES.GRP_MEETING});
promise.then((imResponse)=> {
switch (imResponse.data.status) {
case TIM.TYPES.JOIN_STATUS_WAIT_APPROVAL: // 等待管理员同意
uni.showModal({
title: '提示',
content: '等待管理员同意',
showCancel:false,
success: (res)=> {
if (res.confirm) {
uni.switchTab({
url:"../index/index"
})
}
}
});
break;
case TIM.TYPES.JOIN_STATUS_SUCCESS: // 加群成功
uni.showModal({
title: '提示',
content: '加入成功',
showCancel:false,
success: (res)=> {
if (res.confirm) {
uni.switchTab({
url:"../index/index"
})
}
}
});
break;
case TIM.TYPES.JOIN_STATUS_ALREADY_IN_GROUP: // 已经在群中
uni.showModal({
title: '提示',
content: '您已在群中',
showCancel:false,
success: (res)=> {
if (res.confirm) {
uni.switchTab({
url:"../index/index"
})
}
}
});
break;
default:
break;
}
}).catch(function(imError){
uni.showModal({
title: '提示',
content: '加入失败',
showCancel:false,
success: (res)=> {
if (res.confirm) {
uni.switchTab({
url:"../index/index"
})
}
}
});
});
},
▲由于后端该部分回调尚未来得及完成,改为点击进行复制群ID操作。
三、群资料
进入页面时调用相应接口获取群资料:
getGroupProfile:function(){
let promise_group = tim.getGroupProfile({ groupID: this.groupId });
promise_group.then((imResponse)=> {
this.groupProfile = imResponse.data.group;
if(this.groupProfile.selfInfo.messageRemindType == 'AcceptAndNotify'){
this.switched = false;
}
else{
this.switched = true;
}
console.log(this.groupProfile);
}).catch(function(imError) {
console.warn('getGroupProfile error:', imError); // 获取群详细资料失败的相关信息
});
},
getGroupMemberList:function(){
let promise_memberList = tim.getGroupMemberList({ groupID: this.groupId, count: 20, offset:0 });
promise_memberList.then((imResponse)=> {
console.log(imResponse.data.memberList); // 群成员列表
this.memberList = imResponse.data.memberList;
}).catch(function(imError) {
console.warn('getGroupMemberList error:', imError);
});
},
若为群主,则可以管理群成员(非群主则为查看群成员),修改群头像、群名称、群公告,同时所有用户可以修改自己在群内的昵称。
<uni-card :is-shadow="true">
<view class="group-msg">
<view class="row">
<view class="title">群头像</view>
<view class="cont">
<view class="user-head">
<image :src="groupProfile.avatar" @tap="upload" class="group-img" v-if="groupProfile.ownerID == groupProfile.selfInfo.userID"></image>
<image :src="groupProfile.avatar" class="group-img" v-if="groupProfile.ownerID != groupProfile.selfInfo.userID"></image>
</view>
<!-- <image :src="faceImg" class="group-img" mode="aspectFill"></image> -->
</view>
<view class="more">
<image src="../../static/images/common/more.png" mode="aspectFit"></image>
</view>
</view>
<view class="row">
<view class="title">群Id</view>
<view class="cont" @click="copy(groupProfile.groupID)">{{groupProfile.groupID}}</view>
<view class="more">
<image src="../../static/images/common/more.png" mode="aspectFit"></image>
</view>
</view>
<view class="row" @tap="open('t-name','群名称',groupProfile.name,true)" v-if="groupProfile.ownerID == groupProfile.selfInfo.userID">
<view class="title">群名称</view>
<view class="cont">{{groupProfile.name}}</view>
<view class="more">
<image src="../../static/images/common/more.png" mode="aspectFit"></image>
</view>
</view>
<view class="row" v-if="groupProfile.ownerID != groupProfile.selfInfo.userID">
<view class="title">群名称</view>
<view class="cont">{{groupProfile.name}}</view>
<view class="more">
<image src="../../static/images/common/more.png" mode="aspectFit"></image>
</view>
</view>
<view class="row" @tap="open('t-introduction','群介绍',groupProfile.introduction,true)" v-if="groupProfile.ownerID == groupProfile.selfInfo.userID">
<view class="title">群简介</view>
<view class="cont">{{groupProfile.introduction}}</view>
<view class="more">
<image src="../../static/images/common/more.png" mode="aspectFit"></image>
</view>
</view>
<view class="row" v-if="groupProfile.ownerID != groupProfile.selfInfo.userID">
<view class="title">群简介</view>
<view class="cont">{{groupProfile.introduction}}</view>
<view class="more">
<image src="../../static/images/common/more.png" mode="aspectFit"></image>
</view>
</view>
<view class="row" @tap="open('t-notice','群公告',groupProfile.notification,true)" v-if="groupProfile.ownerID == groupProfile.selfInfo.userID">
<view class="title">群公告</view>
<view class="cont">{{groupProfile.notification}}</view>
<view class="more">
<image src="../../static/images/common/more.png" mode="aspectFit"></image>
</view>
</view>
<view class="row" v-if="groupProfile.ownerID != groupProfile.selfInfo.userID">
<view class="title">群公告</view>
<view class="cont">{{groupProfile.notification}}</view>
<view class="more">
<image src="../../static/images/common/more.png" mode="aspectFit"></image>
</view>
</view>
<view class="row" @tap="open('t-nicknameingroup','群昵称',groupProfile.selfInfo.nameCard,true)">
<view class="title">群昵称</view>
<view class="cont">{{groupProfile.selfInfo.nameCard}}</view>
<view class="more">
<image src="../../static/images/common/more.png" mode="aspectFit"></image>
</view>
</view>
</view>
</uni-card>
<uni-card :is-shadow="true">
<view class="member">
<view class="member-top">
<view class="title">群成员</view>
<view class="more" @tap="manageChange()" v-if="info.userID == groupProfile.ownerID">管理群成员</view>
<view class="more" @tap="manageChange()" v-if="info.userID != groupProfile.ownerID">查看群成员</view>
<image src="../../static/images/common/more.png" mode="aspectFit" class="more-img" v-if="info.userID == groupProfile.ownerID"></image>
</view>
<view class="member-ls" v-if="info.userID == groupProfile.ownerID">
<view class="member-one" v-if="membermanage == false">
<u-avatar-group
:urls="avatarList"
size="60"
gap="0.4"
></u-avatar-group>
</view>
<view class="member-one" style="margin-left: 300rpx;" v-if="membermanage == false">
<view class="imgs">
<image src="../../static/images/index/add.png" mode="aspectFill" class="user-add" @tap="open('t-invite','邀请','',true)"></image>
</view>
</view>
<view v-if="membermanage == true">
<view v-for="(item,index) in memberList" :key="index">
<view style="display: flex;margin-top: 20rpx;">
<view style="width: 120rpx;display: flex;">
<view><u-avatar :src="item.avatar" shape="circle" size="100rpx"></u-avatar></view>
</view>
<view style="width: 500rpx;display: flex;padding-top: 25rpx; padding-left:20rpx;">
<view class="name" v-if="item.nameCard != ''">{{item.nameCard}}</view>
<view class="name" v-if="item.nameCard == '' && item.nick != ''">{{item.nick}}</view>
<view class="name" v-if="item.nameCard == '' && item.nick == ''">{{item.userID}}</view>
</view>
<view><u-button type="error" text="移除" @click="deleteGroupMember(item.userID,item.nick)"></u-button></view>
</view>
</view>
</view>
</view>
<view class="member-ls" v-if="info.userID != groupProfile.ownerID">
<view class="member-one" v-if="membermanage == false">
<u-avatar-group
:urls="avatarList"
size="60"
gap="0.4"
></u-avatar-group>
</view>
<view class="member-one" style="margin-left: 300rpx;" v-if="membermanage == false">
<view class="imgs">
<image src="../../static/images/index/add.png" mode="aspectFill" class="user-add" @tap="open('t-invite','邀请','',true)"></image>
</view>
</view>
<view v-if="membermanage == true">
<view v-for="(item,index) in memberList" :key="index">
<view style="display: flex;margin-top: 20rpx;">
<view style="width: 120rpx;display: flex;">
<view><u-avatar :src="item.avatar" shape="circle" size="100rpx"></u-avatar></view>
</view>
<view style="width: 500rpx;display: flex;padding-top: 25rpx; padding-left:20rpx;">
<view class="name" v-if="item.nameCard != ''">{{item.nameCard}}</view>
<view class="name" v-if="item.nameCard == '' && item.nick != ''">{{item.nick}}</view>
<view class="name" v-if="item.nameCard == '' && item.nick == ''">{{item.userID}}</view>
</view>
<!-- <view><u-button type="error" text="移除" @click="deleteGroupMember(item.userID,item.nick)"></u-button></view> -->
</view>
</view>
</view>
</view>
<view class="clear"></view>
</view>
</uni-card>
邀请,点击后输入用户id进行邀请:
invite:function(){
let promise = tim.addGroupMember({groupID: this.groupId, userIDList: ['user1','user2','user3']});
promise.then((imResponse)=> {
}).catch(function(imError) {
console.warn('addGroupMember error:', imError); // 错误信息
});
},
管理群成员,点击移除按钮移除相应群成员:
deleteGroupMember:function(userID,nick){
console.log(userID)
this.modify();
uni.showModal({
title: '提示',
content: '是否删除该用户' + "昵称:" + nick + " userID:" + userID,
success: (res)=> {
if (res.confirm) {
let promise = tim.deleteGroupMember({groupID: this.groupId, userIDList:[userID], reason: ''});
promise.then((imResponse)=> {
this.getGroupProfile();
this.getGroupMemberList();
this.modify();
}).catch(function(imError) {
console.warn('deleteGroupMember error:', imError); // 错误信息
});
} else if (res.cancel) {
console.log('用户点击取消');
}
}
});
退出群/解散群,点击退出/解散按钮可退出/解散群聊:
exit:function(){
uni.showModal({
title: '提示',
content: '确定退出群聊吗',
success: (res)=> {
if (res.confirm) {
console.log('用户点击确定');
let promise = tim.quitGroup(this.groupId);
promise.then((imResponse)=> {
console.log(imResponse.data.groupID); // 退出成功的群 ID
uni.switchTab({
url:"../index/index"
})
}).catch(function(imError){
console.warn('quitGroup error:', imError); // 退出群组失败的相关信息
});
} else if (res.cancel) {
console.log('用户点击取消');
}
}
});
breakup:function(){
uni.showModal({
title: '提示',
content: '确定解散群聊吗',
success: (res)=> {
if (res.confirm) {
console.log('用户点击确定');
let promise = tim.dismissGroup(this.groupId);
promise.then((imResponse)=> { // 解散成功
console.log(imResponse.data.groupID); // 被解散的群组 ID
uni.switchTab({
url:"../index/index"
})
}).catch(function(imError) {
console.warn('dismissGroup error:', imError); // 解散群组失败的相关信息
});
} else if (res.cancel) {
console.log('用户点击取消');
}
}
});
},