showWxCustomerService
调起微信客服会话,仅支持微信小程序端。
前置依赖
依赖参数
| 参数名 | 类型 | 说明 |
|---|---|---|
deps.openCustomerServiceChat | UniApp['openCustomerServiceChat'] | 打开客服会话 API(可选) |
deps.detectEnvironment | () => 'h5' | 'miniapp' | 'app' | 'unknown' | 检测环境函数(可选) |
deps.hasWx | () => boolean | 检测是否有 wx 对象(可选) |
deps.getWx | () => { openCustomerServiceChat?: ... } | 获取 wx 对象(可选,降级使用) |
deps.error | Console['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
}参数
| 参数名 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
options | CustomerServiceOptions | 否 | {} | 客服选项 |
options.corpId | string | 是(企业微信客服) | - | 企业微信客服 corpId(需在公众平台绑定企业微信客服) |
options.extInfo | Record<string, unknown> | 否 | - | 扩展信息 |
options.showMessageCard | boolean | 否 | - | 是否显示消息卡片 |
deps | ShowCustomerServiceDeps | 是 | - | 依赖注入对象 |
返回值
| 类型 | 说明 |
|---|---|
Promise<CustomerServiceResult> | Promise,成功时返回客服结果 |
工作原理
- 环境检测:检查当前是否为微信小程序环境
- API调用:
- 优先使用
uni.openCustomerServiceChat - 降级使用
wx.openCustomerServiceChat
- 优先使用
- 调起客服:调用微信客服接口
- 返回结果:返回客服调用结果
使用方式:也可以使用 <button open-type="contact"> 触发客服会话。
注意:H5 和 App 端不支持此功能,会返回错误提示。