-
Notifications
You must be signed in to change notification settings - Fork 0
/
wechat.js
139 lines (113 loc) · 3.5 KB
/
wechat.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
/*! CopyRight: sofish http://github.com/sofish/wechat.js, Licensed under: MIT */
;(function(global, doc) {
var noop = function() {};
// map 掉恶心的不统一的 api
var Wechat = function() {
this.calls = [];
this.map = {
events: {
friend: 'menu:share:appmessage',
timeline: 'menu:share:timeline',
weibo: 'menu:share:weibo'
},
actions: {
friend: 'sendAppMessage',
timeline: 'shareTimeline',
weibo: 'shareWeibo'
},
direct: {
network: 'getNetworkType',
hideToolbar: 'hideToolbar',
hideOptionMenu: 'hideOptionMenu',
showOptionMenu: 'showOptionMenu'
}
};
};
// 有些 data 是延时获取的,这时候应该支持传入 callback
Wechat.prototype._data = function(data) {
var tmp = {};
for(var p in data) {
if(!data.hasOwnProperty(p)) return;
tmp[p] = (typeof data[p] === 'function') ? data[p]() : data[p];
}
// 接口命名统一
tmp.appid = tmp.app;
tmp.img_url = tmp.img;
delete tmp.app;
delete tmp.img;
return tmp;
};
// 处理数据接入
Wechat.prototype._make = function(obj) {
if(typeof WeixinJSBridge === 'undefined') return this.calls.push(obj);
var name = obj.name
, direct = this.map.direct[name]
//, data = obj.data
, getData = obj.getData
, callback = obj.callback;
// 直接获取的情况
if(direct) {
// 获取用户网络状态的返回值如下:
// network_type:wifi wifi网络
// network_type:edge 非wifi,包含3G/2G
// network_type:fail 网络断开连接
// network_type:wwan(2g或者3g)
if(name === 'network') return WeixinJSBridge.invoke(direct, {}, callback);
return WeixinJSBridge.call(direct, callback);
}
var that = this;
// 当 WeixinJSBridge 存在则直接绑定事件
WeixinJSBridge.on(this.map.events[name], function() {
var data = getData() || {}
data = that._data(data)
// 分享到微博的接口不同
if(name === 'weibo') {
data.content = data.desc;
data.url = data.link;
// 朋友圈的 title 是不显示的,直接拼接
} else if(name === 'timeline') {
data.title = data.title + ' - ' + data.desc;
// Android 下有时候会需要 desc (*-.-)
data.desc = data.title;
}
WeixinJSBridge.invoke(that.map.actions[name], data, callback);
});
};
// 添加监听
Wechat.prototype.on = function(name, getData, callback) {
if(!name) return;
//if(typeof data === 'function') {
// callback = data;
// data = null;
//}
this._make({
name: name,
//data: data ? this._data(data) : {},
getData: getData || noop,
callback: callback || noop
});
// 返回本身,支持链式
return this;
};
// WeixingJSBridgeReady 后执行绑定的队列
var ready = function() {
for(var i = 0, len = wx.calls.length; i < len; i++) {
wx._make(wx.calls[i]);
}
};
// 对外只分享一个接口,不过会返回本身,可以有备用
var wx = new Wechat();
global.wechat = global.wechat || function() {
return wx.on.apply(wx, arguments);
};
if(typeof WeixinJSBridge === 'undefined'){
if(doc.addEventListener) {
doc.addEventListener('WeixinJSBridgeReady', ready, false);
} else {
doc.attachEvent('WeixinJSBridgeReady', ready);
doc.attachEvent('onWeixinJSBridgeReady', ready);
}
} else {
ready();
}
})(window, document);