-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbrowser.js
More file actions
162 lines (154 loc) · 3.97 KB
/
browser.js
File metadata and controls
162 lines (154 loc) · 3.97 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
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
'use strict';
/**
* [WeChat client sdk]
* @param {[type]} config [description]
* @param {[type]} ready [description]
* @docs http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html
*/
function WeChat(config){
if(!(this instanceof WeChat)){
return new WeChat(config, apis, done);
}
var defaults = {
signType: 'sha1'
};
for(var key in config){
defaults[ key ] = config[ key ];
}
this.config = defaults;
return this;
};
/**
* [function merge]
* @param {[type]} a [description]
* @param {[type]} b [description]
* @return {[type]} [description]
*/
WeChat.merge = function(a, b){
var obj = {}, key;
for(key in a) obj[key] = a[key];
for(key in b) obj[key] = b[key];
return obj;
};
/**
* [function copy]
* @param {[type]} src [description]
* @param {[type]} maps [description]
* @return {[type]} [description]
*/
WeChat.copy = function(src, maps){
var obj = {};
for(var to in maps){
var from = maps[to];
obj[ to ] = String(src[ from ]);
}
return obj;
};
/**
* [function log]
* @param {[type]} msg [description]
* @return {[type]} [description]
*/
WeChat.prototype.log = function(msg){
msg = [ '[WeChat]', msg ].join(': ');
if(this.config.debug) alert(msg);
else console.debug(msg);
return this;
};
/**
* [function ready]
* @param {Function} callback [description]
* @return {[type]} [description]
*/
WeChat.prototype.ready = function(callback){
var self = this;
function done(){
self.bridge = WeixinJSBridge;
callback.call(self, self);
return self;
};
if(typeof WeixinJSBridge === 'object'){
return done();
}
if(document.addEventListener){
document.addEventListener("WeixinJSBridgeReady", done, false);
}else if(document.attachEvent){
document.attachEvent("WeixinJSBridgeReady", done);
document.attachEvent("onWeixinJSBridgeReady", done);
}
return this;
};
/**
* [function init]
* @param {[type]} apis [description]
* @param {Function} callback [description]
* @return {[type]} [description]
*/
WeChat.prototype.init = function(apis, callback){
return this.invoke('preVerifyJSAPI', {
verifyJsApiList: apis
}, callback);
};
/**
* [function description]
* @param {[type]} event [description]
* @param {[type]} handler [description]
* @return {[type]} [description]
*/
WeChat.prototype.on = function(event, handler){
if(!this.bridge) return this.log('bridge is not defined');
this.bridge.on(event, handler);
return this;
};
/**
* [function description]
* @param {[type]} method [description]
* @param {[type]} args [description]
* @param {Function} callback [description]
* @return {[type]} [description]
*/
WeChat.prototype.invoke = function(method, args, callback){
if(!this.bridge) return this.log('bridge is not defined');
var params = WeChat.copy(this.config, {
appId : 'appId' ,
verifyAppId : 'appId' ,
verifySignType : 'signType' ,
verifyTimestamp : 'timestamp' ,
verifyNonceStr : 'noncestr' ,
verifySignature : 'signature'
});
this.bridge.invoke(method, WeChat.merge(params, args), callback);
return this;
};
/**
* [function network]
* @param {Function} callback [description]
* @return {[type]} [description]
*/
WeChat.prototype.network = function(callback){
return this.invoke('getNetworkType', null, function(result){
callback(result.err_msg.match(/network_type:(\w+)/)[1]);
});
};
/**
* [function description]
* @param {Function} callback [description]
* @return {[type]} [description]
*/
WeChat.prototype.close = function(callback){
return this.invoke('closeWindow', null, callback);
};
/**
* [expose wechat]
* @param {[type]}
* @return {[type]}
*/
if (typeof define === 'function' && define.amd) {
define([], function() {
return WeChat;
});
} else if (typeof module !== 'undefined' && module.exports) {
module.exports = WeChat;
} else {
window.WeChat = WeChat;
}