// 通用格式化:运行台历史、定时任务、下载弹窗共用,避免各组件重复实现。 export function fmtBytes(n) { if (!n) return '0 B' const u = ['B', 'KB', 'MB', 'GB'] let i = 0 while (n >= 1024 && i < u.length - 1) { n /= 1024; i++ } return (n >= 100 || i === 0 ? n.toFixed(0) : n.toFixed(1)) + ' ' + u[i] } const pad2 = (n) => String(n).padStart(2, '0') // 时间戳(秒)→ "10-26 14:30";空值/非法值返回占位符 export function fmtTime(ts, fallback = '—') { if (!ts) return fallback const d = new Date(ts * 1000) if (isNaN(d.getTime())) return fallback return `${pad2(d.getMonth() + 1)}-${pad2(d.getDate())} ${pad2(d.getHours())}:${pad2(d.getMinutes())}` } // 时间戳(秒)→ 口语化相对日期:"今天 14:30" / "昨天 09:05" / "10-20 10:00" // 场景库卡片底部、总览「最近运行场景」表格共用,口径一致 export function fmtDayTime(ts, fallback = '—') { if (!ts) return fallback const d = new Date(ts * 1000) if (isNaN(d.getTime())) return fallback const hm = `${pad2(d.getHours())}:${pad2(d.getMinutes())}` const now = new Date() const dayStart = new Date(now.getFullYear(), now.getMonth(), now.getDate()).getTime() const t = d.getTime() if (t >= dayStart) return `今天 ${hm}` if (t >= dayStart - 86400000) return `昨天 ${hm}` return `${pad2(d.getMonth() + 1)}-${pad2(d.getDate())} ${hm}` } // 时间字符串(如后端 "2026-10-26 14:30:00")→ "10-26 14:30" export function fmtShort(s) { if (!s) return '' const d = new Date(String(s).replace(' ', 'T')) if (isNaN(d.getTime())) return String(s) return `${pad2(d.getMonth() + 1)}-${pad2(d.getDate())} ${pad2(d.getHours())}:${pad2(d.getMinutes())}` } // 内存 MB → "3.4 GB" / "512 MB"(运行历史峰值、状态卡共用) export function fmtMemMb(mb) { const n = Number(mb) || 0 if (n <= 0) return '—' return n >= 1024 ? `${(n / 1024).toFixed(n >= 10240 ? 0 : 1)} GB` : `${n} MB` } // 执行超时秒数 → "6 小时" / "30 分钟" / "90 秒" / "不限时"(0 或非法值) // 设置页与场景卡/运行台的超时徽章共用同一口径,避免两处文案不一致 export function fmtTimeout(sec) { const n = Number(sec) || 0 if (!n) return '不限时' if (n % 86400 === 0) return `${n / 86400} 天` if (n % 3600 === 0) return `${n / 3600} 小时` if (n % 60 === 0) return `${n / 60} 分钟` return `${n} 秒` } // 耗时秒数 → "45s" / "2分05s" export function durText(s) { const n = parseInt(s, 10) || 0 if (n < 60) return n + 's' const m = Math.floor(n / 60) return m + '分' + (n % 60 ? pad2(n % 60) + 's' : '') } // 场景描述清洗:去掉与页头标题重复的「XQ0020 插件名:」前缀(各插件 manifest 里习惯这么写) export function cleanDesc(desc) { const s = String(desc || '').trim() if (!s) return '' return s.replace(/^XQ\d{3,4}[^::]{0,40}[::]\s*/i, '').trim() } // 描述拆句:manifest 的 description 常是一长串「;」句,详情弹窗按句分段更好读 export function splitDesc(desc) { const s = cleanDesc(desc) if (!s) return [] return s.split(/[;;]/).map((x) => x.trim()).filter(Boolean) } // 插件通道徽章:中心机 /api/plugins 按账号下发 channel=test|prod(管理员在测通道 = test)。 // 返回 { text, kind, title };中心机不可达/旧版服务端无 channel 时返回 null(不显示徽章,避免瞎猜)。 export function pluginChannelTag(plugin) { const ch = plugin && plugin.channel if (ch !== 'test' && ch !== 'prod') return null const isTest = ch === 'test' const local = String(plugin.version || '') const cloud = String(plugin.cloud_version || '') const stale = !!cloud && !!local && cloud !== local const base = isTest ? '测试版' : '正式版' const title = stale ? `${base}:云端 v${cloud},本地 v${local},点左栏「插件同步」升级` : `${base}:当前已是最新(v${local || cloud})` return { text: base, kind: isTest ? 'test' : 'prod', title, stale } } // 版本比较:数字段按 int(1.10.0 > 1.9.0),非数字段按小写字符串;对齐 electron/crypto.js 的 versionLt export function versionLt(a, b) { const key = (s) => String(s || '').split('.') .map((x) => (/^\d+$/.test(x) ? [1, parseInt(x, 10)] : [0, x.toLowerCase()])) const ka = key(a) const kb = key(b) for (let i = 0; i < Math.max(ka.length, kb.length); i++) { const x = ka[i] || [0, ''] const y = kb[i] || [0, ''] if (x[0] !== y[0]) return x[0] < y[0] if (x[1] !== y[1]) return x[1] < y[1] } return false }