QrPanel.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <script setup>
  2. import { ref } from 'vue';
  3. import { useI18n } from 'vue-i18n';
  4. import { CopyOutlined, DownloadOutlined, PictureOutlined } from '@ant-design/icons-vue';
  5. import { message } from 'ant-design-vue';
  6. import { ClipboardManager, FileManager } from '@/utils';
  7. const { t } = useI18n();
  8. const props = defineProps({
  9. value: { type: String, required: true },
  10. remark: { type: String, default: '' },
  11. downloadName: { type: String, default: '' },
  12. size: { type: Number, default: 360 },
  13. showQr: { type: Boolean, default: true },
  14. });
  15. const qrRef = ref(null);
  16. async function copy() {
  17. const ok = await ClipboardManager.copyText(props.value);
  18. if (ok) message.success(t('copied'));
  19. }
  20. function download() {
  21. if (!props.downloadName) return;
  22. FileManager.downloadTextFile(props.value, props.downloadName);
  23. }
  24. function svgToPngBlob(size = 360) {
  25. const svgEl = qrRef.value?.querySelector('svg');
  26. if (!svgEl) return Promise.resolve(null);
  27. const svgData = new XMLSerializer().serializeToString(svgEl);
  28. const svgBlob = new Blob([svgData], { type: 'image/svg+xml;charset=utf-8' });
  29. const url = URL.createObjectURL(svgBlob);
  30. return new Promise((resolve) => {
  31. const img = new Image();
  32. img.onload = () => {
  33. const canvas = document.createElement('canvas');
  34. canvas.width = size;
  35. canvas.height = size;
  36. const ctx = canvas.getContext('2d');
  37. ctx.fillStyle = '#ffffff';
  38. ctx.fillRect(0, 0, size, size);
  39. ctx.drawImage(img, 0, 0, size, size);
  40. URL.revokeObjectURL(url);
  41. canvas.toBlob(resolve, 'image/png');
  42. };
  43. img.onerror = () => { URL.revokeObjectURL(url); resolve(null); };
  44. img.src = url;
  45. });
  46. }
  47. async function copyImage() {
  48. const blob = await svgToPngBlob(props.size);
  49. if (!blob) return;
  50. try {
  51. await navigator.clipboard.write([new ClipboardItem({ 'image/png': blob })]);
  52. message.success(t('copied'));
  53. } catch {
  54. downloadImageBlob(blob);
  55. }
  56. }
  57. function downloadImageBlob(blob) {
  58. const url = URL.createObjectURL(blob);
  59. const link = document.createElement('a');
  60. link.href = url;
  61. link.download = `${props.remark || 'qrcode'}.png`;
  62. link.click();
  63. URL.revokeObjectURL(url);
  64. }
  65. async function downloadImage() {
  66. const blob = await svgToPngBlob(props.size);
  67. if (blob) downloadImageBlob(blob);
  68. }
  69. </script>
  70. <template>
  71. <div class="qr-panel">
  72. <div class="qr-panel-header">
  73. <a-tag color="green" class="qr-remark">{{ remark }}</a-tag>
  74. <a-tooltip :title="t('copy')">
  75. <a-button size="small" @click="copy">
  76. <template #icon>
  77. <CopyOutlined />
  78. </template>
  79. </a-button>
  80. </a-tooltip>
  81. <a-tooltip v-if="showQr" :title="t('downloadImage', 'Download Image')">
  82. <a-button size="small" @click="downloadImage">
  83. <template #icon>
  84. <PictureOutlined />
  85. </template>
  86. </a-button>
  87. </a-tooltip>
  88. <a-tooltip v-if="downloadName" :title="t('download')">
  89. <a-button size="small" @click="download">
  90. <template #icon>
  91. <DownloadOutlined />
  92. </template>
  93. </a-button>
  94. </a-tooltip>
  95. </div>
  96. <div v-if="showQr" ref="qrRef" class="qr-panel-canvas">
  97. <a-tooltip :title="t('copy')">
  98. <a-qrcode class="qr-code" :value="value" :size="size" type="svg" :bordered="false" color="#000000"
  99. bg-color="#ffffff" @click="copyImage" />
  100. </a-tooltip>
  101. </div>
  102. </div>
  103. </template>
  104. <style scoped>
  105. .qr-panel {
  106. border: 1px solid rgba(128, 128, 128, 0.2);
  107. border-radius: 8px;
  108. padding: 10px;
  109. margin-bottom: 10px;
  110. display: flex;
  111. flex-direction: column;
  112. gap: 6px;
  113. }
  114. .qr-panel-header {
  115. display: flex;
  116. align-items: center;
  117. gap: 6px;
  118. flex-wrap: wrap;
  119. }
  120. .qr-remark {
  121. margin: 0;
  122. }
  123. .qr-panel-canvas {
  124. display: flex;
  125. justify-content: center;
  126. padding: 6px 0;
  127. }
  128. .qr-panel-canvas .qr-code {
  129. cursor: pointer;
  130. background: #fff;
  131. border-radius: 4px;
  132. line-height: 0;
  133. }
  134. .qr-panel-canvas .qr-code :deep(svg) {
  135. display: block;
  136. width: 100%;
  137. height: auto;
  138. max-width: 360px;
  139. }
  140. </style>