-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwechatshare.pure.js
157 lines (144 loc) · 6.21 KB
/
wechatshare.pure.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
!(function () {
var jsonpData = function(url, callback) {
let callbackName = 'jsonp_callback_' + Math.round(100000 * Math.random());
window[callbackName] = function(data) {
delete window[callbackName];
document.body.removeChild(script);
callback(data);
};
let script = document.createElement('script');
script.src = url + (url.indexOf('?') >= 0 ? '&' : '?') + 'callback=' + callbackName;
document.body.appendChild(script);
}
var cloneObj = function(oldObj) { //复制对象方法
if (typeof(oldObj) != 'object') return oldObj;
if (oldObj == null) return oldObj;
var newObj = new Object();
for (var i in oldObj)
newObj[i] = cloneObj(oldObj[i]);
return newObj;
}
var extendObj = function() { //扩展对象
var args = arguments;
if (args.length < 2) return;
var temp = cloneObj(args[0]); //调用复制对象方法
for (var n = 1; n < args.length; n++) {
for (var i in args[n]) {
temp[i] = args[n][i];
}
}
return temp;
}
var wechatShare = function (opt) {
if (!(this instanceof wechatShare)) {
return new wechatShare(opt);
}
var config = {
title: '微信分享标题',
desc: '微信分享内容',
link: window.location.href,
img: 'http://makefriends.bs2dl.yy.com/48980134342886519512.png',
request_share_url: 'http://h5.yy.com/wap/getsharecf?url=' + encodeURIComponent(window.location.href.split('#')[0]),
success_callback: function () {
},
cancel_callback: function () {
},
debug: false
}
this.defaults = extendObj(config, opt);
this.init();
}
wechatShare.prototype = {
init: function () {
var self = this;
self.doconfig();
},
doconfig: function () {
var self = this;
/*share*/
var request_share_url = self.defaults.request_share_url;
jsonpData(request_share_url, function(jdata){
console.log('doconfig', jdata);
console.log('defaluts', self.defaults);
var msg = jdata.wxapi;
wx.config({
debug: self.defaults.debug, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId: msg.appId, // 必填,公众号的唯一标识
timestamp: msg.timestamp, // 必填,生成签名的时间戳
nonceStr: msg.nonceStr, // 必填,生成签名的随机串
signature: msg.signature,// 必填,签名,见附录1
jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareQQ', 'chooseWXPay', 'onMenuShareWeibo', 'startRecord', 'stopRecord', 'onVoiceRecordEnd', 'playVoice', 'pauseVoice', 'stopVoice', 'onVoicePlayEnd', 'uploadVoice', 'downloadVoice', 'translateVoice'] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
});
wx.ready(function () {
var share_title = self.defaults.title;
var share_desc = self.defaults.desc;
var share_link = self.defaults.link;
var share_img = self.defaults.img;
//分享到朋友圈
wx.onMenuShareTimeline({
title: share_title, // 分享标题
link: share_link, // 分享链接
imgUrl: share_img, // 分享图标
success: function () {
self.defaults.success_callback();
},
cancel: function () {
self.defaults.cancel_callback();
}
});
//分享给朋友
wx.onMenuShareAppMessage({
title: share_title, // 分享标题
desc: share_desc, // 分享描述
link: share_link, // 分享链接
imgUrl: share_img, // 分享图标
type: '', // 分享类型,music、video或link,不填默认为link
dataUrl: '', // 如果type是music或video,则要提供数据链接,默认为空
success: function () {
self.defaults.success_callback();
},
cancel: function () {
self.defaults.cancel_callback();
}
});
wx.onMenuShareQQ({
title: share_title, // 分享标题
desc: share_desc, // 分享描述
link: share_link, // 分享链接
imgUrl: share_img, // 分享图标
success: function () {
self.defaults.success_callback();
},
cancel: function () {
self.defaults.cancel_callback();
}
});
wx.onMenuShareWeibo({
title: share_title, // 分享标题
desc: share_desc, // 分享描述
link: share_link, // 分享链接
imgUrl: share_img, // 分享图标
success: function () {
self.defaults.success_callback();
},
cancel: function () {
self.defaults.cancel_callback();
}
})
});
});
/*share end*/
}
}
// RequireJS && SeaJS
if (typeof define === 'function') {
define(function () {
return wechatShare;
});
// NodeJS
} else if (typeof exports !== 'undefined') {
module.exports = wechatShare;
} else {
this.wechatShare = wechatShare;
}
})()