群组

群组功能概述

SDK 提供了普通群以及高级群两种形式的群功能。 高级群拥有更多的权限操作,两种群聊形式在共有操作上保持了接口一致。推荐 APP 开发时只选择一种群类型进行开发。普通群和高级群在原则上是不能互相转换的,他们的群类型在创建时就已经确定。在 SDK 2.4.0 版本后,高级群可以拥有普通群的全部功能,推荐使用高级群进行开发。

初始化参数

示例代码

var nim = NIM.getInstance({
  onteams: onTeams,
  onsynccreateteam: onCreateTeam,
  onteammembers: onTeamMembers,
  //onsyncteammembersdone: onSyncTeamMembersDone,
  onupdateteammember: onUpdateTeamMember,
  shouldCountNotifyUnread: function (msg) {
    // 根据msg的属性自己添加过滤器
    if (msg.something === someting) {
    return true
    }
  }
});
function onTeams(teams) {
  console.log('收到群列表', teams);
  data.teams = nim.mergeTeams(data.teams, teams);
  onInvalidTeams(teams.invalid);
}
function onInvalidTeams(teams) {
  data.teams = nim.cutTeams(data.teams, teams);
  data.invalidTeams = nim.mergeTeams(data.invalidTeams, teams);
  refreshTeamsUI();
}
function onCreateTeam(team) {
  console.log('你创建了一个群', team);
  data.teams = nim.mergeTeams(data.teams, team);
  refreshTeamsUI();
  onTeamMembers({
    teamId: team.teamId,
    members: owner
  });
}
function refreshTeamsUI() {
  // 刷新界面
}
function onTeamMembers(obj) {
  console.log('群id', teamId, '群成员', members);
  var teamId = obj.teamId;
  var members = obj.members;
  data.teamMembers = data.teamMembers || {};
  data.teamMembers[teamId] = nim.mergeTeamMembers(data.teamMembers[teamId], members);
  data.teamMembers[teamId] = nim.cutTeamMembers(data.teamMembers[teamId], members.invalid);
  refreshTeamMembersUI();
}
// function onSyncTeamMembersDone() {
//     console.log('同步群成员列表完成');
// }
function onUpdateTeamMember(teamMember) {
  console.log('群成员信息更新了', teamMember);
  onTeamMembers({
    teamId: teamMember.teamId,
    members: teamMember
  });
}
function refreshTeamMembersUI() {
  // 刷新界面
}

参数解释

群对象

群对象有如下字段

群类型

群对象有一个字段type来标明群类型, 具体类型如下

群加入方式

群加入方式有以下几种

群被邀请模式

群被邀请模式有以下几种

群邀请模式

群邀请模式有以下几种

群信息修改权限

群信息修改权限有以下几种

群信息自定义字段修改权限

群信息自定义字段修改权限有以下几种

群成员对象

群成员对象有如下字段

群成员类型

创建群

// 创建普通群
nim.createTeam({
  type: 'normal',
  name: '普通群',
  avatar: 'avatar',
  accounts: ['a1', 'a2'],
  level: 50,
  ps: '我建了一个普通群',
  done: createTeamDone
});
// 创建高级群
nim.createTeam({
  type: 'advanced',
  name: '高级群',
  avatar: 'avatar',
  accounts: ['a1', 'a2'],
  intro: '群简介',
  announcement: '群公告',
  // joinMode: 'needVerify',
  // beInviteMode: 'needVerify',
  // inviteMode: 'manager',
  // updateTeamMode: 'manager',
  // updateCustomMode: 'manager',
  level: 50,
  ps: '我建了一个高级群',
  custom: '群扩展字段, 建议封装成JSON格式字符串',
  done: createTeamDone
});
function createTeamDone(error, obj) {
  console.log(error);
  console.log(obj);
  console.log('创建' + obj.team.type + '群' + (!error?'成功':'失败'));
  if (!error) {
    onCreateTeam(obj.team, obj.owner);
  }
}

发送群消息

发送群消息时只需将上文中各个发送消息接口的scene替换为'team', 将to替换为群ID

接收群消息

参考上文的接收消息

更新群

nim.updateTeam({
  teamId: 123,
  name: '群名字',
  avatar: 'avatar',
  intro: '群简介',
  announcement: '群公告',
  joinMode: 'needVerify',
  custom: '自定义字段',
  done: updateTeamDone
});
function updateTeamDone(error, team) {
  console.log(error);
  console.log(team);
  console.log('更新群' + (!error?'成功':'失败'));
}

拉人入群

nim.addTeamMembers({
  teamId: 123,
  accounts: ['a3', 'a4'],
  ps: '加入我们的群吧',
  done: addTeamMembersDone
});
function addTeamMembersDone(error, obj) {
  console.log(error);
  console.log(obj);
  console.log('入群邀请发送' + (!error?'成功':'失败'));
}

踢人出群

nim.removeTeamMembers({
  teamId: 123,
  accounts: ['a3', 'a4'],
  done: removeTeamMembersDone
});
function removeTeamMembersDone(error, obj) {
  console.log(error);
  console.log(obj);
  console.log('踢人出群' + (!error?'成功':'失败'));
}

接受入群邀请

// 假设 sysMsg 是通过回调 `onsysmsg` 收到的系统通知
nim.acceptTeamInvite({
  idServer: sysMsg.idServer,
  teamId: 123,
  from: 'zyy1',
  done: acceptTeamInviteDone
});
function acceptTeamInviteDone(error, obj) {
  console.log(error);
  console.log(obj);
  console.log('接受入群邀请' + (!error?'成功':'失败'));
}

拒绝入群邀请

// 假设 sysMsg 是通过回调 `onsysmsg` 收到的系统通知
nim.rejectTeamInvite({
  idServer: sysMsg.idServer,
  teamId: 123,
  from: 'zyy1',
  ps: '就不',
  done: rejectTeamInviteDone
});
function rejectTeamInviteDone(error, obj) {
  console.log(error);
  console.log(obj);
  console.log('拒绝入群邀请' + (!error?'成功':'失败'));
}

申请入群

nim.applyTeam({
  teamId: 123,
  ps: '请加',
  done: applyTeamDone
});
function applyTeamDone(error, obj) {
  console.log(error);
  console.log(obj);
  console.log('申请入群' + (!error?'成功':'失败'));
}

通过入群申请

// 假设 sysMsg 是通过回调 `onsysmsg` 收到的系统通知
nim.passTeamApply({
  idServer: sysMsg.idServer,
  teamId: 123,
  from: 'a2',
  done: passTeamApplyDone
});
function passTeamApplyDone(error, obj) {
  console.log(error);
  console.log(obj);
  console.log('通过入群申请' + (!error?'成功':'失败'));
}

拒绝入群申请

// 假设 sysMsg 是通过回调 `onsysmsg` 收到的系统通知
nim.rejectTeamApply({
  idServer: sysMsg.idServer,
  teamId: 123,
  from: 'a2',
  ps: '就不',
  done: rejectTeamApplyDone
});
function rejectTeamApplyDone(error, obj) {
  console.log(error);
  console.log(obj);
  console.log('拒绝入群申请' + (!error?'成功':'失败'));
}

添加群管理员

nim.addTeamManagers({
  teamId: 123,
  accounts: ['a2', 'a3'],
  done: addTeamManagersDone
});
function addTeamManagersDone(error, obj) {
  console.log(error);
  console.log(obj);
  console.log('添加群管理员' + (!error?'成功':'失败'));
}

移除群管理员

nim.removeTeamManagers({
  teamId: 123,
  accounts: ['a2', 'a3'],
  done: removeTeamManagersDone
});
function removeTeamManagersDone(error, obj) {
  console.log(error);
  console.log(obj);
  console.log('移除群管理员' + (!error?'成功':'失败'));
}

主动退群

nim.leaveTeam({
  teamId: 123,
  done: leaveTeamDone
});
function leaveTeamDone(error, obj) {
  console.log(error);
  console.log(obj);
  console.log('主动退群' + (!error?'成功':'失败'));
}

转让群

nim.transferTeam({
  teamId: 123,
  account: 'zyy2',
  leave: false,
  done: transferOwnerDone
});
function transferOwnerDone(error, obj) {
  console.log(error);
  console.log(obj);
  console.log('转让群' + (!error?'成功':'失败'));
}

解散群

nim.dismissTeam({
  teamId: 123,
  done: dismissTeamDone
});
function dismissTeamDone(error, obj) {
  console.log(error);
  console.log(obj);
  console.log('解散群' + (!error?'成功':'失败'));
}

修改自己的群属性

目前支持修改的属性有这些

nim.updateInfoInTeam({
  teamId: 123,
  // 此参数为可选参数
  // nickInTeam: '群昵称',
  // 静音群, 此参数为可选参数
  // muteTeam: true,
  // muteNotiType: 1,
  // 第三方扩展字段
  // custom: '{}'
  done: updateInfoInTeamDone
});
function updateInfoInTeamDone(error, obj) {
  console.log(error);
  console.log(obj);
  console.log('修改自己的群属性' + (!error?'成功':'失败'));
}

修改别人的群昵称

nim.updateNickInTeam({
  teamId: 123,
  account: 'a2',
  nickInTeam: '群昵称',
  done: updateNickInTeamDone
});
function updateNickInTeamDone(error, obj) {
  console.log(error);
  console.log(obj);
  console.log('修改自己的群属性' + (!error?'成功':'失败'));
}

更新群成员禁言状态

nim.updateMuteStateInTeam({
  teamId: '123',
  account: 'a',
  mute: true,
  done: updateMuteStateInTeamDone
})
function updateMuteStateInTeamDone(error, obj) {
  console.log('更新群成员禁言状态' + (!error?'成功':'失败'), error, obj);
}

获取群禁言成员列表

nim.getMutedTeamMembers({
  teamId: 'teamId',
  done: getMutedTeamMembersDone
})
function getMutedTeamMembersDone (error, obj) {
  console.log('获取群禁言成员列表' + (!error?'成功':'失败'))
  console.log(obj)
}

群整体禁言

  nim.muteTeamAll({
    teamId: 'teamId',
    type: 'none',
    done: muteTeamAllDone
  })
  function muteTeamAllDone (error, obj) {
    console.log('群禁言:' + (!error?'成功':'失败'))
    console.log(obj)
  }

获取群

nim.getTeam({
  teamId: 123,
  done: getTeamDone
});
function getTeamDone(error, obj) {
  console.log(error);
  console.log(obj);
  console.log('获取群' + (!error?'成功':'失败'));
}

获取群列表

nim.getTeams({
  done: getTeamsDone
});
function getTeamsDone(error, teams) {
  console.log(error);
  console.log(teams);
  console.log('获取群列表' + (!error?'成功':'失败'));
  if (!error) {
    onTeams(teams);
  }
}

获取群成员

nim.getTeamMembers({
  teamId: 123,
  done: getTeamMembersDone
});
function getTeamMembersDone(error, obj) {
  console.log(error);
  console.log(obj);
  console.log('获取群成员' + (!error?'成功':'失败'));
  if (!error) {
    onTeamMembers(obj);
  }
}

通过群ID及成员账号获取群成员信息

nim.getTeamMemberByTeamIdAndAccount({
  teamId: 123
  account: 'abc',
  done: getTeamMemberDone
});
function getTeamMemberDone(error, obj) {
  console.log(error);
  console.log(obj);
  console.log('获取群成员' + (!error?'成功':'失败'));
  if (!error) {
    onTeamMember(obj);
  }
}

是否需要群消息通知

群消息已读通知

  NIM.getInstance({
    // ...
    onTeamMsgReceipt: onTeamMsgReceipt
  })
  function onTeamMsgReceipt (obj) {
    console.log(obj)
  }

标记群组消息已读

  nim.sendTeamMsgReceipt({
    teamMsgReceipts: [{
      teamId: '1027484',
      idServer: '68953284018302'
    }],
    done: sendTeamMsgReceiptDone
  })
  function sendTeamMsgReceiptDone (error, obj, content) {
    console.log('标记群组消息已读' + (!error?'成功':'失败'));
  }

查询群组消息已读、未读数量

  nim.getTeamMsgReads({
    teamMsgReceipts: [{
      teamId: '1027484',
      idServer: '68953284018302'
    }],
    done: getTeamMsgReadsDone
  })
  function getTeamMsgReadsDone (error, obj, content) {
    console.log('获取群组消息已读' + (!error?'成功':'失败'));
    /* content.teamMsgReceipts 为
      [ {
        idClient: "c7575fca32bf142787986e752fdeff6a"
        idServer: "68527276949899"
        read: "0"
        teamId: "1021136"
        unread: "187"
      } ]
    */

查询群组消息未读账号列表

  nim.getTeamMsgReadAccounts({
    teamMsgReceipt: {
      teamId: '1027484',
      idServer: '68953284018302'
    },
    done: getTeamMsgReadAccountsDone
  })
  function getTeamMsgReadAccountsDone (error, obj, content) {
    console.log('获取群组消息已读' + (!error?'成功':'失败'));
    /* content.teamMsgReceipt 为
      idClient: "c7575fca32bf142787986e752fdeff6a",
      readAccounts: Array[],
      unreadAccounts: Array[]
    */