index.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import qrjs from './qr.js/index'
  2. /**
  3. * 字符串转换成 UTF-8
  4. * @param {String} str 文本内容
  5. */
  6. const utf16to8 = (str) => {
  7. const len = str.length
  8. let out = ''
  9. for (let i = 0; i < len; i++) {
  10. const c = str.charCodeAt(i)
  11. if ((c >= 0x0001) && (c <= 0x007F)) {
  12. out += str.charAt(i)
  13. } else if (c > 0x07FF) {
  14. out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F))
  15. out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F))
  16. out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F))
  17. } else {
  18. out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F))
  19. out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F))
  20. }
  21. }
  22. return out
  23. }
  24. Component({
  25. properties: {
  26. typeNumber: {
  27. type: Number,
  28. value: -1,
  29. observer(newVal) {
  30. this.draw({
  31. typeNumber: newVal,
  32. })
  33. },
  34. },
  35. errorCorrectLevel: {
  36. type: Number,
  37. value: 2,
  38. observer(newVal) {
  39. this.draw({
  40. errorCorrectLevel: newVal,
  41. })
  42. },
  43. },
  44. width: {
  45. type: Number,
  46. value: 200,
  47. observer(newVal) {
  48. this.draw({
  49. width: newVal,
  50. })
  51. },
  52. },
  53. height: {
  54. type: Number,
  55. value: 200,
  56. observer(newVal) {
  57. this.draw({
  58. height: newVal,
  59. })
  60. },
  61. },
  62. fgColor: {
  63. type: String,
  64. value: 'black',
  65. observer(newVal) {
  66. this.draw({
  67. fgColor: newVal,
  68. })
  69. },
  70. },
  71. bgColor: {
  72. type: String,
  73. value: 'white',
  74. observer(newVal) {
  75. this.draw({
  76. bgColor: newVal,
  77. })
  78. },
  79. },
  80. canvasId: {
  81. type: String,
  82. value: 'wux-qrcode',
  83. },
  84. data: {
  85. type: String,
  86. value: '',
  87. observer(newVal) {
  88. this.draw({
  89. data: newVal,
  90. })
  91. },
  92. },
  93. },
  94. methods: {
  95. /**
  96. * 将之前在绘图上下文中的描述(路径、变形、样式)画到 canvas 中
  97. */
  98. draw(opts = {}) {
  99. const { typeNumber, errorCorrectLevel, width, height, fgColor, bgColor, canvasId, data } = Object.assign({}, this.data, opts)
  100. const qrcode = qrjs(utf16to8(data), {
  101. typeNumber,
  102. errorCorrectLevel,
  103. })
  104. const cells = qrcode.modules
  105. const tileW = width / cells.length
  106. const tileH = height / cells.length
  107. this.ctx = this.ctx || wx.createCanvasContext(canvasId, this)
  108. this.ctx.scale(1, 1)
  109. cells.forEach((row, rdx) => {
  110. row.forEach((cell, cdx) => {
  111. this.ctx.setFillStyle(cell ? fgColor : bgColor)
  112. const w = (Math.ceil((cdx + 1) * tileW) - Math.floor(cdx * tileW))
  113. const h = (Math.ceil((rdx + 1) * tileH) - Math.floor(rdx * tileH))
  114. this.ctx.fillRect(Math.round(cdx * tileW), Math.round(rdx * tileH), w, h)
  115. })
  116. })
  117. this.ctx.draw()
  118. },
  119. /**
  120. * 手指触摸后马上离开
  121. */
  122. onTap() {
  123. this.triggerEvent('click')
  124. },
  125. },
  126. attached() {
  127. this.draw()
  128. },
  129. detached() {
  130. this.ctx = null
  131. },
  132. })