新版微信 weixin js sdk-demo 分享接口自定义标题

一直以来都是用非官方的分享接口,最近腾讯推出了js分享接口,相信很多用户都需要紧急修改。

本人运营的平台也都做了更新,相信按照官方的文档都能弄好分享。只是有些游戏类的要根据相关参数来设置就会麻烦不少。我们有一个游戏,分享的时候会根据玩的分数自定义分享标题,这样官方的文档就不好用了。

我附上我们实现的代码,方便更多从事微信公众平台开发的朋友。

<script>
    wx.config({
        debug: false,
        appId: '<?php echo $signPackage["appId"];?>',
        timestamp: <?php echo $signPackage["timestamp"];?>,
        nonceStr: '<?php echo $signPackage["nonceStr"];?>',
        signature: '<?php echo $signPackage["signature"];?>',
        jsApiList: [
            'onMenuShareTimeline',
            'onMenuShareAppMessage'
          // 所有要调用的 API 都要加到这个列表中
        ]
    });

        var imgUrl = 'http://wx.socialtok.com/source/modules/xiwan/template/mobile/img/share.jpg';
        var link ='http://wx.socialtok.com/{php echo $this->createMobileUrl('index')}&wxref=mp.weixin.qq.com#wechat_redirect';
        var desc = '西门子家电疯狂刷碗比赛,根本停不下来!这分数我也是醉了。';
        var title = '西门子家电疯狂刷碗比赛';
         
        function shareFriend() {
            WeixinJSBridge.invoke('sendAppMessage',{
                "title": title, // 分享标题
                "desc": desc,
                "link": link, // 分享链接
                "img_url": imgUrl// 分享图标
            }, function(res) {
                   //_report('timeline', res.err_msg);
                   //alert(res.err_msg);
                   if(res.err_msg=='send_app_msg:cancel'){
                        alert('已经取销');
                   }else if(res.err_msg=='send_app_msg:ok'){
                        //alert('分享成功'); 
                         $.post('http://wx.socialtok.com/'+"{php echo $this->createMobileUrl('updatecount')}",{},function(result){
                            //alert('分享成功');
                            alert(result);
                        });
                   }
            });
        }
        function shareTimeline() {
            WeixinJSBridge.invoke('shareTimeline',{
                "title": title, // 分享标题
                "desc": desc,
                "link": link, // 分享链接
                "img_url": imgUrl// 分享图标
            }, function(res) {
                   //_report('timeline', res.err_msg);
                   if(res.err_msg=='share_timeline:cancel'){
                        alert('已经取销');
                   }else if(res.err_msg=='share_timeline:ok'){
                        $.post('http://wx.socialtok.com/'+"{php echo $this->createMobileUrl('updatecount')}",{},function(result){
                            //alert('分享成功');
                            alert(result);
                        }); 
                   }
            });
        }

        // 当微信内置浏览器完成内部初始化后会触发WeixinJSBridgeReady事件。
        document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() {
            // 发送给好友
            WeixinJSBridge.on('menu:share:appmessage', function(argv){
                shareFriend();
            });
            // 分享到朋友圈
            WeixinJSBridge.on('menu:share:timeline', function(argv){
                if ($("#score").val()>0) { title = "我创造了15秒刷洗"+$("#score").val()+"个盘子,朋友们一起来挑战吧,还有大奖等你哦!";};  
                shareTimeline();
            });
        }, false);
</script>

这样分享后,用户的成绩就会成绩来显示了。

附上我们的分享截屏

 

posted @ 2015-02-05 15:55  乐享工作室  阅读(467)  评论(0)    收藏  举报