js實現QQ、微信、新浪微博分享功能


微信分享需要手機掃描二維碼,需要對url進行編碼。在https協議下,掃描二維碼時,瀏覽器打不開可能時安全證書導致的。

var shareModel = {

        /**
         * 分享QQ好友
         * @param  {[type]} title [分享標題]
         * @param  {[type]} url   [分享url鏈接,默認當前頁面鏈接]
         * @param  {[type]} pic   [分享圖片]
         * @return {[type]}       [description]
         */
        shareQQ: function (url, title, pic) {
            var param = {
                url: url || window.location.href,
                desc: '', /*分享理由*/
                title : title || '', /*分享標題(可選)*/
                summary : '',/*分享描述(可選)*/
                pics : pic || '',/*分享圖片(可選)*/
                flash : '', /*視頻地址(可選)*/
                site: '' /*分享來源 (可選) */
            };
            var s = [];
            for (var i in param) {
                s.push(i + '=' + encodeURIComponent(param[i] || ''));
            }
            var targetUrl = "http://connect.qq.com/widget/shareqq/iframe_index.html?" + s.join('&') ;
            window.open(targetUrl, 'qq', 'height=520, width=720');
        },

       /**
         * 微信分享
         * @return {[type]} [description]
         */
        weixin: function () {
            var url = window.location.href,
                encodePath = encodeURIComponent(url),
                targetUrl = 'http://qr.liantu.com/api.php?text=' + encodePath;
            window.open(targetUrl, 'weixin', 'height=320, width=320');
        },

       /**
         * 分享新浪微博
         * @param  {[type]} title [分享標題]
         * @param  {[type]} url   [分享url鏈接,默認當前頁面]
         * @param  {[type]} pic   [分享圖片]
         * @return {[type]}       [description]
         */
        sinaWeiBo: function (title, url, pic) {
            var param = {
                url: url || window.location.href,
                type: '3',
                count: '1', /** 是否顯示分享數,1顯示(可選)*/
                appkey: '', /** 您申請的應用appkey,顯示分享來源(可選)*/
                title: '', /** 分享的文字內容(可選,默認為所在頁面的title)*/
                pic: pic || '', /**分享圖片的路徑(可選)*/ 
                ralateUid:'', /**關聯用戶的UID,分享微博會@該用戶(可選)*/
                rnd: new Date().valueOf()
            }
            var temp = [];
            for( var p in param ) {
                temp.push(p + '=' +encodeURIComponent( param[p ] || '' ) )
            }
            var targetUrl = 'http://service.weibo.com/share/share.php?' + temp.join('&');
            window.open(targetUrl, 'sinaweibo', 'height=430, width=400');
        }
};

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM