Ben Lin
2025-01-01 3f3817a39238b262155cd5ec76fa351bb344602d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<template>
  <PageWrapper title="二维码组件使用示例">
    <div class="flex flex-wrap">
      <CollapseContainer title="基础示例" :canExpand="true" class="text-center mb-6 w-1/5 mr-6">
        <QrCode :value="qrCodeUrl" />
      </CollapseContainer>
 
      <CollapseContainer title="渲染成img标签示例" class="text-center mb-6 w-1/5 mr-6">
        <QrCode :value="qrCodeUrl" tag="img" />
      </CollapseContainer>
 
      <CollapseContainer title="配置样式示例" class="text-center mb-6 w-1/5 mr-6">
        <QrCode
          :value="qrCodeUrl"
          :options="{
            color: { dark: '#55D187' },
          }"
        />
      </CollapseContainer>
 
      <CollapseContainer title="本地logo示例" class="text-center mb-6 w-1/5 mr-6">
        <QrCode :value="qrCodeUrl" :logo="LogoImg" />
      </CollapseContainer>
 
      <CollapseContainer title="在线logo示例" class="text-center mb-6 w-1/5 mr-6">
        <QrCode
          :value="qrCodeUrl"
          logo="https://vebn.oss-cn-beijing.aliyuncs.com/vben/logo.png"
          :options="{
            color: { dark: '#55D187' },
          }"
        />
      </CollapseContainer>
 
      <CollapseContainer title="logo配置示例" class="text-center mb-6 w-1/5 mr-6">
        <QrCode
          :value="qrCodeUrl"
          :logo="{
            src: 'https://vebn.oss-cn-beijing.aliyuncs.com/vben/logo.png',
            logoSize: 0.2,
            borderSize: 0.05,
            borderRadius: 50,
            bgColor: 'blue',
          }"
        />
      </CollapseContainer>
 
      <CollapseContainer title="下载示例" class="text-center mb-6 w-1/5 mr-6">
        <QrCode :value="qrCodeUrl" ref="qrRef" :logo="LogoImg" />
        <a-button class="mb-2" type="primary" @click="download"> 下载 </a-button>
        <div class="msg">(在线logo会导致图片跨域,需要下载图片需要自行解决跨域问题)</div>
      </CollapseContainer>
 
      <CollapseContainer title="配置大小示例" class="text-center w-1/5 mr-6">
        <QrCode :value="qrCodeUrl" :width="300" />
      </CollapseContainer>
 
      <CollapseContainer title="扩展绘制示例" class="text-center w-1/5 mr-6">
        <QrCode
          :value="qrCodeUrl"
          :width="200"
          :options="{ margin: 5 }"
          ref="qrDiyRef"
          :logo="LogoImg"
          @done="onQrcodeDone"
        />
        <a-button class="mb-2" type="primary" @click="downloadDiy"> 下载 </a-button>
        <div class="msg">要进行扩展绘制则不能将tag设为img</div>
      </CollapseContainer>
 
      <CollapseContainer title="Antdv QRCode" class="text-center w-1/5 mr-6">
        <QRCode :value="qrCodeUrl" :size="200" />
      </CollapseContainer>
 
      <CollapseContainer title="Antdv QRCode 带icon" class="text-center w-1/5 mr-6">
        <QRCode :value="qrCodeUrl" :size="200" :icon="LogoImg" />
      </CollapseContainer>
    </div>
  </PageWrapper>
</template>
<script lang="ts" setup>
  import LogoImg from '@/assets/images/logo.png';
  import { CollapseContainer } from '@/components/Container';
  import { PageWrapper } from '@/components/Page';
  import { QrCode, QrCodeActionType } from '@/components/Qrcode';
  import { type Nullable } from '@vben/types';
  import { QRCode } from 'ant-design-vue';
  import { ref, unref } from 'vue';
 
  const qrCodeUrl = 'https://www.vvbin.cn';
  const qrRef = ref<Nullable<QrCodeActionType>>(null);
  const qrDiyRef = ref<Nullable<QrCodeActionType>>(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 }: any) {
    if (ctx instanceof CanvasRenderingContext2D) {
      // 额外绘制
      ctx.fillStyle = 'black';
      ctx.font = '16px "微软雅黑"';
      ctx.textBaseline = 'bottom';
      ctx.textAlign = 'center';
      ctx.fillText('你帅你先扫', 100, 195, 200);
    }
  }
</script>