一键分享到QQ空间、QQ好友、新浪微博、微信代码

JS or jQuery 5 年前 2条评论

, , ,

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

使用到的接口(测试时需要登录,网址和图片必须是公网的,不能localhost):

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 class="fl">分享到:</div> 
<div onclick="shareTo('qzone')">     
    <img src="http://zixuephp.net/static/images/qqzoneshare.png" width="30"/> 
</div> 
<div onclick="shareTo('qq')">     
    <img src="http://zixuephp.net/static/images/qqshare.png" width="32"/> 
</div> 
<div onclick="shareTo('sina')">     
    <img src="http://zixuephp.net/static/images/sinaweiboshare.png" width="36"/> 
</div> 
<div onclick="shareTo('wechat')">     
    <img src="http://zixuephp.net/static/images/wechatshare.png" width="32"/> 
</div>

建一个函数:

function shareTo(stype){
    var ftit = '';
    var flink = '';
    var lk = '';
    //获取文章标题
    ftit = document.title;
    //获取网页中内容的第一张图片地址作为分享图
    flink = document.images[0].src;
    if(typeof flink == 'undefined'){
        flink='';
    }
    //当内容中没有图片时,设置分享图片为网站logo
    if(flink == ''){
        lk = 'http://'+window.location.host+'/static/images/logo.png';
    }
    //如果是上传的图片则进行绝对路径拼接
    if(flink.indexOf('/uploads/') != -1) {
        lk = 'http://'+window.location.host+flink;
    }
    //百度编辑器自带图片获取
    if(flink.indexOf('ueditor') != -1){
        lk = flink;
    }
    //qq空间接口的传参
    if(stype=='qzone'){
        window.open('https://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url='+document.location.href+'?sharesource=qzone&title='+ftit+'&pics='+lk+'&summary='+document.querySelector('meta[name="description"]').getAttribute('content'));
    }
    //新浪微博接口的传参
    if(stype=='sina'){
        window.open('http://service.weibo.com/share/share.php?url='+document.location.href+'?sharesource=weibo&title='+ftit+'&pic='+lk+'&appkey=2706825840');
    }
    //qq好友接口的传参
    if(stype == 'qq'){
        window.open('http://connect.qq.com/widget/shareqq/index.html?url='+document.location.href+'?sharesource=qzone&title='+ftit+'&pics='+lk+'&summary='+document.querySelector('meta[name="description"]').getAttribute('content')+'&desc=php自学网,一个web开发交流的网站');
    }
    //生成二维码给微信扫描分享,php生成,也可以用jquery.qrcode.js插件实现二维码生成
    if(stype == 'wechat'){
        window.open('http://zixuephp.net/inc/qrcode_img.php?url=http://zixuephp.net/article-1.html');
    }
}

使用说明:

这里的如获取文章标题、文章图片、logo图片地址等一些其他信息是按照本站的规则来的,使用时需要修改成自己站点的calss或id选择器来获取。如果调试不成功,可以尝试本站中的分享功能,分享时会打开新窗口,那条链接是最终要分享的,已经拼接好的参数链接,可以复制进行比对参考。

最终分享链接示例:

1.分享到qq空间:

https://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=http://zixuephp.net/article-309.html?sharesource=qzone&title=一键分享到QQ空间、QQ好友、新浪微博、微信代码&pics=http://zixuephp.net/uploads/image/20170810/1502335815192079.png&summary=通过各自平台的开发接口,进行参数指定,进行一键分享javascript代码功能
2.分享到qq好友:

https://connect.qq.com/widget/shareqq/index.html?url=http://zixuephp.net/article-309.html?sharesource=qzone&title=一键分享到QQ空间、QQ好友、新浪微博、微信代码&pics=http://zixuephp.net/uploads/image/20170810/1502335815192079.png&summary=通过各自平台的开发接口,进行参数指定,进行一键分享javascript代码功能&desc=php自学网,一个web开发交流的网站
3.分享到新浪微博:

http://service.weibo.com/share/share.php?url=http://zixuephp.net/article-309.html?sharesource=weibo&title=一键分享到QQ空间、QQ好友、新浪微博、微信代码&pic=http://zixuephp.net/uploads/image/20170810/1502335815192079.png&appkey=2706825840&sudaref=zixuephp.net&display=0&retcode=6102#_loginLayer_1528860698455
4.分享到微信(js生成二维码方式)

    <script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
    <script src="https://cdn.bootcss.com/jquery.qrcode/1.0/jquery.qrcode.min.js"></script>
   
  <div id="qrcode"></div>
  <script>
    jQuery(function(){
      jQuery('#qrcode').qrcode("http://zixuephp.net/article-309.html");
    })
  </script>

感谢PHP自学网的分享

支付宝打赏微信打赏

如果此文对你有帮助,欢迎打赏作者。

2条评论

  1. #1
    智慧宫

    智慧宫

    4年前 Firefox 69 Windows 10 -@

    你造的轮子很好用,谢谢

    来自青海
    • 皇家元林

      皇家元林

      4年前 Google Chrome 63 Windows 7 -@

      @智慧宫 嗯嗯 其实现在的分享按钮直接链接写就行了
      这里主要是那个微信二维码生成有点特色。

      来自河北

发表评论

欢迎回来 (打开)

(必填)