feat:增加运动健康模块

This commit is contained in:
2025-11-04 20:05:31 +08:00
parent ed0c8e6664
commit 5f3a7ca5b4
20 changed files with 513 additions and 139 deletions

130
src/store/health.ts Normal file
View File

@@ -0,0 +1,130 @@
import { defineStore } from 'pinia'
import type { IHandleApi } from 'vue3-common/types'
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[],
queryInfo: {
startDate: '',
endDate: ''
} as IExerciseQuery,
weightApiType: 'ADD' as IHandleApi,
exerciseApiType: 'ADD' as IHandleApi,
currentExercise: {
project: '',
duration: 0,
date: ''
} as IExercise,
currentHealth: {
project: '',
duration: 0,
value: 0,
date: ''
} as IHealth,
currentWeight: {
value: '',
date: ''
} as IWeight,
selectDate: '',
isScrollEnd: false,
isRefresh: false
}),
actions: {
async queryExerciseRecordList() {
this.exerciseRecordList = await queryExerciseApi(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
}
await this.refreshInfo()
},
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()
if (exercise) {
this.currentHealth.project = exercise.project
this.currentHealth.duration = exercise.duration
}
if (weight) {
this.currentHealth.value = weight.value
}
this.currentHealth.date = date
},
async refreshInfo() {
await this.queryExerciseRecordList()
await this.queryWeightRecordList()
this.isRefresh = !this.isRefresh
},
getEmptyExercise(): IExercise {
return {
project: '',
duration: 0,
date: ''
}
},
getEmptyWeight(): IWeight {
return {
value: 0,
date: ''
}
},
getEmptyHealth(): IHealth {
return {
date: '',
duration: 0,
project: '',
value: 0
}
}
}
})