feat: 支持多页面分享与统一默认头像

Made-with: Cursor
This commit is contained in:
nepiedg
2026-03-10 23:00:37 +08:00
parent 585b0a203f
commit bdb8d425eb
9 changed files with 128 additions and 68 deletions
+46 -44
View File
@@ -43,7 +43,7 @@ export const useLogsStore = defineStore('logs', {
return []
}
// 获取时间戳的辅助函数
// 获取时间戳的辅助函数(统一处理 smoke_at / smoke_time / createtime
const getTime = (log) => {
if (log.smoke_at) {
return new Date(log.smoke_at).getTime()
@@ -52,60 +52,62 @@ export const useLogsStore = defineStore('logs', {
return new Date(log.smoke_time).getTime()
}
if (log.createtime) {
return typeof log.createtime === 'number' ? log.createtime * 1000 : new Date(log.createtime).getTime()
return typeof log.createtime === 'number'
? log.createtime * 1000
: new Date(log.createtime).getTime()
}
return 0
}
// 先按时间倒序排序
// 先按时间正序(最早在前)计算「距上次抽烟」的时间间隔,
// 再按时间倒序用于页面展示,保证间隔只和上一次「抽烟」记录有关
const logsAsc = [...state.logs].sort((a, b) => {
const timeA = getTime(a)
const timeB = getTime(b)
return timeA - timeB
})
const intervalById = new Map()
let lastSmokeTime = null
logsAsc.forEach((log) => {
const type = normalizeLogType(log)
const currentTime = getTime(log)
let interval = ''
// 已存在「上次抽烟」时间,计算与其的间隔
if (lastSmokeTime !== null && currentTime > lastSmokeTime) {
const diff = currentTime - lastSmokeTime
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 = '刚刚'
}
}
intervalById.set(log.id, interval)
// 仅当当前记录是「抽烟」时,更新「上次抽烟时间」
if (type === 'smoke' && currentTime > 0) {
lastSmokeTime = currentTime
}
})
// 再按时间倒序排序用于展示
const sortedLogs = [...state.logs].sort((a, b) => {
const timeA = getTime(a)
const timeB = getTime(b)
return timeB - timeA // 倒序:最新的在前
})
return sortedLogs.map((log, index) => {
return sortedLogs.map((log) => {
const type = normalizeLogType(log)
// 计算间隔时间
let interval = ''
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) {
interval = `${minutes}分钟`
} else {
interval = '刚刚'
}
}
}
const interval = intervalById.get(log.id) || ''
// 获取显示日期(用本地日期,避免 UTC 导致差一天)
let displayDate = ''