一鍵分享到QQ空間、QQ好友、新浪微博、微信代碼

  經過qq空間qq聊天新浪微博和微信二維碼分享平臺提供的接口實現把網頁中對應的圖片、標題、描述的信息參數用javascript獲取後傳進接口中,實現一鍵分享javascript

        使用到的接口(測試時須要登陸,網址和圖片必須是公網的,不能localhost):
php

            1.分享到QQ空間接口https://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=你的網址&sharesource=qzone&title=你的分享標題&pics=你的分享圖片&summary=你的分享描述信息html

            2.分享給QQ好友接口http://connect.qq.com/widget/shareqq/index.html?url=你的分享網址&sharesource=qzone&title=你的分享標題&pics=你的分享圖片地址&summary=你的分享描述&desc=你的分享簡述java

            3.分享到新浪微博接口http://service.weibo.com/share/share.php?url=你的分享網址&sharesource=weibo&title=你的分享標題&pic=你的分享圖片&appkey=你的key,須要在新浪微博開放平臺中申請jquery

 

一鍵分享代碼參考以下:web

html
    <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>

 

 

js

    function shareTo(stype){
        var ftit = '';
        var flink = '';
        var lk = '';
        //獲取文章標題
        ftit = $('.pctitle').text();
        //獲取網頁中內容的第一張圖片
        flink = $('.pcdetails img').eq(0).attr('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選擇器來獲取。若是調試不成功,能夠嘗試本站中的分享功能,分享時會打開新窗口,那條連接是最終要分享的,已經拼接好的參數連接,能夠複製進行比對參考。app

最終分享連接示例:編輯器

    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生成二維碼方式)

jquery生成二維碼方式:

jquery.qrcode.js生成二維碼demo.html

下載代碼:

jquery.qrcode.js生成二維碼demo.rar

分享效果點擊本站文章分享功能,效果圖以下:

一鍵分享js代碼

http://www.zixuephp.net/article-309.html

相關文章
相關標籤/搜索