share.js
3.45 KB
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
/**
* Created by PhpStorm.
* User: Targaryen
* Date: 2016/7/29
* Time: 16:55
*/
let qs = require('yoho-qs');
let jsApiList = [
'checkJsApi',
'onMenuShareTimeline',
'onMenuShareAppMessage',
'onMenuShareQQ',
'onMenuShareWeibo',
'onMenuShareQZone'
];
let shareData = {
title: document.title,
link: location.href,
desc: 'YOHO!BUY',
imgUrl: 'http://static.yohobuy.com/m/v1/img/touch/apple-touch-icon-144x144-precomposed-new.png'
};
const getShareData = function(shareType) {
let data = {
success: function() {
window._yas.sendCustomInfo && window._yas.sendCustomInfo({
op: 'YB_SHARE_SUCCESS_L',
param: JSON.stringify({
SHARE_TYPE: shareType,
SHARE_URL: location.href
})
}, true);
}
};
return Object.assign(data, shareData);
};
const setShareData = function() {
window.wx.onMenuShareTimeline(getShareData('2'));
window.wx.onMenuShareAppMessage(getShareData('3'));
window.wx.onMenuShareQQ(getShareData('5'));
window.wx.onMenuShareQZone(getShareData('6'));
window.wx.onMenuShareWeibo(getShareData('7'));
};
if (/QQ/i.test(navigator.userAgent)) {
$.ajax({
url: '//qzonestyle.gtimg.cn/qzone/qzact/common/share/share.js',
dataType: 'script',
cache: true,
success: function() {
window.setShareInfo && window.setShareInfo({
title: shareData.title,
summary: shareData.desc,
pic: shareData.imgUrl,
url: shareData.link
});
}
});
}
if (/MicroMessenger/i.test(navigator.userAgent)) {
$.ajax({
url: '//res.wx.qq.com/open/js/jweixin-1.3.2.js',
dataType: 'script',
cache: true,
success: function() {
$.ajax({
url: location.protocol + '//m.yohobuy.com/activity/wechat/share',
data: {
url: location.href
},
dataType: 'jsonp',
success: function(res) {
if (window.wx) {
window.wx.config({
debug: false,
appId: res.appId,
timestamp: res.timestamp,
nonceStr: res.nonceStr,
signature: res.signature,
jsApiList: jsApiList
});
window.wx.ready(function() {
setShareData();
});
}
}
});
}
});
}
if (qs && qs.share_id) {
$.ajax({
method: 'GET',
url: location.protocol + '//m.yohobuy.com/activity/share',
data: {
shareId: qs.share_id
},
dataType: 'jsonp',
success: function(res) {
if (res && res.code === 200 && res.data) {
shareData.desc = res.data.content || res.data.title;
shareData.imgUrl = res.data.pic;
shareData.title = res.data.title;
}
}
});
}
module.exports = function(data) {
shareData = data;
if (window.wx) {
setShareData();
}
window.setShareInfo && window.setShareInfo({
title: shareData.title,
summary: shareData.desc,
pic: shareData.imgUrl,
url: shareData.link
});
};