From 27c244f3cdc4d220e3997cac32e4db14ae5be346 Mon Sep 17 00:00:00 2001 From: zzc <1761997216@qq.com> Date: Mon, 8 Jun 2026 06:47:04 +0800 Subject: [PATCH] fix:rating --- api/topicItem.js | 10 ++++++++ pages/rating/index.vue | 57 +++++++++++++++++++++++++++--------------- 2 files changed, 47 insertions(+), 20 deletions(-) diff --git a/api/topicItem.js b/api/topicItem.js index 0aec464..04ea562 100644 --- a/api/topicItem.js +++ b/api/topicItem.js @@ -42,4 +42,14 @@ export const fetchTopicRatingItems = async (itemId) => { url: `/api/rating/topic/item/detail/${itemId}`, method: "GET", }); +}; + +// 添加评分项 +// data = { topicId: 'xxx', name: 'xxx', avatarUrl: 'xxx' } +export const topicItemAdd = async (data) => { + return request({ + url: "/api/rating/topic/item/add", + method: "POST", + data, + }); }; \ No newline at end of file diff --git a/pages/rating/index.vue b/pages/rating/index.vue index 4276f2d..f2d6dfe 100644 --- a/pages/rating/index.vue +++ b/pages/rating/index.vue @@ -161,7 +161,8 @@ import { fetchTopicDetail, fetchTopicRatingItems } from "@/api/topic"; import { FILE_BASE_URL } from "@/utils/constants"; import RatingCard from "@/components/RatingCard/RatingCard.vue"; import SuccessPopup from "@/components/SuccessPopup/SuccessPopup.vue"; -import { topicItemScore } from "@/api/topicItem"; +import { topicItemScore, topicItemAdd } from "@/api/topicItem"; +import { uploadImage } from "@/utils/common"; // 状态栏高度处理 const statusBarHeight = ref(getStatusBarHeight() || 44); @@ -216,30 +217,46 @@ const chooseAvatar = () => { }); }; -const confirmAddItem = () => { +const confirmAddItem = async () => { if (!newItem.value.name.trim()) { uni.showToast({ title: '请输入名称', icon: 'none' }); return; } + if (!newItem.value.avatar) { + uni.showToast({ title: '请上传头像', icon: 'none' }); + return; + } - // 模拟添加 - const newRatingItem = { - id: Date.now(), - rank: ratingItems.value.length + 1, - name: newItem.value.name, - avatar: newItem.value.avatar || `https://api.dicebear.com/7.x/avataaars/svg?seed=${Date.now()}`, - score: '0.0', - quote: '刚刚加入讨论,快来发表你的看法吧。', - userAction: '' - }; - - ratingItems.value.push(newRatingItem); - - // 重置并关闭 - newItem.value = { name: '', avatar: '' }; - showAddPopup.value = false; - - uni.showToast({ title: '添加成功', icon: 'success' }); + try { + uni.showLoading({ title: '添加中...' }); + + // 1. 先通过 uni.uploadFile 上传头像图片 + const uploadRes = await uploadImage(newItem.value.avatar); + + // 2. 将上传成功后的图片 URL 和其他数据一起提交给添加接口 + await topicItemAdd({ + topicId: currentTopicId.value, + name: newItem.value.name, + avatarUrl: uploadRes + }); + + uni.hideLoading(); + uni.showToast({ title: '添加成功', icon: 'success' }); + + // 重置表单并关闭弹窗 + newItem.value = { name: '', avatar: '' }; + showAddPopup.value = false; + + // 重新加载列表数据 + currentPage.value = 1; + hasMore.value = true; + loadItems(currentTopicId.value, 1); + + } catch (error) { + uni.hideLoading(); + uni.showToast({ title: '添加失败,请重试', icon: 'none' }); + console.error('添加评分项失败:', error); + } }; const ratingItems = ref([]);