qiyan-qrcode.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <template>
  2. <canvas @click="onClick()" :style="{ width: `${size}px`, height: `${size}px` }" :canvas-id="id" :id="id" type="2d" :hidpi="false"></canvas>
  3. </template>
  4. <script>
  5. /**
  6. * Qrcode 显示二维码
  7. * @description 二维码生成的组件
  8. * @property {String} id 多个时需设置不同id
  9. * @property {String} text 二维码内容
  10. * @property {Number} size 二维码大小
  11. * @property {String} background 背景颜色
  12. * @property {String} foreground 二维码颜色
  13. * @property {String} logo 中间logo资源
  14. * @property {String} logoSize logo大小
  15. * @property {Boolean} logoRound logo圆角
  16. *
  17. * @event {Function} click 点击 Qrcode 触发事件
  18. * @example <QiyanQrcode text="321"></QiyanQrcode>
  19. */
  20. import drawQrcode from "./utils.js";
  21. let canvasObj = {};
  22. export default {
  23. name: 'QiyanQrcode',
  24. emits: ['click'],
  25. props: {
  26. id: {
  27. type: String,
  28. default: "qrCanvas",
  29. },
  30. text: {
  31. type: String,
  32. default: "",
  33. },
  34. size: {
  35. type: Number,
  36. default: 200,
  37. },
  38. background: {
  39. type: String,
  40. default: "#ffffff",
  41. },
  42. foreground: {
  43. type: String,
  44. default: "#000000",
  45. },
  46. logo: {
  47. type: String,
  48. default: "",
  49. },
  50. logoSize: {
  51. type: Number,
  52. default: 60,
  53. },
  54. logoRound: {
  55. type: Boolean,
  56. default: false,
  57. },
  58. },
  59. watch: {
  60. text(nVal) {
  61. if (nVal) {
  62. this.draw();
  63. } else {
  64. console.error('qrcode text is null');
  65. }
  66. }
  67. },
  68. mounted() {
  69. this.$nextTick(() => {
  70. let _this = this;
  71. // #ifdef H5
  72. const canvas = document.getElementById(this.id).childNodes[0]
  73. canvasObj[this.id] = canvas;
  74. _this.draw();
  75. // #endif
  76. // #ifndef H5
  77. const query = uni.createSelectorQuery().in(this);
  78. query.select("#" + this.id).fields({
  79. node: true,
  80. size: true,
  81. },
  82. (data) => {
  83. canvasObj[this.id] = data.node;
  84. _this.draw();
  85. }
  86. )
  87. .exec();
  88. // #endif
  89. })
  90. },
  91. beforeUnmount() {
  92. delete canvasObj[this.id];
  93. },
  94. methods: {
  95. draw() {
  96. if (this.text && canvasObj[this.id]) {
  97. let canvas = canvasObj[this.id];
  98. const drawText = (imageResource) => {
  99. let image = {};
  100. if (imageResource) {
  101. image = {
  102. imageResource,
  103. width: this.logoSize,
  104. height: this.logoSize,
  105. round: this.logoRound
  106. }
  107. }
  108. drawQrcode({
  109. canvas,
  110. canvasId: this.id,
  111. width: this.size,
  112. height: this.size,
  113. padding: 0,
  114. background: this.background,
  115. foreground: this.foreground,
  116. text: this.text,
  117. image
  118. });
  119. }
  120. setTimeout(() => {
  121. if (this.logo) {
  122. let img;
  123. // #ifdef H5
  124. img = new Image();
  125. // #endif
  126. // #ifndef H5
  127. img = canvas.createImage();
  128. // #endif
  129. img.src = this.logo;
  130. img.onload = () => {
  131. drawText(img);
  132. }
  133. } else {
  134. drawText();
  135. }
  136. }, 100);
  137. }
  138. },
  139. onClick() {
  140. this.$emit('click');
  141. },
  142. }
  143. }
  144. </script>