diff --git a/pages/usage-records/index.ts b/pages/usage-records/index.ts index c0c46c5..e9d35d6 100644 --- a/pages/usage-records/index.ts +++ b/pages/usage-records/index.ts @@ -1,116 +1,79 @@ -import type { UsageDaily, UsageDay, UsageSession } from '../../utils/api' -import { getUsageDay, getUsageDays } from '../../utils/api' +import type { UsageSession } from '../../utils/api' +import { getUsageSessions } 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[] + iconUrl: string + timeText: string } Page({ data: { - days: [] as DayRow[], - loading: false + sessions: [] as SessionRow[], + loading: true }, onShow() { - void this.loadDays() + void this.load() }, toast(title: string) { wx.showToast({ title, icon: 'none' }) }, - async loadDays() { + async load() { const sn = getDeviceSn() if (!sn) { this.toast('请先选择设备') + this.setData({ loading: false }) 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: [] - })) - }) + const list = await getUsageSessions(sn, 7, 200) + const rows: SessionRow[] = list + .filter((item) => (Number(item.durationMs) || 0) >= 1000) + .map((item, index) => toRow(item, index)) + this.setData({ sessions: rows }) } 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 || []) - .filter((item) => (Number(item.durationMs) || 0) >= 1000) - .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 { +function toRow(item: UsageSession, index: number): SessionRow { const start = new Date(Number(item.startTs) || 0) const end = new Date(Number(item.endTs) || 0) + const range = item.open + ? `${clock(start)} 起 · 使用中 ${formatDuration(Number(item.durationMs) || 0)}` + : `${clock(start)} - ${clock(end)} · 用时 ${formatDuration(Number(item.durationMs) || 0)}` return { - key: String(index), - time: `${clock(start)} - ${item.open ? '使用中' : clock(end)}`, + key: `${index}_${item.startTs}`, appName: item.appName || item.packageName || '未知应用', - duration: formatDuration(Number(item.durationMs) || 0), - open: !!item.open + iconUrl: item.iconUrl || '', + timeText: `${dayText(start)} ${range}` } } +function pad(n: number): string { + return String(n).padStart(2, '0') +} + function clock(time: Date): string { - const pad = (n: number) => String(n).padStart(2, '0') - return `${pad(time.getHours())}:${pad(time.getMinutes())}` + return `${pad(time.getHours())}:${pad(time.getMinutes())}:${pad(time.getSeconds())}` +} + +function dayText(time: Date): string { + return `${time.getFullYear()}-${pad(time.getMonth() + 1)}-${pad(time.getDate())}` } 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` + if (seconds >= 60) return `${Math.floor(seconds / 60)}m${seconds % 60}s` return `${seconds}s` } diff --git a/pages/usage-records/index.wxml b/pages/usage-records/index.wxml index 8d6e6de..0d174e2 100644 --- a/pages/usage-records/index.wxml +++ b/pages/usage-records/index.wxml @@ -1,21 +1,11 @@ - 暂无使用记录 - - - {{item.date}} - - {{item.total}} - {{item.expanded ? '▾' : '▸'}} + 暂无使用记录 + + + 📱 + + {{item.appName}} + {{item.timeText}} - {{item.summary}} - - - 暂无记录 - - {{session.time}} - {{session.appName}} - {{session.duration}} - - diff --git a/pages/usage-records/index.wxss b/pages/usage-records/index.wxss index ad4fe28..426fa92 100644 --- a/pages/usage-records/index.wxss +++ b/pages/usage-records/index.wxss @@ -2,72 +2,45 @@ padding: 16rpx 0 32rpx; } -.day-card { - margin-bottom: 16rpx; -} - -.day-head { +.record-row { display: flex; align-items: center; } -.day-date { - font-size: 30rpx; - font-weight: 600; - color: var(--ink); -} - -.spacer { - flex: 1; -} - -.day-total { - font-size: 28rpx; - font-weight: 600; - color: #07c160; -} - -.chevron { - margin-left: 12rpx; - font-size: 24rpx; - color: var(--sub); -} - -.day-summary { - margin-top: 8rpx; - font-size: 22rpx; - color: var(--sub); -} - -.divider { - height: 1rpx; - margin: 16rpx 0 8rpx; +.app-icon { + width: 84rpx; + height: 84rpx; + flex-shrink: 0; + border-radius: 20rpx; background: var(--track); } -.session-row { +.app-icon-fallback { display: flex; align-items: center; - padding: 10rpx 0; + justify-content: center; + font-size: 36rpx; } -.session-time { - width: 220rpx; - font-size: 24rpx; - color: var(--ink); -} - -.session-app { +.record-body { flex: 1; + margin-left: 20rpx; + display: flex; + flex-direction: column; overflow: hidden; - white-space: nowrap; - text-overflow: ellipsis; - font-size: 24rpx; - color: var(--ink); } -.session-duration { - margin-left: 12rpx; +.record-title { + font-size: 28rpx; + font-weight: 600; + color: var(--ink); + white-space: nowrap; + overflow: hidden; + text-overflow: ellipsis; +} + +.record-meta { + margin-top: 6rpx; font-size: 22rpx; color: var(--sub); } @@ -78,7 +51,3 @@ font-size: 26rpx; color: var(--sub); } - -.empty.small { - padding: 24rpx 0; -} diff --git a/utils/api.ts b/utils/api.ts index 1c0e7ce..28babab 100644 --- a/utils/api.ts +++ b/utils/api.ts @@ -548,6 +548,7 @@ export interface UsageApp { export interface UsageSession { packageName: string appName?: string + iconUrl?: string startTs: number | string endTs: number | string durationMs: number | string @@ -589,3 +590,9 @@ export const getUsageDays = (sn: string, days = 7) => request({ url: `/v1/client/sn/usage/days?${query({ sn, days })}` }) + +/** 应用使用记录(跨天,按开始时间倒序,含应用图标) */ +export const getUsageSessions = (sn: string, days = 7, limit = 200) => + request({ + url: `/v1/client/sn/usage/sessions?${query({ sn, days, limit })}` + })