share.js 8.69 KB
/**
 * @author DELL
 */
/**
 * @share 分享插件
 * @author:王威
 * @date:2014-09-19
 * yohoo-share-button-sina
 * yohoo-share-button-facebook
 * yohoo-share-button-tweet
 *      fbAppId:YohoConfig.girl.fbAppId,'657464311006064',
        fbUrl:YohoConfig.girl.mainUrl+'/channel/yohood/closedialog',
 */
define('plugins/share', function(require, exports) {
    var $ = require("jquery");
    exports.init = function(info) {
        var defaults = {
            width: 800,
            height: 500,
            img: "",
            imgFunction: null,
            sinaText: "",
            sinaTextFunction: null,
            tweetText: "",
            tweetTextFunction: null,
            fbCaption: "",
            fbDescription: "",
            fbName: "",
            fbTextFunction: null,
            shareUrl: "",
            shareUrlFunction: null,
            fbAppId: "",
            fbUrl: "",
            qqText: ""
        }
        var options = $.extend(defaults, info);

        $(".yohoo-share-button-sina").unbind().on("click", function() {
            if (options.sinaTextFunction != null) {
                options.sinaText = (function() {
                    return options.sinaTextFunction.apply($(".yohoo-share-button-sina"));
                })();
            }
            options.sinaText = options.sinaText.replace(/[ ]/g, "%20")
                .replace(/[&]/g, "%26")
                .replace(/[#]/g, "%23")
                .replace(/[+]/g, "%2B");

            window.sharetitle = options.sinaText;
            if (options.imgFunction != null) {
                options.img = (function() {
                    return options.imgFunction.apply($(".yohoo-share-button-sina"));
                })();
            }
            window.shareUrl = options.img
            share();
        });
        if (options.shareUrlFunction != null) {
            options.shareUrl = (function() {
                return options.shareUrlFunction.apply({
                    sina: $(".yohoo-share-button-sina"),
                    facebook: $(".yohoo-share-button-facebook"),
                    tweet: $(".yohoo-share-button-tweet")
                });
            })();
        } else {
            options.shareUrl = options.shareUrl == "" ? document.location.href : options.shareUrl;
        }

        function share() {
            (function(s, d, e) {
                try {} catch (e) {}
                var f = 'http://v.t.sina.com.cn/share/share.php?',
                    u = options.shareUrl,
                    p = ['url=', e(u), '&title=', e(window.sharetitle), '&appkey=2924220432', '&pic=', e(window.shareUrl)].join('');

                function a() {
                    if (!window.open([f, p].join(''), 'mb', ['toolbar=0,status=0,resizable=1,width=620,height=450,left=', (s.width - 620) / 2, ',top=', (s.height - 450) / 2].join('')))
                        u.href = [f, p].join('');
                };
                if (/Firefox/.test(navigator.userAgent)) {
                    setTimeout(a, 0)
                } else {
                    a()
                }
            })(screen, document, encodeURIComponent);
        }

        var iTop = (window.screen.availHeight - 30 - options.height) / 2; //获得窗口的垂直位置;
        var iLeft = (window.screen.availWidth - 10 - options.width) / 2; //获得窗口的水平位置;
        $(".yohoo-share-button-facebook").unbind().on("click", function() {
            if (options.imgFunction != null) {
                options.img = (function() {
                    return options.imgFunction.apply($(".yohoo-share-button-facebook"));
                })();
            }
            var tmpResult = {
                des: "",
                name: "",
                caption: ""
            };
            var appid = "",
                domain = document.domain;
            if (options.fbTextFunction != null) {
                tmpResult = (function() {
                    return options.fbTextFunction.apply($(".yohoo-share-button-facebook"));
                })();
                if (!tmpResult.url) {
                    options.fbCaption = tmpResult.caption;
                    options.fbDescription = tmpResult.des;
                    options.fbName = tmpResult.name;
                } else {
                    options.fbUrl = tmpResult.url;
                }
            }

            if (options.fbUrl) {
                window.open("https://www.facebook.com/sharer/sharer.php?u=" + options.fbUrl, "",
                    "height=" + options.height + ",width=" + options.width + ",left=" + iLeft + ",top=" + iTop);
            } else {
                options.fbCaption = options.fbCaption.replace(/[ ]/g, "%20")
                    .replace(/[&]/g, "%26")
                    .replace(/[#]/g, "%23")
                    .replace(/[+]/g, "%2B");
                options.fbDescription = tmpResult.des.replace(/[ ]/g, "%20")
                    .replace(/[&]/g, "%26")
                    .replace(/[#]/g, "%23")
                    .replace(/[+]/g, "%2B");
                options.fbName = tmpResult.name.replace(/[ ]/g, "%20")
                    .replace(/[&]/g, "%26")
                    .replace(/[#]/g, "%23")
                    .replace(/[+]/g, "%2B");
                if (domain === "yohood.dev.yoho.cn") {
                    appid = "146057275796393";
                } else if (domain === "yohood.test.yoho.cn") {
                    appid = "146057275796393";
                } else if (domain === "www.yohood.cn") {
                    appid = "146050529130401";
                }
                if (appid !== "") {
                    window.open("http://www.facebook.com/dialog/feed?app_id=" + appid + 
                        // "&redirect_uri=" + location.href +
                        "&link=" + options.shareUrl +
                        "&picture=" + options.img +
                        "&caption=" + options.fbName +
                        "&description=" + options.fbDescription + (options.fbDescription == "" ? "" : "") +
                        "&name=" + options.fbCaption,
                        "",
                        "height=" + options.height + ",width=" + options.width + ",left=" + iLeft + ",top=" + iTop);
                    return;
                }
                window.open("http://www.facebook.com/dialog/feed?app_id=1431491300468916&redirect_uri=http://www.yohoboys.com/channel/yohood/closedialog&" +
                    "link=" + options.shareUrl +
                    "&picture=" + options.img +
                    "&caption=" + options.fbName +
                    "&description=" + options.fbDescription + (options.fbDescription == "" ? "" : "(分享自 @yohoboy)") +
                    "&name=" + options.fbCaption,
                    "",
                    "height=" + options.height + ",width=" + options.width + ",left=" + iLeft + ",top=" + iTop);
            }
        });

        $(".yohoo-share-button-tweet").unbind().on("click", function() {
            if (options.tweetTextFunction != null) {
                options.tweetText = (function() {
                    return options.tweetTextFunction.apply($(".yohoo-share-button-tweet"));
                })();
            }
            options.tweetText = options.tweetText.replace(/[ ]/g, "%20")
                .replace(/[&]/g, "%26")
                .replace(/[#]/g, "%23")
                .replace(/[+]/g, "%2B");
            window.open("http://twitter.com/intent/tweet?url=" + options.shareUrl +
                "&text=" + options.tweetText,
                "",
                "height=" + options.height + ",width=" + options.width + ",left=" + iLeft + ",top=" + iTop);
        });

        $(".yohoo-share-button-qq").unbind().on("click", function() {
            window.open('http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=' + encodeURIComponent(location.href) +
                '&title=' + options.qqText +
                '&pics=' + options.img, '_blank', 'height=' + options.height + ',width=' + options.width + ',left=' + iLeft + ',top=' + iTop);
        });
    };

    $(".yohoo-share-button-wx").on("mouseover", function() {
        $(".wx-qrcode-dialog").show();
    }).on("mouseout", function() {
        $(".wx-qrcode-dialog").hide();
    });

    //ipad微信
    $('.bd').on("click.closeWxDialog", function(event) {
        if ($(event.target).is(".yohoo-share-button-wx") || $(event.target).parents(".yohoo-share-button-wx").size() > 0) {
            $(".wx-qrcode-dialog").show();
        } else {
            $(".wx-qrcode-dialog").hide();
        }
    })

    $(".bottom-content").on("click.closeWxDialog", function() {
        $(".detail-container").trigger("click.closeWxDialog");
    })
    $(".enterprise").on("click.closeWxDialog", function() {
        $(".detail-container").trigger("click.closeWxDialog");
    })
});