有4个注意事项:1.现在腾讯规定不可以自定按钮进行分享,只能点击右上角进行分享。
2.分享的地址域名,必须和当前域名相同如:当前页面域名为。分享的链接必须在这个域名下如:/index.html,不然不能实现自定义。
3.导入微信的js <script type="text/javascript" src="http://res./open/js/jweixin-1.2.0.js"></script>
4.wx.config里面必须初始化,调用你需要使用的微信接口。
window.οnlοad=function(){ //用window的onload事件,窗体加载完毕的时候
url=location.href.split('#')[0];
var url2 = window.location.host;//当前域名
var url3 = location.href.split('?')[0];//当前域名不要问号传参后面的值
$.ajax({
type: "POST",
url:"${pageContext.request.contextPath}/wechat/inquiry/getSignatureForWx.shtml",
dataType : "json",
data:{
appid:"${appid}",
url:url
},
success: function(data) {
var timestamp = data.timestamp;
var noncestr = data.noncestr;
var signature = data.signature;
//appid,timestamp,等必填信息我是通过ajax从后台获取,你也可以直接从微信公众平台copy过来
wx.config({
debug: false, //开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId: data.appid , // 必填,公众号的唯一标识
timestamp: data.timestamp, // 必填,生成签名的时间戳
nonceStr: data.nonceStr, // 必填,生成签名的随机串
signature: data.signature,// 必填,签名,见附录1
// jsApiList: ['checkJsApi', 'chooseImage','getLocalImgData'] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
jsApiList: [
//所有要调用的 API 都要加到这个列表中
'scanQRCode',
'checkJsApi',
'openLocation',
'getLocation',
'onMenuShareTimeline',
'onMenuShareAppMessage'
] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
});
}
});
wx.ready(function(){
//...
wx.onMenuShareTimeline({
title: '点击入驻xx平台',
link: url3+'?appid=${appid}&phoneNumber=${phoneNumber}',//链接
imgUrl: "http://"+url2+'/images/wechat/logo_big_04.png',//图标
success: function () {
// 用户确认分享后执行的回调函数
alert('分享到朋友圈成功');
},
cancel: function () {
// 用户取消分享后执行的回调函数
alert('你没有分享到朋友圈');
}
});
wx.onMenuShareAppMessage({
title: '点击入驻xx平台',
desc: '加入平台,杜绝资源浪费,赚超丰厚评估分红',
link: url3+'?appid=${appid}&phoneNumber=${phoneNumber}',//链接
imgUrl: "http://"+url2+'/images/wechat/logo_big_04.png',//图标
trigger: function (res) {
// 不要尝试在trigger中使用ajax异步请求修改本次分享的内容,因为客户端分享操作是一个同步操作,这时候使用ajax的回包会还没有返回
},
success: function (res) {
alert('分享给朋友成功');
},
cancel: function (res) {
alert('你没有分享给朋友');
},
fail: function (res) {
alert(JSON.stringify(res));
}
});
});
wx.error(function(res){
//...
});