h5分享朋友圈
出现以上错误解决方法:
1、需要后台配置安全域名
2、分享的链接一定不能包含中文,需要用encodeURIComponent()转换一下
3、title,link,imgUrl 都需要和安全域名一致(查找其他文章都只提到链接域名需要和安全域名一致,但是实际操作发现标题或图片仍不显示,直到把他们都配置为安全域名,分享朋友圈才显示正常)
// #ifdef H5
var jweixin = require('jweixin-module')
// #endif
jweixin.config({
debug: true, // 开启调试模式,调用的所有 api 的返回值会在客户端 alert 出来,若要查看传入的参数,可以在 pc 端打开,参数信息会通过 log 打出,仅在 pc 端时才会打印。
appId: '', // 必填,公众号的唯一标识
timestamp: , // 必填,生成签名的时间戳
nonceStr: '', // 必填,生成签名的随机串
signature: '',// 必填,签名
jsApiList: ['updateTimelineShareData', 'updateAppMessageShareData'] // 必填,需要使用的 JS 接口列表
}); 这个里面的内容我们是后台接口返回的
// #ifdef H5
jweixin.ready(function () {
jweixin.updateTimelineShareData({
title: that.shareInfo.title, // 分享标题
link: that.shareLink, // 分享链接
imgUrl: encodeURI(that.shareInfo.img), // 分享图标
success: function (res) {
},
cancel: () => {
},
});
})
// #endif
小程序分享朋友圈
- 首先要在onload里面添加shareAppMessage和shareTimeline,分享朋友圈必须加上分享好友按钮shareAppMessage
- 如果点击胶囊按钮,发现没有分享朋友圈按钮则需要升级微信版本即可
onLoad(options) {
// #ifdef MP-WEIXIN
wx.showShareMenu({
withShareTicket: true,
menus: ["shareAppMessage","shareTimeline"]
})
// #endif
},
// 分享好友
onShareAppMessage() {
return {
title: this.shareInfo.title,
path: this.shareLink,
imageUrl: this.shareInfo.img
};
},
// 分享到朋友圈
onShareTimeline() {
return {
title: this.shareInfo.title,
path: this.shareLink,
imageUrl: this.shareInfo.img
};
},