4cb354d4
杨鑫
提交
|
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
|
import type { SystemLabelTemplate, SystemTemplateElementBase } from '../print/types/printer'
function normalizePaletteSlug (raw: string): string {
return String(raw ?? '').trim().toLowerCase().replace(/[^a-z0-9]+/g, '')
}
function readTypeAdd (el: SystemTemplateElementBase): string {
return String((el as SystemTemplateElementBase & { typeAdd?: string }).typeAdd ?? '').trim().toLowerCase()
}
/** 当前登录用户展示名(与侧栏 / Profile 一致) */
export function getLoggedInEmployeeDisplayName (): string {
const name = String(uni.getStorageSync('userName') || '').trim()
if (name) return name
const email = String(uni.getStorageSync('user_email') || '').trim()
if (email) {
const at = email.indexOf('@')
return at > 0 ? email.slice(0, at) : email
}
return 'Employee'
}
/** 是否为模板「Entered Automatically → Employee」控件 */
export function isEmployeeTemplateElement (el: SystemTemplateElementBase): boolean {
const typeAdd = readTypeAdd(el)
if (typeAdd.startsWith('auto_') && typeAdd.includes('employee')) return true
const en = normalizePaletteSlug(String(el.elementName ?? ''))
if (/^employee\d*$/.test(en)) return true
const vst = String(el.valueSourceType ?? '').toUpperCase()
const type = String(el.type ?? '').toUpperCase()
if (vst === 'AUTO_DB' && type === 'TEXT_STATIC' && /^employee/.test(en)) return true
return false
}
export function templateIncludesEmployeeElement (
template: SystemLabelTemplate | null | undefined,
): boolean {
return !!(template?.elements?.some(isEmployeeTemplateElement))
}
/** 将当前登录人姓名写入 Employee AUTO 控件(预览/出纸同源) */
export function applyEmployeeDisplayToTemplate (
template: SystemLabelTemplate,
employeeName?: string | null,
): SystemLabelTemplate {
const name = String(employeeName ?? '').trim() || getLoggedInEmployeeDisplayName()
const elements = (template.elements || []).map((el) => {
if (!isEmployeeTemplateElement(el)) return el
const cfg = { ...(el.config || {}) } as Record<string, unknown>
return {
...el,
config: { ...cfg, text: name, Text: name },
}
})
return { ...template, elements }
}
|