Skip to content

IM Web SDK Widget 接入指南

Widget 模式适合在业务网站中通过一段 <script> 引入在线客服插件。SDK 会自动获取配置、渲染悬浮入口和聊天窗口。

快速接入

建议先创建 window.mchat 预队列,再异步加载 SDK。SDK 加载期间调用的命令会进入队列,并在初始化完成后执行。

html
<script>
  window.mchat = window.mchat || {
    _q: [],
    push: function () {
      this._q.push(Array.prototype.slice.call(arguments))
    },
  }

  window.mchat.push('setLoginInfo', {
    user_id: 'user_123',
    nickname: '张三',
    email: 'user@example.com',
  })
</script>

<script
  src="https://your-domain.com/plugin.js"
  data-app-id="YOUR_APP_KEY"
  async
></script>

data-app-id 填写聊天插件的 AppKey。可以在聊天插件列表中查看。

应用标识读取顺序

Widget 会按以下顺序读取应用标识:

  1. 当前 SDK 脚本的 data-app-id
  2. 当前 SDK 脚本的 data-token
  3. 页面中任意 script[data-app-id]
  4. 当前页面 URL 的 ?token=

开发环境也可以使用:

text
http://localhost:5173/?token=YOUR_APP_KEY

设置登录用户

建议在 SDK 加载前或聊天窗口首次打开前调用:

js
window.mchat.push('setLoginInfo', {
  user_id: 'user_10001',
  nickname: '张三',
  user_name: '张三',
  language: 'zh-CN',
  phone: '13800138000',
  email: 'user@example.com',
  description: 'VIP 用户',
  label_names: ['VIP', '已登录'],
  update_label_type: 'append',
  custom_fields_ext: {
    member_level: 'gold',
    order_count: 12,
  },
})
字段类型必填说明
user_idstring业务系统中的用户唯一标识
nicknamestring用户昵称
user_namestring兼容的用户名称字段
languagestring用户语言,例如 zh-CNen-US
phonestring手机号
emailstring邮箱
descriptionstring用户备注
label_namesstring[]用户标签
update_label_typeappend | update追加或覆盖标签
custom_fields_extRecord<string, unknown>自定义扩展字段

用户在站内切换账号时,使用新的 user_id 再次调用 setLoginInfo。退出登录时调用:

js
window.mchat.push('clearLoginInfo')

Page 直开模式传递用户信息

Page 模式可以通过 URL 查询参数传递登录信息。至少需要 user_id

text
https://chat.example.com/?user_id=user_10001&nickname=%E5%BC%A0%E4%B8%89&language=zh-CN

支持 nicknameuser_namelanguagephoneemaildescriptionlabel_namesupdate_label_typecustom_fields_ext。其中:

  • label_names 使用英文逗号分隔。
  • update_label_type 只接受 appendupdate
  • custom_fields_ext 必须是 URL 编码后的 JSON 对象字符串。

隐私

URL 可能出现在浏览器历史、日志和统计系统中。不要把密码、验证码、令牌或其他敏感凭据放入查询参数。

控制聊天窗口

js
window.mchat.push('openChat')
window.mchat.push('closeChat')
window.mchat.push('hideIcon')
window.mchat.push('showIcon')

把站内“联系客服”按钮绑定到聊天窗口:

html
<button type="button" id="contact-service">联系客服</button>
<script>
  document.getElementById('contact-service').addEventListener('click', function () {
    window.mchat.push('openChat')
  })
</script>

事件回调

命令回调参数触发时机
onReadyfunction()SDK 初始化完成
onUnreadfunction(count)未读消息数变化
onSendMessagefunction(message)访客发送消息
onReceiveMessagefunction(message)收到客服或系统消息
onWindowOpenfunction()聊天窗口打开
onWindowClosefunction()聊天窗口关闭
onIconClickfunction()访客点击悬浮入口
onOpenInfoCollectionfunction()聊前调查或离线留资打开
onCompleteInfoCollectionfunction(data)信息收集提交完成
js
window.mchat.push('onReady', function () {
  console.log('SDK ready')
})

window.mchat.push('onUnread', function (count) {
  document.title = count > 0 ? '(' + count + ') 新消息' : '首页'
})

window.mchat.push('onCompleteInfoCollection', function (data) {
  console.log('信息收集完成:', data)
})

TypeScript 类型示例

ts
type MChatCommand =
  | 'setLoginInfo'
  | 'clearLoginInfo'
  | 'openChat'
  | 'closeChat'
  | 'hideIcon'
  | 'showIcon'
  | 'onReady'
  | 'onUnread'
  | 'onSendMessage'
  | 'onReceiveMessage'
  | 'onWindowOpen'
  | 'onWindowClose'
  | 'onIconClick'
  | 'onOpenInfoCollection'
  | 'onCompleteInfoCollection'

interface MChatApi {
  version?: string
  _q?: Array<[string, unknown]>
  push(command: MChatCommand, data?: unknown): void
}

declare global {
  interface Window {
    mchat: MChatApi
  }
}

连接全球客户,让跨境沟通更高效。