Blame view

泰额版/Food Labeling Management App UniApp/src/utils/print/drivers/genericEsc.ts 1.11 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
  import { buildEscPosLabelData, buildEscPosTestPrintData } from '../protocols/escPosBuilder'
  import type { PrinterCandidate, PrinterDriver } from '../types/printer'
  
  const GENERIC_ESC_KEYWORDS = [
    'printer', 'print', 'esc', 'escpos', 'pos', 'receipt', 'ticket',
  ]
  
  function scoreByKeywords (device: PrinterCandidate, keywords: string[]): number {
    const text = `${device.name || ''} ${device.deviceId || ''}`.toLowerCase()
    let score = 0
    keywords.forEach((keyword) => {
      if (text.includes(keyword)) score += 8
    })
    return score
  }
  
  export const genericEscDriver: PrinterDriver = {
    key: 'generic-esc',
    brand: 'Generic',
    model: 'ESC/POS',
    displayName: 'Generic ESC/POS Printer',
    protocol: 'esc',
    preferredBleMtu: 20,
    imageMaxWidthDots: 384,
    imageDpi: 203,
    keywords: GENERIC_ESC_KEYWORDS,
    matches (device) {
      return scoreByKeywords(device, GENERIC_ESC_KEYWORDS)
    },
    resolveConnectionType (device) {
      return device.type === 'ble' ? 'ble' : 'classic'
    },
    buildTestPrintData () {
      return buildEscPosTestPrintData()
    },
    buildLabelData (payload) {
      return buildEscPosLabelData(payload)
    },
  }