微信分享,我们后台用的是PHP,并且再拿config时需要用当前网址来请求,但是微信在从朋友圈点过来的链接和从好友分享过来的链接时,会在后面自带参数,所以需要使用正则去去掉这些参数。
$(function () {
//微信分享
try {
if(window.location.href.toString().match(/singlemessage/ig) || window.location.href.toString().match(/timeline/ig) || window.location.href.toString().match(/groupmessage/ig)
){
var win = window.location.href.toString().split("?")[0];
window.location.href = win
}
}
catch(e) {
}
var url = 'http://tg.dili360.com/tclys/index/getJsSign?url=' + window.location.href.toString();
$.ajax({
url: url,
dataType: 'json',
success: function (data) {
wx.config({
debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId: data.appId, // 必填,公众号的唯一标识
timestamp: data.timestamp, // 必填,生成签名的时间戳
nonceStr: data.nonceStr, // 必填,生成签名的随机串
signature: data.signature,// 必填,签名
jsApiList: ['onMenuShareTimeline','onMenuShareAppMessage','onMenuShareQQ'] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
});
wx.ready(function () {
//分享到朋友圈
wx.onMenuShareTimeline({
title: wxText, // 分享标题
link: '', // 分享链接
imgUrl: imgSrc, // 分享图标
success: function () {
// 用户确认分享后执行的回调函数
},
cancel: function () {
// 用户取消分享后执行的回调函数
}
});
//分享给朋友
wx.onMenuShareAppMessage({
title: wxText, // 分享标题
desc: '“无所争 自有声”摄影大赛-中国国家地理&TCL', // 分享描述
link: '', // 分享链接
imgUrl: imgSrc, // 分享图标
type: '', // 分享类型,music、video或link,不填默认为link
dataUrl: '', // 如果type是music或video,则要提供数据链接,默认为空
success: function () {
// 用户确认分享后执行的回调函数
},
cancel: function () {
// 用户取消分享后执行的回调函数
}
});
//分享给qq朋友
wx.onMenuShareQQ({
title: wxText, // 分享标题
desc: '“无所争 自有声”摄影大赛-中国国家地理&TCL', // 分享描述
link: '', // 分享链接
imgUrl: imgSrc, // 分享图标
success: function () {
// 用户确认分享后执行的回调函数
},
cancel: function () {
// 用户取消分享后执行的回调函数
}
});
})
},
error: function (data) {
console.log(data)
}
})
})