feat:更新运动健康模块

This commit is contained in:
2025-11-04 22:35:53 +08:00
parent 5f3a7ca5b4
commit ee20c19b72
16 changed files with 182 additions and 394 deletions

View File

@@ -1,129 +1,56 @@
import { defineStore } from 'pinia'
import type { IHandleApi } from 'vue3-common/types'
import { queryHealthApi, updateHealthApi } from '@/apis/health.ts'
import { IHealth, IHealthQuery } from '@/types/health'
import { ElMessage } from 'element-plus'
import {
addExerciseApi,
deleteExerciseApi,
queryExerciseApi,
updateExerciseApi
} from '@/apis/exercise'
import { addWeightApi, deleteWeightApi, queryWeightApi, updateWeightApi } from '@/apis/weight'
import { IExercise, IExerciseQuery, IHealth, IWeight } from '@/types/health'
export const useHealthStore = defineStore('health', {
state: () => ({
weightRecordList: [] as IWeight[],
exerciseRecordList: [] as IExercise[],
healthRecordList: [] as IHealth[],
queryInfo: {
startDate: '',
endDate: ''
} as IExerciseQuery,
weightApiType: 'ADD' as IHandleApi,
exerciseApiType: 'ADD' as IHandleApi,
currentExercise: {
project: '',
duration: 0,
date: ''
} as IExercise,
} as IHealthQuery,
healthApiType: 'ADD' as IHandleApi,
currentHealth: {
project: '',
duration: 0,
value: 0,
sportProject: '',
sportDuration: 0,
weight: 0,
date: ''
} as IHealth,
currentWeight: {
value: '',
date: ''
} as IWeight,
selectDate: '',
isScrollEnd: false,
isRefresh: false
}),
actions: {
async queryExerciseRecordList() {
this.exerciseRecordList = await queryExerciseApi(this.queryInfo)
async queryHealthRecordList() {
this.healthRecordList = await queryHealthApi(this.queryInfo)
},
async queryWeightRecordList() {
this.weightRecordList = await queryWeightApi(this.queryInfo)
},
async handleExerciseApi(exercise: IExercise) {
switch (this.exerciseApiType) {
case 'ADD':
await addExerciseApi(exercise)
ElMessage.success('新增锻炼记录成功')
break
case 'UPDATE':
await updateExerciseApi(exercise.id as number, exercise)
ElMessage.success('更新锻炼记录成功')
break
case 'DELETE':
await deleteExerciseApi(exercise.id as number)
ElMessage.success('删除锻炼记录成功')
break
default:
break
}
await this.refreshInfo()
},
async handleWeightApi(weight: IWeight) {
switch (this.weightApiType) {
case 'ADD':
await addWeightApi(weight)
ElMessage.success('新增体重记录成功')
break
case 'UPDATE':
await updateWeightApi(weight.id as number, weight)
ElMessage.success('更新体重记录成功')
break
case 'DELETE':
await deleteWeightApi(weight.id as number)
ElMessage.success('删除体重记录成功')
break
default:
break
}
async updateHealthDay() {
await updateHealthApi(this.currentHealth.id as number, this.currentHealth)
await this.refreshInfo()
ElMessage.success('更新记录成功')
},
getHealthDay(date: string): IHealth {
const exercise = this.exerciseRecordList.find((item) => item.date === date)
const weight = this.weightRecordList.find((item) => item.date === date)
this.currentHealth = this.getEmptyHealth()
const result = this.healthRecordList.find((item) => item.date === date)
if (exercise) {
this.currentHealth.project = exercise.project
this.currentHealth.duration = exercise.duration
if (result) {
return result
} else {
return this.getEmptyHealth(date)
}
if (weight) {
this.currentHealth.value = weight.value
}
this.currentHealth.date = date
},
async refreshInfo() {
await this.queryExerciseRecordList()
await this.queryWeightRecordList()
await this.queryHealthRecordList()
this.isRefresh = !this.isRefresh
},
getEmptyExercise(): IExercise {
getEmptyHealth(date: string): IHealth {
return {
project: '',
duration: 0,
date: ''
}
},
getEmptyWeight(): IWeight {
return {
value: 0,
date: ''
}
},
getEmptyHealth(): IHealth {
return {
date: '',
duration: 0,
project: '',
value: 0
id: 0,
date,
sportDuration: 0,
sportProject: '',
weight: 0
}
}
}