Blame view

泰额版/Food Labeling Management App UniApp/src/utils/barcodeFormat.ts 2.2 KB
59e51671   “wangming”   1
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
  /** 与 Web 端 `barcodeFormat.ts` 选项顺序与取值一致 */
  
  export const BARCODE_FORMAT_OPTIONS = [
    { label: 'Codabar', value: 'CODABAR' },
    { label: 'Code39', value: 'CODE39' },
    { label: 'Code128', value: 'CODE128' },
    { label: 'EAN-2', value: 'EAN2' },
    { label: 'EAN-5', value: 'EAN5' },
    { label: 'EAN-8', value: 'EAN8' },
    { label: 'EAN-13', value: 'EAN13' },
    { label: 'ITF-14', value: 'ITF14' },
    { label: 'MSI', value: 'MSI' },
    { label: 'MSI10', value: 'MSI10' },
    { label: 'MSI11', value: 'MSI11' },
    { label: 'MSI1010', value: 'MSI1010' },
    { label: 'MSI1110', value: 'MSI1110' },
    { label: 'Pharmacode', value: 'PHARMACODE' },
    { label: 'UPC(A)', value: 'UPCA' },
  ] as const
  
  export type BarcodeFormatValue = (typeof BARCODE_FORMAT_OPTIONS)[number]['value']
  
  export const DEFAULT_BARCODE_FORMAT: BarcodeFormatValue = BARCODE_FORMAT_OPTIONS[0].value
  
  export function normalizeBarcodeType (raw: unknown): BarcodeFormatValue {
    const key = String(raw ?? '')
      .trim()
      .toUpperCase()
      .replace(/[^A-Z0-9]/g, '')
    const allowed = new Set(BARCODE_FORMAT_OPTIONS.map((o) => o.value))
    if (allowed.has(key as BarcodeFormatValue)) return key as BarcodeFormatValue
    if (key === 'UPC' || key === 'UPCA') return 'UPCA'
    if (key === 'ITF') return 'ITF14'
    return DEFAULT_BARCODE_FORMAT
  }
  
  export function toTscBarcodeSymbology (barcodeType: unknown): string {
    const key = normalizeBarcodeType(barcodeType)
    const map: Record<BarcodeFormatValue, string> = {
      CODABAR: 'CODA',
      CODE39: '39',
      CODE128: '128',
      EAN2: '128',
      EAN5: '128',
      EAN8: 'EAN8',
      EAN13: 'EAN13',
      ITF14: 'ITF14',
      MSI: 'MSI',
      MSI10: 'MSI',
      MSI11: 'MSI',
      MSI1010: 'MSI',
      MSI1110: 'MSI',
      PHARMACODE: '128',
      UPCA: 'UPCA',
    }
    return map[key] ?? 'CODA'
  }
  
  export function toEscBarcodeTypeCode (barcodeType: unknown): number {
    const key = normalizeBarcodeType(barcodeType)
    const map: Record<BarcodeFormatValue, number> = {
      CODABAR: 71,
      CODE39: 69,
      CODE128: 73,
      EAN2: 73,
      EAN5: 73,
      EAN8: 68,
      EAN13: 67,
      ITF14: 70,
      MSI: 73,
      MSI10: 73,
      MSI11: 73,
      MSI1010: 73,
      MSI1110: 73,
      PHARMACODE: 73,
      UPCA: 65,
    }
    return map[key] ?? 71
  }