Commit ce7a6d75 authored by spc's avatar spc

feeding fixed

parent caf6d404
This diff is collapsed.
...@@ -8,9 +8,13 @@ ...@@ -8,9 +8,13 @@
<view class="nav-left"> <view class="nav-left">
<image :src="feedingRecordRes.icon_return" class="back-btn" @click="goBack" /> <image :src="feedingRecordRes.icon_return" class="back-btn" @click="goBack" />
<image :src="feedingRecordRes.icon_star" class="baby-icon-star" /> <image :src="feedingRecordRes.icon_star" class="baby-icon-star" />
<view class="baby-info" @click="showBabySwitch"> <view class="baby-info" @click="showBabySwitch" v-if="babyList.length > 1">
<text class="baby-name">{{ babyList[currentBabyIndex].name }}</text> <text class="baby-name">{{ getCurrentBabyName() }}</text>
<text class="baby-age">{{ calculateBabyAge(babyList[currentBabyIndex].birthday) }}</text> <text class="baby-age">{{ getCurrentBabyAge() }}</text>
</view>
<view class="baby-info" v-else>
<text class="baby-name">{{ getCurrentBabyName() }}</text>
<text class="baby-age">{{ getCurrentBabyAge() }}</text>
</view> </view>
<image :src="feedingRecordRes.icon_baby_change" class="baby-icon-change" /> <image :src="feedingRecordRes.icon_baby_change" class="baby-icon-change" />
</view> </view>
...@@ -206,6 +210,7 @@ ...@@ -206,6 +210,7 @@
:baby-list="babyList" :baby-list="babyList"
:selected-index="currentBabyIndex" :selected-index="currentBabyIndex"
@change="onBabyChange" @change="onBabyChange"
v-if="babyList.length > 1"
/> />
</template> </template>
...@@ -715,7 +720,10 @@ function goBack() { ...@@ -715,7 +720,10 @@ function goBack() {
// 宝宝切换相关方法 // 宝宝切换相关方法
function showBabySwitch() { function showBabySwitch() {
showBabySwitchPopup.value = true // 只有当有多个宝宝时才显示切换弹窗
if (babyList.value.length > 1) {
showBabySwitchPopup.value = true
}
} }
function onBabyChange(baby, index) { function onBabyChange(baby, index) {
...@@ -737,18 +745,69 @@ function onBabyChange(baby, index) { ...@@ -737,18 +745,69 @@ function onBabyChange(baby, index) {
} }
function calculateBabyAge(birthday) { function calculateBabyAge(birthday) {
const birthDate = new Date(birthday) // 检查生日参数是否有效
const today = new Date() if (!birthday) {
const diffTime = Math.abs(today - birthDate) return '0天'
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24)) }
const years = Math.floor(diffDays / 365)
const days = diffDays % 365
if (years > 0) { try {
return `${years}${days}天` const birthDate = new Date(birthday)
} else { const today = new Date()
return `${days}天`
// 检查日期是否有效
if (isNaN(birthDate.getTime()) || isNaN(today.getTime())) {
console.warn('无效的日期格式:', birthday)
return '0天'
}
const diffTime = Math.abs(today - birthDate)
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24))
// 检查计算结果是否有效
if (isNaN(diffDays) || diffDays < 0) {
console.warn('日期计算错误:', { birthday, diffDays })
return '0天'
}
const years = Math.floor(diffDays / 365)
const days = diffDays % 365
if (years > 0) {
return `${years}${days}天`
} else {
return `${days}天`
}
} catch (error) {
console.error('计算宝宝年龄时出错:', error, birthday)
return '0天'
}
}
// 安全获取当前宝宝姓名
function getCurrentBabyName() {
try {
const currentBaby = babyList.value[currentBabyIndex.value]
if (currentBaby && currentBaby.name) {
return currentBaby.name
}
return '宝宝'
} catch (error) {
console.error('获取宝宝姓名时出错:', error)
return '宝宝'
}
}
// 安全获取当前宝宝年龄
function getCurrentBabyAge() {
try {
const currentBaby = babyList.value[currentBabyIndex.value]
if (currentBaby && currentBaby.birthday) {
return calculateBabyAge(currentBaby.birthday)
}
return '0天'
} catch (error) {
console.error('获取宝宝年龄时出错:', error)
return '0天'
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment