- utils/api 新增 getUsageDay/getUsageDays 与类型 - 首页使用时长卡:真实总时长、Top3 应用占比、环形进度,入口改为「使用记录」 - 新增 pages/usage-records:近 7 天汇总 + 展开每日会话时间线
114 lines
3.0 KiB
TypeScript
114 lines
3.0 KiB
TypeScript
import type { UsageDaily, UsageDay, UsageSession } from '../../utils/api'
|
|
import { getUsageDay, getUsageDays } from '../../utils/api'
|
|
import { getDeviceSn } from '../../utils/token'
|
|
|
|
interface SessionRow {
|
|
key: string
|
|
time: string
|
|
appName: string
|
|
duration: string
|
|
open: boolean
|
|
}
|
|
|
|
interface DayRow {
|
|
date: string
|
|
total: string
|
|
summary: string
|
|
expanded: boolean
|
|
loaded: boolean
|
|
sessions: SessionRow[]
|
|
}
|
|
|
|
Page({
|
|
data: {
|
|
days: [] as DayRow[],
|
|
loading: false
|
|
},
|
|
|
|
onShow() {
|
|
void this.loadDays()
|
|
},
|
|
|
|
toast(title: string) {
|
|
wx.showToast({ title, icon: 'none' })
|
|
},
|
|
|
|
async loadDays() {
|
|
const sn = getDeviceSn()
|
|
if (!sn) {
|
|
this.toast('请先选择设备')
|
|
return
|
|
}
|
|
this.setData({ loading: true })
|
|
try {
|
|
const list: UsageDaily[] = await getUsageDays(sn, 7)
|
|
this.setData({
|
|
days: list.map((item) => ({
|
|
date: item.statDate,
|
|
total: formatDuration(Number(item.totalMs) || 0),
|
|
summary: `${Number(item.appCount) || 0} 个应用 · ${Number(item.sessionCount) || 0} 条记录`,
|
|
expanded: false,
|
|
loaded: false,
|
|
sessions: []
|
|
}))
|
|
})
|
|
} catch (err) {
|
|
this.toast((err as Error).message || '加载失败')
|
|
} finally {
|
|
this.setData({ loading: false })
|
|
}
|
|
},
|
|
|
|
async onToggle(e: WechatMiniprogram.TouchEvent) {
|
|
const index = Number(e.currentTarget.dataset.index)
|
|
const days = this.data.days
|
|
const row = days[index]
|
|
if (!row) return
|
|
if (row.expanded) {
|
|
this.setData({ [`days[${index}].expanded`]: false })
|
|
return
|
|
}
|
|
if (row.loaded) {
|
|
this.setData({ [`days[${index}].expanded`]: true })
|
|
return
|
|
}
|
|
const sn = getDeviceSn()
|
|
if (!sn) return
|
|
try {
|
|
const day: UsageDay = await getUsageDay(sn, row.date)
|
|
const sessions = (day.sessions || []).map((item, i) => toSessionRow(item, i))
|
|
this.setData({
|
|
[`days[${index}].sessions`]: sessions,
|
|
[`days[${index}].loaded`]: true,
|
|
[`days[${index}].expanded`]: true
|
|
})
|
|
} catch (err) {
|
|
this.toast((err as Error).message || '加载失败')
|
|
}
|
|
}
|
|
})
|
|
|
|
function toSessionRow(item: UsageSession, index: number): SessionRow {
|
|
const start = new Date(Number(item.startTs) || 0)
|
|
const end = new Date(Number(item.endTs) || 0)
|
|
return {
|
|
key: String(index),
|
|
time: `${clock(start)} - ${item.open ? '使用中' : clock(end)}`,
|
|
appName: item.appName || item.packageName || '未知应用',
|
|
duration: formatDuration(Number(item.durationMs) || 0),
|
|
open: !!item.open
|
|
}
|
|
}
|
|
|
|
function clock(time: Date): string {
|
|
const pad = (n: number) => String(n).padStart(2, '0')
|
|
return `${pad(time.getHours())}:${pad(time.getMinutes())}`
|
|
}
|
|
|
|
function formatDuration(ms: number): string {
|
|
const seconds = Math.max(0, Math.floor(ms / 1000))
|
|
if (seconds >= 3600) return `${Math.floor(seconds / 3600)}h${Math.floor((seconds % 3600) / 60)}m`
|
|
if (seconds >= 60) return `${Math.floor(seconds / 60)}m`
|
|
return `${seconds}s`
|
|
}
|