diff --git a/src/components/Profile/StoragePopup.vue b/src/components/Profile/StoragePopup.vue index 0e6ae37..2fa75a2 100644 --- a/src/components/Profile/StoragePopup.vue +++ b/src/components/Profile/StoragePopup.vue @@ -33,7 +33,7 @@ - - diff --git a/src/static/tabs/home-active.png b/src/static/tabs/home-active.png index d93dfd6..dbc24b5 100644 Binary files a/src/static/tabs/home-active.png and b/src/static/tabs/home-active.png differ diff --git a/src/static/tabs/home.png b/src/static/tabs/home.png index 70e693e..b1e7f55 100644 Binary files a/src/static/tabs/home.png and b/src/static/tabs/home.png differ diff --git a/src/static/tabs/record-active.png b/src/static/tabs/record-active.png index 99c07c9..fd30555 100644 Binary files a/src/static/tabs/record-active.png and b/src/static/tabs/record-active.png differ diff --git a/src/static/tabs/record.png b/src/static/tabs/record.png index a518a40..f22b2d4 100644 Binary files a/src/static/tabs/record.png and b/src/static/tabs/record.png differ diff --git a/src/static/tabs/stats-active.png b/src/static/tabs/stats-active.png new file mode 100644 index 0000000..37cb91e Binary files /dev/null and b/src/static/tabs/stats-active.png differ diff --git a/src/static/tabs/stats.png b/src/static/tabs/stats.png new file mode 100644 index 0000000..1b3e78a Binary files /dev/null and b/src/static/tabs/stats.png differ diff --git a/src/static/tabs/user-active.png b/src/static/tabs/user-active.png index 9140455..0a80822 100644 Binary files a/src/static/tabs/user-active.png and b/src/static/tabs/user-active.png differ diff --git a/src/static/tabs/user.png b/src/static/tabs/user.png index 55b4cd4..6993189 100644 Binary files a/src/static/tabs/user.png and b/src/static/tabs/user.png differ diff --git a/src/utils/record.ts b/src/utils/record.ts index 1228e9b..7df51fb 100644 --- a/src/utils/record.ts +++ b/src/utils/record.ts @@ -38,6 +38,12 @@ function cleanOldMonths() { }) } +/** + * 新增记录 + * @param type 类型 + * @param data 数据 + * @param timestamp 时间戳 + */ function addRecord(type: string, data: Record, timestamp: number) { // 键为月份 例如 health_2026-08 const key = getMonthKey(timestamp) @@ -47,17 +53,74 @@ function addRecord(type: string, data: Record, timestamp: number) { if (!all[date]) all[date] = [] - // 在按天存储 - all[date].push({ + const record = { id: Date.now() + Math.random().toString(36).slice(2, 6), time: getTimeStr(timestamp), + timestamp, type, ...data, - }) + } + + // 在按天存储 + all[date].push(record) wx.setStorageSync(key, all) + + appendRecentRecord(type, record) } +/** + * 添加最近记录 + * @param type 类型 + * @param record 记录 + * @param limit + */ +function appendRecentRecord(type: string, record: any, limit = 7) { + const key = `recent_${type}` + const recent = wx.getStorageSync(key) || [] + + // 正序:timestamp 小的在前 + let i = 0 + while (i < recent.length && recent[i].timestamp < record.timestamp) { + i++ + } + recent.splice(i, 0, record) + + // 截断:砍掉前面的(最旧的) + if (recent.length > limit) { + recent.splice(0, recent.length - limit) + } + + wx.setStorageSync(key, recent) +} + +/** + * 删除最近记录的数据 + * @param type 类型 + * @param id id + */ +function removeFromRecentIndex(type: string, id: string) { + const key = `recent_${type}` + const recent = wx.getStorageSync(key) || [] + + const idx = recent.findIndex((item: any) => item.id === id) + if (idx !== -1) { + recent.splice(idx, 1) + wx.setStorageSync(key, recent) + } +} + +export function getRecentRecords(type: string, limit = 7): IHealthRecord[] { + const key = `recent_${type}` + const recent = wx.getStorageSync(key) || [] + return limit ? recent.slice(0, limit) : recent +} + +/** + * 删除单条记录 + * @param date 日期 + * @param id 记录id + */ export function deleteRecordByDate(date: string, id: string) { const key = `health_${date.slice(0, 7)}` const all = wx.getStorageSync(key) || {} @@ -68,6 +131,9 @@ export function deleteRecordByDate(date: string, id: string) { const idx = list.findIndex((item: any) => item.id === id) if (idx === -1) return + const record = list[idx] + const type = record.type + list.splice(idx, 1) if (list.length === 0) { @@ -79,6 +145,41 @@ export function deleteRecordByDate(date: string, id: string) { } else { wx.setStorageSync(key, all) } + + removeFromRecentIndex(type, id) +} + +/** + * 删除月份记录 + * @param month + */ +export function deleteRecordByMonth(month: string) { + const key = `health_${month}` + const all = wx.getStorageSync(key) || {} + + // 1. 先收集这个月所有记录的 id 和 type 的对应关系 + const idsByType: Record = {} + Object.values(all).forEach((list: any) => { + if (Array.isArray(list)) { + list.forEach((item: any) => { + if (!item.type) return + if (!idsByType[item.type]) idsByType[item.type] = [] + idsByType[item.type].push(item.id) + }) + } + }) + + // 2. 删主存储 + wx.removeStorageSync(key) + + // 3. 按 type 逐个清理索引里对应的 id + Object.entries(idsByType).forEach(([type, ids]) => { + const recentKey = `recent_${type}` + const recent = wx.getStorageSync(recentKey) || [] + const idSet = new Set(ids) + const filtered = recent.filter((item: any) => !idSet.has(item.id)) + wx.setStorageSync(recentKey, filtered) + }) } export function addWeight(value: number, note: string, timestamp: number) {