share.js 4.33 KB
/**
 * 分享
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2016/3/1
 */

var $ = require('yoho-jquery');

function shareBase(options) {
    var openUrl = '';
    var defOption = {
        title: '',
        url: window.location.href,
        weixinUrl: '',
        image: '',
        desc: '',
        channel: ''
    };

    // var shareChannels = ['weibo', 'tweibo', 'qzone', 'renren', 'qq', 'douban', 'weixin'];
    var sharebox;
    var shareCon;

    defOption = $.extend(defOption, options);

    // if ($.inArray(defOption.channel, shareChannels) === -1) {
    //     tip('不存在的分享平台!');
    //     return false;
    // }

    switch (defOption.channel) {
        case 'weibo':
            openUrl = 'http://service.weibo.com/share/share.php?url=' + defOption.url + '&title=' +
                defOption.title + '&appkey=3739328910&searchPic=true&pic=https:' + defOption.image;
            break;
        case 'tweibo':
            openUrl = 'http://share.v.t.qq.com/index.php?c=share&a=index&url=' + defOption.url + '&title=' +
                defOption.title + '&appkey=c0af9c29e0900813028c2ccb42021792&pic=https:' + defOption.image;
            break;
        case 'qzone':
            openUrl = 'http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=' + defOption.url + '&title=' +
                defOption.title + '&desc=&summary=' + defOption.desc + '&site=YOHO!有货&pics=https:' + defOption.image;
            break;
        case 'renren':
            openUrl = 'http://widget.renren.com/dialog/share?resourceUrl=' + defOption.url + '&srcUrl=' +
                defOption.url + '&desc=' + defOption.desc + '&title=' + defOption.title + '&description=' +
                defOption.desc + '&pic=https:' + defOption.image;
            break;
        case 'qq':
            openUrl = 'http://connect.qq.com/widget/shareqq/index.html?url=' + defOption.url + '&desc=' +
                defOption.desc + '&title=' + defOption.title.replace('%', '') + '&desc=&summary=' +
                defOption.desc + '&site=YOHO!有货&pics=http:' + defOption.image;
            break;
        case 'weixin':
            // console.log(defOption.weixinUrl)
            if (defOption.weixinUrl.indexOf('http:') < 0) {
                defOption.weixinUrl = 'http:' + defOption.weixinUrl;
            }
            openUrl = 'http://s.jiathis.com/qrcode.php?url=' + defOption.weixinUrl + '&desc=' +
                defOption.desc + '&title=' + defOption.title + '&description=' +
                defOption.desc + '&pic=' + defOption.image;
            break;
        case 'douban':
            openUrl = 'http://www.douban.com/share/service?href=' + defOption.url + '&text=' +
                defOption.desc + '&image=' + defOption.image + '&title=' + defOption.title + '&comment=';
            break;
        default:
    }

    if (defOption.channel === 'weixin') {
        if (!defOption.self) {
            return;
        }
        sharebox = defOption.self.closest('.share-to').find('.weixin-share-box');

        if (sharebox.length > 0) {
            shareCon = '<i class="top"></i><div class="con"><p class="pic"><img src="' +
            openUrl + '" /></p><h2>分享到微信朋友圈</h2><p class="w">打开微信使用“扫一扫“<br/>即可将网页分享到我的朋友圈。</p></div>';
            sharebox.find('div').length > 0 ? sharebox.show() : sharebox.html(shareCon).show();
        }
    } else {
        window.open(encodeURI(openUrl));
    }
}

function share(channel, self) {
    var title = document.title.replace(/(^\s*)|(\s*$)/g, '');
    var desc = $('#share-desc').val();
    var image = $('#share-img').val();
    var weixinUrl = $('#weixin-url').val();


    if (channel === 'weibo' || channel === 'tqq') {
        shareBase({
            channel: channel,
            title: title,
            image: image
        });
    } else {
        shareBase({
            channel: channel,
            title: title,
            desc: desc,
            image: image,
            self: self,
            weixinUrl: weixinUrl
        });
    }
}

$('.share-wrapper').on('click', 'i', function() {
    var $el = $(this),
        type = $el.data('type');

    share(type);
});

$('.weixin').mouseenter(function() {
    var $el = $(this),
        type = $el.data('type');

    share(type, $el);
});

$('.weixin').mouseleave(function() {
    console.log(2);
    $('.weixin-share-box').hide();
});