65 lines
1.8 KiB
TypeScript
65 lines
1.8 KiB
TypeScript
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 { getCurrentMonth, formatDate, getStartEndDate } from 'vue3-common/utils/dateUtil'
|
|
|
|
export const useHealthStore = defineStore('health', {
|
|
state: () => ({
|
|
healthRecordList: [] as IHealth[],
|
|
queryInfo: {
|
|
startDate: '',
|
|
endDate: ''
|
|
} as IHealthQuery,
|
|
queryMonth: getCurrentMonth(),
|
|
healthApiType: 'ADD' as IHandleApi,
|
|
currentHealth: {
|
|
sportProject: '',
|
|
sportDuration: 0,
|
|
weight: 0,
|
|
date: ''
|
|
} as IHealth,
|
|
selectDate: '',
|
|
isScrollEnd: false,
|
|
isRefresh: false,
|
|
isLoading: false
|
|
}),
|
|
actions: {
|
|
async queryHealthRecordList() {
|
|
this.healthRecordList = await queryHealthApi(this.queryInfo)
|
|
},
|
|
async updateHealthDay() {
|
|
await updateHealthApi(this.currentHealth.id as number, this.currentHealth)
|
|
await this.refreshInfo()
|
|
ElMessage.success('更新记录成功')
|
|
},
|
|
getHealthDay(date: string): IHealth {
|
|
const result = this.healthRecordList.find((item) => item.date === date)
|
|
|
|
if (result) {
|
|
return result
|
|
} else {
|
|
return this.getEmptyHealth(date)
|
|
}
|
|
},
|
|
async refreshInfo() {
|
|
this.isLoading = true
|
|
const result = getStartEndDate(formatDate(this.queryMonth), 'month')
|
|
this.queryInfo = { startDate: result[0], endDate: result[1] }
|
|
await this.queryHealthRecordList()
|
|
this.isRefresh = !this.isRefresh
|
|
this.isLoading = false
|
|
},
|
|
getEmptyHealth(date: string): IHealth {
|
|
return {
|
|
id: 0,
|
|
date,
|
|
sportDuration: 0,
|
|
sportProject: '',
|
|
weight: 0
|
|
}
|
|
}
|
|
}
|
|
})
|