备注: 在外链中,不能直接调起小程序原生客服(wx.openCustomerServiceChat)。
解决方法: 小程序里新增一个原生的过渡页面。外链跳转到过渡页面,在过渡页面添加点击事件,打开原生客服
在外链中劫持跳转,并跳转到原生的小程序页面
// 检测是否是微信环境
var isWeChat = /micromessenger/i.test(navigator.userAgent);
if (isWeChat && qywx_company_id) {
console.log("当前是微信环境");
jWeixin.miniProgram.navigateTo({
url: "/pageE/transitionPage/transitionPage?corpId="+qywx_company_id+"&extInfoUrl="+data_info.cs_link
})
} else {
console.log("当前不是微信环境");
window.location.href = data_info.cs_link
}
当外链打开过渡页面时,注意过渡页面的生命周期执行顺序,
created() {
setTimeout(() => {
// 打开微信客服
// #ifdef MP
if (this.$Route.query.corpId || this.$Route.query.extInfoUrl) {
this.goChat()
}
// #endif
}, 0)
},
// 企业微信id
getQywxCs() {
let _this = this
return new Promise((resolve, reject) => {
this.$api.buy.getQywxCs().then(res => {
if (res.code == 0) {
this.qywx_company_id = res.data.qywx_company_id
resolve(res.data)
}
})
})
},
// 去客服
goChat() {
let _this = this
uni.showModal({
title: '微信客服',
content: '您即将进入客服对话框。',
complete(res) {
if (res.confirm) {
if (!_this.$Route.query.corpId && !_this.qywx_company_id) {
_this.getQywxCs().then((res) => {
wx.openCustomerServiceChat({
extInfo: {
url: _this.$Route.query.extInfoUrl
},
corpId: res.qywx_company_id,
success(res) {
console.log(res, 'success');
},
fail(err) {
console.log(err, 'err');
},
complete(res) {
setTimeout(()=>{
uni.navigateBack()
},1000)
}
})
}).catch(err=>{
console.log(err);
})
} else {
wx.openCustomerServiceChat({
extInfo: {
url: _this.$Route.query.extInfoUrl
},
corpId: _this.$Route.query.corpId||_this.qywx_company_id,
success(res) {
console.log(res, 'success');
},
fail(err) {
console.log(err, 'err');
},
complete(res) {
setTimeout(()=>{
uni.navigateBack()
},1000)
}
})
}
} else {
uni.navigateBack()
}
}
})
},