优化代码结构

This commit is contained in:
gs committed 2026-09-26 02:11:19 +08:00
1 parent 8c7a9040d2
commit cbcd6b4c7f
97 files changed
+4350 -1183

No files matched your search

+113 -5
View File
@@ -1,18 +1,60 @@
import { defineStore } from 'pinia'
import {
createBaby,
createFeeding,
createManualRecord,
createMember,
createPee,
createPoop,
deleteMember,
endFeeding,
endSleep,
getFamilyContext,
getDailySummary,
getFamilyContext,
setDefaultBaby as apiSetDefaultBaby,
startFeeding,
updateBaby,
startSleep,
updateCurrentBaby
updateBaby,
updateCurrentBaby,
updateFamily,
updateMember,
updateRecord
} from '../api/client'
import type { BabyProfile, DailySummary, FamilyContext, FamilyMember, FeedingMethod } from '../types/domain'
import type {
ActionPreferences,
BabyProfile,
CreateBabyPayload,
CreateManualRecordPayload,
CreateMemberPayload,
DailySummary,
FamilyContext,
FamilyMember,
FeedingMethod,
UpdateCareRecordPayload,
UpdateFamilyPayload,
UpdateMemberPayload
} from '../types/domain'
const defaultPreferences: ActionPreferences = {
defaultFeedingMethod: 'breastfeed',
defaultBottleAmount: 60,
defaultBreastSide: 'left',
defaultSleepMode: 'timer',
quickFeedbackToast: true,
autoBurpReminder: true
}
function loadInitialPreferences(): ActionPreferences {
try {
const raw = localStorage.getItem('actionPreferences')
if (raw) {
return { ...defaultPreferences, ...JSON.parse(raw) }
}
} catch {
// fallback
}
return defaultPreferences
}
export const useCareStore = defineStore('care', {
state: () => ({
@@ -23,6 +65,7 @@ export const useCareStore = defineStore('care', {
baby: null as BabyProfile | null,
summary: null as DailySummary | null,
operatorName: localStorage.getItem('operatorName') || '宝妈',
preferences: loadInitialPreferences() as ActionPreferences,
loading: false,
error: ''
}),
@@ -63,11 +106,62 @@ export const useCareStore = defineStore('care', {
this.operatorName = name || '宝妈'
localStorage.setItem('operatorName', this.operatorName)
},
savePreferences(prefs: Partial<ActionPreferences>) {
this.preferences = { ...this.preferences, ...prefs }
localStorage.setItem('actionPreferences', JSON.stringify(this.preferences))
},
async saveBaby(payload: Pick<BabyProfile, 'name' | 'gender' | 'birthDate' | 'deliveryType'>) {
this.baby = this.baby ? await updateBaby(this.baby.babyId, payload) : await updateCurrentBaby(payload)
this.babies = this.babies.map(baby => baby.babyId === this.baby?.babyId ? this.baby : baby)
this.babies = this.babies.map(baby => baby.babyId === this.baby?.babyId ? this.baby! : baby)
await this.refreshSummary()
},
async addBaby(payload: CreateBabyPayload) {
const newBaby = await createBaby(payload)
this.babies.push(newBaby)
if (payload.isDefault || this.babies.length === 1) {
await this.selectBaby(newBaby.babyId)
}
return newBaby
},
async setDefaultBaby(babyId: string) {
const updated = await apiSetDefaultBaby(babyId)
this.babies = this.babies.map(b => ({
...b,
isDefault: b.babyId === babyId ? 1 : 0
}))
await this.selectBaby(babyId)
return updated
},
async saveFamily(payload: UpdateFamilyPayload) {
if (!this.context?.family.familyId) return
const updated = await updateFamily(this.context.family.familyId, payload)
this.context.family = updated
},
async addMember(payload: CreateMemberPayload) {
if (!this.context?.family.familyId) return
const newMember = await createMember(this.context.family.familyId, payload)
if (payload.defaultOperator) {
this.members.forEach(m => { m.defaultOperator = false })
this.setOperatorName(newMember.roleName)
}
this.members.push(newMember)
return newMember
},
async editMember(memberId: string, payload: UpdateMemberPayload) {
if (!this.context?.family.familyId) return
const updated = await updateMember(this.context.family.familyId, memberId, payload)
if (payload.defaultOperator) {
this.members.forEach(m => { m.defaultOperator = false })
this.setOperatorName(updated.roleName)
}
this.members = this.members.map(m => m.memberId === memberId ? updated : m)
return updated
},
async removeMember(memberId: string) {
if (!this.context?.family.familyId) return
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') {
if (!this.baby) return
await createFeeding({
@@ -106,6 +200,20 @@ export const useCareStore = defineStore('care', {
if (!this.baby) return
await createPoop({ babyId: this.baby.babyId, operatorName: this.operatorName })
await this.refreshSummary()
},
async manualLog(payload: Omit<CreateManualRecordPayload, 'babyId'>) {
if (!this.baby) return
const res = await createManualRecord({
...payload,
babyId: this.baby.babyId
})
await this.refreshSummary()
return res
},
async editRecord(recordId: string, payload: UpdateCareRecordPayload) {
const res = await updateRecord(recordId, payload)
await this.refreshSummary()
return res
}
}
})