feat: 增加存储功能
This commit is contained in:
126
src/utils/record.ts
Normal file
126
src/utils/record.ts
Normal file
@@ -0,0 +1,126 @@
|
||||
const MONTH_KEEP = 12
|
||||
|
||||
function getMonthKey(timestamp: number) {
|
||||
const d = new Date(timestamp)
|
||||
return `health_${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}`
|
||||
}
|
||||
|
||||
export function getDateStr(timestamp: number) {
|
||||
const d = new Date(timestamp)
|
||||
return d.toISOString().slice(0, 10)
|
||||
}
|
||||
|
||||
export function getMonthStr(timestamp: number): string {
|
||||
const d = new Date(timestamp)
|
||||
const y = d.getFullYear()
|
||||
const m = String(d.getMonth() + 1).padStart(2, '0')
|
||||
return `${y}-${m}`
|
||||
}
|
||||
|
||||
export function getTimeStr(timestamp: number) {
|
||||
const d = new Date(timestamp)
|
||||
return d.toTimeString().slice(0, 5)
|
||||
}
|
||||
|
||||
function cleanOldMonths() {
|
||||
const cutoff = new Date()
|
||||
cutoff.setMonth(cutoff.getMonth() - MONTH_KEEP)
|
||||
const cutoffKey = `health_${cutoff.getFullYear()}-${String(cutoff.getMonth() + 1).padStart(2, '0')}`
|
||||
const keys = wx.getStorageInfoSync().keys
|
||||
keys.forEach((key) => {
|
||||
if (key.startsWith('health_') && key < cutoffKey) {
|
||||
wx.removeStorageSync(key)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function addRecord(type: string, data: Record<string, any>, timestamp: number) {
|
||||
const key = getMonthKey(timestamp)
|
||||
const all = wx.getStorageSync(key) || {}
|
||||
const date = getDateStr(timestamp)
|
||||
|
||||
if (!all[date])
|
||||
all[date] = []
|
||||
|
||||
all[date].push({
|
||||
id: Date.now() + Math.random().toString(36).slice(2, 6),
|
||||
time: getTimeStr(timestamp),
|
||||
type,
|
||||
...data,
|
||||
})
|
||||
|
||||
wx.setStorageSync(key, all)
|
||||
}
|
||||
|
||||
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 addSport(category: string, duration: number, note: string, timestamp: number) {
|
||||
addRecord('sport', { category, duration, note }, timestamp)
|
||||
}
|
||||
|
||||
export function addBloodPressure(systolic: number, diastolic: number, heartRate: number, note: string, timestamp: number) {
|
||||
addRecord('bloodPressure', { systolic, diastolic, heartRate, note }, timestamp)
|
||||
}
|
||||
|
||||
export function addBloodSugar(value: number, period: string, note: string, timestamp: number) {
|
||||
addRecord('bloodSugar', { value, period, note }, timestamp)
|
||||
}
|
||||
|
||||
export function addTemperature(value: number, location: string, note: string, timestamp: number) {
|
||||
addRecord('temperature', { value, location, note }, timestamp)
|
||||
}
|
||||
|
||||
function getAllMonthKeys(): string[] {
|
||||
return wx.getStorageInfoSync().keys.filter(k => k.startsWith('health_')).sort()
|
||||
}
|
||||
|
||||
// ─── 根据时间戳反推它属于哪个月的 key ───
|
||||
function getMonthKeyForTimestamp(timestamp: number): string {
|
||||
const d = new Date(timestamp)
|
||||
return `health_${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}`
|
||||
}
|
||||
|
||||
// ─── 根据 "YYYY-MM-DD" 反推月份 key ───
|
||||
function getMonthKeyForDate(dateStr: string): string {
|
||||
const [y, m] = dateStr.split('-')
|
||||
return `health_${y}-${m}`
|
||||
}
|
||||
|
||||
export function getDayRecords(date: string): Record<string, any>[] {
|
||||
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>[] {
|
||||
const key = `health_${yearMonth}`
|
||||
const all = wx.getStorageSync(key) || {}
|
||||
|
||||
const result: Record<string, any>[] = []
|
||||
Object.keys(all).forEach((date) => {
|
||||
all[date].forEach((r: Record<string, any>) => {
|
||||
if (!type || r.type === type) {
|
||||
result.push({ date, ...r })
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
return sortRecords(result)
|
||||
}
|
||||
|
||||
// 排序:先按 date 正序,再按 time 正序
|
||||
function sortRecords(records: Record<string, any>[]): Record<string, any>[] {
|
||||
return records.sort((a, b) => {
|
||||
const dateCmp = a.date.localeCompare(b.date)
|
||||
if (dateCmp !== 0)
|
||||
return dateCmp
|
||||
return a.time.localeCompare(b.time)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user