Commit fe4eae37146068f01ba08f033e0c2e8bd03e48b5

Authored by 江麻妞
Committed by GitHub
1 parent 14fb21d0

fix(qrcode): Fix the problem that the QR code cannot be dynamically generated (#974)

* fix: Fix the problem that the QR code cannot be dynamically generated

* Fix the problem that the size of the QR code is automatically changed when dynamically generated
src/components/Qrcode/src/Qrcode.vue
@@ -4,7 +4,7 @@ @@ -4,7 +4,7 @@
4 </div> 4 </div>
5 </template> 5 </template>
6 <script lang="ts"> 6 <script lang="ts">
7 - import { defineComponent, watchEffect, PropType, ref, unref } from 'vue'; 7 + import { defineComponent, watch, PropType, ref, unref } from 'vue';
8 import { toCanvas, QRCodeRenderersOptions, LogoType } from './qrcodePlus'; 8 import { toCanvas, QRCodeRenderersOptions, LogoType } from './qrcodePlus';
9 import { toDataURL } from 'qrcode'; 9 import { toDataURL } from 'qrcode';
10 import { downloadByUrl } from '/@/utils/file/download'; 10 import { downloadByUrl } from '/@/utils/file/download';
@@ -93,11 +93,16 @@ @@ -93,11 +93,16 @@
93 }); 93 });
94 } 94 }
95 95
96 - watchEffect(() => {  
97 - setTimeout(() => {  
98 - createQrcode();  
99 - }, 30);  
100 - }); 96 + // 监听参数变化重新生成二维码
  97 + watch(
  98 + props,
  99 + () => {
  100 + createQrcode()
  101 + },
  102 + {
  103 + deep: true,
  104 + }
  105 + )
101 106
102 return { wrapRef, download }; 107 return { wrapRef, download };
103 }, 108 },
src/components/Qrcode/src/drawCanvas.ts
1 import { toCanvas } from 'qrcode'; 1 import { toCanvas } from 'qrcode';
2 import type { QRCodeRenderersOptions } from 'qrcode'; 2 import type { QRCodeRenderersOptions } from 'qrcode';
3 import { RenderQrCodeParams, ContentType } from './typing'; 3 import { RenderQrCodeParams, ContentType } from './typing';
4 -export const renderQrCode = ({ canvas, content, width = 0, options = {} }: RenderQrCodeParams) => { 4 +import { cloneDeep } from 'lodash-es';
  5 +
  6 +export const renderQrCode = ({
  7 + canvas,
  8 + content,
  9 + width = 0,
  10 + options: params = {}
  11 +}: RenderQrCodeParams) => {
  12 + const options = cloneDeep(params)
5 // 容错率,默认对内容少的二维码采用高容错率,内容多的二维码采用低容错率 13 // 容错率,默认对内容少的二维码采用高容错率,内容多的二维码采用低容错率
6 options.errorCorrectionLevel = options.errorCorrectionLevel || getErrorCorrectionLevel(content); 14 options.errorCorrectionLevel = options.errorCorrectionLevel || getErrorCorrectionLevel(content);
7 15