From 8c2dd408ba88746f23e2af438e68e480dd05af85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=BF=9C=E6=B4=8B?= Date: Wed, 20 May 2020 14:44:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=BB=98=E5=88=B6=E5=AE=8C?= =?UTF-8?q?=E6=88=90=E5=9B=9E=E8=B0=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 23 ++++--- app.json | 17 +++--- pages/main/index.js | 105 +++++++++++++++++--------------- pages/main/index.json | 2 +- pages/main/index.wxml | 12 ++-- pages/main/index.wxss | 54 ++++++++++++++--- project.config.json | 25 ++++++-- sitemap.json | 7 +++ utils/qrcode.js | 137 +++++++++++++++++++++++------------------- 9 files changed, 230 insertions(+), 152 deletions(-) create mode 100644 sitemap.json diff --git a/README.md b/README.md index 1d86388..0f58577 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,7 @@ > 本项目用于在微信小程序中生成二维码,也可用于第三方框架Mpvue,Taro等。 ## 生成预览 + ![jsh5css.cn](http://jsh5css.cn/blog/wp-content/uploads/2016/12/20161207143611_73427.png) * 可支持输入中文文本 @@ -13,15 +14,16 @@ git clone https://github.com/demi520/wxapp-qrcode.git ``` - ## 使用 1.创建canvas节点,以及设置canvas-id。(可以控制该区域不显示,但是必须要存在) + ```html ``` -2.引入qrcode.js,将` utils/qrcode.js` 文件复制到自己工程里,并引入。 +2.引入qrcode.js,将`utils/qrcode.js` 文件复制到自己工程里,并引入。 + ```javascript // 注意: 这里xxx是你自己的路径 let QR = require("xxxx/qrcode.js") // require方式 @@ -29,11 +31,12 @@ import QR from 'xxxx/qrcode.js' // es6的方式 ``` 3.在js文件中,定义相关的方法,**要注意在data中创建imagePath(最终生成的图片路径),可以将img的src属性绑定imagePath** + ```javascript createQrCode: function (content, canvasId, cavW, cavH) { //调用插件中的draw方法,绘制二维码图片 - QR.api.draw(content, canvasId, cavW, cavH); - this.canvasToTempImage(canvasId); + //this.canvasToTempImage 为绘制完成的回调函数,可根据自己的业务添加 + QR.api.draw(content, canvasId, cavW, cavH, this, this.canvasToTempImage); }, //获取临时缓存图片路径,存入data中 @@ -45,7 +48,7 @@ canvasToTempImage: function (canvasId) { let tempFilePath = res.tempFilePath; console.log(tempFilePath); that.setData({ // 如果采用mpvue,即 this.imagePath = tempFilePath - imagePath:tempFilePath, + imagePath:tempFilePath, }); }, fail: function (res) { @@ -56,14 +59,14 @@ canvasToTempImage: function (canvasId) { ``` 4.绑定事件,调用createQrCode,生成二维码 + ```javascript createQrCode ('wxapp-qrcode', 'mycanvas', 300, 300) ``` ## FAQ - -### 自定义组件中不能生成qrcode? +### 自定义组件中不能生成qrcode? 封装方法时: 添加上this, `QR.api.draw(url, canvasId, cavW, cavH, this);` 可参考qrcode.js 768行,*[wx.createCanvasContext](https://developers.weixin.qq.com/miniprogram/dev/api/canvas/wx.createCanvasContext.html)* @@ -71,6 +74,8 @@ createQrCode ('wxapp-qrcode', 'mycanvas', 300, 300) 可参考 `pages/main/index.js` 中的 `setCanvasSize` 方法。 +## Ps + +感谢[微信小程序|联盟](http://www.wxapp-union.com/) [@amis](http://www.wxapp-union.com/home.php?mod=space&uid=310)提供的素材和创意; -* 感谢[微信小程序|联盟](http://www.wxapp-union.com/) [@amis](http://www.wxapp-union.com/home.php?mod=space&uid=310)提供的素材和创意; -* 测试有其他问题请回帖哦,感激!! +测试有其他问题请回帖哦,感激!! diff --git a/app.json b/app.json index 22eade7..994d78f 100644 --- a/app.json +++ b/app.json @@ -1,14 +1,13 @@ { - "pages":[ - "pages/main/index", + "pages": [ + "pages/main/index", "pages/logs/logs" - - ], - "window":{ - "backgroundTextStyle":"light", + "window": { + "backgroundTextStyle": "light", "navigationBarBackgroundColor": "#fff", "navigationBarTitleText": "WeChat", - "navigationBarTextStyle":"black" - } -} + "navigationBarTextStyle": "black" + }, + "sitemapLocation": "sitemap.json" +} \ No newline at end of file diff --git a/pages/main/index.js b/pages/main/index.js index 37d25f1..332bcc3 100644 --- a/pages/main/index.js +++ b/pages/main/index.js @@ -1,106 +1,111 @@ // pages/main/index.js var QR = require("../../utils/qrcode.js"); Page({ - data:{ - canvasHidden:false, - maskHidden:true, - imagePath:'', - placeholder:'http://wxapp-union.com'//默认二维码生成文本 + data: { + maskHidden: true, + imagePath: '', + placeholder: 'http://wxapp-union.com' //默认二维码生成文本 }, - onLoad:function(options){ + onLoad: function (options) { // 页面初始化 options为页面跳转所带来的参数 - var size = this.setCanvasSize();//动态设置画布大小 + var size = this.setCanvasSize(); //动态设置画布大小 var initUrl = this.data.placeholder; this.createQrCode(initUrl, "mycanvas", size.w, size.h); - + }, - onReady:function(){ - + onReady: function () { + }, - onShow:function(){ - + onShow: function () { + // 页面显示 }, - onHide:function(){ + onHide: function () { // 页面隐藏 }, - onUnload:function(){ + onUnload: function () { // 页面关闭 }, //适配不同屏幕大小的canvas - setCanvasSize:function(){ - var size={}; + setCanvasSize: function () { + var size = {}; try { - var res = wx.getSystemInfoSync(); - var scale = 750/686;//不同屏幕下canvas的适配比例;设计稿是750宽 - var width = res.windowWidth/scale; - var height = width;//canvas画布为正方形 - size.w = width; - size.h = height; - } catch (e) { - // Do something when catch error - console.log("获取设备信息失败"+e); - } + var res = wx.getSystemInfoSync(); + var scale = 750 / 686; //不同屏幕下canvas的适配比例;设计稿是750宽 + var width = res.windowWidth / scale; + var height = width; //canvas画布为正方形 + size.w = width; + size.h = height; + } catch (e) { + // Do something when catch error + console.log("获取设备信息失败" + e); + } return size; - } , - createQrCode:function(url,canvasId,cavW,cavH){ + }, + createQrCode: function (url, canvasId, cavW, cavH) { //调用插件中的draw方法,绘制二维码图片 - QR.api.draw(url,canvasId,cavW,cavH); - setTimeout(() => { this.canvasToTempImage();},1000); - + QR.api.draw(url, canvasId, cavW, cavH, this, this.canvasToTempImage); + // setTimeout(() => { this.canvasToTempImage();},100); + }, //获取临时缓存照片路径,存入data中 - canvasToTempImage:function(){ + canvasToTempImage: function () { var that = this; wx.canvasToTempFilePath({ canvasId: 'mycanvas', success: function (res) { - var tempFilePath = res.tempFilePath; - console.log(tempFilePath); - that.setData({ - imagePath:tempFilePath, - // canvasHidden:true - }); + var tempFilePath = res.tempFilePath; + console.log(tempFilePath); + that.setData({ + imagePath: tempFilePath, + }); }, fail: function (res) { - console.log(res); + console.log(res); } - }); + }, that); }, //点击图片进行预览,长按保存分享图片 - previewImg:function(e){ + previewImg: function (e) { var img = this.data.imagePath; - console.log(img); wx.previewImage({ current: img, // 当前显示图片的http链接 urls: [img] // 需要预览的图片http链接列表 }) }, - formSubmit: function(e) { + formSubmit: function (e) { var that = this; var url = e.detail.value.url; + if (url === "") { + wx.showToast({ + icon: 'none', + title: '请输入网址', + duration: 2000 + }); + return; + } that.setData({ - maskHidden:false, + maskHidden: false, }); wx.showToast({ title: '生成中...', icon: 'loading', - duration:2000 + duration: 2000 }); - var st = setTimeout(function(){ + var st = setTimeout(function () { wx.hideToast() var size = that.setCanvasSize(); //绘制二维码 - that.createQrCode(url,"mycanvas",size.w,size.h); + that.createQrCode(url, "mycanvas", size.w, size.h); that.setData({ - maskHidden:true + maskHidden: true }); clearTimeout(st); - },2000) - + }, 2000) + } }) \ No newline at end of file diff --git a/pages/main/index.json b/pages/main/index.json index 77ce547..af5e236 100644 --- a/pages/main/index.json +++ b/pages/main/index.json @@ -1,6 +1,6 @@ { "navigationBarBackgroundColor": "#37373b", - "navigationBarTextStyle": "#fff", + "navigationBarTextStyle": "white", "navigationBarTitleText": "二维码生成器", "backgroundColor": "#efeff4", "backgroundTextStyle": "light" diff --git a/pages/main/index.wxml b/pages/main/index.wxml index c94672e..cb37a03 100644 --- a/pages/main/index.wxml +++ b/pages/main/index.wxml @@ -1,4 +1,4 @@ - + @@ -6,16 +6,12 @@
- + -
- + +
\ No newline at end of file diff --git a/pages/main/index.wxss b/pages/main/index.wxss index 095d881..2bff00b 100644 --- a/pages/main/index.wxss +++ b/pages/main/index.wxss @@ -1,21 +1,57 @@ /* pages/main/index.wxss */ -.container-box{background-color: #efeff4;height: 100vh} -.img-box{padding: 32rpx;background-color: #fff;border-bottom: 1rpx solid #e5e5e5;position: relative} -.img-box image{width: 686rpx;height: 686rpx;background-color: #f9f9f9} -.input-row{ +.container-box { + background-color: #efeff4; + height: 100vh +} + +.img-box { + padding: 32rpx; + background-color: #fff; + border-bottom: 1rpx solid #e5e5e5; + position: relative +} + +.img-box image { + width: 686rpx; + height: 686rpx; + background-color: #f9f9f9 +} + +.input-row { margin: 30rpx auto; border-bottom: 1rpx solid #e5e5e5; border-top: 1rpx solid #e5e5e5; display: flex; align-items: center; height: 88rpx; - padding: 0 32rpx; + padding: 0 32rpx; background-color: #fff; font-size: 34rpx; color: #000 } -.input-row input{margin-left: 100rpx;flex: 1} -.mybtn{width:686rpx;margin: 60rpx auto } -.mask{position: fixed;top: 0;left: 0;z-index: 3; width: 100%;height: 100%;opacity: 0;} -.canvas-box{position: fixed;top:999999rpx;left: 0} +.input-row input { + margin-left: 100rpx; + flex: 1 +} + +.mybtn { + width: 686rpx; + margin: 60rpx auto +} + +.mask { + position: fixed; + top: 0; + left: 0; + z-index: 3; + width: 100%; + height: 100%; + opacity: 0; +} + +.canvas-box { + position: fixed; + top: 999999rpx; + left: 0 +} \ No newline at end of file diff --git a/project.config.json b/project.config.json index f3727e1..3d79033 100644 --- a/project.config.json +++ b/project.config.json @@ -1,16 +1,33 @@ { "description": "项目配置文件。", "setting": { - "urlCheck": true, + "urlCheck": false, "es6": true, "postcss": true, + "preloadBackgroundData": false, "minified": true, - "newFeature": true + "newFeature": true, + "coverView": true, + "autoAudits": false, + "showShadowRootInWxmlPanel": true, + "scopeDataCheck": false, + "checkInvalidKey": true, + "checkSiteMap": true, + "uploadWithSourceMap": true, + "babelSetting": { + "ignore": [], + "disablePlugins": [], + "outputPath": "" + }, + "useCompilerModule": false, + "userConfirmedUseCompilerModuleSwitch": false }, "compileType": "miniprogram", - "libVersion": "1.6.6", - "appid": "XXXXXXX", + "libVersion": "2.11.0", + "appid": "wxa15f1206011ec023", "projectname": "qrcode", + "simulatorType": "wechat", + "simulatorPluginLibVersion": {}, "condition": { "search": { "current": -1, diff --git a/sitemap.json b/sitemap.json new file mode 100644 index 0000000..ca02add --- /dev/null +++ b/sitemap.json @@ -0,0 +1,7 @@ +{ + "desc": "关于本文件的更多信息,请参考文档 https://developers.weixin.qq.com/miniprogram/dev/framework/sitemap.html", + "rules": [{ + "action": "allow", + "page": "*" + }] +} \ No newline at end of file diff --git a/utils/qrcode.js b/utils/qrcode.js index 45b62ab..9b6fe06 100644 --- a/utils/qrcode.js +++ b/utils/qrcode.js @@ -18,10 +18,10 @@ // final format bits with mask: level << 3 | mask var fmtword = [ - 0x77c4, 0x72f3, 0x7daa, 0x789d, 0x662f, 0x6318, 0x6c41, 0x6976, //L - 0x5412, 0x5125, 0x5e7c, 0x5b4b, 0x45f9, 0x40ce, 0x4f97, 0x4aa0, //M - 0x355f, 0x3068, 0x3f31, 0x3a06, 0x24b4, 0x2183, 0x2eda, 0x2bed, //Q - 0x1689, 0x13be, 0x1ce7, 0x19d0, 0x0762, 0x0255, 0x0d0c, 0x083b //H + 0x77c4, 0x72f3, 0x7daa, 0x789d, 0x662f, 0x6318, 0x6c41, 0x6976, //L + 0x5412, 0x5125, 0x5e7c, 0x5b4b, 0x45f9, 0x40ce, 0x4f97, 0x4aa0, //M + 0x355f, 0x3068, 0x3f31, 0x3a06, 0x24b4, 0x2183, 0x2eda, 0x2bed, //Q + 0x1689, 0x13be, 0x1ce7, 0x19d0, 0x0762, 0x0255, 0x0d0c, 0x083b //H ]; // 4 per version: number of blocks 1,2; data width; ecc width @@ -110,7 +110,11 @@ // Working buffers: // data input and ecc append, image working buffer, fixed part of image, run lengths for badness - var strinbuf = [], eccbuf = [], qrframe = [], framask = [], rlens = []; + var strinbuf = [], + eccbuf = [], + qrframe = [], + framask = [], + rlens = []; // Control values - width is based on version, last 4 are from table. var version, width, neccblk1, neccblk2, datablkw, eccblkwid; var ecclevel = 2; @@ -171,7 +175,7 @@ strinbuf[ecbuf + i] = 0; for (i = 0; i < dlen; i++) { fb = glog[strinbuf[data + i] ^ strinbuf[ecbuf]]; - if (fb != 255) /* fb term is non-zero */ + if (fb != 255) /* fb term is non-zero */ for (j = 1; j < eclen; j++) strinbuf[ecbuf + j - 1] = strinbuf[ecbuf + j] ^ gexp[modnn(fb + genpoly[eclen - j])]; else @@ -219,7 +223,7 @@ break; case 2: for (y = 0; y < width; y++) - for (r3x = 0, x = 0; x < width; x++ , r3x++) { + for (r3x = 0, x = 0; x < width; x++, r3x++) { if (r3x == 3) r3x = 0; if (!r3x && !ismasked(x, y)) @@ -227,10 +231,10 @@ } break; case 3: - for (r3y = 0, y = 0; y < width; y++ , r3y++) { + for (r3y = 0, y = 0; y < width; y++, r3y++) { if (r3y == 3) r3y = 0; - for (r3x = r3y, x = 0; x < width; x++ , r3x++) { + for (r3x = r3y, x = 0; x < width; x++, r3x++) { if (r3x == 3) r3x = 0; if (!r3x && !ismasked(x, y)) @@ -240,7 +244,7 @@ break; case 4: for (y = 0; y < width; y++) - for (r3x = 0, r3y = ((y >> 1) & 1), x = 0; x < width; x++ , r3x++) { + for (r3x = 0, r3y = ((y >> 1) & 1), x = 0; x < width; x++, r3x++) { if (r3x == 3) { r3x = 0; r3y = !r3y; @@ -250,10 +254,10 @@ } break; case 5: - for (r3y = 0, y = 0; y < width; y++ , r3y++) { + for (r3y = 0, y = 0; y < width; y++, r3y++) { if (r3y == 3) r3y = 0; - for (r3x = 0, x = 0; x < width; x++ , r3x++) { + for (r3x = 0, x = 0; x < width; x++, r3x++) { if (r3x == 3) r3x = 0; if (!((x & y & 1) + !(!r3x | !r3y)) && !ismasked(x, y)) @@ -262,10 +266,10 @@ } break; case 6: - for (r3y = 0, y = 0; y < width; y++ , r3y++) { + for (r3y = 0, y = 0; y < width; y++, r3y++) { if (r3y == 3) r3y = 0; - for (r3x = 0, x = 0; x < width; x++ , r3x++) { + for (r3x = 0, x = 0; x < width; x++, r3x++) { if (r3x == 3) r3x = 0; if (!(((x & y & 1) + (r3x && (r3x == r3y))) & 1) && !ismasked(x, y)) @@ -274,10 +278,10 @@ } break; case 7: - for (r3y = 0, y = 0; y < width; y++ , r3y++) { + for (r3y = 0, y = 0; y < width; y++, r3y++) { if (r3y == 3) r3y = 0; - for (r3x = 0, x = 0; x < width; x++ , r3x++) { + for (r3x = 0, x = 0; x < width; x++, r3x++) { if (r3x == 3) r3x = 0; if (!(((r3x && (r3x == r3y)) + ((x + y) & 1)) & 1) && !ismasked(x, y)) @@ -290,7 +294,10 @@ } // Badness coefficients. - var N1 = 3, N2 = 3, N3 = 40, N4 = 10; + var N1 = 3, + N2 = 3, + N3 = 40, + N4 = 10; // Using the table of the length of each run, calculate the amount of bad image // - long runs or those that look like finders; called twice, once each for X and Y @@ -302,14 +309,17 @@ runsbad += N1 + rlens[i] - 5; // BwBBBwB as in finder for (i = 3; i < length - 1; i += 2) - if (rlens[i - 2] == rlens[i + 2] - && rlens[i + 2] == rlens[i - 1] - && rlens[i - 1] == rlens[i + 1] - && rlens[i - 1] * 3 == rlens[i] + if (rlens[i - 2] == rlens[i + 2] && + rlens[i + 2] == rlens[i - 1] && + rlens[i - 1] == rlens[i + 1] && + rlens[i - 1] * 3 == rlens[i] // white around the black pattern? Not part of spec - && (rlens[i - 3] == 0 // beginning - || i + 3 > length // end - || rlens[i - 3] * 3 >= rlens[i] * 4 || rlens[i + 3] * 3 >= rlens[i] * 4) + && + (rlens[i - 3] == 0 // beginning + || + i + 3 > length // end + || + rlens[i - 3] * 3 >= rlens[i] * 4 || rlens[i + 3] * 3 >= rlens[i] * 4) ) runsbad += N3; return runsbad; @@ -324,10 +334,11 @@ // blocks of same color. for (y = 0; y < width - 1; y++) for (x = 0; x < width - 1; x++) - if ((qrframe[x + width * y] && qrframe[(x + 1) + width * y] - && qrframe[x + width * (y + 1)] && qrframe[(x + 1) + width * (y + 1)]) // all black - || !(qrframe[x + width * y] || qrframe[(x + 1) + width * y] - || qrframe[x + width * (y + 1)] || qrframe[(x + 1) + width * (y + 1)])) // all white + if ((qrframe[x + width * y] && qrframe[(x + 1) + width * y] && + qrframe[x + width * (y + 1)] && qrframe[(x + 1) + width * (y + 1)]) // all black + || + !(qrframe[x + width * y] || qrframe[(x + 1) + width * y] || + qrframe[x + width * (y + 1)] || qrframe[(x + 1) + width * (y + 1)])) // all white thisbad += N2; // X runs @@ -437,7 +448,7 @@ if (version > 1) { t = adelta[version]; y = width - 7; - for (; ;) { + for (;;) { x = width - 7; while (x > t - 3) { putalign(x, y); @@ -484,25 +495,25 @@ setmask(8 + x, 6); setmask(6, 8 + x); } - else { - qrframe[(8 + x) + width * 6] = 1; - qrframe[6 + width * (8 + x)] = 1; - } + else { + qrframe[(8 + x) + width * 6] = 1; + qrframe[6 + width * (8 + x)] = 1; + } // version block if (version > 6) { t = vpat[version - 7]; k = 17; for (x = 0; x < 6; x++) - for (y = 0; y < 3; y++ , k--) + for (y = 0; y < 3; y++, k--) if (1 & (k > 11 ? version >> (k - 12) : t >> k)) { qrframe[(5 - x) + width * (2 - y + width - 11)] = 1; qrframe[(2 - y + width - 11) + width * (5 - x)] = 1; } - else { - setmask(5 - x, 2 - y + width - 11); - setmask(2 - y + width - 11, 5 - x); - } + else { + setmask(5 - x, 2 - y + width - 11); + setmask(2 - y + width - 11, 5 - x); + } } // sync mask bits - only set above for white spaces, so add in black bits @@ -541,8 +552,7 @@ strinbuf[2] |= 255 & (v << 4); strinbuf[1] = v >> 4; strinbuf[0] = 0x40 | (v >> 12); - } - else { + } else { strinbuf[i + 1] = 0; strinbuf[i + 2] = 0; while (i--) { @@ -568,8 +578,8 @@ for (i = 0; i < eccblkwid; i++) { genpoly[i + 1] = 1; for (j = i; j > 0; j--) - genpoly[j] = genpoly[j] - ? genpoly[j - 1] ^ gexp[modnn(glog[genpoly[j]] + i)] : genpoly[j - 1]; + genpoly[j] = genpoly[j] ? + genpoly[j - 1] ^ gexp[modnn(glog[genpoly[j]] + i)] : genpoly[j - 1]; genpoly[0] = gexp[modnn(glog[genpoly[0]] + i)]; } for (i = 0; i <= eccblkwid; i++) @@ -605,15 +615,15 @@ // pack bits into frame avoiding masked area. x = y = width - 1; - k = v = 1; // up, minus + k = v = 1; // up, minus /* inteleaved data and ecc codes */ m = (datablkw + eccblkwid) * (neccblk1 + neccblk2) + neccblk2; for (i = 0; i < m; i++) { t = strinbuf[i]; - for (j = 0; j < 8; j++ , t <<= 1) { + for (j = 0; j < 8; j++, t <<= 1) { if (0x80 & t) qrframe[x + width * y] = 1; - do { // find next fill position + do { // find next fill position if (v) x--; else { @@ -629,8 +639,7 @@ y = 9; } } - } - else { + } else { if (y != width - 1) y++; else { @@ -650,29 +659,29 @@ // save pre-mask copy of frame strinbuf = qrframe.slice(0); - t = 0; // best - y = 30000; // demerit + t = 0; // best + y = 30000; // demerit // for instead of while since in original arduino code // if an early mask was "good enough" it wouldn't try for a better one // since they get more complex and take longer. for (k = 0; k < 8; k++) { - applymask(k); // returns black-white imbalance + applymask(k); // returns black-white imbalance x = badcheck(); if (x < y) { // current mask better than previous best? y = x; t = k; } if (t == 7) - break; // don't increment i to a void redoing mask + break; // don't increment i to a void redoing mask qrframe = strinbuf.slice(0); // reset for next pass } - if (t != k) // redo best mask - none good enough, last wasn't t + if (t != k) // redo best mask - none good enough, last wasn't t applymask(t); // add in final mask/ecclevel bytes y = fmtword[t + ((ecclevel - 1) << 3)]; // low byte - for (k = 0; k < 8; k++ , y >>= 1) + for (k = 0; k < 8; k++, y >>= 1) if (y & 1) { qrframe[(width - 1 - k) + width * 8] = 1; if (k < 6) @@ -681,7 +690,7 @@ qrframe[8 + width * (k + 1)] = 1; } // high byte - for (k = 0; k < 7; k++ , y >>= 1) + for (k = 0; k < 7; k++, y >>= 1) if (y & 1) { qrframe[8 + width * (width - 7 + k)] = 1; if (k) @@ -749,8 +758,8 @@ }, /** * 新增$this参数,传入组件的this,兼容在组件中生成 - */ - draw: function (str, canvas, cavW, cavH, $this, ecc) { + */ + draw: function (str, canvas, cavW, cavH, $this, cb = function () {}, ecc) { var that = this; ecclevel = ecc || ecclevel; canvas = canvas || _canvas; @@ -760,11 +769,11 @@ } var size = Math.min(cavW, cavH); - str = that.utf16to8(str);//增加中文显示 + str = that.utf16to8(str); //增加中文显示 var frame = that.getFrame(str), // 组件中生成qrcode需要绑定this - ctx = wx.createCanvasContext(canvas,$this), + ctx = wx.createCanvasContext(canvas, $this), px = Math.round(size / (width + 8)); var roundedSize = px * (width + 8), offset = Math.floor((size - roundedSize) / 2); @@ -780,10 +789,14 @@ } } } - ctx.draw(); + //--增加绘制完成回调 + ctx.draw(false, function () { + cb(); + }) + } } - module.exports = { api } - // exports.draw = api; - + module.exports = { + api + } })(); \ No newline at end of file