企业微信中第三方应用(h5)不能像小程序那样将button标签的open-type属性设置为contact即可跳转到客服会话页面。但是js sdk为了开发者提供了openThirdAppServiceChat Api,让用户可快速打开应用客服的会话。接下来,记录前端需要做的工作:
1. 通过企业微信登录应用管理后台
配置第三方应用的客服。(可能这不是前端的工作,但是这一步必不可少)
2. 引入 JS SDK,设置触发按钮
<script src="//res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
<script src="https://open.work.weixin.qq.com/wwopen/js/jwxwork-1.0.0.js"></script>
<div @click="goToCustom">立即咨询</div>
3. 调用 wx.agentConfig引入需要用的Api
// signature,corpid,agentid,timestamp,nonceStr是需要拿path调接口从后端获取。
goToCustom(){
wx.ready(() => {
wx.agentConfig({
signature,
corpid,
agentid,
timestamp,
nonceStr,
jsApiList: ['openThirdAppServiceChat'],
success(res) {
wx.invoke('openThirdAppServiceChat', {}, function (res) {
console.log('openThirdAppServiceChat res', res);
if (res.err_msg === 'openThirdAppServiceChat:ok') {
//
}
})
},
fail: function (res) {
if (res.errMsg.indexOf('function not exist') > -1) {
alert('版本过低请升级')
} else {
alert('agentConfig error info:' + res.errMsg)
}
}
})
})
}
注意:在微信环境中,IOS与Andriod的页面真实URL不同,IOS为第一次刚进入应用时的URL。Andriod为当前的URL井号前面的部分。
如有什么问题欢迎批评指正。