wx.config({
debug: true, // 开启调试模式
appId: '你的公众号appId',
timestamp: new Date().getTime(), // 获取当前时间戳
nonceStr: '随机字符串',
signature: '生成的签名',
jsApiList: ['需要使用的JS接口列表'] // 例如 ['onMenuShareTimeline', 'onMenuShareAppMessage']
});但是报错微信权限验证失败: {"realAuthUrl": "[]", "errMsg": "config:invalid signature"},所以想问下h5里面打开wx.openCustomerServiceChat是不是只能先跳到小程序,在小程序里面用这个微信客服接口
不需要跳到小程序也可以使用,确保signature签名有效,每次打开页面都需要重新调用wx.config
import wx from 'weixin-js-sdk';
import Vue from 'vue'
// 调用接口获取微信权限验证配置信息
const ajaxShare = () => {
return new Promise((resolve,reject) => {
const url = encodeURIComponent(window.location.href)
const Body = {
head: wechatshareData.head,
body:{
url: url,
}
}
wechatshareData.getPost(Body).then(response => {
const data = response.data.tradeMap.data.body
if(data){
if(data.state === '00'){
// 微信权限验证配置信息
const mshopConfigData = {
appId: data.respInfo.appId,
timestamp: data.respInfo.timestamp,
nonceStr: data.respInfo.nonceStr,
signature: data.respInfo.signature
}
resolve({
config: mshopConfigData
})
}else {
Vue.prototype.$dialog.alert({ title: '温馨提示', message: data.message})
resolve({
flag: false,
})
}
}else{
reject('系统繁忙,请稍后重试。');
}
}).catch((error) => {
reject(error);
})
})
};
// 微信权限验证配置
const configWXMenu = (mshopConfigData,value, callback) => {
wx.config({
debug: false,
appId: mshopConfigData.appId,
timestamp: mshopConfigData.timestamp,
nonceStr: mshopConfigData.nonceStr,
signature: mshopConfigData.signature,
jsApiList: value.apiList
})
wx.error(function(res) {
console.log('微信权限验证失败:', res);
});
callback && callback()
}
// 微信小程跳转客服链接
const openCustomer = (mshopConfigData, data) => {
configWXMenu(mshopConfigData,
{
apiList: ['openCustomerServiceChat']
},
() => {
wx.ready(() => {
wx.openCustomerServiceChat({
extInfo: {url: data.tgljPath}, //客服链接
corpId: '', //企业ID
success(res) {
console.log(res,'跳转客服成功');
},
fail(error) {
console.log(error,'跳转客服失败');
}
})
})
}
)
}
export const wechatOpenKefu = async function (data) {
const kefuData = await ajaxShare();
if(kefuData.config){
openCustomer(kefuData.config, data)
}
};
wx.config({
debug: true, // 开启调试模式
appId: mshopConfigData.appId,
timestamp: mshopConfigData.timestamp,
nonceStr: mshopConfigData.nonceStr,
signature: mshopConfigData.signature,
jsApiList: ['openCustomerServiceChat']
});
wx.ready(() => {
wx.openCustomerServiceChat({
extInfo: { url: data.tgljPath },
corpId: '',
success(res) {
console.log(res, '跳转客服成功');
},
fail(error) {
console.log(error, '跳转客服失败');
}
});
});
wx.error((res) => {
console.error('微信JS-SDK配置错误:', res);
});
}