-
Notifications
You must be signed in to change notification settings - Fork 9
/
share.js
427 lines (421 loc) · 18.7 KB
/
share.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
(function($,window){
var preventBodyScroll = false;
var ua=window.navigator.userAgent;
//手机qq浏览器js调用分享接口地址
var qqBrowserSrc = {
lower: "http://3gimg.qq.com/html5/js/qb.js",
higher: "http://jsapi.qq.com/get?api=app.share"
};
var level = {
qq: {forbid: 0, lower: 1, higher: 2},
uc: {forbid: 0, allow: 1}
};
//浏览器版本
var version={
qq:'',
uc:''
};
//浏览器接口
var appList={
//参数[ios_uc,andriod_uc,qq浏览器,名称]
sina: ['kSinaWeibo', 'SinaWeibo', 11, '微博'],
wechat: ['kWeixin', 'WechatFriends', 1, '微信'],
friend: ['kWeixinFriend', 'WechatTimeline', 8, '朋友圈'],
qq: ['kQQ', 'QQ', 4, 'QQ好友'],
qzone: ['kQZone', 'QZone', 3, 'QQ空间']
}
var isUCBrowser=checkBrowser('UCBrowser')?level.uc.allow:level.uc.forbid;//uc浏览器
var isMQQBrowser=checkBrowser('MQQBrowser')?level.qq.higher : level.qq.forbid;//qq浏览器
var isWechat=checkBrowser('MicroMessenger');//微信
version.qq = isMQQBrowser ?getVersion(ua.split("MQQBrowser/")[1]) : 0;
version.uc = isUCBrowser ?getVersion(ua.split("UCBrowser/")[1]) : 0;
if(isMQQBrowser){
loadQqBrowserSrc();
}
var defaults ={
ele:false,//触发分享按钮
title:document.title,//分享标题
pic:"",//分享图片地址
url:window.location.href,//分享地址
desc:document.title,//分享描述
summary:document.title,
rightImg:'images/share/share.png',//微信分享提示图片
overlayClick:true,
imgTitle:'',
appendTo:'body',
appId: "",
timestamp: "",
nonceStr: "",
signature: "",
wechatShareSucess:null,
wechatShareCancel:null
};
function getVersion(c){
var a = c.split("."),
b = parseFloat(a[0] + "." + a[1]);
return b;
}
function loadQqBrowserSrc(){
var b = (version.qq < 5.4) ? qqBrowserSrc.lower : qqBrowserSrc.higher;
var d = document.createElement("script");
var a = document.getElementsByTagName("body")[0];
d.setAttribute("src", b);
a.appendChild(d);
};
//检测浏览器
function checkBrowser(name){
return ua.indexOf(name)>-1;
}
//检测手机系统
function checkPlatform(){
var ua =window.navigator.userAgent;
if ((ua.indexOf("iPhone") > -1 || ua.indexOf("iPod") > -1)) {
return "iPhone";
}
return "Android";
}
var platform=checkPlatform();
function AppShare(options){
this.settings=$.extend({},defaults,options || {});
this.init();
}
AppShare.prototype={
init:function(){
var _this=this;
this.settings.sinaWebUrl="http://service.weibo.com/share/share.php?url="+encodeURIComponent(this.settings.url)+"&title="+encodeURIComponent(this.settings.title)+"&pic="+encodeURIComponent(this.settings.pic);
this.settings.qzoneWebUrl="http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url="+encodeURIComponent(this.settings.url)+"&title="+encodeURIComponent(this.settings.title)+"&pics="+encodeURIComponent(this.settings.pic)+"&desc="+encodeURIComponent(this.settings.desc)+"&summary="+encodeURIComponent(this.settings.summary);
this.stopBodyScroll();
if(!this.settings.ele){
//不绑定元素
document.title=this.settings.title;
$("[name='description']").attr("content",this.settings.desc);
if(!isUCBrowser&&!isMQQBrowser&&!isWechat){
$("[data-app='wechat'],[data-app='friend'],[data-app='qq']").addClass('hideIcon');
$("[data-app='qzone']").attr("href",this.settings.qzoneWebUrl);
$("[data-app='sina']").attr("href",this.settings.sinaWebUrl);
}
else{
if(isWechat){
$("[data-app='qq']").addClass('hideIcon');
$("[data-app='qzone']").attr("href",this.settings.qzoneWebUrl);
$("[data-app='sina']").attr("href",this.settings.sinaWebUrl);
$("body").append(this.wechatShareTips());
this.bindWechatShareTipsClick();
}
}
$(document).on("click","[data-app]",function(){
if($(this).attr("href")!==undefined){
window.location.href=$(this).attr("href");
}
else{
_this.shareEvent($(this).data('app'));
}
});
}
else{
//绑定元素
this.create();
$(this.settings.ele).click($.proxy(function(){
this.open();
preventBodyScroll=true;
},this));
}
//加载微信js文件
if(isWechat){
/*要手动将这个文件添加,不能动态添加*/
/*var d = document.createElement("script");
var a = document.getElementsByTagName("head")[0];
d.setAttribute("src", 'http://res.wx.qq.com/open/js/jweixin-1.0.0.js');
a.appendChild(d);
d.onload=d.onreadystatechange=function(){
if(!this.readyState||this.readyState=='loaded'||this.readyState=='complete'){*/
this.wechatEvent();
/*}
d.onload=d.onreadystatechange=null;
}*/
}
},
create:function(){
this.htmlTemplate();
this.eachIcon();
this.appendTo();
this.setLevel();
},
stopBodyScroll:function(){
//针对手机,弹窗阻止body点透滑动
document.addEventListener('touchmove', function(e) {
if (preventBodyScroll){
e.preventDefault();
}
}, false);
},
wechatEvent:function(){
//微信浏览器分享事件
var _this=this;
wx.config({
debug: false,
appId: _this.settings.appId,
timestamp:_this.settings.timestamp,
nonceStr:_this.settings.nonceStr,
signature:_this.settings.signature,
jsApiList: ["onMenuShareTimeline","onMenuShareAppMessage","onMenuShareQQ","onMenuShareWeibo","onMenuShareQZone"]
});
wx.ready(function () {
//分享到朋友圈操作
wx.onMenuShareTimeline({
title:_this.settings.title, // 分享标题
link: _this.settings.url, // 分享链接
imgUrl:_this.settings.pic, // 分享图标
success: function () {
// 用户确认分享后执行的回调函数
if($.isFunction(_this.settings.wechatShareSucess)){
_this.settings.wechatShareSucess();
}
},
cancel: function (){
// 用户取消分享后执行的回调函数
if($.isFunction(_this.settings.wechatShareCancel)){
_this.settings.wechatShareCancel();
}
}
});
//发送给朋友
wx.onMenuShareAppMessage({
title:_this.settings.title, // 分享标题
desc: _this.settings.desc, // 分享描述
link: _this.settings.url, // 分享链接
imgUrl:_this.settings.pic, // 分享图标
type: 'link', // 分享类型,music、video或link,不填默认为link
dataUrl: '', // 如果type是music或video,则要提供数据链接,默认为空
success: function () {
if($.isFunction(_this.settings.wechatShareSucess)){
_this.settings.wechatShareSucess();
}
},
cancel: function () {
if($.isFunction(_this.settings.wechatShareCancel)){
_this.settings.wechatShareCancel();
}
}
});
//qq好友
wx.onMenuShareQQ({
title:_this.settings.title, // 分享标题
desc: _this.settings.desc, // 分享描述
link: _this.settings.url, // 分享链接
imgUrl: _this.settings.pic, // 分享图标
success: function () {
if($.isFunction(_this.settings.wechatShareSucess)){
_this.settings.wechatShareSucess();
}
},
cancel: function () {
if($.isFunction(_this.settings.wechatShareCancel)){
_this.settings.wechatShareCancel();
}
}
});
//腾讯微博
wx.onMenuShareWeibo({
title: _this.settings.title, // 分享标题
desc: _this.settings.desc, // 分享描述
link:_this.settings.url, // 分享链接
imgUrl:_this.settings.pic, // 分享图标
success: function () {
if($.isFunction(_this.settings.wechatShareSucess)){
_this.settings.wechatShareSucess();
}
},
cancel: function () {
if($.isFunction(_this.settings.wechatShareCancel)){
_this.settings.wechatShareCancel();
}
}
});
//qq空间
wx.onMenuShareQZone({
title: _this.settings.title, // 分享标题
desc: _this.settings.desc, // 分享描述
link: _this.settings.url, // 分享链接
imgUrl:_this.settings.pic, // 分享图标
success: function () {
if($.isFunction(_this.settings.wechatShareSucess)){
_this.settings.wechatShareSucess();
}
},
cancel: function () {
if($.isFunction(_this.settings.wechatShareCancel)){
_this.settings.wechatShareCancel();
}
}
});
})
},
setLevel:function(){
if ((isMQQBrowser && version.qq < 5.4 && platform == "iPhone") || (isMQQBrowser && version.qq < 5.3 && platform == "Android")){
isMQQBrowser = level.qq.forbid;
}else{
if (isMQQBrowser && version.qq < 5.4 && platform == "Android") {
isMQQBrowser = level.qq.lower;
}else{
if (isUCBrowser && ((version.uc < 10.2 && platform == "iPhone") || (version.uc < 9.7 && platform == "Android"))) {
isUCBrowser = level.uc.forbid;
}
}
}
},
shareEvent:function(appname){
//UC浏览器
if (isUCBrowser) {
appname = appname == '' ? '' : (platform == 'iPhone' ? appList[appname][0] : appList[appname][1]);
if (typeof(ucweb) != "undefined") {
ucweb.startRequest("shell.page_share", [this.settings.title,this.settings.desc, this.settings.url, appname,'','','']);
} else {
if (typeof(ucbrowser) != "undefined") {
ucbrowser.web_share(this.settings.title,this.settings.desc,this.settings.url,appname, "", "", "");
}
}
}
else {
//手机QQ浏览器
if (isMQQBrowser && !isWechat) {
appname = appname == '' ? '' :appList[appname][2];
var config = {
url: this.settings.url,
title: this.settings.title,
description: this.settings.desc,
img_url: this.settings.pic,
img_title: this.settings.imgTitle,
to_app: appname,//微信好友1,腾讯微博2,QQ空间3,QQ好友4,生成二维码7,微信朋友圈8,啾啾分享9,复制网址10,分享到微博11,创意分享13
cus_txt: "请输入此时此刻想要分享的内容"
};
config = appname == '' ? '' : config;
if (typeof(browser) != "undefined") {
if (typeof(browser.app) != "undefined" && isMQQBrowser == level.qq.higher) {
browser.app.share(config);
}
} else {
if (typeof(window.qb) != "undefined" && isMQQBrowser == level.qq.lower) {
window.qb.share(config);
}
}
}
//微信浏览器
if((isWechat&&appname=='wechat')||(isWechat&&appname=='friend')){
$("body").find(".wechatShareTips").show();
}
}
},
eachIcon:function(){
var html='';
if(isUCBrowser||isMQQBrowser||isWechat){
html+='<li data-app="wechat"><a href="javascript:void(0)"><em class="appicon wechat"></em><p>'+appList.wechat[3]+'</p></a></li>';
html+='<li data-app="friend"><a href="javascript:void(0)"><em class="appicon friend"></em><p>'+appList.friend[3]+'</p></a></li>';
if(!isWechat){
html+='<li data-app="sina"><a href="javascript:void(0)"><em class="appicon sina"></em><p>'+appList.sina[3]+'</p></a></li>';
html+='<li data-app="qzone"><a href="javascript:void(0)"><em class="appicon qzone"></em><p>'+appList.qzone[3]+'</p></a></li>';
html+='<li data-app="qq"><a href="javascript:void(0)"><em class="appicon qq"></em><p>'+appList.qq[3]+'</p></a></li>';
}
else{
html+='<li><a href="'+this.settings.sinaWebUrl+'"><em class="appicon sina"></em><p>'+appList.sina[3]+'</p></a</li>';
html+='<li><a href="'+this.settings.qzoneWebUrl+'"><em class="appicon qzone"></em><p>'+appList.qzone[3]+'</p></a></li>';
}
}
else{
html+='<li><a href="'+this.settings.sinaWebUrl+'"><em class="appicon sina"></em><p>'+appList.sina[3]+'</p></a</li>';
html+='<li><a href="'+this.settings.qzoneWebUrl+'"><em class="appicon qzone"></em><p>'+appList.qzone[3]+'</p></a></li>';
}
$(this.settings.html).find("ul").append(html);
var _this=this;
$(this.settings.html).find("li").each(function(index,val){
if($(this).attr("data-app")!==undefined){
$(this).click(function(){
_this.shareEvent($(this).data('app'));
});
}
});
return this.settings.html;
},
bindCancel:function(){
$(this.settings.html).find(".cancel").click($.proxy(function(){
this.closed();
},this));
},
appendTo:function(){
$(this.settings.html).appendTo(this.settings.appendTo);
},
open:function(){
$("body").addClass('overflow');
$(this.settings.html).show();
setTimeout($.proxy(function(){
$(this.settings.html).find(".share_content").addClass("show");
},this),100);
},
closed:function(){
var _this=this;
$("body").removeClass('overflow');
$(this.settings.html).find(".share_content").removeClass("show");
/*$(this.settings.html).find(".share_content").on(_this.whichTransitionEvent(),function(){
console.log('finish');
});*/
setTimeout($.proxy(function(){
$(this.settings.html).hide();
},this),400);
preventBodyScroll=false;
},
whichTransitionEvent:function(){
var t;
var el = document.createElement('div');
var transitions = {
'transition':'transitionend',
'OTransition':'oTransitionEnd',
'MozTransition':'transitionend',
'WebkitTransition':'webkitTransitionEnd'
}
for(t in transitions){
if( el.style[t] !== undefined ){
return transitions[t];
}
}
},
wechatShareTips:function(){
return '<div class="wechatShareTips"><img src="'+this.settings.rightImg+'"></div>';
},
bindWechatShareTipsClick:function(){
$(document).on('click', '.wechatShareTips', function(event) {
$(this).hide();
});
},
htmlTemplate:function(){
this.settings.html=$('<div class="app_share" style="display:none">');
var template='<div class="table">\
<div class="table_cell">\
<div class="share_content">\
<div class="icon_wrap">\
<ul></ul>\
</div>\
<div class="cancel">取消</div>\
</div>\
</div>\
</div>';
$(template).appendTo($(this.settings.html));
this.bindCancel();
if(isWechat){
$(this.settings.html).prepend(this.wechatShareTips());
this.bindWechatShareTipsClick();
}
if(this.settings.overlayClick){
$(this.settings.html).find(".table_cell").click($.proxy(function(e){
if($(event.target).attr("class")=='table_cell'){
this.closed();
}
},this));
}
return this.settings.html;
}
}
var appShare=function(options){
return new AppShare(options);
}
window.appShare=$.appShare=appShare;
})(window.jQuery||window.Zepto,window);