ny
22 小时以前 282fbc6488f4e8ceb5fda759f963ee88fbf7b999
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
<script lang="ts" setup>
import { computed, nextTick, onMounted, ref, unref, watch } from 'vue';
 
import { buildShortUUID } from '@jnpf/utils';
 
import JsBarcode from 'jsbarcode';
 
import { barcodeProps } from './props';
 
defineOptions({ inheritAttrs: false, name: 'JnpfBarcode' });
const props = defineProps(barcodeProps);
const id = ref('');
const relationText = ref('');
 
const barcode = computed(() => {
  return props.dataType === 'static' ? props.staticText : relationText.value;
});
 
watch(
  () => props.formData,
  (val) => {
    if (val && props.dataType === 'relation' && props.relationField) {
      relationText.value = val[props.relationField];
    }
  },
  { deep: true, immediate: true },
);
watch(
  () => [props.format, props.lineColor, props.background, props.width, props.height, unref(barcode)],
  () => {
    nextTick(() => {
      buildBarcode();
    });
  },
);
 
function buildBarcode() {
  if (!unref(barcode)) return;
  const reg = /^[A-Z0-9]+$/i;
  if (!reg.test(unref(barcode))) {
    return;
  }
  JsBarcode(`#${id.value}`, unref(barcode), {
    background: props.background,
    displayValue: false,
    format: props.format,
    height: props.height,
    lineColor: props.lineColor,
    margin: 5,
    width: props.width,
  });
}
id.value = buildShortUUID('barcode');
 
onMounted(() => {
  buildBarcode();
});
</script>
 
<template>
  <div class="jnpf-barcode">
    <div v-if="barcode">
      <canvas :id="id" class="barcode"></canvas>
    </div>
  </div>
</template>
<style lang="scss" scoped>
.jnpf-barcode {
  width: 100%;
  min-height: 40px;
  padding: 0;
  overflow: hidden;
  background: #fff;
}
</style>