You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Index.js 4.3 KiB

4 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. (function ($) {
  2.     var Qrcode = function (tempBtn) {
  3.         var _this_ = this;
  4.         var isWeiboWebView = /__weibo__/.test(navigator.userAgent);
  5.         if (isWeiboWebView) {
  6.             if (window.WeiboJSBridge) {
  7.                 _this_.bridgeReady(tempBtn);
  8.             } else {
  9.                 document.addEventListener('WeiboJSBridgeReady', function () {
  10.                     _this_.bridgeReady(tempBtn);
  11.                 });
  12.             }
  13.         } else {
  14.             _this_.nativeReady(tempBtn);
  15.         }
  16.     };
  17.     Qrcode.prototype = {
  18.         nativeReady: function (tempBtn) {
  19.             $('[node-type=jsbridge]', tempBtn).on('click', function (e) {
  20.                 e.stopPropagation();
  21.             });
  22.             $(tempBtn).bind('click', function (e) {
  23.                 $(this).find('input[node-type=jsbridge]').trigger('click');
  24.             });
  25.             $(tempBtn).bind('change', this.getImgFile);
  26.         },
  27.         bridgeReady: function (tempBtn) {
  28.             $(tempBtn).bind('click', this.weiBoBridge);
  29.         },
  30.         weiBoBridge: function () {
  31.             window.WeiboJSBridge.invoke('scanQRCode', null, function (params) {
  32.                 //得到扫码的结果
  33.                 $('.result-qrcode').append(params.result + '<br/>');
  34.             });
  35.         },
  36.         getImgFile: function () {
  37.             var _this_ = this;
  38.             var inputDom = $(this).find('input[node-type=jsbridge]');
  39.             var imgFile = inputDom[0].files;
  40.             var oFile = imgFile[0];
  41.             var oFReader = new FileReader();
  42.             var rFilter = /^(?:image\/bmp|image\/cis\-cod|image\/gif|image\/ief|image\/jpeg|image\/jpeg|image\/jpeg|image\/pipeg|image\/png|image\/svg\+xml|image\/tiff|image\/x\-cmu\-raster|image\/x\-cmx|image\/x\-icon|image\/x\-portable\-anymap|image\/x\-portable\-bitmap|image\/x\-portable\-graymap|image\/x\-portable\-pixmap|image\/x\-rgb|image\/x\-xbitmap|image\/x\-xpixmap|image\/x\-xwindowdump)$/i;
  43.             if (imgFile.length === 0) {
  44.                 return;
  45.             }
  46.             if (!rFilter.test(oFile.type)) {
  47.                 alert("选择正确的图片格式!");
  48.                 return;
  49.             }
  50.             oFReader.onload = function (oFREvent) {
  51.                 qrcode.decode(oFREvent.target.result);
  52.                 qrcode.callback = function (data) {
  53.                     //得到扫码的结果
  54.                     $('.result-qrcode').append(data + '<br/>');
  55.                 };
  56.             };
  57.             oFReader.readAsDataURL(oFile);
  58.         },
  59.         destory: function () {
  60.             $(tempBtn).off('click');
  61.         }
  62.     };
  63.     Qrcode.init = function (tempBtn) {
  64.         var _this_ = this;
  65.         tempBtn.each(function () {
  66.             new _this_($(this));
  67.         });
  68.     };
  69.     window.Qrcode = Qrcode;
  70. })(window.Zepto ? Zepto : jQuery);