vQr.vue 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <template>
  2. <div class="qrbox">
  3. <canvas :canvas-id="id" :style="{
  4. width:`100%`,
  5. height:`100%`,
  6. }"></canvas>
  7. </div>
  8. </template>
  9. <script>
  10. import uQRCode from "@/js_sdk/Sansnn-uQRCode/uqrcode.js";
  11. export default {
  12. props: {
  13. text: {
  14. default: "",
  15. type: String,
  16. required: false,
  17. },
  18. },
  19. data() {
  20. return {
  21. width: 100,
  22. height: 100,
  23. id: `qrcode${Math.random()}`,
  24. };
  25. },
  26. watch: {
  27. text() {
  28. this.getRec();
  29. },
  30. },
  31. methods: {
  32. // 获取尺寸
  33. getRec() {
  34. let query = uni.createSelectorQuery().in(this);
  35. query
  36. .select(".qrbox canvas")
  37. .boundingClientRect((e)=> {
  38. this.width = e.width
  39. this.height = e.height
  40. this.make()
  41. })
  42. .exec();
  43. },
  44. make() {
  45. uQRCode.make({
  46. canvasId: this.id,
  47. componentInstance: this,
  48. text: this.text,
  49. size: this.width,
  50. margin: 0,
  51. backgroundColor: '#ffffff',
  52. foregroundColor: '#000000',
  53. fileType: 'jpg',
  54. correctLevel: uQRCode.errorCorrectLevel.H,
  55. success: res => {
  56. }
  57. })
  58. }
  59. },
  60. mounted() {
  61. this.getRec();
  62. },
  63. };
  64. </script>