Files
food-hub-ui/src/store/food.ts
2025-07-02 19:55:18 +08:00

306 lines
8.7 KiB
TypeScript

import { defineStore } from 'pinia'
import type {
IRecipe, IMaterial, IStep,
IStats, IRecord, IMoment, IFoodSegment
} from '@/types/food'
import {
addRecipeApi, addRecordApi, deleteRecipeApi, deleteRecordApi,
queryFoodNameListApi, queryRecipeByIdApi, queryRecordApi, queryStatsApi,
updateRecipeApi, updateRecordApi, queryCategoryApi, queryRecordStatsApi,
queryCategoryStatsApi, queryRankStatsApi,
queryRecipeApi, queryRecipeByUserApi, deleteRecipeLikeApi,
addRecipeLikeApi, addRecipeCommentApi, addRecipeFavouriteApi, deleteRecipeFavouriteApi, queryRecipeUserFavouriteApi
} from '@/apis/food.ts'
import type { IDialog, IHandleApi, IStatsChart } from 'vue3-common/types'
import { ElMessage } from 'element-plus'
import { getCurrentMonth, getCurrentYear, getStartEndDate } from 'vue3-common/utils/dateUtil'
import {
addMomentLikeApi,
addMomentApi, addMomentCommentApi,
deleteMomentLikeApi,
deleteMomentApi, queryMomentByIdApi,
queryMomentListApi,
updateMomentApi
} from '@/apis/moment'
export const useFoodStore = defineStore('food', {
state: () => ({
recipeList: [] as IRecipe[],
currentRecipe: {
id: 0,
name: '',
category: '',
recommendRate: 0,
remark: '',
isShare: false,
username: '',
userId: 0,
avatar: '',
materialList: [],
stepList: [],
recordList: [],
likeCount: 0,
likeList: [],
favouriteCount: 0,
favouriteList: [],
commentCount: 0,
commentList: []
} as IRecipe,
recordList: [] as IRecord[],
currentRecord: {
id: 0,
name: '',
category: '',
date: '',
person: '',
imageUrl: ''
} as IRecord,
recordSegment: 'recipe' as IFoodSegment,
momentList: [] as IMoment[],
foodNameList: [] as string[],
queryRecipeSegment: {
category: ''
},
queryRecord: {
year: getCurrentYear(),
month: getCurrentMonth()
},
queryDateType: 'month' as 'month' | 'year',
queryDateList: [] as string[],
recipeApiType: 'ADD' as IHandleApi,
materialApiType: 'ADD' as IHandleApi,
materialDialog: {
visible: false,
title: '',
data: {}
} as IDialog<IMaterial>,
editMaterialIndex: 0,
stepApiType: 'ADD' as IHandleApi,
stepDialog: {
visible: false,
title: '',
data: {}
} as IDialog<IStep>,
editStepIndex: 0,
recordApiType: 'ADD' as IHandleApi,
editRecordIndex: 0,
currentMomentUserId: 0,
currentMoment: {
content: '',
imageList: []
} as IMoment,
momentApiType: 'ADD' as IHandleApi,
imagePath: 'food-hub',
rateColorList: ['#E2DEDC', '#F7BA2A', '#009FA8'],
pageInfo: {
currentPage: 1,
pageSize: 2,
totalSize: 1
},
isScrollEnd: false,
categoryList: [] as string[],
stats: {
recipeCount: 0,
categoryCount: 0,
workCount: 0
} as IStats,
recordStats: [] as IStatsChart[],
categoryStats: [] as IStatsChart[],
rankStats: [] as IStatsChart[],
isRefresh: false,
isLoading: false
}),
actions: {
async queryRecipeList() {
// const { currentPage, pageSize } = this.pageInfo
// const result = await queryFoodRecipeByPageApi(currentPage, pageSize, this.queryRecipe)
// this.foodRecipeList.push(...result.records)
// this.pageInfo.totalSize = result.total
this.isLoading = true
this.recipeList = await queryRecipeApi(this.queryRecipeSegment)
this.isRefresh = !this.isRefresh
this.isLoading = false
},
async queryRecipeByUser(id: number) {
this.isLoading = true
this.recipeList = await queryRecipeByUserApi(id)
this.isLoading = false
},
async queryRecipeUserFavourite() {
this.isLoading = true
this.recipeList = await queryRecipeUserFavouriteApi()
this.isLoading = false
},
async queryRecordList() {
const { year, month } = this.queryRecord
const date = this.queryDateType === 'year' ? year : month
this.queryDateList = getStartEndDate(date, this.queryDateType)
this.isLoading = true
this.recordList = await queryRecordApi(this.queryDateList[0], this.queryDateList[1])
this.isRefresh = !this.isRefresh
this.isLoading = false
},
async queryMomentList() {
this.isLoading = true
this.momentList = await queryMomentListApi()
this.isLoading = false
},
async queryFoodNameList() {
this.foodNameList = await queryFoodNameListApi()
},
async queryRecipe(id: number) {
this.currentRecipe = await queryRecipeByIdApi(id)
},
async queryMoment(id: number) {
this.isLoading = true
this.momentList = await queryMomentByIdApi(id)
this.isLoading = false
},
async queryStats() {
this.stats = await queryStatsApi()
this.recordStats = await queryRecordStatsApi()
this.categoryStats = await queryCategoryStatsApi()
this.rankStats = await queryRankStatsApi()
this.isRefresh = !this.isRefresh
},
async queryCategory() {
this.categoryList = await queryCategoryApi()
},
async handleRecipeApi(recipe: IRecipe) {
switch (this.recipeApiType) {
case 'ADD':
await addRecipeApi(recipe)
ElMessage.success('新增菜谱成功')
break
case 'UPDATE':
await updateRecipeApi(recipe.id as number, recipe)
ElMessage.success('更新菜谱成功')
break
case 'DELETE':
await deleteRecipeApi(recipe.id as number)
ElMessage.success('删除菜谱成功')
break
default:
break
}
await this.queryRecipeList()
},
async handleRecordApi(record: IRecord) {
switch (this.recordApiType) {
case 'ADD':
await addRecordApi(record)
ElMessage.success('新增成果成功')
break
case 'UPDATE':
await updateRecordApi(record.id as number, record)
ElMessage.success('更新成果成功')
break
case 'DELETE':
await deleteRecordApi(record.id as number)
ElMessage.success('删除成果成功')
break
default:
break
}
await this.queryRecordList()
},
async handleMomentApi(moment: IMoment) {
switch (this.momentApiType) {
case 'ADD':
await addMomentApi(moment)
ElMessage.success('发布朋友圈成功')
break
case 'UPDATE':
await updateMomentApi(moment.id as number, moment)
ElMessage.success('更新朋友圈成功')
break
case 'DELETE':
await deleteMomentApi(moment.id as number)
ElMessage.success('删除朋友圈成功')
break
default:
break
}
await this.queryMomentList()
},
async addMomentComment(id: number, content: string) {
await addMomentCommentApi(id, content)
await this.queryMomentList()
},
async addMomentLike(id: number) {
await addMomentLikeApi(id)
await this.queryMomentList()
},
async deleteMomentLike(id: number) {
await deleteMomentLikeApi(id)
await this.queryMomentList()
},
async addRecipeComment(id: number, content: string) {
await addRecipeCommentApi(id, content)
await this.queryRecipe(this.currentRecipe.id as number)
},
async addRecipeLike(id: number) {
await addRecipeLikeApi(id)
await this.queryRecipe(this.currentRecipe.id as number)
},
async deleteRecipeLike(id: number) {
await deleteRecipeLikeApi(id)
await this.queryRecipe(this.currentRecipe.id as number)
},
async addRecipeFavourite(id: number) {
await addRecipeFavouriteApi(id)
await this.queryRecipe(this.currentRecipe.id as number)
},
async deleteRecipeFavourite(id: number) {
await deleteRecipeFavouriteApi(id)
await this.queryRecipe(this.currentRecipe.id as number)
},
getEmptyRecipe(): IRecipe {
return {
commentCount: 0,
commentList: [],
likeCount: 0,
likeList: [],
category: '',
materialList: [],
name: '',
recommendRate: 0,
isShare: false,
remark: '',
stepList: [],
recordList: []
}
},
getEmptyMaterial(): IMaterial {
return {
amount: '',
name: '',
type: ''
}
},
getEmptyStep(): IStep {
return {
content: '',
imageUrl: '',
sort: 0
}
},
getEmptyRecord(): IRecord {
return {
person: '',
category: '',
date: '',
id: 0,
imageUrl: '',
name: ''
}
},
getEmptyMoment(): IMoment {
return {
content: '',
imageList: []
}
}
}
})