微信分享朋友圈并記錄分享次數(shù)方法介紹
1.引入JS文件
2.通過(guò)config接口注入權(quán)限驗(yàn)證配置
3.通過(guò)ready接口處理成功驗(yàn)證
4.通過(guò)error接口處理失敗驗(yàn)證
JSDK檔說(shuō)明:https://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html
(1) <script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
(2)頁(yè)面加入獲取webconfig驗(yàn)證信息的值
<?php
$url=dirname(dirname(dirname(dirname(dirname(dirname(dirname(__FILE__)))))));
$url=$url.'/addons/lb_vote/jssdk.php';
include $url;
$jsdk=new JSSDK('wxa3816b432f7291ba','e469db86bec9661650362dc2f9df8956');
$signPackage = $jsdk->GetSignPackage();
?>
(3)驗(yàn)證config
wx.config({
debug: false,
appId:'<?php echo $signPackage["appId"];?>', // 必填,公眾號(hào)的唯一標(biāo)識(shí)
timestamp:<?php echo $signPackage["timestamp"];?>, // 必填,生成簽名的時(shí)間戳
nonceStr: '<?php echo $signPackage["nonceStr"];?>', // 必填,生成簽名的隨機(jī)串
signature:'<?php echo $signPackage["signature"];?>',// 必填,簽名,見(jiàn)附錄1
jsApiList: ['checkJsApi','onMenuShareTimeline'] //
});
(4) 微信分享到朋友圈接口
wx.ready(function(){
wx.onMenuShareTimeline({
title: '測(cè)試分享朋友圈功能', // 分享標(biāo)題
link: "{php echo 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];}", // 分享鏈接
imgUrl: '{php echo $_W['siteroot'];}{$photo}', // 分享圖標(biāo)
success: function () {
// 用戶(hù)確認(rèn)分享后執(zhí)行的回調(diào)函數(shù) 記錄分享的次數(shù)
$.ajax({
url:"{php echo $this->createMobileUrl('Index',array('op'=>'share'))}",
type:'post',
data:"id="+{$userinfo['id']}+"&rid="+{$userinfo['rid']},
dataType:'json',
success:function(data){
if(data.flags==1){
alert(data.msg);
}else if(data.flags==2){
alert(data.msg);
location.href="{php echo $this->createMobileUrl('Index',array('op'=>'display','id'=>$id))}";
}
}
});
},
cancel: function () {
// 用戶(hù)取消分享后執(zhí)行的回調(diào)函數(shù)
alert('取消分享成功!');
}
});
});
(5)驗(yàn)證錯(cuò)誤時(shí)執(zhí)行的函數(shù)
wx.error(function(res){
alert(res);
});
(6)PHP端更新數(shù)據(jù)庫(kù),記錄分享次數(shù) 返回處理信息給用戶(hù)
if($op=='share'){
$voteinfo=pdo_fetch("SELECT *FROM ".tablename('lb_vote_info')." WHERE rid = :rid and id=:id and uniacid=:uniacid and pass=:pass", array(':rid' => $_GPC['rid'],':uniacid'=>$_W['uniacid'],':pass'=>1,'id'=>$_GPC['id']));
$sharenum=intval($voteinfo['sharenum'])+1;
$data=array(
'sharenum'=>$sharenum,
);
$res=pdo_update('lb_vote_info', $data, array('id' =>$_GPC['id'],'uniacid'=>$_W['uniacid'],'rid'=>$_GPC['rid']));
if(!empty($res)){
$msg['msg']='已分享到朋友圈!';
$msg['flags']=2;
echo json_encode($msg);
}else{
$msg['msg']='分享失敗!';
$msg['flags']=1;
echo json_encode($msg);
}
}