步骤一:绑定域名
先登录微信公众平台进入“公众号设置”的“功能设置”里填写“JS接口安全域名”。
步骤二:引入JS文件
在需要调用JS接口的页面引入如下JS文件:http://res.wx.qq.com/open/js/jweixin-1.0.0.js
步骤三:通过config接口注入权限验证配置
//获取access_token
$url='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxf7e7d834bd3687
&secret=450633ea76e5be5b8f70a25c1ce619';
$ch=curl_init();
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($ch,CURLOPT_URL,$url);
$json=curl_exec($ch);
curl_close($ch);
$arr=json_decode($json,1);
//获取ticket
$url='https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$arr['access_token'].'&type=jsapi';
$ch=curl_init();
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($ch,CURLOPT_URL,$url);
$json=curl_exec($ch);
curl_close($ch);
$arr=json_decode($json,1);
js代码
//生成签名的随机串
var createNonceStr=Math.random().toString(36).substr(2, 15);
//生成签名的时间戳
var createTimeStamp=parseInt(new Date().getTime() / 1000) + '';
//签名生成
var str='jsapi_ticket='+''+'&noncestr='+createNonceStr+
'×tamp='+createTimeStamp+'&url='+location.href.split('#')[0];
shaObj = new jsSHA(str, 'TEXT');
var signature =shaObj.getHash('SHA-1', 'HEX');
wx.config({
debug: false,
appId: 'wxf7e7d834bd3687',
timestamp: createTimeStamp,
nonceStr: createNonceStr,
signature: signature,
jsApiList: [
'onMenuShareAppMessage',
'onMenuShareTimeline',
'hideMenuItems',
'showMenuItems',
'hideAllNonBaseMenuItem',
'showAllNonBaseMenuItem',
'hideOptionMenu',
'showOptionMenu',
'closeWindow',
'scanQRCode'
]});
步骤四:通过ready接口处理成功验证
<script>
wx.ready(function () {
// 2.2 监听“分享到朋友圈”按钮点击、自定义分享内容及分享结果接口
document.querySelector('#onMenuShareTimeline').onclick = function () {
wx.onMenuShareTimeline({
title: '分享标题',
link: '分享的url,以http或https开头',
imgUrl: '分享图标的url,以http或https开头',
trigger: function (res) {
alert('用户点击分享到朋友圈');
},
success: function (res) {
alert('已分享');
},
cancel: function (res) {
alert('已取消');
},
fail: function (res) {
alert(JSON.stringify(res));
}
alert('已注册获取“分享到朋友圈”状态事件');
});
$(".wx_zf").show();
$(".start_bottom").hide();
};
function decryptCode(code, callback) {
$.getJSON('/jssdk/decrypt_code.php?code=' + encodeURI(code), function (res) {
if (res.errcode == 0) {
codes.push(res.code);
}
});
}
var shareData = {
title: '分享标题',
desc:'分享描述',
link: '分享的url,以http或https开头',
imgUrl: '分享图标的url,以http或https开头'
};
wx.onMenuShareAppMessage(shareData);
wx.onMenuShareTimeline(shareData);
});
</script>
步骤五:通过error接口处理失败验证
<script>
wx.error(function(res){
alert(res.errMsg);
});
</script>
未经允许不得转载:任鹏个人博客 » 微信中通过JS代码分享到朋友圈实例代码
最新评论
Forex wiki. https://lt.forex-stock-bitcoin-brokers.com
Magnificent items from you, man. I have take note your stuff
Following on from the 3rd March Meetings held by economic de
It is remarkable, rather valuable message dfgdlfg2131.32
一般都会有一个沙盒期的,过了沙盒期就会慢慢放出来
百度不收录是应为是新站的原因吗?
The spike in consumer prices that left inflation at a four-d