优化页面展示
This commit is contained in:
1 parent
cbcd6b4c7f
commit
1ed38c6648
67 files changed
+4484
-370
No files matched your search
@@ -18,7 +18,8 @@ import {
|
||||
updateCurrentBaby,
|
||||
updateFamily,
|
||||
updateMember,
|
||||
updateRecord
|
||||
updateRecord,
|
||||
getServerTime
|
||||
} from '../api/client'
|
||||
import type {
|
||||
ActionPreferences,
|
||||
@@ -37,7 +38,7 @@ import type {
|
||||
|
||||
const defaultPreferences: ActionPreferences = {
|
||||
defaultFeedingMethod: 'breastfeed',
|
||||
defaultBottleAmount: 60,
|
||||
defaultBottleAmount: 30,
|
||||
defaultBreastSide: 'left',
|
||||
defaultSleepMode: 'timer',
|
||||
quickFeedbackToast: true,
|
||||
@@ -56,6 +57,36 @@ function loadInitialPreferences(): ActionPreferences {
|
||||
return defaultPreferences
|
||||
}
|
||||
|
||||
let bfTicker: ReturnType<typeof setInterval> | null = null
|
||||
|
||||
function startBfTicker(store: any) {
|
||||
if (bfTicker) return
|
||||
bfTicker = setInterval(() => {
|
||||
if (!store.bfTimerActive) {
|
||||
stopBfTicker()
|
||||
return
|
||||
}
|
||||
const now = Date.now()
|
||||
const delta = Math.max(0, Math.floor((now - store.bfLastTickTime) / 1000))
|
||||
if (delta > 0) {
|
||||
if (store.bfActiveSide === 'left') {
|
||||
store.bfLeftSeconds += delta
|
||||
} else {
|
||||
store.bfRightSeconds += delta
|
||||
}
|
||||
store.bfLastTickTime = now
|
||||
store.persistBfTimer()
|
||||
}
|
||||
}, 1000)
|
||||
}
|
||||
|
||||
function stopBfTicker() {
|
||||
if (bfTicker) {
|
||||
clearInterval(bfTicker)
|
||||
bfTicker = null
|
||||
}
|
||||
}
|
||||
|
||||
export const useCareStore = defineStore('care', {
|
||||
state: () => ({
|
||||
context: null as FamilyContext | null,
|
||||
@@ -66,14 +97,34 @@ export const useCareStore = defineStore('care', {
|
||||
summary: null as DailySummary | null,
|
||||
operatorName: localStorage.getItem('operatorName') || '宝妈',
|
||||
preferences: loadInitialPreferences() as ActionPreferences,
|
||||
bfTimerActive: false,
|
||||
bfActiveSide: 'left' as 'left' | 'right',
|
||||
bfLeftSeconds: 0,
|
||||
bfRightSeconds: 0,
|
||||
bfLastTickTime: 0,
|
||||
serverOffsetMs: 0,
|
||||
loading: false,
|
||||
error: ''
|
||||
}),
|
||||
actions: {
|
||||
async syncServerTime() {
|
||||
try {
|
||||
const info = await getServerTime()
|
||||
if (info && typeof info.timestamp === 'number') {
|
||||
this.serverOffsetMs = info.timestamp - Date.now()
|
||||
}
|
||||
} catch {
|
||||
// fallback
|
||||
}
|
||||
},
|
||||
getServerNow(): Date {
|
||||
return new Date(Date.now() + this.serverOffsetMs)
|
||||
},
|
||||
async load() {
|
||||
this.loading = true
|
||||
this.error = ''
|
||||
try {
|
||||
await this.syncServerTime()
|
||||
this.context = await getFamilyContext()
|
||||
this.babies = this.context.babies
|
||||
this.members = this.context.members
|
||||
@@ -85,6 +136,7 @@ export const useCareStore = defineStore('care', {
|
||||
if (this.baby) {
|
||||
localStorage.setItem('selectedBabyId', this.baby.babyId)
|
||||
this.summary = await getDailySummary(this.baby.babyId)
|
||||
this.syncBreastfeedTimer()
|
||||
}
|
||||
} catch (error) {
|
||||
this.error = error instanceof Error ? error.message : '加载失败'
|
||||
@@ -94,7 +146,9 @@ export const useCareStore = defineStore('care', {
|
||||
},
|
||||
async refreshSummary() {
|
||||
if (!this.baby) return
|
||||
await this.syncServerTime()
|
||||
this.summary = await getDailySummary(this.baby.babyId)
|
||||
this.syncBreastfeedTimer()
|
||||
},
|
||||
async selectBaby(babyId: string) {
|
||||
this.selectedBabyId = babyId
|
||||
@@ -162,23 +216,185 @@ export const useCareStore = defineStore('care', {
|
||||
await deleteMember(this.context.family.familyId, memberId)
|
||||
this.members = this.members.filter(m => m.memberId !== memberId)
|
||||
},
|
||||
async logFeeding(method: FeedingMethod, amountMl?: number, breastSide?: 'left' | 'right') {
|
||||
async logFeeding(
|
||||
payloadOrMethod: {
|
||||
method: FeedingMethod
|
||||
amountMl?: number
|
||||
breastSide?: string
|
||||
durationMinutes?: number
|
||||
feedingTime?: string
|
||||
endTime?: string
|
||||
leftDurationMinutes?: number
|
||||
rightDurationMinutes?: number
|
||||
note?: string
|
||||
} | FeedingMethod,
|
||||
amountMl?: number,
|
||||
breastSide?: string
|
||||
) {
|
||||
if (!this.baby) return
|
||||
await createFeeding({
|
||||
if (typeof payloadOrMethod === 'string') {
|
||||
await createFeeding({
|
||||
babyId: this.baby.babyId,
|
||||
feedingMethod: payloadOrMethod,
|
||||
amountMl,
|
||||
breastSide,
|
||||
operatorName: this.operatorName
|
||||
})
|
||||
} else {
|
||||
await createFeeding({
|
||||
babyId: this.baby.babyId,
|
||||
feedingMethod: payloadOrMethod.method,
|
||||
amountMl: payloadOrMethod.amountMl,
|
||||
breastSide: payloadOrMethod.breastSide,
|
||||
durationMinutes: payloadOrMethod.durationMinutes,
|
||||
feedingTime: payloadOrMethod.feedingTime,
|
||||
endTime: payloadOrMethod.endTime,
|
||||
leftDurationMinutes: payloadOrMethod.leftDurationMinutes,
|
||||
rightDurationMinutes: payloadOrMethod.rightDurationMinutes,
|
||||
note: payloadOrMethod.note,
|
||||
operatorName: this.operatorName
|
||||
})
|
||||
}
|
||||
await this.refreshSummary()
|
||||
},
|
||||
persistBfTimer() {
|
||||
if (!this.baby) return
|
||||
try {
|
||||
localStorage.setItem(`bfTimer_${this.baby.babyId}`, JSON.stringify({
|
||||
activeSide: this.bfActiveSide,
|
||||
leftSeconds: this.bfLeftSeconds,
|
||||
rightSeconds: this.bfRightSeconds,
|
||||
lastTickTime: this.bfLastTickTime
|
||||
}))
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
},
|
||||
clearBfTimerStorage() {
|
||||
if (!this.baby) return
|
||||
try {
|
||||
localStorage.removeItem(`bfTimer_${this.baby.babyId}`)
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
},
|
||||
syncBreastfeedTimer() {
|
||||
if (!this.baby) return
|
||||
const active = this.summary?.activeFeeding
|
||||
if (active) {
|
||||
let restored = false
|
||||
try {
|
||||
const raw = localStorage.getItem(`bfTimer_${this.baby.babyId}`)
|
||||
if (raw) {
|
||||
const data = JSON.parse(raw)
|
||||
this.bfActiveSide = data.activeSide === 'right' ? 'right' : 'left'
|
||||
this.bfLeftSeconds = Number(data.leftSeconds) || 0
|
||||
this.bfRightSeconds = Number(data.rightSeconds) || 0
|
||||
const lastTick = Number(data.lastTickTime) || Date.now()
|
||||
const delta = Math.max(0, Math.floor((Date.now() - lastTick) / 1000))
|
||||
if (delta > 0) {
|
||||
if (this.bfActiveSide === 'left') {
|
||||
this.bfLeftSeconds += delta
|
||||
} else {
|
||||
this.bfRightSeconds += delta
|
||||
}
|
||||
}
|
||||
this.bfLastTickTime = Date.now()
|
||||
this.bfTimerActive = true
|
||||
restored = true
|
||||
}
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
if (!restored) {
|
||||
const startMs = active.startTime ? new Date(active.startTime).getTime() : Date.now()
|
||||
const totalElapsed = Math.max(0, Math.floor((Date.now() - startMs) / 1000))
|
||||
this.bfActiveSide = this.preferences.defaultBreastSide || 'left'
|
||||
this.bfLeftSeconds = this.bfActiveSide === 'left' ? totalElapsed : 0
|
||||
this.bfRightSeconds = this.bfActiveSide === 'right' ? totalElapsed : 0
|
||||
this.bfLastTickTime = Date.now()
|
||||
this.bfTimerActive = true
|
||||
}
|
||||
this.persistBfTimer()
|
||||
startBfTicker(this)
|
||||
} else {
|
||||
if (this.bfTimerActive) {
|
||||
this.bfTimerActive = false
|
||||
this.bfLeftSeconds = 0
|
||||
this.bfRightSeconds = 0
|
||||
this.clearBfTimerStorage()
|
||||
stopBfTicker()
|
||||
}
|
||||
}
|
||||
},
|
||||
async startBreastfeedTimer(breastSide: 'left' | 'right') {
|
||||
if (!this.baby) return
|
||||
await startFeeding({
|
||||
babyId: this.baby.babyId,
|
||||
feedingMethod: method,
|
||||
amountMl,
|
||||
breastSide,
|
||||
operatorName: this.operatorName
|
||||
})
|
||||
this.bfTimerActive = true
|
||||
this.bfActiveSide = breastSide
|
||||
this.bfLeftSeconds = 0
|
||||
this.bfRightSeconds = 0
|
||||
this.bfLastTickTime = Date.now()
|
||||
this.persistBfTimer()
|
||||
startBfTicker(this)
|
||||
await this.refreshSummary()
|
||||
},
|
||||
switchBreastfeedSide(targetSide: 'left' | 'right') {
|
||||
if (!this.bfTimerActive || this.bfActiveSide === targetSide) return
|
||||
const now = Date.now()
|
||||
const delta = Math.max(0, Math.floor((now - this.bfLastTickTime) / 1000))
|
||||
if (delta > 0) {
|
||||
if (this.bfActiveSide === 'left') {
|
||||
this.bfLeftSeconds += delta
|
||||
} else {
|
||||
this.bfRightSeconds += delta
|
||||
}
|
||||
}
|
||||
this.bfActiveSide = targetSide
|
||||
this.bfLastTickTime = now
|
||||
this.persistBfTimer()
|
||||
},
|
||||
async endBreastfeedTimer() {
|
||||
if (!this.baby) return
|
||||
if (this.bfTimerActive) {
|
||||
const now = Date.now()
|
||||
const delta = Math.max(0, Math.floor((now - this.bfLastTickTime) / 1000))
|
||||
if (delta > 0) {
|
||||
if (this.bfActiveSide === 'left') {
|
||||
this.bfLeftSeconds += delta
|
||||
} else {
|
||||
this.bfRightSeconds += delta
|
||||
}
|
||||
}
|
||||
}
|
||||
const leftMinutes = this.bfLeftSeconds > 0 ? Math.max(1, Math.round(this.bfLeftSeconds / 60)) : 0
|
||||
const rightMinutes = this.bfRightSeconds > 0 ? Math.max(1, Math.round(this.bfRightSeconds / 60)) : 0
|
||||
|
||||
await endFeeding({
|
||||
babyId: this.baby.babyId,
|
||||
breastSide: this.bfActiveSide,
|
||||
leftDurationMinutes: leftMinutes,
|
||||
rightDurationMinutes: rightMinutes,
|
||||
operatorName: this.operatorName
|
||||
})
|
||||
|
||||
this.bfTimerActive = false
|
||||
this.bfLeftSeconds = 0
|
||||
this.bfRightSeconds = 0
|
||||
this.clearBfTimerStorage()
|
||||
stopBfTicker()
|
||||
await this.refreshSummary()
|
||||
},
|
||||
async toggleBreastfeedTimer(breastSide: 'left' | 'right') {
|
||||
if (!this.baby) return
|
||||
if (this.summary?.activeFeeding) {
|
||||
await endFeeding({ babyId: this.baby.babyId, breastSide, operatorName: this.operatorName })
|
||||
await this.endBreastfeedTimer()
|
||||
} else {
|
||||
await startFeeding({ babyId: this.baby.babyId, breastSide, operatorName: this.operatorName })
|
||||
await this.startBreastfeedTimer(breastSide)
|
||||
}
|
||||
await this.refreshSummary()
|
||||
},
|
||||
|
||||
Reference in new issue
Block a user