diff --git a/stores/logs.js b/stores/logs.js index 1fa3261..1277e7a 100644 --- a/stores/logs.js +++ b/stores/logs.js @@ -66,18 +66,37 @@ export const useLogsStore = defineStore('logs', { return sortedLogs.map((log, index) => { const type = normalizeLogType(log) - - // 计算间隔时间:当前记录与上一条记录的间隔(上一条是 index-1,因为已倒序) + + // 计算间隔时间 let interval = '' - if (index > 0) { - const currentTime = getTime(log) - const prevTime = getTime(sortedLogs[index - 1]) - const diff = prevTime - currentTime // 上一条时间 - 当前时间(因为已倒序) - + const currentTime = getTime(log) + + if (index === 0) { + // 第一条记录:显示距离当前时间的间隔 + const now = Date.now() + const diff = now - currentTime + if (diff > 0) { const hours = Math.floor(diff / (1000 * 60 * 60)) const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60)) - + + if (hours > 0) { + interval = `${hours}小时${minutes}分` + } else if (minutes > 0) { + interval = `${minutes}分钟` + } else { + interval = '刚刚' + } + } + } else { + // 后续记录:显示距离上一条记录的间隔 + const prevTime = getTime(sortedLogs[index - 1]) + const diff = prevTime - currentTime + + if (diff > 0) { + const hours = Math.floor(diff / (1000 * 60 * 60)) + const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60)) + if (hours > 0) { interval = `${hours}小时${minutes}分` } else if (minutes > 0) {