微信之分享到朋友圈并记录分享次数

1.引入JS文件
2.通过config接口注入权限验证配置
3.通过ready接口处理成功验证
4.通过error接口处理失败验证
JSDK档说明:https://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html
(1)
(2)页面加入获取webconfig验证信息的值

$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)验证config
wx.config({
debug: false,
appId:'', // 必填,公众号的唯一标识
timestamp:, // 必填,生成签名的时间戳
nonceStr: '', // 必填,生成签名的随机串
signature:'',// 必填,签名,见附录1
jsApiList: ['checkJsApi','onMenuShareTimeline'] //
});

(4) 微信分享到朋友圈接口

wx.ready(function(){
     wx.onMenuShareTimeline({
    title: '测试分享朋友圈功能', // 分享标题
    link: "{php echo 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];}", // 分享链接
    imgUrl: '{php echo $_W['siteroot'];}{$photo}', // 分享图标
    success: function () { 
        // 用户确认分享后执行的回调函数 记录分享的次数
     $.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 () { 
        // 用户取消分享后执行的回调函数
        alert('取消分享成功!');
    }
      });


});

(5)验证错误时执行的函数


    wx.error(function(res){

     alert(res);

});

(6)PHP端更新数据库,记录分享次数 返回处理信息给用户

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);
}
}

你可能感兴趣的:(PHP)