forked from misterchaos/WeChatShare
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwx_share.js
136 lines (114 loc) · 5.81 KB
/
wx_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
WX_Custom_Share = function(){
var xhr = null;
var url = 'https://hellochaos.cn/index.php/action/wx-share?do=ajax-get';
var formData = {
title: '书单',
parameter_type: 'page',
cid: '8',
signature_url: 'https://hellochaos.cn/douban.html'
};
this.init = function(){
if( window.XMLHttpRequest ){
xhr = new XMLHttpRequest();
}
else if( window.ActiveXObject ){
xhr = new ActiveXObject('Microsoft.XMLHTTP');
};
get_share_info();
};
function formatPostData( obj ){
var arr = new Array();
for (var attr in obj ){
arr.push( encodeURIComponent( attr ) + '=' + encodeURIComponent( obj[attr] ) );
};
return arr.join( '&' );
};
var wxapi = "https://res.wx.qq.com/open/js/jweixin-1.6.0.js", qqapi = "//open.mobile.qq.com/sdk/qqapi.js?_bid=152", qzapi = "//qzonestyle.gtimg.cn/qzone/phone/m/v4/widget/mobile/jsbridge.js?_bid=339";
function require(url, onload) {
var doc = document;
var head = doc.head || (doc.getElementsByTagName("head")[0] || doc.documentElement);
var node = doc.createElement("script");
node.onload = onload;
node.onerror = function () {
};
node.async = true;
node.src = url[0];
head.appendChild(node);
}
function initWX(data) {
if (!data.WXconfig) {
return;
}
require([wxapi], function (wx) {
if (!wx.config) {
wx = window.wx;
}
var conf = data.WXconfig;
wx.config({ debug: false, appId: conf.appId, timestamp: conf.timestamp, nonceStr: conf.nonceStr, signature: conf.signature, jsApiList: ["updateTimelineShareData", "updateAppMessageShareData"] });
wx.error(function (res) {
});
wx.ready(function () {
var config = {
title: data.title, desc: data.summary, link: data.url, imgUrl: data.pic, type: "", dataUrl: "", success: function () {
data.callback && data.callback();
}, cancel: function () {
}
};
wx.updateTimelineShareData({
title: config.title, // 分享标题
link: config.link, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
imgUrl: config.imgUrl, // 分享图标
success: function () {
// 设置成功
}
})
wx.updateAppMessageShareData({
title: config.title,// 分享标题
desc: '', // 分享描述
link: config.link, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
imgUrl: config.imgUrl, // 分享图标
success: function () {
// 设置成功
}
})
});
});
}
function get_share_info(){
if( xhr == null ) return;
xhr.onreadystatechange = function(){
if( xhr.readyState == 4 && xhr.status == 200 ){
var data = eval('(' + xhr.responseText + ')');
if( data == null ){
return;
}
//console.log(data);
var info = {
title: data.wx_title,
summary: data.wx_description,
pic: data.wx_image,
url: data.wx_url
};
//info.url = data.wx_url;
if( data.error ){
console.error( data.error );
} else if( data.appId ){
info.WXconfig = {
swapTitleInWX: true,
appId: data.appId,
timestamp: data.timestamp,
nonceStr: data.nonceStr,
signature: data.signature
};
};
initWX( info );
}
};
xhr.open( 'POST', url, true);
xhr.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
xhr.send( formatPostData( formData ) );
};
};
new WX_Custom_Share().init();
console.log("%c", "padding:100px 200px;line-height:220px;background:url('https://hiphotos.baidu.com/feed/pic/item/b999a9014c086e06606a9d0009087bf40bd1cbbf.jpg') no-repeat;");
console.log("%c WeChatShare v2.0 %c By Yuchao Huang https://hellochaos.cn ","color:#444;background:#eee;padding:5px 0;","color:#eee;background:#444;padding:5px 0;");