发布于 4年前

php、js如何自定义微信分享朋友圈的标题和图片

以在Thinkphp中为例,参考微信文档: https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115

首先确保引入了 EasyWechat 扩展包 php代码

<?php 

use EasyWeChat\Foundation\Application;

class Index {
    function test() {
        $options = array(
            'app_id' => "wx85******",
            'secret' => "bcm**********"
        );

        $app = new Application($options);
        $js = $app->js;
        $wechatConfig = $js->config(array('onMenuShareTimeline','onMenuShareAppMessage','updateAppMessageShareData','updateTimelineShareData '));

        $this->assign('wechatConfig',$wechatConfig);
    }
}

?>

JS代码

<script src="http://res2.wx.qq.com/open/js/jweixin-1.4.0.js "></script>

<script>

wx.config({:$wechatConfig});

wx.ready(function(){

  // 获取“分享到朋友圈”按钮点击状态及自定义分享内容接口(即将废弃)
  wx.onMenuShareTimeline({
      title: '', 
      link: '', 
      imgUrl: '',
      success: function () {
      // 用户点击了分享后执行的回调函数
    },
  });

  // 获取“分享给朋友”按钮点击状态及自定义分享内容接口(即将废弃)
  wx.onMenuShareAppMessage({
    title: '',
    desc: '',
    link: '', 
    imgUrl: '',
    type: '', // 分享类型,music、video或link,不填默认为link
    dataUrl: '', // 如果type是music或video,则要提供数据链接,默认为空
    success: function () {
    // 用户点击了分享后执行的回调函数
    }
  });

  // 自定义“分享给朋友”及“分享到QQ”按钮的分享内容(1.4.0)
    wx.updateAppMessageShareData({ 
        title: '',
        desc: '',
        link: '', 
        imgUrl: '',
        success: function () {

        }
    })

    // 自定义“分享到朋友圈”及“分享到QQ空间”按钮的分享内容(1.4.0)
    //试着这个不管用,所以又加上上面两个即将废弃的接口了
    wx.updateTimelineShareData({ 
        title: ', 
        link: '', 
        imgUrl: '',
        success: function () {

        }
    })

});

wx.error(function (res) {
    alert(res.errMsg);
});

</script>
©2020 edoou.com   京ICP备16001874号-3