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 return { ...el, config: { ...cfg, text: name, Text: name }, } }) return { ...template, elements } }