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所示。
如果想獲取用戶信息及分享次數,則可以添加網頁授權,當用戶分享的時候使用回調將用戶信息傳入後台接口中並記錄下來。