Electron 超级群频道与用户组用法

本页给出 Electron 端从“超级群 → 频道 → 用户组 → 按 SUPER_GROUP(10) 收发消息”的完整用法,示例使用 SDK 公开方法(@ni-paas-im/electron),并标注版本与限制。语义与兼容规则见 超级群会话类型。

项内容
包 / 模块@ni-paas-im/electron
超级群类型常量ConversationType.SUPER_GROUP(数值 10;旧版本 SDK 可用数值 10 或继续 2+super)
调用风格主进程客户端 async 方法,返回 RcResult;渲染进程经 preload 白名单桥调用
频道/用户组操作8 个语义操作(创建/列表/加入/离开频道、创建/列表用户组、绑定、按用户反查)
消息频道消息按超级群会话发送;rev23 起可带 options.channelId 按频道投递(不带则群级);10 与存量 2+super 共用同一会话键
版本前提

示例中的 SUPER_GROUP 常量与“按 10 发送”需要 SDK/服务端包含 rev22。

若当前环境尚未升级:① 用数值 10 传参(仍有实现拒绝风险);② 继续用 GROUP(2) + groupType=super(存量兼容写法)。

频道/用户组接口(rev21)不依赖 10,可直接使用。

前置条件​

  • 应用已开通 supergroup 服务(未开通返回 1021);客户端已连接(CONNECTED)。
  • 超级群成员上限 100000;频道/用户组创建需要群主或管理员权限(否则 1003)。
  • 频道列表/用户组列表需要群成员身份;频道 ID 在应用内唯一,跨群同名时用 groupId 消歧。

步骤 1:创建/确认超级群​

JavaScript
// 1) 创建/确认超级群(IDL createGroup,groupType=super)
const group = await client.createGroup({ groupId: 'g_1001', name: '产品社区', groupType: 'super' });

步骤 2:频道管理​

JavaScript
// 2) 频道:创建 → 列表 → 加入 / 离开(主进程客户端)
const created = await client.createSupergroupChannel({ groupId: 'g_1001', name: '产品讨论', type: 'public' });
const channelId = created.data.channelId;
const list = await client.listSupergroupChannels('g_1001');
await client.joinSupergroupChannel(channelId, { groupId: 'g_1001' });
await client.leaveSupergroupChannel(channelId, { groupId: 'g_1001' });

步骤 3:用户组与绑定​

JavaScript
// 3) 用户组:创建 → 绑定频道
const ug = await client.createUserGroup({ groupId: 'g_1001', name: '核心成员', members: ['alice'] });
await client.bindUserGroupToChannel(channelId, ug.data.userGroupId, { groupId: 'g_1001' });
const groups = await client.listUserGroups('g_1001');

步骤 4:按 10 收发与会话​

JavaScript
// 4) 按 10 发送 / 历史 / 会话列表(渲染进程经 preload 白名单桥调用)
const bridge = window.niPaasIMBridge;   // contextBridge 暴露的只读门面
const SUPER_GROUP = 10;                 // ConversationType.SUPER_GROUP(rev22)
const sent = await bridge.imlib.sendTextMessage(SUPER_GROUP, 'g_1001', '大家好');
const history = await bridge.imlib.getHistoryMessages(SUPER_GROUP, 'g_1001', { limit: 20 });
const conversations = await bridge.imlib.getConversationList();

// 事件:bridge.onEvent 回调里按 type 10 或 2+super 判断
const off = bridge.onEvent((frame) => {
  if (frame.event === 'MESSAGES') { /* frame.payload.messages */ }
});

接口与错误码​

操作入参返回错误码
createSupergroupChannelparams:{groupId:string, name:string, type:ChannelType, joinPolicy?:JoinPolicy}SupergroupChannel1001 1002 1003 1005 1015
listSupergroupChannelsgroupId:string{groupId:string, channels:SupergroupChannel[], total:uint32}1001 1002 1003 1005
joinSupergroupChannelchannelId:string, options:{groupId?:string}?{groupId:string, channelId:string, userId:string, joined:bool, alreadyJoined:bool}1001 1002 1003 1005
leaveSupergroupChannelchannelId:string, options:{groupId?:string}?{groupId:string, channelId:string, userId:string, left:bool, wasMember:bool}1001 1002 1003 1005
bindUserGroupToChannelchannelId:string, userGroupId:string, options:{groupId?:string}?{groupId:string, channelId:string, userGroupId:string, bound:bool, reverseIndexed:bool}1001 1002 1003 1005
createUserGroupparams:{groupId:string, name:string, members?:string[]}UserGroup1001 1002 1003 1004 1015
listUserGroupsgroupId:string{groupId:string, userGroups:UserGroup[], total:uint32}1001 1002 1003 1005
getUserGroupsByUseruserId:string, options:{groupId?:string}?{userId:string, userGroups:UserGroup[], total:uint32}1001 1002 1003
错误码名称含义可重试
1001INVALID_PARAMETER参数错误否
1002INVALID_TOKENToken 无效或已过期否
1003FORBIDDEN无权限否
1004USER_NOT_FOUND用户不存在否
1005GROUP_NOT_FOUND群组不存在否
1006NOT_GROUP_MEMBER未加入群组否
1015MEMBERSHIP_LIMIT成员数量超出上限否

限制​

  • 频道消息已支持(rev23):发送传 options.channelId 即按频道投递;不带 channelId 为群级消息。频道消息不单独成会话,未读/回执按群会话聚合。
  • 不自动创建默认频道(RCDefault),频道需要显式创建;默认免打扰未实现。
  • 单群频道与用户组各 100 个上限(超限 1015);频道名/用户组名长度在接口内校验(1001)。
  • IMKit 未提供频道/用户组组件;10 的会话渲染需要 IMKit 升级到识别 rev22 的版本。
  • iOS / HarmonyOS 为契约预留(声明同构,无运行时代码)。

相关链接​