let url = req.query.url // 使用接口的url链接,不包含#后的内容
// 将请求以上字符串,先按字典排序,再以'&'拼接,如下:其中j > n > t > u,此处直接手动排序
let str = 'jsapi_ticket=' + jsapi_ticket + '&noncestr=' + nonce_str + '×tamp=' + timestamp + '&url=' + url
// 用sha1加密
let signature = sha1(str)
res.send({
appId: appid,
timestamp: timpstamp,
nonceStr: nonce_str,
signature: signature,
})
})
})
})
前端请求后端接口,获取配置信息
获取配置
axios.post('/wxJssdk/getJssdk', {url: location.href}).then((response) => {
var data = response.data wx.config({
debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId: data.appId, // 必填,公众号的唯一标识
timestamp: data.timestamp, // 必填,生成签名的时间戳
nonceStr: data.nonceStr, // 必填,生成签名的随机串
signature: data.signature,// 必填,签名,见附录1
jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage'] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
});
})
做你想做的,比如,自定义分享
if (wx) {
axios.post('/wxJssdk/getJssdk', {url: location.href}).then((response) => {
var data = response.data wx.config({
debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId: data.appId, // 必填,公众号的唯一标识
timestamp: data.timestamp, // 必填,生成签名的时间戳
nonceStr: data.nonceStr, // 必填,生成签名的随机串
signature: data.signature,// 必填,签名,见附录1
jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage'] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
});
wx.ready(function () {
wx.onMenuShareTimeline({
title: wxShare.title,
desc: wxShare.desc,
link: wxShare.link,
imgUrl: wxShare.imgUrl
});
wx.onMenuShareAppMessage({
title: wxShare.title,
desc: wxShare.desc,
link: wxShare.link,
imgUrl: wxShare.imgUrl
});
})
wx.error(function (res) {
// config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。









