(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;
}));
share wechat
最新推荐文章于 2020-12-24 15:41:12 发布