feat(usage): 使用记录改为按应用列表展示

跨天按开始时间倒序,每条记录显示应用图标、开始/结束精确到秒与用时。
This commit is contained in:
tongtong committed 2026-12-02 02:08:02 +08:00
1 parent 7a83a2acb3
commit 134fb3dac7
4 files changed
+69 -140

No files matched your search

+31 -68
View File
@@ -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`
}
+7 -17
View File
@@ -1,21 +1,11 @@
<view class="page">
<view wx:if="{{!days.length && !loading}}" class="empty">暂无使用记录</view>
<view wx:for="{{days}}" wx:key="date" class="card day-card">
<view class="day-head" bindtap="onToggle" data-index="{{index}}">
<text class="day-date">{{item.date}}</text>
<view class="spacer"></view>
<text class="day-total">{{item.total}}</text>
<text class="chevron">{{item.expanded ? '▾' : '▸'}}</text>
<view wx:if="{{!sessions.length && !loading}}" class="empty">暂无使用记录</view>
<view wx:for="{{sessions}}" wx:key="key" class="card record-row">
<image wx:if="{{item.iconUrl}}" class="app-icon" src="{{item.iconUrl}}" mode="aspectFill" />
<view wx:else class="app-icon app-icon-fallback">📱</view>
<view class="record-body">
<text class="record-title">{{item.appName}}</text>
<text class="record-meta">{{item.timeText}}</text>
</view>
<view class="day-summary">{{item.summary}}</view>
<block wx:if="{{item.expanded}}">
<view class="divider"></view>
<view wx:if="{{!item.sessions.length}}" class="empty small">暂无记录</view>
<view wx:for="{{item.sessions}}" wx:for-item="session" wx:key="key" class="session-row">
<text class="session-time">{{session.time}}</text>
<text class="session-app">{{session.appName}}</text>
<text class="session-duration">{{session.duration}}</text>
</view>
</block>
</view>
</view>
+24 -55
View File
@@ -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;
}
+7
View File
@@ -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<UsageDaily[]>({
url: `/v1/client/sn/usage/days?${query({ sn, days })}`
})
/** 应用使用记录(跨天,按开始时间倒序,含应用图标) */
export const getUsageSessions = (sn: string, days = 7, limit = 200) =>
request<UsageSession[]>({
url: `/v1/client/sn/usage/sessions?${query({ sn, days, limit })}`
})