Last active
July 22, 2016 11:11
-
-
Save aslijiasheng/15ca0af293bbb972447c68c677a5761a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script> | |
<script type="text/javascript"> | |
var accountID = "<?php echo $data['share']['accountID'];?>";//用户账号 | |
var newsID = "<?php echo $data['share']['newsID'];?>";//文章id | |
var reg = "<?php echo $data['share']['reg'];?>"; | |
wx.config({ | |
debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。 | |
appId: "<?php echo $data['signPackage']['appId'];?>", // 必填,公众号的唯一标识 | |
timestamp: "<?php echo $data['signPackage']['timestamp'];?>", // 必填,生成签名的时间戳 | |
nonceStr: "<?php echo $data['signPackage']['nonceStr'];?>", // 必填,生成签名的随机串 | |
signature: "<?php echo $data['signPackage']['signature'];?>",// 必填,签名,见附录1 | |
jsApiList: ['onMenuShareTimeline','onMenuShareAppMessage','onMenuShareQQ'] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2 | |
}); | |
wx.error(function(res){ | |
alert(res.errMsg); | |
// config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。 | |
}); | |
wx.ready(function(){ | |
// 微信分享的数据 | |
var wxData = { | |
title: "<?php echo $data['share']['title'];?>", // 分享标题 | |
desc: "<?php echo $data['share']['desc'];?>", // 分享描述 | |
link: "<?php echo $data['share']['link'];?>", // 分享链接 | |
imgUrl: "<?php echo $data['share']['img'];?>", // 分享图标 | |
type: '', // 分享类型,music、video或link,不填默认为link | |
dataUrl: '', // 如果type是music或video,则要提供数据链接,默认为空 | |
}; | |
// 分享的回调 | |
var wxCallbacks = { | |
// 分享操作开始之前 | |
ready:function () { | |
alert(resp.errMsg); | |
// 你可以在这里对分享的数据进行重组 | |
}, | |
// 分享被用户自动取消 | |
cancel:function (resp) { | |
alert(resp.errMsg); | |
// 你可以在你的页面上给用户一个小Tip,为什么要取消呢? | |
}, | |
// 分享失败了 | |
fail:function (resp) { | |
alert(resp.errMsg); | |
// 分享失败了,是不是可以告诉用户:不要紧,可能是网络问题,一会儿再试试? | |
}, | |
// 分享成功 | |
confirm:function (resp) { | |
alert(resp.errMsg); | |
// 分享成功了,我们是不是可以做一些分享统计呢? | |
}, | |
// 整个分享过程结束 | |
all:function (resp) { | |
alert(resp.errMsg); | |
// 如果你做的是一个鼓励用户进行分享的产品,在这里是不是可以给用户一些反馈了? | |
} | |
}; | |
wx.onMenuShareAppMessage(wxData, wxCallbacks); | |
// // config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中。 | |
// wx.onMenuShareTimeline({ | |
// title: "<?php echo $data['share']['title'];?>", // 分享标题 | |
// link: "<?php echo $data['share']['link'];?>", // 分享链接 | |
// imgUrl: "<?php echo $data['share']['img'];?>", // 分享图标 | |
// accountID:"<?php echo $data['share']['accountID'];?>",//用户账号 | |
// newsID:"<?php echo $data['share']['newsID'];?>",//文章id | |
// reg:"<?php echo $data['share']['reg'];?>", | |
// success: function () { | |
// // 用户确认分享后执行的回调函数 | |
// if(reg == 'true') | |
// { | |
// reg(accountID,newsID); | |
// } | |
// }, | |
// cancel: function () { | |
// // 用户取消分享后执行的回调函数 | |
// } | |
// }); | |
// wx.onMenuShareQQ({ | |
// title: "<?php echo $data['share']['title'];?>", // 分享标题 | |
// desc: "<?php echo $data['share']['desc'];?>", // 分享描述 | |
// link: "<?php echo $data['share']['link'];?>", // 分享链接 | |
// imgUrl: "<?php echo $data['share']['img'];?>", // 分享图标 | |
// accountID:"<?php echo $data['share']['accountID'];?>",//用户账号 | |
// newsID:"<?php echo $data['share']['newsID'];?>",//文章id | |
// reg:"<?php echo $data['share']['reg'];?>", | |
// success: function () { | |
// // 用户确认分享后执行的回调函数 | |
// if(reg == 'true') | |
// { | |
// reg(accountID,newsID); | |
// } | |
// }, | |
// cancel: function () { | |
// // 用户取消分享后执行的回调函数 | |
// } | |
// }); | |
// wx.onMenuShareAppMessage({ | |
// title: '<?php echo $data['share']['title'];?>', // 分享标题 | |
// desc: '<?php echo $data['share']['desc'];?>', // 分享描述 | |
// link: '<?php echo $data['share']['link'];?>', // 分享链接 | |
// imgUrl: '<?php echo $data['share']['img'];?>', // 分享图标 | |
// type: '', // 分享类型,music、video或link,不填默认为link | |
// dataUrl: '', // 如果type是music或video,则要提供数据链接,默认为空 | |
// success: function (res) { | |
// // 用户确认分享后执行的回调函数 | |
// if(res.errMsg == 'sendAppMessage:ok') | |
// { | |
// $.ajax({ | |
// 'url':'/user/account-share', | |
// 'dataType':'json', | |
// 'type':'post', | |
// 'data':{'newsID':newsID,'accountID':accountID}, | |
// 'success':function(responseJSON){ | |
// if(responseJSON.status=='success') { | |
// alert('操作成功'); | |
// $(obj).attr('class',iClass); | |
// } | |
// } | |
// }) | |
// } | |
// }, | |
// cancel: function (res) { | |
// // 用户取消分享后执行的回调函数 | |
// if(res.errMsg == 'sendAppMessage:cancel') | |
// { | |
// return; | |
// } | |
// } | |
// }); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment