This commit is contained in:
yvan 2025-08-13 17:13:50 +08:00
parent 34a3f7bf1f
commit 8c7e7750b5
3 changed files with 92 additions and 10 deletions

View File

@ -194,6 +194,20 @@ export default {
title: '洗澡护理',
date: '2024-01-17',
content: '给小橘洗了澡,全程很乖没有挣扎,现在毛毛很香'
},
{
id: 4,
type: 'weight',
title: '体重记录',
date: '2024-01-15',
content: '体重4.1kg,保持稳定增长趋势'
},
{
id: 5,
type: 'food',
title: '饮食记录',
date: '2024-01-14',
content: '今天食欲很好,吃完了所有的猫粮'
}
],
layout1Items: [

View File

@ -200,6 +200,7 @@ export default {
this.petId = options.petId || ''
this.petName = options.petName || '宠物'
this.loadPetInfo()
this.initializeTestDataIfNeeded()
this.loadWeightData()
this.generateChartData()
},
@ -220,6 +221,12 @@ export default {
}
},
initializeTestDataIfNeeded() {
//
const records = weightManager.getWeightRecords(this.petId)
console.log(`宠物 ${this.petId} 的体重记录数量:`, records.length)
},
loadWeightData() {
//
this.currentWeight = weightManager.getCurrentWeight(this.petId) || 4.2

View File

@ -16,13 +16,56 @@ class WeightManager {
getWeightRecords(petId) {
try {
const allRecords = uni.getStorageSync(this.storageKey) || {}
return allRecords[petId] || []
let records = allRecords[petId] || []
// 如果没有数据,初始化一些测试数据
if (records.length === 0) {
records = this.initializeTestData(petId)
allRecords[petId] = records
uni.setStorageSync(this.storageKey, allRecords)
}
return records
} catch (error) {
console.error('获取体重记录失败:', error)
return []
return this.initializeTestData(petId)
}
}
/**
* 初始化测试数据
* @param {string} petId 宠物ID
* @returns {Array} 测试数据数组
*/
initializeTestData(petId) {
const now = new Date()
const testData = []
// 生成过去3个月的体重数据模拟真实的体重变化
const baseWeight = 3.8 // 基础体重
const growthRate = 0.015 // 每周增长率
for (let i = 90; i >= 0; i -= 3) { // 每3天一个记录点
const date = new Date(now.getTime() - i * 24 * 60 * 60 * 1000)
const weeksPassed = (90 - i) / 7
// 模拟自然的体重增长,加入一些随机波动
let weight = baseWeight + (weeksPassed * growthRate * baseWeight)
weight += (Math.random() - 0.5) * 0.1 // 添加±0.05kg的随机波动
weight = Math.round(weight * 10) / 10 // 保留一位小数
testData.push({
id: Date.now() + i,
weight: weight,
date: date.toISOString(),
note: i === 0 ? '最新记录' : '自动生成',
timestamp: date.getTime()
})
}
return testData
}
/**
* 添加体重记录
* @param {string} petId 宠物ID
@ -170,46 +213,64 @@ class WeightManager {
generateMockData(timeRange) {
const now = new Date()
const mockData = []
const currentWeight = 4.2
switch (timeRange) {
case 'week':
// 近一周数据:每天一个点
for (let i = 6; i >= 0; i--) {
const date = new Date(now.getTime() - i * 24 * 60 * 60 * 1000)
const weight = currentWeight - (i * 0.02) + (Math.random() - 0.5) * 0.05
mockData.push({
weight: 4.0 + Math.random() * 0.4,
weight: Math.round(weight * 10) / 10,
date: date.toISOString()
})
}
break
case 'month':
// 近一月数据:每周一个点
for (let i = 4; i >= 0; i--) {
const date = new Date(now.getTime() - i * 7 * 24 * 60 * 60 * 1000)
const weight = currentWeight - (i * 0.08) + (Math.random() - 0.5) * 0.1
mockData.push({
weight: 3.9 + i * 0.075,
weight: Math.round(weight * 10) / 10,
date: date.toISOString()
})
}
break
case 'year':
// 近一年数据:每两个月一个点
for (let i = 5; i >= 0; i--) {
const date = new Date(now.getTime() - i * 60 * 24 * 60 * 60 * 1000)
const weight = currentWeight - (i * 0.2) + (Math.random() - 0.5) * 0.15
mockData.push({
weight: 3.2 + (5 - i) * 0.2,
weight: Math.round(weight * 10) / 10,
date: date.toISOString()
})
}
break
default:
for (let i = 6; i >= 0; i--) {
const date = new Date(now.getTime() - i * 90 * 24 * 60 * 60 * 1000)
// 全部历史:从小猫到现在
const milestones = [
{ months: 18, weight: 1.2 },
{ months: 15, weight: 1.8 },
{ months: 12, weight: 2.5 },
{ months: 9, weight: 3.0 },
{ months: 6, weight: 3.4 },
{ months: 3, weight: 3.8 },
{ months: 0, weight: 4.2 }
]
milestones.forEach(milestone => {
const date = new Date(now.getTime() - milestone.months * 30 * 24 * 60 * 60 * 1000)
mockData.push({
weight: 1.2 + (6 - i) * 0.5,
weight: milestone.weight,
date: date.toISOString()
})
}
})
}
return mockData
return mockData.sort((a, b) => new Date(a.date) - new Date(b.date))
}
/**