feat: 更新每日记录模块

This commit is contained in:
2026-08-23 21:56:15 +08:00
parent af0f396ed8
commit 0d587ac241
5 changed files with 292 additions and 161 deletions

View File

@@ -1,3 +1,5 @@
import type { IHealthRecord, IRecordMeta } from '@/types/record'
const MONTH_KEEP = 12
function getMonthKey(timestamp: number) {
@@ -56,8 +58,8 @@ export function addWeight(value: number, note: string, timestamp: number) {
addRecord('weight', { value, note }, timestamp)
}
export function addSleep(startTime: number, endTime: number, note: string, timestamp: number) {
addRecord('sleep', { startTime, endTime, note }, timestamp)
export function addSleep(startTime: number, endTime: number, duration: number, note: string, timestamp: number) {
addRecord('sleep', { startTime, endTime, duration, note }, timestamp)
}
export function addSport(category: string, duration: number, note: string, timestamp: number) {
@@ -92,20 +94,20 @@ function getMonthKeyForDate(dateStr: string): string {
return `health_${y}-${m}`
}
export function getDayRecords(date: string): Record<string, any>[] {
export function getDayRecords(date: string): IHealthRecord[] {
const key = getMonthKeyForDate(date)
const all = wx.getStorageSync(key) || {}
const dayRecords = all[date] || []
return [...dayRecords].sort((a, b) => a.time.localeCompare(b.time))
}
export function getMonthRecords(yearMonth: string, type?: string): Record<string, any>[] {
export function getMonthRecords(yearMonth: string, type?: string): IHealthRecord[] {
const key = `health_${yearMonth}`
const all = wx.getStorageSync(key) || {}
const result: Record<string, any>[] = []
const result: IHealthRecord[] = []
Object.keys(all).forEach((date) => {
all[date].forEach((r: Record<string, any>) => {
all[date].forEach((r: IHealthRecord) => {
if (!type || r.type === type) {
result.push({ date, ...r })
}
@@ -116,7 +118,7 @@ export function getMonthRecords(yearMonth: string, type?: string): Record<string
}
// 排序:先按 date 正序,再按 time 正序
function sortRecords(records: Record<string, any>[]): Record<string, any>[] {
function sortRecords(records: IHealthRecord[]): IHealthRecord[] {
return records.sort((a, b) => {
const dateCmp = a.date.localeCompare(b.date)
if (dateCmp !== 0)
@@ -124,3 +126,106 @@ function sortRecords(records: Record<string, any>[]): Record<string, any>[] {
return a.time.localeCompare(b.time)
})
}
const META_MAP: Record<string, IRecordMeta> = {
weight: { icon: '⚖️', label: '体重', color: 'bg-[#E89E3A]' },
sleep: { icon: '😴', label: '睡眠', color: 'bg-[#2E5B8C]' },
bloodPressure: { icon: '❤️', label: '血压', color: 'bg-[#C04A3C]' },
bloodSugar: { icon: '📈', label: '血糖', color: 'bg-[#9C27B0]' },
sport: { icon: '🏃', label: '运动', color: 'bg-[#3A7C5B]' },
temperature: { icon: '🌡️', label: '体温', color: 'bg-[#D32F2F]' },
}
export function getRecordMeta(type: string): IRecordMeta {
return META_MAP[type] || { icon: '📋', label: '其他', color: 'bg-gray-400' }
}
export function getRecordValue(record: IHealthRecord): string {
switch (record.type) {
case 'weight':
return `${record.value} kg`
case 'bloodPressure':
return `${record.systolic}/${record.diastolic} mmHg${record.heartRate ? ` · 心率 ${record.heartRate}` : ''}`
case 'bloodSugar':
return `${record.value} mmol/L · ${record.period}`
case 'heartRate':
return `${record.value} bpm`
case 'temperature':
return `${record.value}${record.location ? ` · ${record.location}` : ''}`
case 'sport':
return `${record.category} · ${record.duration}min`
case 'sleep':
return `${new Date(record.startTime).toTimeString().slice(0, 5)} ~ ${new Date(record.endTime).toTimeString().slice(0, 5)}${record.duration}h`
default:
return ''
}
}
export function getRecordStatus(record: IHealthRecord): string {
switch (record.type) {
case 'bloodPressure':
if (record.systolic >= 140 || record.diastolic >= 90)
return '偏高'
if (record.systolic < 90 || record.diastolic < 60)
return '偏低'
return '正常'
case 'bloodSugar':
if (record.period.includes('空腹')) {
return record.value >= 6.1 ? '偏高' : '正常'
}
// 餐后
return record.value >= 7.8 ? '偏高' : '正常'
case 'heartRate':
if (record.value < 60)
return '偏慢'
if (record.value <= 100)
return '正常'
return '偏快'
case 'temperature':
if (record.value < 36.1)
return '偏低'
if (record.value <= 37.2)
return '正常'
return '偏高'
case 'sport':
return record.duration >= 30 ? '达标' : '偏少'
case 'sleep':
if (record.duration >= 7)
return '充足'
if (record.duration >= 6)
return '良好'
return '不足'
default:
return '未知'
}
}
const WEEKDAY = ['日', '一', '二', '三', '四', '五', '六']
export function groupByDate(records: IHealthRecord[]): Array<{
date: string
weekday: string
items: IHealthRecord[]
}> {
const map: Record<string, IHealthRecord[]> = {}
records.forEach((r) => {
if (!r.date)
return
if (!map[r.date])
map[r.date] = []
map[r.date].push(r)
})
return Object.keys(map)
.sort((a, b) => b.localeCompare(a)) // 日期倒序,最新的在上
.map(date => ({
date,
weekday: WEEKDAY[new Date(date).getDay()],
items: map[date].sort((a, b) => a.time.localeCompare(b.time)), // 当天按时间正序
}))
}