$.ajax({
type: "get"
,
url: "*************",
//后台分享接口地址
async:
true,
data: {
'url': window.location.href
//返回给后台的参数
},
success: function(res) {
console.log(res.data);
if(res.code == 1
) {
wx.config({
debug: false,
// 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId: res.data.appId,
// 必填,公众号的唯一标识
timestamp: res.data.timestamp,
// 必填,生成签名的时间戳
nonceStr: res.data.nonceStr,
// 必填,生成签名的随机串
signature: res.data.signature,
// 必填,签名,见附录1
jsApiList: [
'checkJsApi'
,
'onMenuShareTimeline'
,
'onMenuShareAppMessage'
] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
});
wx.ready(function() {
// 2.1 监听“分享给朋友”,按钮点击、自定义分享内容及分享结果接口
wx.onMenuShareAppMessage({
title: '分享标题'
,
desc: '分享描述'
,
link: 'www.baidu.com',
//分享链接地址,比如分享到百度
imgUrl: '分享图片'
,
img_width: "50 "
,
img_height: "50 "
,
success: function(res) {
},
cancel: function(res) {
//$.toast('已取消');
},
fail: function(res) {
alert(JSON.stringify(res));
}
});
//分享到朋友圈
wx.onMenuShareTimeline({
title: '分享标题'
,
desc: '分享描述'
,
link: 'www.baidu.com',
//分享链接地址,比如分享到百度
imgUrl: '分享图片'
,
img_width: "50 "
,
img_height: "50 "
,
success: function(res) {
},
cancel: function(res) {
//$.toast('已取消');
},
fail: function(res) {
alert(JSON.stringify(res));
}
});
})
wx.error(function(res) {
// config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。
alert("分享失败 "
);
});
}
}
});
转载于:https://www.cnblogs.com/lingXie/p/10518184.html
相关资源:HTML5微信分享图标和文本设置