feat:增加查询用户菜谱记录功能

This commit is contained in:
2025-07-01 22:42:25 +08:00
parent b6f7869fe8
commit 1037edeed4
13 changed files with 160 additions and 62 deletions

View File

@@ -28,6 +28,12 @@ export const queryRecipeByIdApi = (id: number): Promise<IRecipe> =>
method: 'get' method: 'get'
}) })
export const queryRecipeByUserApi = (id: number): Promise<IRecipe[]> =>
cloudService({
url: `/food-service/food/recipe/user/${id}`,
method: 'get'
})
export const queryRecipeApi = (query: IFoodQuery): Promise<IRecipe[]> => export const queryRecipeApi = (query: IFoodQuery): Promise<IRecipe[]> =>
cloudService({ cloudService({
url: '/food-service/food/recipe', url: '/food-service/food/recipe',
@@ -69,6 +75,25 @@ export const deleteRecordApi = (id: number): Promise<boolean> =>
method: 'DELETE' method: 'DELETE'
}) })
export const addRecipeCommentApi = (id: number, content: string): Promise<boolean> =>
cloudService({
url: `/food-service/food/recipe/${id}/comment`,
method: 'post',
params: { content }
})
export const addRecipeLikeApi = (id: number): Promise<boolean> =>
cloudService({
url: `/food-service/food/recipe/${id}/like`,
method: 'post'
})
export const deleteRecipeLikeApi = (id: number): Promise<boolean> =>
cloudService({
url: `/food-service/food/recipe/${id}/like`,
method: 'delete'
})
export const queryRecordApi = (startDate: string, endDate: string): Promise<IRecord[]> => export const queryRecordApi = (startDate: string, endDate: string): Promise<IRecord[]> =>
cloudService({ cloudService({
url: '/food-service/food/record', url: '/food-service/food/record',

View File

@@ -33,20 +33,20 @@ export const queryMomentByIdApi = (id: number): Promise<IMoment[]> =>
method: 'get' method: 'get'
}) })
export const addCommentApi = (id: number, content: string): Promise<boolean> => export const addMomentCommentApi = (id: number, content: string): Promise<boolean> =>
cloudService({ cloudService({
url: `/food-service/moment/${id}/comment`, url: `/food-service/moment/${id}/comment`,
method: 'post', method: 'post',
params: { content } params: { content }
}) })
export const addLikeApi = (id: number): Promise<boolean> => export const addMomentLikeApi = (id: number): Promise<boolean> =>
cloudService({ cloudService({
url: `/food-service/moment/${id}/like`, url: `/food-service/moment/${id}/like`,
method: 'post' method: 'post'
}) })
export const deleteLikeApi = (id: number): Promise<boolean> => export const deleteMomentLikeApi = (id: number): Promise<boolean> =>
cloudService({ cloudService({
url: `/food-service/moment/${id}/like`, url: `/food-service/moment/${id}/like`,
method: 'delete' method: 'delete'

View File

@@ -1,8 +1,8 @@
<template> <template>
<div class="life-record-card card-item"> <div class="life-record-card card-item">
<el-carousel v-if="props.foodItem?.recordList.length > 0" <el-carousel v-if="props.item?.recordList.length > 0"
arrow="always" @change="changeFoodEvent"> arrow="always" @change="changeFoodEvent">
<el-carousel-item v-for="(item, index) in getRecord(props.foodItem?.recordList)" <el-carousel-item v-for="(item, index) in getRecord(props.item?.recordList)"
:key="index"> :key="index">
<el-image :src="serviceFileRootPath + item.imageUrl" <el-image :src="serviceFileRootPath + item.imageUrl"
fit="contain" alt="暂无成果"> fit="contain" alt="暂无成果">
@@ -21,16 +21,18 @@
<div class="top-container"> <div class="top-container">
<div class="info-list"> <div class="info-list">
<div class="info-item"> <div class="info-item">
<span class="title">{{ props.foodItem.name }}</span> <span class="title">{{ props.item.name }}</span>
</div> </div>
</div> </div>
<div class="button-info"> <div class="button-info">
<el-button type="primary" :icon="Edit" circle :size="'small'" <el-button v-if="isCurrentUser"
type="primary" :icon="Edit" circle :size="'small'"
@click="editClick"/> @click="editClick"/>
<el-button type="primary" :icon="Collection" circle :size="'small'" <el-button type="primary" :icon="Collection" circle :size="'small'"
@click="viewClick()" /> @click="viewClick()" />
<el-button type="danger" :icon="DeleteFilled" circle :size="'small'" <el-button v-if="isCurrentUser"
type="danger" :icon="DeleteFilled" circle :size="'small'"
@click="deleteClick()" /> @click="deleteClick()" />
</div> </div>
</div> </div>
@@ -39,15 +41,15 @@
<div class="info-list"> <div class="info-list">
<div class="info-item category-item"> <div class="info-item category-item">
<i class="fa-solid fa-utensils"></i> <i class="fa-solid fa-utensils"></i>
{{ props.foodItem.category }} {{ props.item.category }}
</div> </div>
<div class="info-item like-item"> <div class="info-item like-item">
<i class="fa-regular fa-thumbs-up"></i> <i class="fa-regular fa-thumbs-up"></i>
{{ props.foodItem.likeCount }} {{ props.item.likeCount }}
</div> </div>
<div class="info-item comment-item"> <div class="info-item comment-item">
<i class="fa-regular fa-comment"></i> <i class="fa-regular fa-comment"></i>
{{ props.foodItem.commentCount }} {{ props.item.commentCount }}
</div> </div>
<div class="info-item start-item"> <div class="info-item start-item">
<i class="fa-solid fa-star"></i> <i class="fa-solid fa-star"></i>
@@ -55,10 +57,10 @@
</div> </div>
</div> </div>
<div v-if="props.foodItem?.recordList.length > 0" class="info-list"> <div v-if="props.item?.recordList.length > 0" class="info-list">
<div class="info-item"> <div class="info-item">
<i class="fa-regular fa-calendar-check"></i> <i class="fa-regular fa-calendar-check"></i>
{{ formatDate(props.foodItem.recordList[foodCardInfo.currentFoodIndex].date) }} {{ formatDate(props.item.recordList[foodCardInfo.currentFoodIndex].date) }}
</div> </div>
</div> </div>
@@ -73,15 +75,16 @@
<script setup lang="ts"> <script setup lang="ts">
import { Edit, Collection, DeleteFilled } from '@element-plus/icons-vue' import { Edit, Collection, DeleteFilled } from '@element-plus/icons-vue'
import { useFoodStore } from '@/store/food' import { useFoodStore } from '@/store/food'
import { defineProps, onMounted, reactive } from 'vue' import { computed, defineProps, onMounted, reactive } from 'vue'
import type { PropType } from 'vue' import type { PropType } from 'vue'
import type { IRecipe, IRecord } from '@/types/food.ts' import type { IRecipe, IRecord } from '@/types/food.ts'
import { useRouter } from 'vue-router' import { useRouter, useRoute } from 'vue-router'
import { formatDate } from 'vue3-common/utils/dateUtil' import { formatDate } from 'vue3-common/utils/dateUtil'
import { commonElMessageBox } from 'vue3-common/utils/elUtil' import { commonElMessageBox } from 'vue3-common/utils/elUtil'
import { serviceFileRootPath } from '@/utils' import { serviceFileRootPath } from '@/utils'
const router = useRouter() const router = useRouter()
const route = useRoute()
const foodStore = useFoodStore() const foodStore = useFoodStore()
const foodCardInfo = reactive({ const foodCardInfo = reactive({
@@ -90,34 +93,36 @@ const foodCardInfo = reactive({
}) })
const props = defineProps({ const props = defineProps({
foodItem: { item: {
required: true, required: true,
type: Object as PropType<IRecipe> type: Object as PropType<IRecipe>
} }
}) })
onMounted(() => { onMounted(() => {
foodCardInfo.recommendRate = props.foodItem?.recommendRate as number foodCardInfo.recommendRate = props.item?.recommendRate as number
}) })
const isCurrentUser = computed(() => route.path.includes('record'))
const getRecord = (recordList: IRecord[]) => { const getRecord = (recordList: IRecord[]) => {
return recordList.filter((item) => item.imageUrl) return recordList.filter((item) => item.imageUrl)
} }
const editClick = async () => { const editClick = async () => {
foodStore.recipeApiType = 'UPDATE' foodStore.recipeApiType = 'UPDATE'
await foodStore.queryRecipe(props.foodItem.id as number) await foodStore.queryRecipe(props.item.id as number)
await router.push('/record/recipeForm') await router.push('/record/recipeForm')
} }
const viewClick = async () => { const viewClick = async () => {
await router.push({ name: 'RecordDetailId', params: { id: props.foodItem?.id } }) await router.push({ name: 'RecordDetailId', params: { id: props.item?.id } })
} }
const deleteClick = () => { const deleteClick = () => {
foodStore.recipeApiType = 'DELETE' foodStore.recipeApiType = 'DELETE'
commonElMessageBox(`请确认是否删除该菜谱: ${props.foodItem?.name}?`).then(() => { commonElMessageBox(`请确认是否删除该菜谱: ${props.item?.name}?`).then(() => {
foodStore.handleRecipeApi(props.foodItem as IRecipe) foodStore.handleRecipeApi(props.item as IRecipe)
}) })
} }

View File

@@ -7,21 +7,14 @@
@change="queryFoodSegmented"/> @change="queryFoodSegmented"/>
</div> </div>
<div class="food-list"> <recipe-list />
<el-empty v-if="foodStore.recipeList.length === 0" description="暂无记录"/>
<div v-else class="list">
<food-card v-for="(item, index) in foodStore.recipeList"
:key="index" :food-item="item"/>
</div>
</div>
<arrive-bottom v-if="foodStore.isScrollEnd"/> <arrive-bottom v-if="foodStore.isScrollEnd"/>
</div> </div>
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
import FoodCard from '@/components/Food/FoodCard.vue' import RecipeList from '@/components/Food/RecipeList.vue'
import ArriveBottom from '@/components/ArriveButton.vue' import ArriveBottom from '@/components/ArriveButton.vue'
import { useFoodStore } from '@/store/food.ts' import { useFoodStore } from '@/store/food.ts'
import { onBeforeUnmount, onMounted, ref, watch } from 'vue' import { onBeforeUnmount, onMounted, ref, watch } from 'vue'
@@ -88,13 +81,5 @@ const handleScroll = () => {
.query-item { .query-item {
padding: 5px 10px; padding: 5px 10px;
} }
.food-list {
.list {
display: flex;
flex-direction: column;
gap: 10px;
}
}
} }
</style> </style>

View File

@@ -0,0 +1,28 @@
<template>
<div class="food-list">
<el-empty v-if="foodStore.recipeList.length === 0" description="暂无记录"/>
<div v-else class="list">
<food-card v-for="(item, index) in foodStore.recipeList"
:key="index" :item="item"/>
</div>
</div>
</template>
<script setup lang="ts">
import FoodCard from '@/components/Food/FoodCard.vue'
import { useFoodStore } from '@/store/food'
const foodStore = useFoodStore()
</script>
<style lang="scss">
.food-list {
.list {
display: flex;
flex-direction: column;
gap: 10px;
}
}
</style>

View File

@@ -124,9 +124,9 @@ const editClick = () => {
const handleLikeClick = () => { const handleLikeClick = () => {
if (isLike.value) { if (isLike.value) {
foodStore.deleteLike(props.item?.id as number) foodStore.deleteMomentLike(props.item?.id as number)
} else { } else {
foodStore.addLike(props.item?.id as number) foodStore.addMomentLike(props.item?.id as number)
} }
} }
@@ -135,7 +135,7 @@ const onSelectEmoji = (emoji) => {
} }
const sendCommentClick = async () => { const sendCommentClick = async () => {
await foodStore.addComment(props.item?.id as number, momentInfo.comment) await foodStore.addMomentComment(props.item?.id as number, momentInfo.comment)
momentInfo.comment = '' momentInfo.comment = ''
momentInfo.isShowComment = false momentInfo.isShowComment = false

View File

@@ -1,7 +1,7 @@
<template> <template>
<div class="food-moment" <div class="food-moment"
v-loading="foodStore.isLoading" element-loading-text="加载中"> v-loading="foodStore.isLoading" element-loading-text="加载中">
<el-empty v-if="foodStore.momentList.length === 0" description="暂无朋友圈记录"/> <el-empty v-if="foodStore.momentList.length === 0" description="暂无记录"/>
<div v-else class="moment-list"> <div v-else class="moment-list">
<moment-card v-for="(item, index) in foodStore.momentList" <moment-card v-for="(item, index) in foodStore.momentList"

View File

@@ -18,7 +18,8 @@
<span class="title">📊排行榜</span> <span class="title">📊排行榜</span>
</div> </div>
<div class="chart rank-chart"> <div class="chart rank-chart">
<food-rank /> <food-rank v-if="foodStore.rankStats.length > 0"/>
<el-empty v-else description="暂无数据"/>
</div> </div>
</div> </div>
</div> </div>

View File

@@ -16,16 +16,21 @@ import {
updateRecordApi, updateRecordApi,
queryCategoryApi, queryCategoryApi,
queryRecordStatsApi, queryRecordStatsApi,
queryCategoryStatsApi, queryRankStatsApi, queryRecipeApi queryCategoryStatsApi,
queryRankStatsApi,
queryRecipeApi,
queryRecipeByUserApi,
deleteRecipeLikeApi,
addRecipeLikeApi,
addRecipeCommentApi
} from '@/apis/food.ts' } from '@/apis/food.ts'
import type { IDialog, IHandleApi, IStatsChart } from 'vue3-common/types' import type { IDialog, IHandleApi, IStatsChart } from 'vue3-common/types'
import { ElMessage } from 'element-plus' import { ElMessage } from 'element-plus'
import { getCurrentMonth, getCurrentYear, getStartEndDate } from 'vue3-common/utils/dateUtil' import { getCurrentMonth, getCurrentYear, getStartEndDate } from 'vue3-common/utils/dateUtil'
import { import {
addCommentApi, addMomentLikeApi,
addLikeApi, addMomentApi, addMomentCommentApi,
addMomentApi, deleteMomentLikeApi,
deleteLikeApi,
deleteMomentApi, queryMomentByIdApi, deleteMomentApi, queryMomentByIdApi,
queryMomentListApi, queryMomentListApi,
updateMomentApi updateMomentApi
@@ -42,7 +47,11 @@ export const useFoodStore = defineStore('food', {
remark: '', remark: '',
materialList: [], materialList: [],
stepList: [], stepList: [],
recordList: [] recordList: [],
likeCount: 0,
likeList: [],
commentCount: 0,
commentList: []
} as IRecipe, } as IRecipe,
recordList: [] as IRecord[], recordList: [] as IRecord[],
currentRecord: { currentRecord: {
@@ -119,6 +128,11 @@ export const useFoodStore = defineStore('food', {
this.isRefresh = !this.isRefresh this.isRefresh = !this.isRefresh
this.isLoading = false this.isLoading = false
}, },
async queryRecipeByUser(id: number) {
this.isLoading = true
this.recipeList = await queryRecipeByUserApi(id)
this.isLoading = false
},
async queryRecordList() { async queryRecordList() {
const { year, month } = this.queryRecord const { year, month } = this.queryRecord
const date = this.queryDateType === 'year' ? year : month const date = this.queryDateType === 'year' ? year : month
@@ -211,20 +225,36 @@ export const useFoodStore = defineStore('food', {
} }
await this.queryMomentList() await this.queryMomentList()
}, },
async addComment(id: number, content: string) { async addMomentComment(id: number, content: string) {
await addCommentApi(id, content) await addMomentCommentApi(id, content)
await this.queryMomentList() await this.queryMomentList()
}, },
async addLike(id: number) { async addMomentLike(id: number) {
await addLikeApi(id) await addMomentLikeApi(id)
await this.queryMomentList() await this.queryMomentList()
}, },
async deleteLike(id: number) { async deleteMomentLike(id: number) {
await deleteLikeApi(id) await deleteMomentLikeApi(id)
await this.queryMomentList() 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)
},
getEmptyRecipe(): IRecipe { getEmptyRecipe(): IRecipe {
return { return {
commentCount: 0,
commentList: [],
likeCount: 0,
likeList: [],
category: '', category: '',
materialList: [], materialList: [],
name: '', name: '',

View File

@@ -204,6 +204,9 @@
.button-container { .button-container {
align-self: center; align-self: center;
display: flex;
gap: 20px;
} }
.food-comment-list { .food-comment-list {

View File

@@ -71,7 +71,7 @@ export interface IRecipe {
* 成果 * 成果
*/ */
recordList: IRecord[]; recordList: IRecord[];
likeList: string[]; likeList: number[];
likeCount: number; likeCount: number;
commentList: IComment[]; commentList: IComment[];
commentCount: number; commentCount: number;

View File

@@ -9,13 +9,14 @@
<moment-list /> <moment-list />
</el-tab-pane> </el-tab-pane>
<el-tab-pane label="菜谱记录" name="recipe"> <el-tab-pane label="菜谱记录" name="recipe">
<recipe-list />
</el-tab-pane> </el-tab-pane>
</el-tabs> </el-tabs>
</div> </div>
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
import RecipeList from '@/components/Food/RecipeList.vue'
import ProfileCard from '@/components/Profile/ProfileCard.vue' import ProfileCard from '@/components/Profile/ProfileCard.vue'
import { useFoodStore } from '@/store/food.ts' import { useFoodStore } from '@/store/food.ts'
import { onMounted, ref } from 'vue' import { onMounted, ref } from 'vue'
@@ -35,6 +36,9 @@ const handleClick = async (tab: TabsPaneContext) => {
case 'moment': case 'moment':
await foodStore.queryMoment(foodStore.currentMomentUserId) await foodStore.queryMoment(foodStore.currentMomentUserId)
break break
case 'recipe':
await foodStore.queryRecipeByUser(foodStore.currentMomentUserId)
break
default: default:
break break
} }

View File

@@ -14,7 +14,12 @@
</div> </div>
<div class="button-container"> <div class="button-container">
<el-button circle><i class="fa-regular fa-thumbs-up"></i></el-button> <el-badge :value="foodStore.currentRecipe.likeList.length" :max="9">
<el-button :type="isLike ? 'danger' : 'default'"
circle @click="handleLikeClick()">
<i class="fa-regular fa-thumbs-up"></i>
</el-button>
</el-badge>
<el-button circle><i class="fa-regular fa-star"></i></el-button> <el-button circle><i class="fa-regular fa-star"></i></el-button>
<!-- <el-button circle><i class="fa-regular fa-comment"></i></el-button>--> <!-- <el-button circle><i class="fa-regular fa-comment"></i></el-button>-->
</div> </div>
@@ -23,23 +28,35 @@
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { useFoodStore } from '@/store/food.ts'
import { useAppStore } from '@/store/app.ts'
import { useRoute } from 'vue-router'
import FoodBasicData from '@/components/Food/FoodBasicData.vue' import FoodBasicData from '@/components/Food/FoodBasicData.vue'
import FoodMaterialData from '@/components/Food/FoodMaterialData.vue' import FoodMaterialData from '@/components/Food/FoodMaterialData.vue'
import FoodStepData from '@/components/Food/FoodStepData.vue' import FoodStepData from '@/components/Food/FoodStepData.vue'
import FoodOthersData from '@/components/Food/FoodOthersData.vue' import FoodOthersData from '@/components/Food/FoodOthersData.vue'
import { onMounted } from 'vue' import { computed, onMounted } from 'vue'
import { useUserStore } from '@/store/user.ts'
import { useFoodStore } from '@/store/food.ts'
import { useAppStore } from '@/store/app.ts'
import { useRoute } from 'vue-router'
const foodStore = useFoodStore() const foodStore = useFoodStore()
const appStore = useAppStore() const appStore = useAppStore()
const route = useRoute() const route = useRoute()
const userStore = useUserStore()
onMounted(() => { onMounted(() => {
appStore.isShowMobileBack = true appStore.isShowMobileBack = true
foodStore.queryRecipe(route.params.id as number) foodStore.queryRecipe(route.params.id as number)
}) })
const isLike = computed(() => foodStore.currentRecipe.likeList.includes(userStore.getUserId()))
const handleLikeClick = () => {
if (isLike.value) {
foodStore.deleteRecipeLike(foodStore.currentRecipe.id as number)
} else {
foodStore.addRecipeLike(foodStore.currentRecipe.id as number)
}
}
</script> </script>
<style lang="scss"> <style lang="scss">