1 | var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
|
2 |
|
3 | import jsonp from './../jsonp';
|
4 | import DB from './../DB';
|
5 | import Device from './../Device';
|
6 | import Toast from './../Toast/instance.js';
|
7 | import Alert from './../Alert/instance.js';
|
8 | import Loading from './../Loading/instance.js';
|
9 |
|
10 | if (!window._seeds_lang) window._seeds_lang = {};
|
11 |
|
12 | var Bridge = {
|
13 | |
14 |
|
15 |
|
16 | debug: false,
|
17 |
|
18 | tel: function tel(number) {
|
19 | if (Device.device === 'pc') {
|
20 | this.showToast(window._seeds_lang['hint_only_mobile'] || '此功能仅可在手机中使用');
|
21 | return;
|
22 | }
|
23 | if (isNaN(number)) return;
|
24 | window.location.href = 'tel:' + number;
|
25 | },
|
26 |
|
27 | toast: null,
|
28 | showToast: function showToast(msg) {
|
29 | var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
30 |
|
31 | if (!msg) return;
|
32 | if (!this.toast) {
|
33 |
|
34 | this.toast = new Toast({
|
35 | parent: document.body,
|
36 | maskClass: 'mask toast-mask' + (params.mask === false ? ' toast-propagation' : ''),
|
37 | toastClass: 'toast ' + (params.position ? params.position : 'middle'),
|
38 | icon: params.icon || '',
|
39 | html: msg,
|
40 | delay: params.delay || 2000
|
41 | });
|
42 | } else {
|
43 | this.toast.setHTML(msg);
|
44 | this.toast.setMaskClassName('mask toast-mask' + (params.mask === false ? ' toast-propagation' : ''));
|
45 | this.toast.setToastClassName('toast ' + (params.position ? params.position : 'middle'));
|
46 | this.toast.setIcon(params.icon || '');
|
47 | this.toast.setDelay(params.delay || 2000);
|
48 | }
|
49 | this.toast.show();
|
50 | if (params.success) {
|
51 | setTimeout(function () {
|
52 | params.success();
|
53 | }, params.delay ? Math.round(params.delay / 2) : 1000);
|
54 | }
|
55 | },
|
56 |
|
57 | loading: null,
|
58 | showLoading: function showLoading() {
|
59 | var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
60 |
|
61 | if (!this.loading) {
|
62 | this.loading = new Loading({
|
63 | caption: params.caption || window._seeds_lang['loading'] || '正在加载...',
|
64 | type: params.type,
|
65 | icon: params.icon || '',
|
66 | maskCss: params.css || null
|
67 | });
|
68 | } else {
|
69 | if (params.caption) this.loading.setCaption(params.caption);
|
70 | if (params.type) this.loading.setType(params.type);
|
71 | if (params.css) this.loading.setMaskCss(params.css);
|
72 | if (params.icon) this.toast.setIcon(params.icon || '');
|
73 | if (params.mask) this.loading.setMaskClassName('mask loading-mask ' + (params.mask === false ? ' loading-propagation' : ''));
|
74 | }
|
75 | this.loading.show();
|
76 | },
|
77 | hideLoading: function hideLoading() {
|
78 | if (!this.loading) {
|
79 | this.toast.showToast(window._seeds_lang['hint_hideloading_after_showloading'] || 'showLoading后才能hideLoading');
|
80 | } else {
|
81 | this.loading.hide();
|
82 | }
|
83 | },
|
84 |
|
85 | alert: null,
|
86 | showAlert: function showAlert(msg) {
|
87 | var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
88 |
|
89 | if (!this.alert) {
|
90 | this.alert = new Alert(_extends({}, params, {
|
91 | html: msg,
|
92 | onClickSubmit: function onClickSubmit(e) {
|
93 | if (params.success) params.success(e);else e.hide();
|
94 | }
|
95 | }));
|
96 | } else {
|
97 | if (params) {
|
98 | this.alert.reset();
|
99 | for (var n in params) {
|
100 | this.alert.params[n] = params[n];
|
101 | }
|
102 | this.alert.updateDOM();
|
103 | this.alert.setHTML(msg);
|
104 | }
|
105 | }
|
106 | this.alert.show();
|
107 | },
|
108 |
|
109 | confirm: null,
|
110 | showConfirm: function showConfirm(msg) {
|
111 | var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
112 |
|
113 | if (!this.confirm) {
|
114 | this.confirm = new Alert(_extends({}, params, {
|
115 | html: msg,
|
116 | onClickSubmit: function onClickSubmit(e) {
|
117 | if (params.success) params.success(e);
|
118 | },
|
119 | onClickCancel: function onClickCancel(e) {
|
120 | if (params.fail) params.fail(e);else e.hide();
|
121 | }
|
122 | }));
|
123 | } else {
|
124 | if (params) {
|
125 | this.confirm.reset();
|
126 | for (var n in params) {
|
127 | this.confirm.params[n] = params[n];
|
128 | }
|
129 | this.confirm.updateDOM();
|
130 | if (params.success) this.confirm.setOnClickSubmit(params.success);
|
131 | if (params.fail) this.confirm.setOnClickCancel(params.fail);
|
132 | }
|
133 | this.confirm.setHTML(msg);
|
134 | }
|
135 | this.confirm.show();
|
136 | },
|
137 | |
138 |
|
139 |
|
140 |
|
141 |
|
142 | getAddress: function getAddress() {
|
143 | var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
144 |
|
145 | var url = 'https://api.map.baidu.com/geocoder/v2/?callback=renderReverse&location=' + params.latitude + ',' + params.longitude + '&output=json&pois=1&ak=IlfRglMOvFxapn5eGrmAj65H&ret_coordtype=gcj02ll';
|
146 | jsonp(url, null, function (err, data) {
|
147 | if (err) {
|
148 | if (params.fail) params.fail({ errMsg: 'getAddress:' + (window._seeds_lang['hint_address_failed'] || '获取地址失败, 请稍后重试') + err });
|
149 | } else {
|
150 | var addrs = {};
|
151 | if (data.result && data.result.formatted_address) {
|
152 | addrs.address = data.result.formatted_address;
|
153 | if (params.success) params.success(addrs);
|
154 | } else {
|
155 | if (params.fail) params.fail({ errMsg: 'getAddress:' + (window._seeds_lang['hint_address_failed'] || '获取地址失败, 请稍后重试') });
|
156 | }
|
157 | }
|
158 | });
|
159 | },
|
160 | |
161 |
|
162 |
|
163 |
|
164 |
|
165 | getWeather: function getWeather() {
|
166 | var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
167 |
|
168 | var url = 'http://api.map.baidu.com/telematics/v3/weather?location=' + (params.location || '南京市') + '&output=json&ak=IlfRglMOvFxapn5eGrmAj65H';
|
169 | jsonp(url, null, function (err, data) {
|
170 | if (err) {
|
171 | if (params.fail) params.fail({ errMsg: 'getWeather:' + (window._seeds_lang['hint_weather_failed'] || '获取天气失败, 请稍后重试') + err });
|
172 | } else {
|
173 | if (data.results && data.results.length) {
|
174 | if (params.success) params.success(data.results);
|
175 | } else {
|
176 | if (params.fail) params.fail({ errMsg: 'getWeather:' + (window._seeds_lang['hint_weather_failed'] || '获取天气失败, 请稍后重试') });
|
177 | }
|
178 | }
|
179 | });
|
180 | },
|
181 |
|
182 | back: function back(argHistory, argBackLvl) {
|
183 |
|
184 | var _history = window.history;
|
185 | if (argHistory && argHistory.go) _history = argHistory;
|
186 | var _backLvl = argBackLvl || -1;
|
187 |
|
188 |
|
189 | var isFromApp = Device.getUrlParameter('isFromApp', location.search) || '';
|
190 | if (isFromApp === '1') {
|
191 |
|
192 | try {
|
193 | Bridge.closeWindow();
|
194 | } catch (error) {
|
195 | console.log(error);
|
196 | }
|
197 | } else if (isFromApp === 'home') {
|
198 |
|
199 | try {
|
200 | Bridge.goHome();
|
201 | } catch (error) {
|
202 | console.log(error);
|
203 | }
|
204 | } else if (isFromApp === 'confirm') {
|
205 |
|
206 | Bridge.showConfirm(Bridge.confirmCaption || window._seeds_lang['confirm_quit_page'] || '您确定要离开此页面吗?', {
|
207 | success: function success(e) {
|
208 | e.hide();
|
209 | _history.go(_backLvl);
|
210 | }
|
211 | });
|
212 | } else if (isFromApp === 'confirm-close') {
|
213 |
|
214 | Bridge.showConfirm(Bridge.confirmCaption || window._seeds_lang['confirm_quit_page'] || '您确定要离开此页面吗?', {
|
215 | success: function success(e) {
|
216 | e.hide();
|
217 | Bridge.closeWindow();
|
218 | }
|
219 | });
|
220 | } else if (isFromApp === 'custom') {
|
221 | console.log('自定义');
|
222 | } else {
|
223 |
|
224 | _history.go(_backLvl);
|
225 | }
|
226 | },
|
227 |
|
228 | isHomePage: function isHomePage(callback, rule) {
|
229 | if (rule && window.location.href.indexOf(rule) >= 0) {
|
230 | callback(true);
|
231 | return;
|
232 | }
|
233 | callback(false);
|
234 | },
|
235 |
|
236 | getAppVersion: function getAppVersion() {
|
237 | return window.navigator.appVersion;
|
238 | },
|
239 |
|
240 | goHome: function goHome() {
|
241 | window.history.go(-1);
|
242 | },
|
243 |
|
244 | logOut: function logOut() {
|
245 | console.log('logOut方法仅在app上工作');
|
246 | },
|
247 |
|
248 | openWindow: function openWindow() {
|
249 | var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
250 |
|
251 | if (params.url) window.location.href = params.url;
|
252 | },
|
253 |
|
254 | closeWindow: function closeWindow() {
|
255 | wx.closeWindow();
|
256 | },
|
257 |
|
258 | addBackPress: function addBackPress(callback) {
|
259 | if (wx.onHistoryBack) {
|
260 |
|
261 | wx.onHistoryBack(function () {
|
262 |
|
263 | if (callback) {
|
264 | callback();
|
265 | } else {
|
266 | Bridge.back();
|
267 | }
|
268 | return false;
|
269 | });
|
270 | }
|
271 | },
|
272 |
|
273 | removeBackPress: function removeBackPress() {
|
274 | if (wx.onHistoryBack) {
|
275 |
|
276 | wx.onHistoryBack(function () {
|
277 |
|
278 | return true;
|
279 | });
|
280 | }
|
281 | },
|
282 | |
283 |
|
284 |
|
285 |
|
286 | |
287 |
|
288 |
|
289 | platform: Device.platform,
|
290 | |
291 |
|
292 |
|
293 |
|
294 |
|
295 |
|
296 |
|
297 |
|
298 |
|
299 | getLocation: function getLocation() {
|
300 | var _this = this;
|
301 |
|
302 | var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
303 |
|
304 |
|
305 | var appLocation = DB.getCookie('app_location');
|
306 | if (appLocation === 'undefined') {
|
307 | DB.removeCookie('app_location');
|
308 | appLocation = '';
|
309 | }
|
310 | try {
|
311 | if (appLocation) appLocation = JSON.parse(appLocation);
|
312 | } catch (error) {
|
313 | appLocation = '';
|
314 | }
|
315 | if (appLocation) {
|
316 | if (params.success) params.success(appLocation);
|
317 | return;
|
318 | }
|
319 |
|
320 | if (this.locating) return;
|
321 | this.locating = true;
|
322 | console.log('调用定位...');
|
323 | wx.getLocation({
|
324 |
|
325 | type: 'gcj02',
|
326 | success: function success(res) {
|
327 | _this.locating = false;
|
328 |
|
329 | if (res.latitude && res.latitude) {
|
330 | if (params.cache) DB.setCookie('app_location', JSON.stringify(res), params.cache || 60);
|
331 | if (params.success) params.success(res);
|
332 | } else {
|
333 | if (params.fail) params.fail(res);else Bridge.showToast('没有获取到经纬度', { mask: false });
|
334 | }
|
335 | },
|
336 | fail: function fail(res) {
|
337 | _this.locating = false;
|
338 | if (params.fail) params.fail(res);else Bridge.showToast('定位失败,请检查微信定位权限是否开启', { mask: false });
|
339 | },
|
340 | complete: function complete(res) {
|
341 | _this.locating = false;
|
342 | if (params.complete) params.complete(res);
|
343 | }
|
344 | });
|
345 | },
|
346 | |
347 |
|
348 |
|
349 |
|
350 | scanQRCode: function scanQRCode() {
|
351 | var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
352 |
|
353 | wx.scanQRCode({
|
354 | needResult: params.needResult || 1,
|
355 | scanType: params.scanType || ['qrCode', 'barCode'],
|
356 | desc: params.desc || '二维码/条码',
|
357 | success: function success(res) {
|
358 | if (!params.success) return;
|
359 | var wxRes = res;
|
360 |
|
361 | if (!params.prefix) {
|
362 | if (res.resultStr.indexOf('QR,') >= 0) {
|
363 | wxRes.resultStr = res.resultStr.substring('QR,'.length);
|
364 | } else if (res.resultStr.indexOf('EAN_13,') >= 0) {
|
365 | wxRes.resultStr = res.resultStr.substring('EAN_13,'.length);
|
366 | } else if (res.resultStr.indexOf('EAN_8,') >= 0) {
|
367 | wxRes.resultStr = res.resultStr.substring('EAN_8,'.length);
|
368 | } else if (res.resultStr.indexOf('AZTEC,') >= 0) {
|
369 | wxRes.resultStr = res.resultStr.substring('AZTEC,'.length);
|
370 | } else if (res.resultStr.indexOf('DATAMATRIX,') >= 0) {
|
371 | wxRes.resultStr = res.resultStr.substring('DATAMATRIX,'.length);
|
372 | } else if (res.resultStr.indexOf('UPCA,') >= 0) {
|
373 | wxRes.resultStr = res.resultStr.substring('UPCA,'.length);
|
374 | } else if (res.resultStr.indexOf('UPCE,') >= 0) {
|
375 | wxRes.resultStr = res.resultStr.substring('UPCE,'.length);
|
376 | } else if (res.resultStr.indexOf('CODABAR,') >= 0) {
|
377 | wxRes.resultStr = res.resultStr.substring('CODABAR,'.length);
|
378 | } else if (res.resultStr.indexOf('CODE_39,') >= 0) {
|
379 | wxRes.resultStr = res.resultStr.substring('CODE_39,'.length);
|
380 | } else if (res.resultStr.indexOf('CODE_93,') >= 0) {
|
381 | wxRes.resultStr = res.resultStr.substring('CODE_93,'.length);
|
382 | } else if (res.resultStr.indexOf('CODE_128,') >= 0) {
|
383 | wxRes.resultStr = res.resultStr.substring('CODE_128,'.length);
|
384 | } else if (res.resultStr.indexOf('ITF,') >= 0) {
|
385 | wxRes.resultStr = res.resultStr.substring('ITF,'.length);
|
386 | } else if (res.resultStr.indexOf('MAXICODE,') >= 0) {
|
387 | wxRes.resultStr = res.resultStr.substring('MAXICODE,'.length);
|
388 | } else if (res.resultStr.indexOf('PDF_417,') >= 0) {
|
389 | wxRes.resultStr = res.resultStr.substring('PDF_417,'.length);
|
390 | } else if (res.resultStr.indexOf('RSS_14,') >= 0) {
|
391 | wxRes.resultStr = res.resultStr.substring('RSS_14,'.length);
|
392 | } else if (res.resultStr.indexOf('RSSEXPANDED,') >= 0) {
|
393 | wxRes.resultStr = res.resultStr.substring('RSSEXPANDED,'.length);
|
394 | }
|
395 | }
|
396 |
|
397 | params.success(wxRes);
|
398 | },
|
399 | fail: function fail(res) {
|
400 | if (params.fail) params.fail(res);else Bridge.showToast(res.errMsg, { mask: false });
|
401 | },
|
402 | cancel: function cancel(res) {
|
403 | if (params.cancel) params.cancel(res);
|
404 | },
|
405 | complete: function complete(res) {
|
406 | if (params.complete) params.complete(res);
|
407 | }
|
408 | });
|
409 | },
|
410 |
|
411 | |
412 |
|
413 |
|
414 |
|
415 |
|
416 | chooseImage: function chooseImage(params) {
|
417 | wx.chooseImage(params);
|
418 | },
|
419 | |
420 |
|
421 |
|
422 |
|
423 |
|
424 |
|
425 |
|
426 |
|
427 |
|
428 |
|
429 | uploadImage: function uploadImage(params) {
|
430 | wx.uploadImage(params);
|
431 | },
|
432 | |
433 |
|
434 |
|
435 |
|
436 | previewImage: function previewImage(params) {
|
437 | wx.previewImage(params);
|
438 | },
|
439 | |
440 |
|
441 |
|
442 |
|
443 |
|
444 |
|
445 |
|
446 |
|
447 | Image: function Image(params) {
|
448 | var s = this;
|
449 | var msg = '';
|
450 |
|
451 | s.choose = function () {
|
452 | var args = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
453 |
|
454 | var option = {
|
455 | enableSafe: args.enableSafe || false,
|
456 | max: args.max || 5,
|
457 | currentCount: args.currentCount || 0,
|
458 | sourceType: args.sourceType || ['album', 'camera'],
|
459 | sizeType: args.sizeType || ['original', 'compressed'],
|
460 | chooseOptions: args.chooseOptions || {},
|
461 | localIds: args.localIds || []
|
462 | };
|
463 | var count = option.max - option.currentCount;
|
464 | if (count <= 0) {
|
465 | msg = (window._seeds_lang['hint_max_upload'] || '最多只能传') + option.max + (window._seeds_lang['photos'] || '张照片');
|
466 | Bridge.showToast(msg);
|
467 | return;
|
468 | }
|
469 |
|
470 | if (option.enableSafe) count = 1;
|
471 | Bridge.chooseImage(Object.assign({
|
472 | count: count,
|
473 | sizeType: option.sizeType,
|
474 | sourceType: option.sourceType,
|
475 | success: function success(res) {
|
476 | var imgMap = {};
|
477 | for (var i = 0, localId; localId = res.localIds[i++];) {
|
478 |
|
479 | if (option.localIds.indexOf(localId) === -1) {
|
480 | imgMap[localId] = {
|
481 | serverId: '',
|
482 | sourceType: res.sourceType
|
483 | };
|
484 | }
|
485 | }
|
486 | if (params.onChooseSuccess) params.onChooseSuccess(imgMap, res);
|
487 | s.upload(imgMap);
|
488 | },
|
489 | fail: function fail(res) {
|
490 | if (params.onChooseFail) params.onChooseFail(res);else Bridge.showToast('选择照片失败', { mask: false });
|
491 | },
|
492 | cancel: function cancel() {},
|
493 | complete: function complete() {}
|
494 | }, option.chooseOptions));
|
495 | };
|
496 |
|
497 | s.upload = function (imgMap) {
|
498 | var imgs = Object.keys(imgMap);
|
499 | var loop = function loop(index) {
|
500 | if (index >= imgs.length) {
|
501 | return;
|
502 | }
|
503 | var img = imgs[index];
|
504 | if (imgMap[img].serverId) {
|
505 | loop(++index);
|
506 | return;
|
507 | }
|
508 | Bridge.uploadImage({
|
509 | localId: img,
|
510 | isShowProgressTips: 1,
|
511 | success: function success(res) {
|
512 | var serverId = res.serverId;
|
513 | imgMap[img].serverId = serverId;
|
514 | if (params.onUploadSuccess) params.onUploadSuccess(imgMap, res);
|
515 | if (index >= imgs.length - 1 && params.onUploadsSuccess) params.onUploadsSuccess(imgMap);
|
516 | loop(++index);
|
517 | },
|
518 | fail: function fail() {
|
519 | var deleteItem = imgMap[img];
|
520 | if (params.onUploadFail) params.onUploadFail(imgMap, { id: img, index: index, item: deleteItem, errMsg: 'uploadImage:' + msg });
|
521 | if (index >= imgs.length - 1 && params.onUploadsSuccess) params.onUploadsSuccess(imgMap);
|
522 | loop(++index);
|
523 | }
|
524 | });
|
525 | };
|
526 | loop(0);
|
527 | };
|
528 | }
|
529 | };
|
530 |
|
531 | export default Bridge; |
\ | No newline at end of file |