1. 程式人生 > >微信分享網頁時自定義標題、描述和圖片

微信分享網頁時自定義標題、描述和圖片

用微信開啟一個網頁,選擇右上角的“傳送給朋友”後,收到的訊息是這樣的:

而為了推廣效果,我們更希望能自定義標題、描述和圖片,效果如下圖:

下面介紹下怎樣來實現這個功能

複製程式碼
<script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js" ></script>
<script type="text/javascript">
    var imgUrl = 'http://dev.mymax.cn/affix/manmanhouse/images/share.jpg'; 
    var lineLink 
= 'http://dev.mymax.cn/affix/manmanhouse/index.html'; var descContent = '人類天生就愛玩遊戲,遊戲為什麼吸引人?如果藉助遊戲的規律,是否可以讓工作學習變得更精彩?'; var shareTitle = '遊戲化學習工作坊第四站-北京清華科技園'; var appid = ''; $.ajax({ type : 'POST', url : "http://dev.mymax.cn/running/comm/weixin/open/jsSDKConfig", //這個地址並非通用且長期有效,請去微信官方檢視文件,並自行配置 dataType :
"json", data:{url:window.location.href}, success : function(response){ var appId = response.s.appId; var timestamp = response.s.timeStamp; var nonceStr = response.s.nonceStr; var signature = response.s.signature; wx.config({ debug:
false, appId: appId, timestamp: timestamp, nonceStr: nonceStr, signature: signature, jsApiList: [ 'checkJsApi', 'onMenuShareTimeline', 'onMenuShareAppMessage' ] }); wx.ready(function() { wx.onMenuShareTimeline({ title: shareTitle, // 分享標題 link: lineLink, // 分享連結,該連結域名或路徑必須與當前頁面對應的公眾號JS安全域名一致 imgUrl: imgUrl, // 分享圖示 success: function () { // 使用者確認分享後執行的回撥函式 }, cancel: function () { // 使用者取消分享後執行的回撥函式 } }); wx.onMenuShareAppMessage({ title: shareTitle, // 分享標題 desc: descContent, // 分享描述 link: lineLink, // 分享連結,該連結域名或路徑必須與當前頁面對應的公眾號JS安全域名一致 imgUrl: imgUrl, // 分享圖示 type: '', // 分享型別,music、video或link,不填預設為link dataUrl:'' , // 如果type是music或video,則要提供資料鏈接,預設為空 success: function () { // 使用者確認分享後執行的回撥函式 }, cancel: function () { // 使用者取消分享後執行的回撥函式 } }); }); }, error:function(response){ window.parent.growl("刪除失敗["+response.responseText+"]!","error"); } }); </script>
複製程式碼