通过qq空间、qq聊天、新浪微博和微信二维码分享平台提供的接口,实现把网页中对应的图片、标题、描述的信息参数用javascript获取后传进接口中,实现一键分享。

 

使用到的接口(测试时需要登录,网址和图片必须是公网的,不能localhost,QQ图片不能太宽,太宽标题描述会undefiend):

 

1.分享到QQ空间接口:

https://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey

?url=你的网址

&sharesource=qzone

&title=你的分享标题

&pics=你的分享图片

&summary=你的分享描述信息

 

2.分享给QQ好友接口:

 

http://connect.qq.com/widget/shareqq/index.html

?url=你的分享网址

&sharesource=qzone

&title=你的分享标题

&pics=你的分享图片地址

&summary=你的分享描述

&desc=你的分享简述

3.分享到新浪微博接口:

http://service.weibo.com/share/share.php

?url=你的分享网址

&sharesource=weibo

&title=你的分享标题

&pic=你的分享图片

&appkey=你的key需要在新浪微博开放平台中申请

一键分享代码参考如下:

<div>分享到:</div> 
<div onclick="shareTo('qzone')">     
    <img src=".png" width="32"> 
</div> 
<div onclick="shareTo('qq')">     
    <img src=".png" width="32"> 
</div> 
<div onclick="shareTo('sina')">     
    <img src=".png" width="32"> 
</div> 
<div onclick="shareTo('wechat')">     
    <img src="" width="32"> 
</div>
function shareTo(types){
 
    var title,imageUrl,url,description,keywords;
 
    //获取文章标题
    title = document.title;
 
    //获取网页中内容的第一张图片地址作为分享图
    //imageUrl = document.images[0].src;
    imageUrl = document.getElementById("pcdetails").getElementsByTagName("img")[0];
    //当内容中没有图片时,设置分享图片为网站logo
    if(typeof imageUrl == 'undefined'){
        imageUrl = 'https://'+window.location.host+'/static/images/logo.png';
    } else {
        imageUrl = imageUrl.src;
    }
 
    //获取当前网页url
    url = document.location.href;
 
    //获取网页描述
    description = document.querySelector('meta[name="description"]').getAttribute('content');
 
    //获取网页关键字
    keywords = document.querySelector('meta[name="keywords"]').getAttribute('content');
 
    //qq空间接口的传参
    if(types=='qzone'){
        window.open('https://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url='+url+'&sharesource=qzone&title='+title+'&pics='+imageUrl+'&summary='+description);
    }
    //新浪微博接口的传参
    if(types=='sina'){
        window.open('http://service.weibo.com/share/share.php?url='+url+'&sharesource=weibo&title='+title+'&pic='+imageUrl+'&appkey=2706825840');
    }
    //qq好友接口的传参
    if(types == 'qq'){
        window.open('http://connect.qq.com/widget/shareqq/index.html?url='+url+'&sharesource=qzone&title='+title+'&pics='+imageUrl+'&summary='+description+'&desc='+keywords);
    }
    
    
}

使用说明:

                纯js使用时id改成自己站点的id选择器来获取。如果调试不成功,可以尝试本站中的分享功能,分享时会打开新窗口,那条链接是最终要分享的,已经拼接好的参数链接,可以复制进行比对参考。


评论(0条)

刀客源码 游客评论