From 425bd74f0c2bd6aed0998149eeff0236b9a814e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=97=A0=E6=9C=A8?= Date: Mon, 10 May 2021 20:41:10 +0800 Subject: [PATCH] feat(qrcode): custom drawing support --- src/components/Qrcode/src/index.vue | 18 +++++++++------ src/components/Qrcode/src/types.ts | 5 +++++ src/views/demo/comp/qrcode/index.vue | 33 ++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 7 deletions(-) diff --git a/src/components/Qrcode/src/index.vue b/src/components/Qrcode/src/index.vue index 9e0a9f91ce2..f11260ddb5d 100644 --- a/src/components/Qrcode/src/index.vue +++ b/src/components/Qrcode/src/index.vue @@ -8,6 +8,7 @@ import { toCanvas, QRCodeRenderersOptions, LogoType } from './qrcodePlus'; import { toDataURL } from 'qrcode'; import { downloadByUrl } from '/@/utils/file/download'; + import { QrcodeDoneEventParams } from './types'; export default defineComponent({ name: 'QrCode', @@ -38,10 +39,9 @@ validator: (v: string) => ['canvas', 'img'].includes(v), }, }, - emits: { done: (url: string) => !!url, error: (error: any) => !!error }, + emits: { done: (data: QrcodeDoneEventParams) => !!data, error: (error: any) => !!error }, setup(props, { emit }) { const wrapRef = ref(null); - const urlRef = ref(''); async function createQrcode() { try { const { tag, value, options = {}, width, logo } = props; @@ -58,8 +58,7 @@ content: renderValue, options: options || {}, }); - urlRef.value = url; - emit('done', url); + emit('done', { url, ctx: (wrapEl as HTMLCanvasElement).getContext('2d') }); return; } @@ -70,8 +69,7 @@ ...options, }); (unref(wrapRef) as HTMLImageElement).src = url; - urlRef.value = url; - emit('done', url); + emit('done', { url }); } } catch (error) { emit('error', error); @@ -81,7 +79,13 @@ * file download */ function download(fileName?: string) { - const url = unref(urlRef); + let url = ''; + const wrapEl = unref(wrapRef); + if (wrapEl instanceof HTMLCanvasElement) { + url = wrapEl.toDataURL(); + } else if (wrapEl instanceof HTMLImageElement) { + url = wrapEl.src; + } if (!url) return; downloadByUrl({ url, diff --git a/src/components/Qrcode/src/types.ts b/src/components/Qrcode/src/types.ts index 1b0ce11cb0c..3a037e9db13 100644 --- a/src/components/Qrcode/src/types.ts +++ b/src/components/Qrcode/src/types.ts @@ -31,3 +31,8 @@ export type ToCanvasFn = (options: RenderQrCodeParams) => Promise; export interface QrCodeActionType { download: (fileName?: string) => void; } + +export interface QrcodeDoneEventParams { + url: string; + ctx?: CanvasRenderingContext2D | null; +} diff --git a/src/views/demo/comp/qrcode/index.vue b/src/views/demo/comp/qrcode/index.vue index 50074910364..98534f7dafe 100644 --- a/src/views/demo/comp/qrcode/index.vue +++ b/src/views/demo/comp/qrcode/index.vue @@ -58,6 +58,19 @@ + + + + 下载 +
要进行扩展绘制则不能将tag设为img
+
@@ -73,16 +86,36 @@ components: { CollapseContainer, QrCode, PageWrapper }, setup() { const qrRef = ref>(null); + const qrDiyRef = ref>(null); function download() { const qrEl = unref(qrRef); if (!qrEl) return; qrEl.download('文件名'); } + function downloadDiy() { + const qrEl = unref(qrDiyRef); + if (!qrEl) return; + qrEl.download('Qrcode'); + } + + function onQrcodeDone({ ctx }) { + if (ctx instanceof CanvasRenderingContext2D) { + // 额外绘制 + ctx.fillStyle = 'black'; + ctx.font = '16px "微软雅黑"'; + ctx.textBaseline = 'bottom'; + ctx.textAlign = 'center'; + ctx.fillText('你帅你先扫', 100, 195, 200); + } + } return { + onQrcodeDone, qrCodeUrl, LogoImg, download, + downloadDiy, qrRef, + qrDiyRef, }; }, });