Skip to content

openCustomerServiceInMiniapp

微信小程序端企业微信客服会话适配器,showWxCustomerService 内部调用

前置依赖

通过 deps 注入,便于单测与跨运行时复用:

typescript
interface CustomerServiceInMiniappDeps {
  openCustomerServiceChat?: UniApp['openCustomerServiceChat']
  hasWx?: () => boolean
  getWx?: () => { openCustomerServiceChat?: (options: ...) => void }
  error: Console['error']
}
依赖说明
openCustomerServiceChat优先使用的 UniApp 客服 API
hasWx / getWx降级至原生 wx.openCustomerServiceChat 时使用
error错误日志

openCustomerServiceInMiniapp

函数签名

typescript
function openCustomerServiceInMiniapp(
  options?: CustomerServiceOptions,
  deps: CustomerServiceInMiniappDeps
): Promise<CustomerServiceResult>

参数

参数名类型必填说明
options.corpIdstring企业微信客服 corpId(公众平台需已绑定)
options.extInfoRecord<string, unknown>扩展信息,默认 {}
options.showMessageCardboolean是否发送小程序气泡消息
options.sendMessageTitlestring气泡消息标题
options.sendMessagePathstring气泡消息小程序路径
options.sendMessageImgstring气泡消息封面图
depsCustomerServiceInMiniappDeps环境依赖

返回值

类型说明
Promise<CustomerServiceResult>成功含 errMsgraw;缺少 corpId 或不支持时 reject

工作原理

  1. 校验 options.corpId,缺失则 reject 并提示需在公众平台绑定企业微信客服。
  2. deps.openCustomerServiceChat 存在,优先调用 UniApp 封装 API。
  3. 否则检测 wx.openCustomerServiceChat 并降级调用。
  4. 两者均不可用时 reject,提示使用 <button open-type="contact"> 触发客服会话。