/** * 单次「点击打印」任务的时间线(仅 JS,不依赖改 AAR)。 * Logcat 搜关键词: [print-diag] * 预览页打印中可点「Log」弹窗整段复制截图。 */ let t0 = 0 const lines: string[] = [] const MAX_LINES = 120 export function startPrintRunDiagnostics (): void { t0 = Date.now() lines.length = 0 printRunDiag('session_start') } export function printRunDiag (msg: string, extra?: Record): void { const dt = Date.now() - t0 let s = `[+${dt}ms] ${msg}` if (extra && Object.keys(extra).length) { s += ` | ${Object.entries(extra) .filter(([, v]) => v !== undefined && v !== '') .map(([k, v]) => `${k}=${String(v)}`) .join(' ')}` } lines.push(s) if (lines.length > MAX_LINES) { lines.splice(0, lines.length - MAX_LINES) } try { console.warn('[print-diag]', s) } catch (_) {} } export function getPrintRunDiagnosticsText (): string { return lines.join('\n') } /** 弹窗安全长度(避免部分机型 showModal 截断过长无提示) */ export function getPrintRunDiagnosticsTextForModal (maxChars = 3800): string { const full = getPrintRunDiagnosticsText() if (full.length <= maxChars) return full return `...(start omitted, ${full.length - maxChars} chars)\n\n${full.slice(-maxChars)}` } const STORAGE_LAST_PRINT_DIAG = 'lastPrintRunDiagnostics' const STORAGE_LAST_PRINT_DIAG_AT = 'lastPrintRunDiagnosticsAt' /** 打印结束后写入 storage,Printer 页可查看最近一次任务时间线 */ export function persistPrintRunDiagnostics (): void { try { uni.setStorageSync(STORAGE_LAST_PRINT_DIAG, getPrintRunDiagnosticsText()) uni.setStorageSync(STORAGE_LAST_PRINT_DIAG_AT, String(Date.now())) } catch (_) {} } export function getPersistedPrintRunDiagnostics (): string { try { return String(uni.getStorageSync(STORAGE_LAST_PRINT_DIAG) || '') } catch (_) { return '' } } export function getPersistedPrintRunDiagnosticsAt (): number { try { return Number(uni.getStorageSync(STORAGE_LAST_PRINT_DIAG_AT) || 0) || 0 } catch (_) { return 0 } }