a001da6d
杨鑫
APP 预览打印
|
1
2
3
4
|
/**
* Android NativeTemplateCommandBuilder 仅处理:TEXT_*、QRCODE、BARCODE、IMAGE、
* 以及 border=line 的 BLANK;其余类型在原生路径下会被静默跳过(与画布预览不一致)。
*/
|
58d2e61c
杨鑫
最新代码
|
5
6
7
8
9
10
|
import type {
LabelTemplateData,
SystemLabelTemplate,
SystemTemplateElementBase,
} from './types/printer'
import { applyTemplateData } from './templateRenderer'
|
a001da6d
杨鑫
APP 预览打印
|
11
12
13
14
|
function isElementHandledByNativeFastPrinter (el: SystemTemplateElementBase): boolean {
const type = String(el.type || '').toUpperCase()
if (type.startsWith('TEXT_')) return true
|
3ead62fc
杨鑫
优化
|
15
|
if (type === 'NUTRITION') return true
|
58d2e61c
杨鑫
最新代码
|
16
17
|
if (type === 'QRCODE' || type === 'IMAGE') return true
if (type === 'BARCODE') return true
|
a001da6d
杨鑫
APP 预览打印
|
18
19
20
21
|
if (type === 'BLANK') return true
return false
}
|
58d2e61c
杨鑫
最新代码
|
22
23
24
25
26
27
28
29
|
/**
* 将 WEIGHT / DATE / TIME / DURATION 转为 TEXT_STATIC(展示文案与合并后的 config.text 一致),
* LOGO → IMAGE,使同一套模板可走 native printTemplate,避免仅因元素类型名而整页光栅(进度长期停在 ~12–14%)。
*/
export function normalizeTemplateForNativeFastJob (
template: SystemLabelTemplate,
data: LabelTemplateData
): SystemLabelTemplate {
|
3ead62fc
杨鑫
优化
|
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
|
const now = new Date()
const pad2 = (n: number): string => String(n).padStart(2, '0')
const formatDateByPreset = (fmt: string, d: Date): string => {
const yyyy = String(d.getFullYear())
const yy = yyyy.slice(-2)
const mm = pad2(d.getMonth() + 1)
const dd = pad2(d.getDate())
const hh = pad2(d.getHours())
const min = pad2(d.getMinutes())
switch (fmt) {
case 'DD/MM/YYYY': return `${dd}/${mm}/${yyyy}`
case 'MM/DD/YYYY': return `${mm}/${dd}/${yyyy}`
case 'DD/MM/YY': return `${dd}/${mm}/${yy}`
case 'MM/DD/YY': return `${mm}/${dd}/${yy}`
case 'MM/YY': return `${mm}/${yy}`
case 'MM/DD': return `${mm}/${dd}`
case 'MM': return mm
case 'DD': return dd
case 'YY': return yy
case 'YYYY-MM-DD': return `${yyyy}-${mm}-${dd}`
case 'YYYY-MM-DD HH:mm': return `${yyyy}-${mm}-${dd} ${hh}:${min}`
case 'HH:mm': return `${hh}:${min}`
default:
return String(fmt || '')
.replace('YYYY', yyyy)
.replace('YY', yy)
.replace('MM', mm)
.replace('DD', dd)
.replace('HH', hh)
.replace('mm', min)
}
}
|
58d2e61c
杨鑫
最新代码
|
63
64
65
66
67
68
69
70
71
72
|
const elements = (template.elements || []).map((el) => {
const type = String(el.type || '').toUpperCase()
const config = (el.config || {}) as Record<string, any>
if (type === 'LOGO') {
return { ...el, type: 'IMAGE' as typeof el.type }
}
if (type === 'WEIGHT' || type === 'DATE' || type === 'TIME' || type === 'DURATION') {
let text = ''
/** 与 renderLabelPreviewCanvas.previewTextForElement 一致:后端 AUTO_DB 的 DATE/TIME 常把算好的展示串写在 format 而非 text */
let raw = String(config.text ?? config.Text ?? '').trim()
|
3ead62fc
杨鑫
优化
|
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
|
const vst = String(el.valueSourceType || '').toUpperCase()
const inputType = String(config.inputType ?? config.InputType ?? '').toLowerCase()
if (type === 'DATE') {
if (raw) {
text = applyTemplateData(raw, data)
} else if (vst === 'PRINT_INPUT' && (inputType === 'date' || inputType === 'datetime')) {
const fmt = String(config.format ?? config.Format ?? (inputType === 'datetime' ? 'YYYY-MM-DD HH:mm' : 'DD/MM/YYYY')).trim()
text = fmt
} else {
const fmt = String(config.format ?? config.Format ?? 'DD/MM/YYYY').trim() || 'DD/MM/YYYY'
const offset = Number(config.offsetDays ?? config.OffsetDays ?? 0) || 0
const d = new Date(now.getTime())
d.setDate(d.getDate() + offset)
text = formatDateByPreset(fmt, d)
}
} else if (type === 'TIME') {
if (raw) {
text = applyTemplateData(raw, data)
} else if (vst === 'PRINT_INPUT') {
text = String(config.format ?? config.Format ?? 'HH:mm').trim() || 'HH:mm'
} else {
text = formatDateByPreset('HH:mm', now)
}
} else if (type === 'DURATION') {
if (raw) {
text = applyTemplateData(raw, data)
} else {
const unit = String(config.format ?? config.Format ?? 'Days').trim() || 'Days'
const rawV = config.durationValue ?? config.value ?? config.offsetDays ?? config.DurationValue ?? config.Value ?? config.OffsetDays
const val = Number.isFinite(Number(rawV)) ? Number(rawV) : 0
text = `${val} ${unit}`.trim()
}
|
58d2e61c
杨鑫
最新代码
|
105
|
} else if (type === 'WEIGHT') {
|
3ead62fc
杨鑫
优化
|
106
107
108
109
110
|
if (!raw) raw = String(config.value ?? config.Value ?? '').trim()
if (raw) text = applyTemplateData(raw, data)
}
if (!text && type === 'WEIGHT') {
|
58d2e61c
杨鑫
最新代码
|
111
112
113
114
115
116
117
118
|
const v = String(config.value ?? config.Value ?? '')
const u = String(config.unit ?? config.Unit ?? '')
if (v && u && !v.endsWith(u)) text = `${v}${u}`
else text = v || u
}
return {
...el,
type: 'TEXT_STATIC' as typeof el.type,
|
699ea6e8
杨鑫
完善打印逻辑
|
119
|
config: { ...config, text, nativeSourceType: type },
|
58d2e61c
杨鑫
最新代码
|
120
121
122
123
124
125
126
|
}
}
return el
})
return { ...template, elements }
}
|
a001da6d
杨鑫
APP 预览打印
|
127
128
129
130
131
132
133
|
/** 存在任一原生不支持的元素时,预览打印应走光栅,避免「成功但缺内容/不出纸」与画布不一致 */
export function templateHasUnsupportedNativeFastElements (template: SystemLabelTemplate): boolean {
for (const el of template.elements || []) {
if (!isElementHandledByNativeFastPrinter(el)) return true
}
return false
}
|