share wechat


(function(factory) {

    if (typeof define === 'function' && define.amd) {
        define(['jquery', 'Event', 'http://m.show.dji.com/wechat/sdk/share'], factory);
    } else {
        factory(jQuery, Event, weixinShare);
    }

}(function($, Event, weixinShare) {

    function Share(inputOpts) {
        this.opts = $.extend({
            id: 'share',
            buttons: [],
            props: {
                url: '',
                description: '',
                title: '',
                img: ''
            }
        }, inputOpts);

        this.$target = $('#' + this.opts.id);
    }
    Share.Buttons = {
        weibo: 'weibo',
        weixin: 'weixin',
        facebook: 'facebook',
        twitter: 'twitter'
    };
    Share.getUrl = function(button, props) {
        var url = '';
        switch(button){
            case Share.Buttons.twitter:
                url = '//twitter.com/intent/tweet?url='+ props.url +'&text='+ props.description[Share.Buttons.twitter];
                break;
            case Share.Buttons.facebook:
                url = '//www.facebook.com/sharer/sharer.php?p[url]=' + props.url;
                break;
            case Share.Buttons.weibo:
                url = 'http://service.weibo.com/share/share.php?url='+ props.url +'&title='+ props.title +' '+ props.description[Share.Buttons.weibo] +'&pic='+ props.img +'&searchPic=false';
                break;
        }
        return url;
    };

    Share.prototype = {
        init: function() {
            this._generate();
        },

        setButtons: function(buttons) {
            this.opts.buttons = buttons;
            this.$target.empty();
        },

        setProps: function(props) {
            this.opts.props = props;
            this.$target.empty();
        },

        /**
         *
         * l   : Button position left
         * t   : Button position top
         * w   : Button position width
         * h   : Button position height
         *
         */
        showWeixinQrcode: function(l, t, w, h) {
            if (this.$weixinQrcode == null) {
                this._generateWeixinQrcode();
            }

            if (l > 0 && t > 0 && w > 0 && h > 0) {
                var width = this.$weixinQrcode.width();

                var docHeight = document.documentElement.scrollHeight;
                var qrcodeStyle = {};

                if (t + h + 128 >= docHeight) {
                    qrcodeStyle = {
                        top: document.body.scrollTop + document.documentElement.clientHeight / 2
                    }
                } else {
                    qrcodeStyle = {
                        left: l - width / 2 + w / 2,
                        top: t + h,
                        margin: 0
                    };
                }

                this.$weixinQrcode.css(qrcodeStyle);
            }

            this.$weixinQrcode.fadeIn('normal');
        },

        hideWeixinQrcode: function() {
            if (this.$weixinQrcode == null) {
                return;
            }

            this.$weixinQrcode.fadeOut('normal');
        },

        shareToWeixin: function(props) {
            var weixinShareAvailable = typeof weixinShare !== 'undefined';
            if (weixinShareAvailable) {
                weixinShare({
                    shareData:{
                        title: props.title,
                        only_title: props.title,
                        desc: props.description[Share.Buttons.weixin],
                        imgUrl: props.img
                    }
                });
            }

            return weixinShareAvailable;
        },

        _generateWeixinQrcode: function() {
            if (this.$weixinQrcode == null) {
                this.$weixinQrcode = $('<span>').addClass('share-weixin-qrcode');
                $('body').append(this.$weixinQrcode);
            }

            return this.$weixinQrcode;
        },

        _generate: function() {
            var _this = this;

            var buttons = this.opts.buttons;
            var props = this.opts.props;

            if (buttons.length == 0) {
                this.$target.hide();
            }

            for (var i = 0; i < buttons.length; i++) {
                var buttonData = buttons[i];
                var url = Share.getUrl(buttonData, props);
                var $button = $('<a>')
                    .attr('href', url)
                    .addClass('share-btn share-' + buttonData)
                    .attr('target', '_blank');

                if (buttonData === Share.Buttons.weixin) {
                    $button.on(Event.Browser.versions.mobile ? 'touchstart' : 'click', function(e) {
                        e.stopPropagation();
                        e.preventDefault();

                        if (!navigator.userAgent.toLowerCase().match(/MicroMessenger/i)) {
                            var position = $(this).offset();
                            _this.showWeixinQrcode.call(_this, position.left, position.top, $(this).width(), $(this).height());
                        }
                    });
                }

                this.$target.append($button);
            }

            this.$target.addClass('share');
        }
    };

    window.Share = Share;

    return Share;
}));
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值