微信分享網頁連結至朋友、朋友圈、微博程式碼

antzone發表於2017-04-03

下面是一段簡單的分享連結功能的程式碼,需要的朋友可以做一下參考。

程式碼如下:

[JavaScript] 純文字檢視 複製程式碼
var lineLink = 'http://../..',
  imgUrl = 'http://../..',
  shareTitle = '頁面標題',
  descContent='內容簡介',
  appid = '';
//判斷是否支援微信js
if(typeof WeixinJsBridge == 'undefined'){
  if(document.addEventListener){
   document.addEventListener('WeixinJsBridgeReady',onBridgeReady,false);
 }
  else if(document.attachEvent){
   document.attachEvent('WeixinJsBridgeReady',onBridgeReady);
  document.attachEvent('onWeixinJsBridgeReady',onBridgeReady);
 }
}
else{
  onBridgeReady();
}
function onBridgeReady (){
  WeixinJsBridgeReady.on('menu:share:appmessage',wx_shareFriend);//分享朋友
 WeixinJsBridgeReady.on('menu:share:timeline',wx_shareTimeline);//分享到朋友圈
 WeixinJsBridgeReady.on('menu:share:weibo',wx_shareWeibo);//分享朋友
}
function wx_shareFriend (){
  WeixinJsBridge.invoke('sendAppMessage',{
   "appid":appid,
  "img_url":imgurl,
  "img_width":'640',
  "img_height":'500',
  "link":lineLink,
  "desc":descContent,
  "title":shareTitle
 },function(res){
   console.log(res.err_msg);
 });
}
function wx_shareTimeline (){
  WeixinJsBridge.invoke('sendTimeline',{
   "appid":appid,
  "img_url":imgurl,
  "img_width":'640',
  "img_height":'500',
  "link":lineLink,
  "desc":descContent,
  "title":shareTitle
 },function(res){
    console.log(res.err_msg);
 })
}  
function wx_shareWeibo (){
  WeixinJsBridge.invoke('sendWeibo',{
   "appid":appid,
  "img_url":imgurl,
  "img_width":'640',
  "img_height":'500',
  "link":lineLink,
  "desc":descContent,
  "title":shareTitle
 },function(res){
   console.log(res.err_msg);
  })
}

WeixinApi的地址是https://github.com/zxlie/WeixinApi。

相關文章