Skip to content

showWxCustomerService

调起微信客服会话,仅支持微信小程序端。

前置依赖

依赖参数

参数名类型说明
deps.openCustomerServiceChatUniApp['openCustomerServiceChat']打开客服会话 API(可选)
deps.detectEnvironment() => 'h5' | 'miniapp' | 'app' | 'unknown'检测环境函数(可选)
deps.hasWx() => boolean检测是否有 wx 对象(可选)
deps.getWx() => { openCustomerServiceChat?: ... }获取 wx 对象(可选,降级使用)
deps.errorConsole['error']错误输出

环境要求

  • 微信小程序: 仅支持微信小程序端
  • 注意: H5 和 App 端不支持,需要使用其他客服方式

函数签名

typescript
function showWxCustomerService(
  options?: CustomerServiceOptions,
  deps: ShowCustomerServiceDeps
): Promise<CustomerServiceResult>

interface ShowCustomerServiceDeps {
  openCustomerServiceChat?: UniApp['openCustomerServiceChat']
  detectEnvironment?: () => 'h5' | 'miniapp' | 'app' | 'unknown'
  hasWx?: () => boolean
  getWx?: () => { openCustomerServiceChat?: (options: {
    corpId: string
    extInfo?: Record<string, unknown>
    showMessageCard?: boolean
    sendMessageTitle?: string
    sendMessagePath?: string
    sendMessageImg?: string
    success?: (res: { errMsg?: string }) => void
    fail?: (err: { errMsg: string; [key: string]: unknown }) => void
  }) => void }
  error: Console['error']
}

interface CustomerServiceOptions {
  sessionFrom?: string
  sessionFromParam?: string
  extraData?: Record<string, unknown>
  corpId?: string
  extInfo?: Record<string, unknown>
  showMessageCard?: boolean
  sendMessageTitle?: string
  sendMessagePath?: string
  sendMessageImg?: string
}

interface CustomerServiceResult {
  errMsg?: string
  raw?: unknown
}

参数

参数名类型必填默认值说明
optionsCustomerServiceOptions{}客服选项
options.corpIdstring是(企业微信客服)-企业微信客服 corpId(需在公众平台绑定企业微信客服)
options.extInfoRecord<string, unknown>-扩展信息
options.showMessageCardboolean-是否显示消息卡片
depsShowCustomerServiceDeps-依赖注入对象

返回值

类型说明
Promise<CustomerServiceResult>Promise,成功时返回客服结果

工作原理

  1. 环境检测:检查当前是否为微信小程序环境
  2. API调用
    • 优先使用 uni.openCustomerServiceChat
    • 降级使用 wx.openCustomerServiceChat
  3. 调起客服:调用微信客服接口
  4. 返回结果:返回客服调用结果

使用方式:也可以使用 <button open-type="contact"> 触发客服会话。

注意:H5 和 App 端不支持此功能,会返回错误提示。