图片预览

滚轮缩放 · 拖动移动 · Esc 关闭

Skip to content

即时通信渠道配置接口与前端对接说明 ​

文档版本:2026-09-18 适用范围:当前 muotai-ai-biz 中已经实现的微信、QQ、钉钉、企业微信 Channel 接口依据:当前代码中的 Controller、DTO、Service 和 Runtime Manager

1. 接口范围 ​

当前后端一共暴露四组 Channel 配置接口:

平台接口前缀接入方式配置入口查询形态
微信/v1/channel/wechat微信 iLink 扫码绑定、长连接收发消息扫码一个 Agent 可返回 Channel 列表
QQ/v1/channel/qqQQ Bot WebSocket手工填写 AppID、AppSecret一个 Agent 返回当前用户最近一条 Channel
钉钉/v1/channel/dingtalk钉钉 Stream手工填写 Client ID、Client Secret;也支持扫码创建应用一个 Agent 返回当前用户最近一条 Channel
企业微信/v1/channel/wecom企业微信智能机器人 WebSocket 长连接手工填写 Bot ID、Bot Secret一个 Agent 返回当前用户最近一条 Channel

当前钉钉和企业微信都不是“群机器人 Webhook”模式。钉钉使用 Stream SDK,企业微信使用标准智能机器人的 WebSocket 接口,因此都可以接收用户消息并把消息转入 Agent。

2. 请求链路与基础地址 ​

不同调用位置对应的 URL 如下:

场景示例基础地址完整示例
当前前端开发环境/api/ai/v1/channel/api/ai/v1/channel/qq/agents/{agentId}
直接访问 Gatewayhttp://<gateway-host>:9999/ai/v1/channelhttp://192.168.4.32:9999/ai/v1/channel/qq/agents/{agentId}
直接访问 AI 服务http://<ai-host>:6062/v1/channelhttp://192.168.4.32:6062/v1/channel/qq/agents/{agentId}

当前前端 vite.config.ts 默认把 /api 代理到 VITE_API_SERVER_URL,开发环境默认值为 http://localhost:9999,并去掉 /api。Gateway 再把 /ai/** 路由到 AI 服务。因此前端推荐统一定义:

ts
export const CHANNEL_API_BASE = '/api/ai/v1/channel'

不要在业务组件中写死 IP、端口或完整域名。

3. 通用约定 ​

3.1 认证与请求头 ​

所有接口都要求当前登录用户,后端通过 SecurityUtils.getUser() 取得用户、租户信息,并校验 Channel 所属用户。

http
Authorization: Bearer <access_token>
Content-Type: application/json

未登录、无权访问、Agent 不存在、参数错误、密钥加密失败或平台连接失败都会返回业务失败。

3.2 统一响应结构 ​

成功响应:

json
{
  "code": 0,
  "msg": null,
  "data": {},
  "ok": true
}

失败响应:

json
{
  "code": 1,
  "msg": "保存企业微信 Channel失败: ...",
  "data": null,
  "ok": false
}

Controller 会捕获多数业务异常并返回 R.failed(...),因此部分失败的 HTTP 状态仍可能是 200。前端必须检查 code === 0 或 ok === true,不能只检查 HTTP 状态。

建议封装统一请求函数:

ts
export interface ApiResult<T> {
  code: number
  msg: string | null
  data: T
  ok: boolean
}

export async function unwrap<T>(request: Promise<ApiResult<T>>): Promise<T> {
  const result = await request
  if (result.code !== 0 || !result.ok) {
    throw new Error(result.msg || '请求失败')
  }
  return result.data
}

3.3 ID 类型 ​

agentId、channelId、userId 都是 Java Long,通常是大于 JavaScript 安全整数上限的雪花 ID。公共 Jackson 配置会把 Long 序列化成字符串,前端也必须始终使用 string:

ts
type EntityId = string

禁止执行 Number(channelId)、parseInt(channelId) 或用数值类型保存这些 ID,否则可能发生精度丢失并访问错误的 Channel。

3.4 时间类型 ​

接口中的 expireTime、lastActiveTime 是 LocalDateTime,典型格式为:

text
2026-09-18T14:30:00

它不携带时区。当前环境应按服务所在时区展示,不要自动附加 Z 当作 UTC。

3.5 密钥字段 ​

平台密钥只在配置请求中提交,查询接口不会把密钥明文返回。前端通过以下布尔字段判断后端是否已经保存密钥:

平台布尔字段
QQhasAppSecret
钉钉hasClientSecret
企业微信hasBotSecret

编辑已有配置时,密钥输入框应保持空白并显示“已配置”。用户不修改密钥时可以省略密钥字段或传空字符串,后端会继续使用已加密保存的旧密钥。首次配置时密钥必填。

3.6 通用长连接状态 ​

QQ、钉钉、企业微信使用同一组状态:

状态含义前端建议
UNBOUND当前用户在该 Agent 下尚未配置显示配置表单
STARTING配置已保存,正在建立平台连接显示“连接中”,轮询状态
ACTIVE已连接,可收发消息显示“运行中”
ERROR启动或运行失败显示 lastErrorMessage,允许修改配置或重试
CLOSED已停用显示“已停用”,提供启用按钮

running 表示“收到本次请求的 AI 服务实例本地是否持有该运行时”,属于运行诊断字段。在多实例部署下,Channel 可能由另一台 AI 实例持有,此时当前实例返回的 running 可能是 false,但数据库状态仍为 ACTIVE。前端业务状态应以 status 为主,running 只作为辅助诊断信息。

4. 接口总表 ​

所有路径均相对于 CHANNEL_API_BASE=/api/ai/v1/channel。

平台方法路径用途
通用GET/agents/{agentId}/statuses一次查询四个平台的三态绑定状态
微信POST/wechat/agents/{agentId}/open创建或取得扫码绑定
微信GET/wechat/agents/{agentId}查询当前用户的微信 Channel 列表
微信GET/wechat/{channelId}查询单个 Channel 状态
微信POST/wechat/{channelId}/reopen重新生成二维码或强制重绑
微信POST/wechat/{channelId}/close关闭 Channel
微信DELETE/wechat/{channelId}删除 Channel 和微信凭证
QQGET/qq/agents/{agentId}查询 Agent 的 QQ Channel
QQPOST/qq/agents/{agentId}/config保存配置并立即启用
QQGET/qq/{channelId}查询状态
QQPOST/qq/{channelId}/enable启用
QQPOST/qq/{channelId}/disable停用
QQDELETE/qq/{channelId}删除 Channel 配置
钉钉GET/dingtalk/agents/{agentId}查询 Agent 的钉钉 Channel
钉钉POST/dingtalk/agents/{agentId}/config手工保存配置并立即启用
钉钉POST/dingtalk/agents/{agentId}/scan发起扫码创建应用
钉钉GET/dingtalk/agents/{agentId}/scan/{flowId}轮询扫码状态
钉钉DELETE/dingtalk/agents/{agentId}/scan/{flowId}取消扫码会话
钉钉GET/dingtalk/{channelId}查询状态
钉钉POST/dingtalk/{channelId}/enable启用
钉钉POST/dingtalk/{channelId}/disable停用
钉钉DELETE/dingtalk/{channelId}删除 Channel 配置
企业微信GET/wecom/agents/{agentId}查询 Agent 的企业微信 Channel
企业微信POST/wecom/agents/{agentId}/config保存配置并立即启用
企业微信GET/wecom/{channelId}查询状态
企业微信POST/wecom/{channelId}/enable启用
企业微信POST/wecom/{channelId}/disable停用
企业微信DELETE/wecom/{channelId}删除 Channel 配置

4.1 聚合三态状态接口 ​

http
GET /api/ai/v1/channel/agents/{agentId}/statuses

该接口用于配置页快速判断每个平台是“启用、停用、未绑定”,不替代各平台的配置详情接口。三态字段为 status:

三态含义
ENABLED已启用或正在启用。平台原始状态为 ACTIVE 或 STARTING
DISABLED已有 Channel 记录,但当前不可收消息,例如 CLOSED、ERROR、CONFIGURED
UNBOUND当前用户在该 Agent 下没有有效绑定。微信 PENDING、EXPIRED 也归入该状态

数据类型:

ts
export type ChannelBindingStatus = 'ENABLED' | 'DISABLED' | 'UNBOUND'

export type ChannelPlatform =
  | 'WECHAT_ILINK'
  | 'QQ_BOT'
  | 'DINGTALK'
  | 'WECOM'

export interface ChannelBindingStatusItem {
  platform: ChannelPlatform
  status: ChannelBindingStatus
  rawStatus: string | null
  channelId: EntityId | null
  channelCode: string | null
  running: boolean | null
  lastActiveTime: string | null
  lastMessage: string | null
}

export interface ChannelBindingStatusResponse {
  agentId: EntityId
  userId: EntityId
  channels: ChannelBindingStatusItem[]
}

示例响应:

json
{
  "code": 0,
  "msg": null,
  "ok": true,
  "data": {
    "agentId": "2067916016398761985",
    "userId": "1",
    "channels": [
      {
        "platform": "WECHAT_ILINK",
        "status": "UNBOUND",
        "rawStatus": "PENDING",
        "channelId": "2099413200976412673",
        "channelCode": "wc_2067916016398761985_1_0f9ce6d5a05b",
        "running": null,
        "lastActiveTime": null,
        "lastMessage": null
      },
      {
        "platform": "QQ_BOT",
        "status": "ENABLED",
        "rawStatus": "ACTIVE",
        "channelId": "2099413200976412674",
        "channelCode": "qq_2067916016398761985_1_a1b2c3d4e5f6",
        "running": true,
        "lastActiveTime": "2026-09-18T14:30:00",
        "lastMessage": null
      }
    ]
  }
}

rawStatus 保留平台原始状态,用于调试或跳转到具体平台配置卡片后显示更细的连接状态。running 仍然只是当前 AI 服务实例的运行诊断字段,业务展示以三态 status 为主。

5. 微信渠道 ​

5.1 数据类型 ​

ts
export type WechatChannelStatus = 'PENDING' | 'ACTIVE' | 'EXPIRED' | 'CLOSED'

export interface WechatChannelOpenRequest {
  channelName?: string
}

export interface WechatChannelReopenRequest {
  force?: boolean
}

export interface WechatChannelOpenResponse {
  channelId: EntityId
  channelCode: string
  status: WechatChannelStatus
  qrCodeUrl: string | null
  qrCodeImageContent: string | null
  deepLink: string | null
  expireTime: string | null
}

export interface WechatChannelStatusResponse {
  channelId: EntityId
  channelCode: string
  status: WechatChannelStatus
  agentId: EntityId
  userId: EntityId
  wechatAccountId: string | null
  wechatAccountUserId: string | null
  wechatNickname: string | null
  qrCodeUrl: string | null
  qrCodeImageContent: string | null
  deepLink: string | null
  expireTime: string | null
  lastActiveTime: string | null
  expireReason: string | null
}

channelName 已经出现在请求 DTO 中,但当前 Service 没有持久化或展示该值。前端可以暂不提供该输入项。

5.2 开通或取得扫码绑定 ​

http
POST /api/ai/v1/channel/wechat/agents/{agentId}/open

请求体可省略,也可以传:

json
{
  "channelName": "我的微信机器人"
}

示例响应:

json
{
  "code": 0,
  "msg": null,
  "ok": true,
  "data": {
    "channelId": "2099413200976412673",
    "channelCode": "wc_2067916016398761985_1_0f9ce6d5a05b",
    "status": "PENDING",
    "qrCodeUrl": "https://ilinkai.weixin.qq.com/...",
    "qrCodeImageContent": "data:image/svg+xml;base64,...",
    "deepLink": "https://ilinkai.weixin.qq.com/...",
    "expireTime": "2026-09-18T15:10:00"
  }
}

qrCodeUrl 是微信二维码中实际编码的链接,不是图片 URL,不能直接赋给 <img>。后端会将该链接生成二维码 SVG Data URL,放在 qrCodeImageContent。前端显示二维码时应优先使用:

ts
const qrSrc = data.qrCodeImageContent

qrSrc 是 data:image/svg+xml;base64,...,可直接赋给 <img :src="qrSrc">。deepLink 与 qrCodeUrl 相同,前端如需展示或复制二维码链接,应使用这个字段。

接口具有复用语义:

  • 已是 ACTIVE:返回现有 Channel,二维码字段通常为空。
  • 已是 PENDING:复用尚未过期的扫码票据;本机没有票据时会重新生成。
  • 是 EXPIRED 或 CLOSED:重置旧记录并生成新二维码。

5.3 查询 Agent 下的微信 Channel ​

http
GET /api/ai/v1/channel/wechat/agents/{agentId}

返回 WechatChannelStatusResponse[],按更新时间倒序。与另外三个平台不同,此接口的数据是数组。即使当前业务通常只保留一条微信记录,前端也必须按数组处理。

5.4 查询单个微信 Channel ​

http
GET /api/ai/v1/channel/wechat/{channelId}

在扫码过程中建议每 2 秒轮询一次。状态处理:

状态处理
PENDING继续轮询,显示二维码和剩余时间
ACTIVE停止轮询,关闭二维码弹窗,刷新列表
EXPIRED停止轮询,显示 expireReason 和“重新扫码”按钮
CLOSED停止轮询,显示已关闭

5.5 重新扫码 ​

http
POST /api/ai/v1/channel/wechat/{channelId}/reopen

请求体:

json
{
  "force": true
}
  • force=false 且 Channel 已经 ACTIVE:保留现有绑定,不会清除微信凭证。
  • force=true:停止当前运行时、清除当前绑定关系并生成新二维码。

“重新绑定其他微信账号”按钮必须传 force: true,并建议弹出确认提示,因为它会替换当前有效绑定。

5.6 关闭微信 Channel ​

http
POST /api/ai/v1/channel/wechat/{channelId}/close

无请求体。成功时 data 为 null,Channel 状态变为 CLOSED,对应运行时停止。

5.7 删除微信 Channel ​

http
DELETE /api/ai/v1/channel/wechat/{channelId}

无请求体,成功时 data 为 null。接口会停止运行时、关闭外部会话路由,并逻辑删除该 Channel 及其历史微信凭证。删除后再次使用微信需要重新调用 open 扫码绑定。

5.8 推荐前端流程 ​

6. QQ 渠道 ​

6.1 数据类型 ​

ts
export type RuntimeChannelStatus =
  | 'UNBOUND'
  | 'STARTING'
  | 'ACTIVE'
  | 'ERROR'
  | 'CLOSED'

export interface QqChannelConfigRequest {
  appId?: string
  appSecret?: string
  sandbox?: boolean
}

export interface QqChannelStatusResponse {
  channelId: EntityId | null
  channelCode: string | null
  status: RuntimeChannelStatus
  agentId: EntityId
  userId: EntityId
  appId: string | null
  hasAppSecret: boolean
  sandbox: boolean
  running: boolean
  lastActiveTime: string | null
  lastErrorMessage: string | null
}

6.2 查询配置 ​

http
GET /api/ai/v1/channel/qq/agents/{agentId}

尚未配置时仍是成功响应,data.status 为 UNBOUND,此时 channelId、appId 等字段可能为 null。

6.3 保存并启用 ​

http
POST /api/ai/v1/channel/qq/agents/{agentId}/config

首次配置:

json
{
  "appId": "1905581411",
  "appSecret": "<QQ Bot AppSecret>",
  "sandbox": false
}

更新已有配置但不替换密钥:

json
{
  "appId": "1905581411",
  "sandbox": false
}

保存接口会立即启动 Channel。正常情况下先返回 STARTING,随后异步变为 ACTIVE;失败时变为 ERROR 并填写 lastErrorMessage。前端保存成功后建议每 2 秒查询状态,最长等待 30 至 60 秒。

sandbox 新建时未传等价于 false;更新时未传会保留原值。同一个 appId + sandbox 只能有一个 ACTIVE/STARTING Channel,新配置启用后,重复的旧 Channel 会自动变成 CLOSED。

6.4 查询、启用、停用和删除 ​

http
GET  /api/ai/v1/channel/qq/{channelId}
POST /api/ai/v1/channel/qq/{channelId}/enable
POST /api/ai/v1/channel/qq/{channelId}/disable
DELETE /api/ai/v1/channel/qq/{channelId}

启用和停用均无请求体,返回最新 QqChannelStatusResponse。删除成功时 data 为 null,删除后需要重新填写 AppID 和 AppSecret 才能再次配置。

7. 钉钉渠道 ​

钉钉支持两种配置方式:

  1. 用户手工填写企业内部应用的 Client ID(AppKey)和 Client Secret(AppSecret)。
  2. 用户扫码授权,后端调用钉钉一键创建应用接口取得凭证并自动保存、启用。

两种方式最终写入同一条 Channel 配置。

7.1 数据类型 ​

ts
export interface DingTalkChannelConfigRequest {
  clientId?: string
  clientSecret?: string
}

export interface DingTalkChannelStatusResponse {
  channelId: EntityId | null
  channelCode: string | null
  status: RuntimeChannelStatus
  agentId: EntityId
  userId: EntityId
  clientId: string | null
  hasClientSecret: boolean
  running: boolean
  lastActiveTime: string | null
  lastErrorMessage: string | null
}

export type DingTalkScanStatus = 'WAITING' | 'SUCCESS' | 'FAIL' | 'EXPIRED'

export interface DingTalkScanResponse {
  flowId: string
  status: DingTalkScanStatus
  userCode: string | null
  verificationUri: string | null
  verificationUriComplete: string | null
  qrCodeDataUrl: string | null
  expiresAt: number
  intervalSeconds: number
  nextPollAfterSeconds: number
  failReason: string | null
  channel: DingTalkChannelStatusResponse | null
}

expiresAt 是 Unix 秒时间戳,不是毫秒:

ts
const expiresAtMs = response.expiresAt * 1000

7.2 查询配置 ​

http
GET /api/ai/v1/channel/dingtalk/agents/{agentId}

未配置时返回 UNBOUND,而不是 404。

7.3 手工保存并启用 ​

http
POST /api/ai/v1/channel/dingtalk/agents/{agentId}/config

首次配置:

json
{
  "clientId": "dingxxxxxxxxxxxxxxxx",
  "clientSecret": "<DingTalk AppSecret>"
}

已有配置只修改 Client ID 时可不传 clientSecret。保存后自动启动,前端按 STARTING -> ACTIVE/ERROR 轮询。

同一个 Client ID 只能有一个 ACTIVE/STARTING Channel,新 Channel 启用时会自动关闭重复的旧 Channel。

7.4 发起扫码创建应用 ​

http
POST /api/ai/v1/channel/dingtalk/agents/{agentId}/scan

无请求体。示例响应:

json
{
  "code": 0,
  "msg": null,
  "ok": true,
  "data": {
    "flowId": "9613059a6b8743b4ba251403c45e5b9a",
    "status": "WAITING",
    "userCode": "ABCD-EFGH",
    "verificationUri": "https://open.dingtalk.com/...",
    "verificationUriComplete": "https://open.dingtalk.com/...?user_code=...",
    "qrCodeDataUrl": "data:image/svg+xml;base64,...",
    "expiresAt": 1789718400,
    "intervalSeconds": 2,
    "nextPollAfterSeconds": 2,
    "failReason": null,
    "channel": null
  }
}

qrCodeDataUrl 可以直接赋给 <img> 的 src。同时建议显示 userCode,并提供打开 verificationUriComplete 的备用按钮。

钉钉上游在用户完成扫码、但尚未完成授权确认时可能返回 SCANNED。后端会将该中间状态归一为 WAITING 返回,前端应继续按 nextPollAfterSeconds 轮询,不要将其当作失败。

7.5 轮询扫码状态 ​

http
GET /api/ai/v1/channel/dingtalk/agents/{agentId}/scan/{flowId}

不要用固定轮询间隔。每次都按响应中的 nextPollAfterSeconds 安排下一次查询,避免触发上游频控。

扫码状态字段表现前端处理
WAITING二维码、验证码和跳转链接存在继续轮询
SUCCESSchannel 非空;一次性扫码字段会被清空停止轮询,显示 Channel 启动状态
FAILfailReason 非空停止轮询,显示原因并允许重新发起
EXPIREDfailReason 提示过期停止轮询并重新发起

扫码成功后,后端会自动保存 Client ID/Secret 并启用 Channel,前端不会接触 Client Secret。

可直接采用以下轮询逻辑:

ts
async function waitDingTalkScan(
  agentId: EntityId,
  first: DingTalkScanResponse,
  signal: AbortSignal
): Promise<DingTalkChannelStatusResponse> {
  let state = first

  while (!signal.aborted) {
    if (state.status === 'SUCCESS' && state.channel) return state.channel
    if (state.status === 'FAIL' || state.status === 'EXPIRED') {
      throw new Error(state.failReason || '钉钉扫码接入失败')
    }

    const delaySeconds = Math.max(1, state.nextPollAfterSeconds || state.intervalSeconds)
    await new Promise<void>((resolve, reject) => {
      const timer = window.setTimeout(resolve, delaySeconds * 1000)
      signal.addEventListener('abort', () => {
        window.clearTimeout(timer)
        reject(new DOMException('Aborted', 'AbortError'))
      }, { once: true })
    })

    state = await unwrap(api.get(
      `/dingtalk/agents/${agentId}/scan/${state.flowId}`
    ))
  }

  throw new DOMException('Aborted', 'AbortError')
}

7.6 取消扫码 ​

http
DELETE /api/ai/v1/channel/dingtalk/agents/{agentId}/scan/{flowId}

用户主动关闭扫码弹窗时可以调用。成功后 data 为 null。组件卸载时也应停止前端定时器;取消接口失败不应阻塞弹窗关闭。

7.7 查询、启用、停用和删除 ​

http
GET  /api/ai/v1/channel/dingtalk/{channelId}
POST /api/ai/v1/channel/dingtalk/{channelId}/enable
POST /api/ai/v1/channel/dingtalk/{channelId}/disable
DELETE /api/ai/v1/channel/dingtalk/{channelId}

启用和停用均无请求体。删除成功时 data 为 null,删除后需要重新填写 Client ID 和 Client Secret 或重新发起扫码接入。

8. 企业微信渠道 ​

当前实现对接的是企业微信“标准智能机器人”WebSocket 长连接,需要 Bot ID 和 Bot Secret。群机器人 Webhook URL 不能直接用于这组接口。

8.1 数据类型 ​

ts
export interface WeComChannelConfigRequest {
  botId?: string
  botSecret?: string
}

export interface WeComChannelStatusResponse {
  channelId: EntityId | null
  channelCode: string | null
  status: RuntimeChannelStatus
  agentId: EntityId
  userId: EntityId
  botId: string | null
  hasBotSecret: boolean
  running: boolean
  lastActiveTime: string | null
  lastErrorMessage: string | null
}

8.2 查询配置 ​

http
GET /api/ai/v1/channel/wecom/agents/{agentId}

未配置时返回 UNBOUND。

8.3 保存并启用 ​

http
POST /api/ai/v1/channel/wecom/agents/{agentId}/config

首次配置:

json
{
  "botId": "<WeCom Bot ID>",
  "botSecret": "<WeCom Bot Secret>"
}

编辑已有配置且不替换 Secret:

json
{
  "botId": "<WeCom Bot ID>"
}

接口保存后立即启动长连接。正常状态变化为 STARTING -> ACTIVE,失败时为 ERROR。同一个 Bot ID 只能有一个 ACTIVE/STARTING Channel,重复配置启用后旧 Channel 会自动关闭。

8.4 查询、启用、停用和删除 ​

http
GET  /api/ai/v1/channel/wecom/{channelId}
POST /api/ai/v1/channel/wecom/{channelId}/enable
POST /api/ai/v1/channel/wecom/{channelId}/disable
DELETE /api/ai/v1/channel/wecom/{channelId}

启用和停用均无请求体,返回 WeComChannelStatusResponse。删除成功时 data 为 null,删除后需要重新填写 Bot ID 和 Bot Secret。

9. 前端 API 封装示例 ​

以下示例只表达接口组织方式,可替换成项目现有的 Axios 封装:

ts
const BASE = '/api/ai/v1/channel'

export const channelApi = {
  statuses: (agentId: EntityId) =>
    request.get<ApiResult<ChannelBindingStatusResponse>>(`${BASE}/agents/${agentId}/statuses`),

  wechat: {
    list: (agentId: EntityId) =>
      request.get<ApiResult<WechatChannelStatusResponse[]>>(`${BASE}/wechat/agents/${agentId}`),
    open: (agentId: EntityId, body: WechatChannelOpenRequest = {}) =>
      request.post<ApiResult<WechatChannelOpenResponse>>(`${BASE}/wechat/agents/${agentId}/open`, body),
    status: (channelId: EntityId) =>
      request.get<ApiResult<WechatChannelStatusResponse>>(`${BASE}/wechat/${channelId}`),
    reopen: (channelId: EntityId, force = true) =>
      request.post<ApiResult<WechatChannelOpenResponse>>(`${BASE}/wechat/${channelId}/reopen`, { force }),
    close: (channelId: EntityId) =>
      request.post<ApiResult<null>>(`${BASE}/wechat/${channelId}/close`),
    delete: (channelId: EntityId) =>
      request.delete<ApiResult<null>>(`${BASE}/wechat/${channelId}`)
  },

  qq: {
    get: (agentId: EntityId) =>
      request.get<ApiResult<QqChannelStatusResponse>>(`${BASE}/qq/agents/${agentId}`),
    save: (agentId: EntityId, body: QqChannelConfigRequest) =>
      request.post<ApiResult<QqChannelStatusResponse>>(`${BASE}/qq/agents/${agentId}/config`, body),
    status: (channelId: EntityId) =>
      request.get<ApiResult<QqChannelStatusResponse>>(`${BASE}/qq/${channelId}`),
    enable: (channelId: EntityId) =>
      request.post<ApiResult<QqChannelStatusResponse>>(`${BASE}/qq/${channelId}/enable`),
    disable: (channelId: EntityId) =>
      request.post<ApiResult<QqChannelStatusResponse>>(`${BASE}/qq/${channelId}/disable`),
    delete: (channelId: EntityId) =>
      request.delete<ApiResult<null>>(`${BASE}/qq/${channelId}`)
  },

  dingtalk: {
    get: (agentId: EntityId) =>
      request.get<ApiResult<DingTalkChannelStatusResponse>>(`${BASE}/dingtalk/agents/${agentId}`),
    save: (agentId: EntityId, body: DingTalkChannelConfigRequest) =>
      request.post<ApiResult<DingTalkChannelStatusResponse>>(`${BASE}/dingtalk/agents/${agentId}/config`, body),
    startScan: (agentId: EntityId) =>
      request.post<ApiResult<DingTalkScanResponse>>(`${BASE}/dingtalk/agents/${agentId}/scan`),
    pollScan: (agentId: EntityId, flowId: string) =>
      request.get<ApiResult<DingTalkScanResponse>>(`${BASE}/dingtalk/agents/${agentId}/scan/${flowId}`),
    cancelScan: (agentId: EntityId, flowId: string) =>
      request.delete<ApiResult<null>>(`${BASE}/dingtalk/agents/${agentId}/scan/${flowId}`),
    status: (channelId: EntityId) =>
      request.get<ApiResult<DingTalkChannelStatusResponse>>(`${BASE}/dingtalk/${channelId}`),
    enable: (channelId: EntityId) =>
      request.post<ApiResult<DingTalkChannelStatusResponse>>(`${BASE}/dingtalk/${channelId}/enable`),
    disable: (channelId: EntityId) =>
      request.post<ApiResult<DingTalkChannelStatusResponse>>(`${BASE}/dingtalk/${channelId}/disable`),
    delete: (channelId: EntityId) =>
      request.delete<ApiResult<null>>(`${BASE}/dingtalk/${channelId}`)
  },

  wecom: {
    get: (agentId: EntityId) =>
      request.get<ApiResult<WeComChannelStatusResponse>>(`${BASE}/wecom/agents/${agentId}`),
    save: (agentId: EntityId, body: WeComChannelConfigRequest) =>
      request.post<ApiResult<WeComChannelStatusResponse>>(`${BASE}/wecom/agents/${agentId}/config`, body),
    status: (channelId: EntityId) =>
      request.get<ApiResult<WeComChannelStatusResponse>>(`${BASE}/wecom/${channelId}`),
    enable: (channelId: EntityId) =>
      request.post<ApiResult<WeComChannelStatusResponse>>(`${BASE}/wecom/${channelId}/enable`),
    disable: (channelId: EntityId) =>
      request.post<ApiResult<WeComChannelStatusResponse>>(`${BASE}/wecom/${channelId}/disable`),
    delete: (channelId: EntityId) =>
      request.delete<ApiResult<null>>(`${BASE}/wecom/${channelId}`)
  }
}

如果项目的 Axios 拦截器已经自动返回 response.data.data,应相应删掉示例中的 ApiResult<T> 外层,避免重复解包。

10. 推荐页面结构 ​

一个 Agent 的 Channel 配置页可以按四张卡片组织:

text
AgentChannelSettings
├── WechatChannelCard
│   ├── 状态、微信昵称、最后活跃时间
│   └── WechatQrDialog(开通/重绑/轮询)
├── QqChannelCard
│   ├── AppID、沙箱开关、密钥已配置标记
│   └── 配置/启用/停用/状态轮询
├── DingTalkChannelCard
│   ├── 手工配置表单
│   ├── DingTalkScanDialog
│   └── 启用/停用/状态轮询
└── WeComChannelCard
    ├── Bot ID、密钥已配置标记
    └── 配置/启用/停用/状态轮询

页面加载时如果只需要展示四个平台的启用/停用/未绑定状态,优先调用聚合接口:

ts
const statusSummary = await unwrap(channelApi.statuses(agentId))

进入单个平台配置卡片或弹窗时,再请求该平台的详情接口。若页面需要首屏直接展示完整配置详情,也可以继续并发请求四个平台:

ts
await Promise.allSettled([
  channelApi.wechat.list(agentId),
  channelApi.qq.get(agentId),
  channelApi.dingtalk.get(agentId),
  channelApi.wecom.get(agentId)
])

使用 Promise.allSettled 可以避免一个平台详情查询失败导致整个配置页空白。每张卡片独立显示加载、成功和失败状态。

11. 表单与交互规则 ​

11.1 保存按钮 ​

  • 首次配置时,平台 ID 和 Secret 都必填。
  • 已配置 Secret 时,Secret 输入框允许为空;为空表示保留原密钥。
  • Secret 输入框使用密码模式,不要回填伪造的星号字符串。
  • 保存成功并返回 STARTING 后禁用重复提交,开始查询 Channel 状态。
  • 进入 ACTIVE、ERROR 或超过最长等待时间后停止轮询。

11.2 启用与停用 ​

  • CLOSED 显示“启用”。
  • ACTIVE、STARTING 显示“停用”。
  • ERROR 同时允许“重试启用”和“编辑配置”。
  • 操作进行中禁用按钮,防止用户连续发起相同请求。

11.3 错误展示 ​

优先级建议:

  1. 请求失败时显示响应 msg。
  2. Channel 进入 ERROR 时显示 lastErrorMessage。
  3. 微信 EXPIRED 时显示 expireReason。
  4. 钉钉扫码失败时显示 failReason。

不要把错误对象、堆栈、密钥或完整请求体写入浏览器日志。

11.4 轮询清理 ​

以下场景必须停止定时器或中止请求:

  • 组件卸载。
  • 切换 Agent。
  • 用户关闭扫码弹窗。
  • 状态进入终态。
  • access token 失效。

建议为每个扫码或连接等待流程创建独立的 AbortController。

12. 后端运行前置配置 ​

这些配置不由前端提交,但联调前必须确认 AI 服务已设置,否则配置接口可能成功写库后进入 ERROR,或直接返回业务失败。

环境变量平台是否必需说明
WECOM_CHANNEL_SECRET_KEY企业微信必需加密/解密 Bot Secret;所有 AI 实例必须一致
DINGTALK_CHANNEL_SECRET_KEY钉钉必需加密/解密 Client Secret 和扫码临时凭证;所有实例必须一致
QQ_CHANNEL_SECRET_KEYQQ强烈建议显式配置当前代码有开发默认值,生产环境必须换成高强度随机密钥并在所有实例保持一致
WECHAT_CHANNEL_TOKEN_KEY微信强烈建议显式配置加密/解密微信 Token;生产环境必须换成高强度随机密钥并在所有实例保持一致
DINGTALK_CHANNEL_REGISTRATION_SOURCE钉钉扫码按钉钉接入条件合作方获得的来源标识;未申请时可为空

Spring Boot 会把这些环境变量映射到 wecom.channel.secret-key、dingtalk.channel.secret-key、qq.channel.secret-key、wechat.channel.token-key 等属性。

多实例环境还必须满足:

  • 所有实例连接同一个 Redis,以实现 Channel 运行时分布式锁和钉钉扫码会话共享。
  • allow-local-runtime-lock-fallback 保持 false。
  • 同一平台加密密钥在所有实例中完全一致。
  • 数据库中同一个平台账号只保留一个有效运行时;重复 Channel 会由当前代码自动关闭。

13. 常见错误与定位 ​

前端看到的消息或现象常见原因处理
未登录 / Unauthenticated没有 Bearer Token,或 Token 失效重新登录并检查请求拦截器
Agent 不存在agentId 错误或数据不存在保留 ID 字符串精度,检查 Agent
Channel 不存在或无权访问Channel 不属于当前用户,或 ID 精度丢失不要把 ID 转成 Number;检查登录用户
密钥加密失败对应 *_CHANNEL_SECRET_KEY 未配置或非法配置 AI 服务环境变量并重启
长时间停在 STARTING平台网络、平台凭证、运行时锁或服务实例异常继续查询状态并查看 lastErrorMessage、AI 日志
ACTIVE 但 running=false多实例下请求落到非持有运行时的节点以 status 为主,并检查 Redis 分布式锁
钉钉扫码会话不存在flowId 过期、已取消、用户/租户/Agent 不匹配重新发起扫码,不复用旧 flowId
钉钉扫码请求过快但仍返回 WAITING后端限制了轮询窗口使用 nextPollAfterSeconds 安排下一次请求
微信二维码为空且状态为 ACTIVE已绑定,无需扫码直接显示运行状态

14. 当前接口限制 ​

  • 四个平台均支持删除 Channel。删除采用逻辑删除;微信会同时删除该 Channel 的凭证记录。
  • 微信使用 close,没有独立的 enable;关闭后再次调用 open 会重新扫码。
  • 聚合三态接口只返回启用、停用、未绑定和少量诊断字段;完整配置仍需调用各平台详情接口。
  • QQ、钉钉、企业微信按 agentId + 当前用户 返回最近一条 Channel;微信返回列表。
  • 钉钉扫码会话依赖 Redis;扫码完成后的临时状态只保留一段时间。
  • 配置保存接口会立即尝试启动平台连接,目前没有“只保存不启用”的接口。
  • 当前钉钉和企业微信配置接口不接收 Webhook URL。
  • channelName 当前虽在微信开通 DTO 中,但尚未进入实际存储和返回模型。

15. 前后端联调验收清单 ​

  • [ ] 四个平台的查询接口都携带登录 Token。
  • [ ] 所有 Long ID 在前端均使用字符串。
  • [ ] 页面能正确处理 HTTP 200 但 code=1 的业务失败。
  • [ ] 首次配置要求 Secret,编辑配置时允许 Secret 留空。
  • [ ] 页面不会显示或缓存平台 Secret。
  • [ ] 微信二维码可以同时兼容普通 URL 和 Data URL。
  • [ ] 微信扫码能从 PENDING 正确切换到 ACTIVE 或 EXPIRED。
  • [ ] 钉钉扫码严格使用服务端返回的轮询间隔。
  • [ ] 用户关闭钉钉扫码弹窗时停止轮询并调用取消接口。
  • [ ] QQ、钉钉、企业微信保存后能从 STARTING 更新到 ACTIVE/ERROR。
  • [ ] 停用后可以直接重新启用,不要求再次填写 Secret。
  • [ ] 删除 Channel 后页面会刷新为未绑定状态,微信需重新扫码,其余平台需重新提交配置。
  • [ ] 页面切换 Agent 或卸载组件时清除所有轮询任务。
  • [ ] 多个平台之一失败时,其他平台配置卡片仍能正常使用。
  • [ ] 后端已配置各平台加密密钥,并在多实例间保持一致。
  • [ ] 多实例环境连接同一个 Redis,且未启用本地运行时锁回退。