|
@@ -0,0 +1,219 @@
|
|
1
|
+function jssdk(wid, domain) {
|
|
2
|
+ this.wid = wid;
|
|
3
|
+ if (typeof domain == "undefined") {
|
|
4
|
+ domain = window.location.origin;
|
|
5
|
+ }
|
|
6
|
+ this.domain = domain;
|
|
7
|
+}
|
|
8
|
+
|
|
9
|
+// title,desc,link,imgUrl
|
|
10
|
+jssdk.prototype._shareQQ = function (shareData) {
|
|
11
|
+ wx.onMenuShareQQ(shareData);
|
|
12
|
+};
|
|
13
|
+
|
|
14
|
+// title,desc,link,imgUrl
|
|
15
|
+jssdk.prototype._shareWeibo = function (shareData) {
|
|
16
|
+ wx.onMenuShareWeibo(shareData);
|
|
17
|
+};
|
|
18
|
+
|
|
19
|
+// title,link,imgUrl
|
|
20
|
+jssdk.prototype._shareTimeline = function (shareData) {
|
|
21
|
+ wx.onMenuShareTimeline(shareData);
|
|
22
|
+};
|
|
23
|
+// title,desc,link,imgUrl
|
|
24
|
+// type 分享类型 music/video/link[default]
|
|
25
|
+// dataUrl type=music/video,则要提供数据链接,默认为空
|
|
26
|
+jssdk.prototype._shareAppMessage = function (shareData) {
|
|
27
|
+ wx.onMenuShareAppMessage(shareData);
|
|
28
|
+};
|
|
29
|
+//title 分享给朋友标题 desc 分享给朋友描述 imgUrl 分享图标 timetitle 分享到朋友圈标题
|
|
30
|
+jssdk.prototype.share = function (title, desc, imgUrl, timetitle, success) {
|
|
31
|
+ if (typeof success != "function") {
|
|
32
|
+ success = function () {
|
|
33
|
+ };
|
|
34
|
+ }
|
|
35
|
+ var link=arguments[5] ? arguments[5] : _sdk_config.getUrl(true);
|
|
36
|
+ _sdk_config.config(this.wid, this.domain);
|
|
37
|
+ var that = this;
|
|
38
|
+ wx.ready(function () {
|
|
39
|
+ that._shareQQ({title: title, desc: desc, link: link, imgUrl: imgUrl, success: success});
|
|
40
|
+ that._shareWeibo({title: title, desc: desc, link: link, imgUrl: imgUrl, success: success});
|
|
41
|
+ that._shareTimeline({title: timetitle, link: link, imgUrl: imgUrl, success: success});
|
|
42
|
+ that._shareAppMessage({title: title, desc: desc, link: link, imgUrl: imgUrl, success: success});
|
|
43
|
+ });
|
|
44
|
+}
|
|
45
|
+
|
|
46
|
+jssdk.prototype.config = function (debug) {
|
|
47
|
+ _sdk_config.config(this.wid, this.domain, debug);
|
|
48
|
+}
|
|
49
|
+jssdk.prototype.previewImage = function (data) {
|
|
50
|
+ wx.previewImage(data);
|
|
51
|
+}
|
|
52
|
+
|
|
53
|
+jssdk.prototype.qrcode = function (callback) {
|
|
54
|
+ wx.scanQRCode({
|
|
55
|
+ needResult: 1,
|
|
56
|
+ scanType: ["qrCode","barCode"],
|
|
57
|
+ success: function (res) {
|
|
58
|
+ callback(res.resultStr);
|
|
59
|
+ }
|
|
60
|
+ });
|
|
61
|
+};
|
|
62
|
+
|
|
63
|
+jssdk.prototype.getLocation = function (callback) {
|
|
64
|
+ wx.ready(function(){
|
|
65
|
+ wx.getLocation({
|
|
66
|
+ type: 'wgs84', // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
|
|
67
|
+ success: function (res) {
|
|
68
|
+ callback(res);
|
|
69
|
+ }
|
|
70
|
+ })
|
|
71
|
+ });
|
|
72
|
+}
|
|
73
|
+
|
|
74
|
+jssdk.prototype.chooseWXPay = function (option) {
|
|
75
|
+ wx.chooseWXPay(option);
|
|
76
|
+};
|
|
77
|
+
|
|
78
|
+// 控制分享接口
|
|
79
|
+jssdk.prototype.controlShareMenu = function(option){
|
|
80
|
+ _sdk_config.config(this.wid, this.domain);
|
|
81
|
+ var that = this;
|
|
82
|
+ wx.ready(function(){
|
|
83
|
+ if (option != 'hideAllNonBaseMenuItem') {
|
|
84
|
+ that._hideMenuItems(option);
|
|
85
|
+ } else {
|
|
86
|
+ wx.hideAllNonBaseMenuItem(); // 隐藏所有
|
|
87
|
+ }
|
|
88
|
+ });
|
|
89
|
+};
|
|
90
|
+// 选择隐藏
|
|
91
|
+jssdk.prototype._hideMenuItems = function(option){
|
|
92
|
+ var option = eval("("+option+")");
|
|
93
|
+ wx.hideMenuItems({
|
|
94
|
+ menuList: option
|
|
95
|
+ });
|
|
96
|
+};
|
|
97
|
+
|
|
98
|
+jssdk.prototype.chooseImage = function (option, callback) {
|
|
99
|
+ var def = {
|
|
100
|
+ count: 9,
|
|
101
|
+ sizeType: ['original', 'compressed'],
|
|
102
|
+ sourceType: ['album', 'camera'],
|
|
103
|
+ success: function(res) {
|
|
104
|
+ callback(res);
|
|
105
|
+ }
|
|
106
|
+ };
|
|
107
|
+ for(var i in option) {
|
|
108
|
+ def[i] = option[i];
|
|
109
|
+ }
|
|
110
|
+ wx.chooseImage(def);
|
|
111
|
+};
|
|
112
|
+
|
|
113
|
+jssdk.prototype.uploadImage = function (option, callback) {
|
|
114
|
+ var def = {
|
|
115
|
+ localId: '',
|
|
116
|
+ isShowProgressTips: 1,
|
|
117
|
+ success: function(res) {
|
|
118
|
+ callback(res);
|
|
119
|
+ }
|
|
120
|
+ };
|
|
121
|
+ for(var i in option) {
|
|
122
|
+ def[i] = option[i];
|
|
123
|
+ }
|
|
124
|
+ wx.uploadImage(def);
|
|
125
|
+};
|
|
126
|
+
|
|
127
|
+var _sdk_config = {
|
|
128
|
+ _config_state: [],
|
|
129
|
+ _url: "",
|
|
130
|
+ _apiList: [
|
|
131
|
+ 'onMenuShareTimeline',
|
|
132
|
+ 'onMenuShareAppMessage',
|
|
133
|
+ 'onMenuShareQQ',
|
|
134
|
+ 'onMenuShareWeibo',
|
|
135
|
+ 'scanQRCode',
|
|
136
|
+ 'chooseWXPay',
|
|
137
|
+ 'previewImage',
|
|
138
|
+ 'openLocation',
|
|
139
|
+ 'getLocation',
|
|
140
|
+ 'openEnterpriseChat',
|
|
141
|
+ 'openEnterpriseContact',
|
|
142
|
+ 'hideAllNonBaseMenuItem', // 添加
|
|
143
|
+ 'hideMenuItems',
|
|
144
|
+ 'chooseImage',
|
|
145
|
+ 'uploadImage'
|
|
146
|
+ ],
|
|
147
|
+ addApiList: function (api) {
|
|
148
|
+ this._apiList.push(api);
|
|
149
|
+ },
|
|
150
|
+ config: function (wid, domain, debug, force) {
|
|
151
|
+ if (this._config_state[wid] && !force) {
|
|
152
|
+ return;
|
|
153
|
+ }
|
|
154
|
+ this._config_state[wid] = true;
|
|
155
|
+ var that = this;
|
|
156
|
+ $.ajax({
|
|
157
|
+ type: "get",
|
|
158
|
+ url: domain + '/uc/api/wx-jssdk/config',
|
|
159
|
+ dataType: "jsonp",
|
|
160
|
+ jsonp: "callback",
|
|
161
|
+ data: ({
|
|
162
|
+ wid: wid,
|
|
163
|
+ url: this.getUrl()
|
|
164
|
+ }),
|
|
165
|
+ success: function (data) {
|
|
166
|
+ var configData = {
|
|
167
|
+ // beta: true,// 必须这么写,否则wx.invoke调用形式的jsapi会有问题
|
|
168
|
+ debug: debug ? true : false,
|
|
169
|
+ appId: data.appId,
|
|
170
|
+ timestamp: data.timestamp,
|
|
171
|
+ nonceStr: data.nonceStr,
|
|
172
|
+ signature: data.signature,
|
|
173
|
+ jsApiList: that._apiList
|
|
174
|
+ };
|
|
175
|
+ wx.config(configData);
|
|
176
|
+ }
|
|
177
|
+ });
|
|
178
|
+ },
|
|
179
|
+ getUrl: function (clear) {
|
|
180
|
+ if (!this._url) {
|
|
181
|
+ var l = window.location;
|
|
182
|
+ this._url = l.protocol + '//' + l.host + l.pathname + l.search;
|
|
183
|
+ }
|
|
184
|
+
|
|
185
|
+ if (typeof clear != "undefined") {
|
|
186
|
+ return this.delQueStr(this._url, ['code', 'CODE', 'state', 'STATE']);
|
|
187
|
+ }
|
|
188
|
+ return this._url;
|
|
189
|
+ },
|
|
190
|
+ delQueStr: function (url, strs) {
|
|
191
|
+ var str = "";
|
|
192
|
+
|
|
193
|
+ if (url.indexOf('?') != -1)
|
|
194
|
+ str = url.substr(url.indexOf('?') + 1);
|
|
195
|
+ else
|
|
196
|
+ return url;
|
|
197
|
+ for (var i = 0; i < strs.length; i++) {
|
|
198
|
+ var returnurl = "";
|
|
199
|
+ var ref = strs[i];
|
|
200
|
+ if (str.indexOf('&') != -1) {
|
|
201
|
+ var arr = str.split('&');
|
|
202
|
+ for (var ii in arr) {
|
|
203
|
+ var nf = arr[ii].split('=');
|
|
204
|
+ if (nf[0] != ref) {
|
|
205
|
+ returnurl += arr[ii] + "&";
|
|
206
|
+ }
|
|
207
|
+ }
|
|
208
|
+ url = url.substr(0, url.indexOf('?')) + "?" + returnurl.substr(0, returnurl.length - 1);
|
|
209
|
+ } else {
|
|
210
|
+ var arr = str.split('=');
|
|
211
|
+ if (arr[0] == ref)
|
|
212
|
+ url = url.substr(0, url.indexOf('?'));
|
|
213
|
+ else
|
|
214
|
+ url = url;
|
|
215
|
+ }
|
|
216
|
+ }
|
|
217
|
+ return url;
|
|
218
|
+ }
|
|
219
|
+};
|