公众号H5自定义分享

有时候看到别人在微信上转发的H5页面封面很好看,有标题,描述,还有一个缩略图
看看自己开发的分享出去就是一个网络地址,手里的鸡腿突然不香了~

要做这个功能很简单的,下面是官方的文档

点了会胖的链接

后端使用easywechat插件包

//实例化操作对象
use EasyWeChat\Factory;

$config = [
    'app_id' => 'wx3cf0f39249eb0exx',
    'secret' => 'f1c242f4f28f735d4687abb469072axx',
    'response_type' => 'array',
];

$app = Factory::officialAccount($config);

//实例化对象后直接在前端生成jssdk的配置
<script src="https://res.wx.qq.com/open/js/jweixin-1.4.0.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
    wx.config(<?php echo $app->jssdk->buildConfig(array('updateAppMessageShareData', 'updateTimelineShareData'), true) ?>);
</script>

//生成的配置如下
<script src="https://res.wx.qq.com/open/js/jweixin-1.4.0.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
wx.config({
    debug: true, // 请在上线前删除它
    appId: 'wx3cf0f39249eb0e60',
    timestamp: 1430009304,
    nonceStr: 'qey94m021ik',
    signature: '4F76593A4245644FAE4E1BC940F6422A0C3EC03E',
    jsApiList: ['updateAppMessageShareData', 'updateTimelineShareData']
});
</script>

前端分享代码

wx.ready(function () {

    wx.updateAppMessageShareData({
        title: "标题",
        desc: "描述XXXX",
        link: "域名链接",
        imgUrl: "图片链接",
        success: function () {
            // 设置成功
        }
    });

    wx.updateTimelineShareData({
        title: "标题",
        link: "域名链接",
        imgUrl: "图片链接",
        success: function () {
            // 设置成功
        }
    });

});
Posted in PHP