欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

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

1.引入JS文件 
2.通过config接口注入权限验证配置 
3.通过ready接口处理成功验证 
4.通过error接口处理失败验证 
JSDK档说明: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)页面加入获取webconfig验证信息的值

<?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(); 
?>
  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

(3)验证config 
wx.config({ 
debug: false, 
appId:'<?php echo $signPackage["appId"];?>', // 必填,公众号的唯一标识 
timestamp:<?php echo $signPackage["timestamp"];?>, // 必填,生成签名的时间戳 
nonceStr: '<?php echo $signPackage["nonceStr"];?>', // 必填,生成签名的随机串 
signature:'<?php echo $signPackage["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('取消分享成功!');
    }
      });


});
  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

  • 11

  • 12

  • 13

  • 14

  • 15

  • 16

  • 17

  • 18

  • 19

  • 20

  • 21

  • 22

  • 23

  • 24

  • 25

  • 26

  • 27

  • 28

  • 29

  • 30

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


    wx.error(function(res){

     alert(res);

});
  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012493556/article/details/52494114

来自  https://blog.csdn.net/u012493556/article/details/52494114

普通分类: