Blame view

public/js/common/share.js 4.65 KB
刘传洋 authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/**
 * 分享
 * @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: ''
    };
刘传洋 authored
19
    var shareChannels = ['weibo', 'tweibo', 'qzone', 'renren', 'qq', 'douban', 'weixin'];
刘传洋 authored
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    var sharebox;
    var shareCon = '<em><i></i></em>';

    defOption = $.extend(defOption, options);

    if (defOption.image && defOption.image.substr(0, 2) === '//') {
        defOption.image = 'http:' + defOption.image;
    }

    if (defOption.weixinUrl && defOption.weixinUrl.substr(0, 2) === '//') {
        defOption.weixinUrl = 'http:' + defOption.weixinUrl;
    }

    if (defOption.url && defOption.url.substr(0, 2) === '//') {
        defOption.url = 'http:' + defOption.url;
    }

    if ($.inArray(defOption.channel, shareChannels) === -1) {
        alert('不存在的分享平台!');
        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=' + 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=' + 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!BUY 有货&pics=' + 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=' + 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!BUY 有货&pics=' + defOption.image;
            break;
        case 'weixin':
            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 = '//www.douban.com/share/service?href=' + defOption.url + '&text=' +
                defOption.desc + '&image=' + defOption.image + '&title=' + defOption.title + '&comment=';
            break;
    }

    if (defOption.channel === 'weixin') {
        if (!defOption.self) {
            return;
        }

        sharebox = defOption.self.closest('.share-to').find('.weixin-share-box');

        if (sharebox.length > 0) {
            shareCon += '<div class="con"><h2>分享到微信朋友圈</h2><p class="pic">' +
            '<img src="' + openUrl + '" /></p><p class="w">打开微信,点击底部得“发现”,使用<br/>“扫一扫“即可将网页分享到我的朋友圈。</p>' +
            '<a href="javascript:void(0)" class="close">x</a></div>';
            sharebox.find('div').length > 0 ? sharebox.show() : sharebox.html(shareCon).show();

            sharebox.find('.close').click(function() {
                $(this).closest('.weixin-share-box').hide();
            });
        }
    } 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'),
        $weixinShareBox = $('.weixin-share-box');

    if (type === 'weixin') {
        share(type, $el);
    } else {
        $weixinShareBox.hide();
        share(type);
    }
});