14.4.1 分享到朋友圈后查看内容
分享到朋友圈的接口开发中,需要先设置分享参数,包括标题、图标URL以及链接URL。这些参数用于分享时显示的内容。
另外,在用户确认分享后执行的回调函数中,应执行跳转功能。这样就达到了分享后查看内容的效果。相关代码如下。
分享后的页面的代码如下。
1 <?php 2 require_once('wxjssdk.class.php'); 3 $weixin = new class_weixin; 4 $signPackage = $weixin->GetSignPackage; 5 6 $news = array("Title" =>"微信公众平台开发实践", "Description"=>"本书共分10章,案例 程序采用广泛流行的PHP、MySQL、XML、CSS、JavaScript、HTML5等程序语言及数据库实现。", "PicUrl" =>'http:// images.cnitblog.com/i/340216/201404/301756448922305.jpg', "Url" =>'http:// www.cnblogs.com/txw1958/p/weixin-development-best-practice.html'); 7 ?> 8 <!DOCTYPE html> 9 <html> 10 <head> 11 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 12 <meta name="viewport" content="width=device-width, initial-scale=1.0, maxi mum-scale=2.0, minimum-scale=1.0, user-scalable=no" /> 13 <meta name="format-detection" content="telephone=no" /> 14 <title>秘密信件</title> 15 <meta name="viewport" content="width=device-width, initial-scale=1, user-sc alable=0"> 16 <link rel="stylesheet" href="http:// demo.open.weixin.qq.com/jssdk/css/style. css"> 17 </head> 18 <body ontouchstart=""> 19 点右上角分享后查看 20 </body> 21 <script src="https:// res.wx.qq.com/open/js/jweixin-1.1.0.js"></script> 22 <script> 23 wx.config({ 24 debug: false, 25 appId: '<?php echo $signPackage["appId"];?>', 26 timestamp: <?php echo $signPackage["timestamp"];?>, 27 nonceStr: '<?php echo $signPackage["nonceStr"];?>', 28 signature: '<?php echo $signPackage["signature"];?>', 29 // url:'<?php echo $signPackage["url"];?>', 30 jsApiList: [ 31 // 所有要调用的 API 都要加到这个列表中 32 'checkJsApi', 33 'onMenuShareTimeline', 34 'onMenuShareAppMessage' 35 ] 36 }); 37 </script> 38 <script> 39 wx.ready(function { 40 wx.checkJsApi({ 41 jsApiList: [ 42 'onMenuShareTimeline', 43 'onMenuShareAppMessage' 44 ], 45 success: function (res) { 46 } 47 }); 48 49 wx.onMenuShareTimeline({ 50 title: '<?php echo $news['Title'];?>', 51 link: '<?php echo $news['Url'];?>', 52 imgUrl: '<?php echo $news['PicUrl'];?>', 53 trigger: function (res) { 54 // alert('用户点击分享到朋友圈'); 55 }, 56 success: function (res) { 57 // alert('已分享'); 58 window.location.href = "shared.php"; 59 }, 60 cancel: function (res) { 61 // alert('已取消'); 62 }, 63 fail: function (res) { 64 // alert(JSON.stringify(res)); 65 } 66 }); 67 68 }); 69 70 wx.error(function (res) { 71 alert(res.errMsg); 72 }); 73 </script> 74 </html>
分享后的页面的代码如下。
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0, maxi mum-scale=2.0, minimum-scale=1.0, user-scalable=no" /> <meta name="format-detection" content="telephone=no" /> <title>道歉信</title> <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=0"> <link rel="stylesheet" href="http:// demo.open.weixin.qq.com/jssdk/css/style.css"> </head> <body ontouchstart=""> 这是分享后查看的内容 </body> </html>
分享到朋友圈时,效果如图14-2所示。
如果想获取用户信息及分享次数,则可以添加网页授权,当用户分享的时候使用回调将用户信息传入后台接口中并记录下来。