xiaoyi
23 小时以前 9637c9dd2994f73def8b906411545617016553c5
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<script lang="ts" setup>
import type { QRCodeRenderersOptions } from 'qrcode';
 
import type { BarcodeFormat } from './types';
 
import { computed, nextTick, ref, unref, watch } from 'vue';
 
import JsBarcode from 'jsbarcode';
import QRCode from 'qrcode';
 
import { BARCODE_FORMAT_MAP, BarcodeFormatEnum } from './types';
 
defineOptions({ name: 'Barcode' });
 
const props = withDefaults(
  defineProps<{
    content?: string;
    displayValue?: boolean;
    format?: BarcodeFormat;
    height?: number;
    width?: number;
  }>(),
  {
    content: '',
    displayValue: true,
    format: BarcodeFormatEnum.QR_CODE,
    height: 100,
    width: 200,
  },
);
 
const emit = defineEmits<{
  done: [base64: string];
}>();
 
const loading = ref(true);
const canvasRef = ref<HTMLCanvasElement>();
const imgRef = ref<HTMLImageElement>();
const isQRCode = computed(() => props.format === BarcodeFormatEnum.QR_CODE);
 
const wrapStyle = computed(() => {
  if (isQRCode.value) {
    return {
      height: `${props.width}px`,
      width: `${props.width}px`,
    };
  }
  return {
    width: `${props.width}px`,
  };
});
 
async function generateQRCode() {
  const canvas = unref(canvasRef);
  if (!canvas) {
    return;
  }
  const options: QRCodeRenderersOptions = {
    errorCorrectionLevel: 'M',
    width: props.width,
  };
  await QRCode.toCanvas(canvas, props.content, options);
  emit('done', canvas.toDataURL());
}
 
function generateOneDimensionalBarcode() {
  const img = unref(imgRef);
  if (!img) {
    return;
  }
  JsBarcode(img, props.content, {
    displayValue: props.displayValue,
    format: BARCODE_FORMAT_MAP[props.format] || 'CODE39',
    height: props.height,
    margin: 10,
    width: 2,
  });
  emit('done', img.src);
}
 
async function generateBarcode() {
  if (!props.content) {
    loading.value = false;
    return;
  }
 
  loading.value = true;
  await nextTick();
  try {
    if (isQRCode.value) {
      await generateQRCode();
      return;
    }
    generateOneDimensionalBarcode();
  } catch (error) {
    console.error('生成条码失败:', error);
  } finally {
    loading.value = false;
  }
}
 
watch(
  () => [
    props.content,
    props.displayValue,
    props.format,
    props.height,
    props.width,
  ],
  () => {
    generateBarcode();
  },
  { immediate: true },
);
 
function getImageBase64() {
  if (isQRCode.value) {
    return unref(canvasRef)?.toDataURL() || '';
  }
  return unref(imgRef)?.src || '';
}
 
defineExpose({ getImageBase64 });
</script>
 
<template>
  <div :aria-busy="loading" class="inline-block" :style="wrapStyle">
    <canvas v-if="isQRCode" ref="canvasRef" class="block max-w-full"></canvas>
    <img v-else ref="imgRef" alt="barcode" class="block max-w-full" />
  </div>
</template>