Browse Source

添加收藏取消收藏

gcz 1 week ago
parent
commit
fa9a736b2c
4 changed files with 65 additions and 15 deletions
  1. BIN
      src/assets/icon-star-fill.png
  2. 1 0
      src/assets/icon-star-fill.svg
  3. 48 9
      src/views/AIChat.vue
  4. 16 6
      src/views/Favorites.vue

BIN
src/assets/icon-star-fill.png


File diff suppressed because it is too large
+ 1 - 0
src/assets/icon-star-fill.svg


+ 48 - 9
src/views/AIChat.vue

@@ -20,9 +20,10 @@
               <button class="action-btn" @click="copyContent(message.content)">
                 <img src="@/assets/icon-copy.png" alt="复制">
               </button>
-              <!-- <button class="action-btn" @click="toggleFavorite(message.id)">
-                <img src="@/assets/icon-star.png" alt="收藏">
-              </button> -->
+              <button class="action-btn" @click="toggleFavorite(message)">
+                <img src="@/assets/icon-star.png" v-if="!message.isCollect" alt="收藏">
+                <img src="@/assets/icon-star-fill.svg" style="width: 20px;height: 20px;"  v-else alt="已收藏">
+              </button>
               <button class="action-btn reanswer" @click="reAnswer(message.id)">
                 <img src="@/assets/icon-reanswer.png" alt="重新回答">
                 <span>重新回答</span>
@@ -102,7 +103,7 @@ import { ref, onMounted, nextTick, watch } from 'vue'
 import { useRoute, useRouter } from 'vue-router'
 import userAvatar from '@/assets/user-avatar.png'
 import assistantAvatar from '@/assets/assistant-avatar.png'
-import { post,get } from '@/utils/request'
+import { post,get,del } from '@/utils/request'
 import { ElMessage } from 'element-plus'
 import MarkdownIt from 'markdown-it'
 import hljs from 'highlight.js'
@@ -253,7 +254,8 @@ const simulateStreamResponse = async (question) => {
     id: Date.now(),
     role: 'assistant',
     content: '',
-    time: new Date()
+    time: new Date(),
+    chunksID: ''
   });
   
   // 立即滚动到底部确保"正在输入"可见
@@ -279,6 +281,7 @@ const simulateStreamResponse = async (question) => {
       if (data.data && data.data.answer) {
         accumulatedText += data.data.answer;
         messages.value[messageIndex].content = accumulatedText;
+        messages.value[messageIndex].chunksID = data.data.id;
         await scrollToBottom();
       }
 
@@ -371,8 +374,40 @@ const copyContent = (content) => {
   ElMessage.success('复制成功')
 }
 
-const toggleFavorite = (messageId) => {
-  // TODO: 实现收藏功能
+const toggleFavorite = async (message) => {
+  const answerId = message.chunksID || message.answerId;
+  //如果没有answerId,则不进行收藏
+  if(!answerId){
+    ElMessage.error('没有answerId')
+    return
+  }
+  try {
+    let response;
+    if (!message.isCollect) {
+      // 添加收藏
+      response = await post('/admin/userCollect/save', {
+        relatedType: 'ai.answer',
+        answerId: answerId
+      });
+      if (response.data) {
+        message.isCollect = true;
+        ElMessage.success('收藏成功');
+      }
+    } else {
+      // 取消收藏
+      response = await del('/admin/userCollect/deleteById', {
+        // relatedType: 'ai.answer',
+        id: answerId
+      });
+      if (response.data) {
+        message.isCollect = false;
+        ElMessage.success('已取消收藏');
+      }
+    }
+  } catch (error) {
+    console.error('收藏操作失败:', error);
+    ElMessage.error(message.isCollect ? '取消收藏失败' : '收藏失败');
+  }
 }
 
 const reAnswer = (messageId) => {
@@ -418,11 +453,13 @@ const getHistoryChat = async (historyId) => {
 
       // 添加所有对话记录
       for (const message of sortedMessages) {
+        // console.log('message',message);
+        
         // 添加用户问题
         messages.value.push({
           role: 'user',
           content: message.askContent,
-          time: new Date(message.createTime)
+          time: new Date(message.createTime),
         })
 
         // 处理AI回答
@@ -506,7 +543,9 @@ const getHistoryChat = async (historyId) => {
             id: message.id,
             role: 'assistant',
             content: finalContent,
-            time: new Date(message.createTime)
+            time: new Date(message.createTime),
+            chunksID: message.answerId,
+            isCollect: message.isCollect
           })
         } catch (error) {
           console.error('处理回答内容失败:', error)

+ 16 - 6
src/views/Favorites.vue

@@ -3,8 +3,8 @@
     <!-- 左侧类型选择 -->
     <div class="left-panel">
       <el-select v-model="currentType" placeholder="选择收藏类型" class="type-select">
-        <el-option label="全部" value="all" />
-        <!-- <el-option label="对话" value="ai.answer" /> -->
+        <!-- <el-option label="全部" value="all" /> -->
+        <el-option label="对话" value="ai.answer" />
         <!-- <el-option label="方案" value="scheme" /> -->
         <el-option label="文档" value="file.search" />
       </el-select>
@@ -52,7 +52,17 @@
                 v-model="item.checked"
                 @change="handleItemSelect"
               />
-              <div class="item-title" @click="handleItemClick(item)">{{ item.name||item.fileName }}</div>
+              <div class="item-title" @click="handleItemClick(item)">
+                <span v-if="item.relatedType=='ai.answer'" class="ellipsis-1">
+                  {{ item.askContent }}
+                </span>
+                <!-- <span v-else-if="item.relatedType=='file.search'">
+                  {{ item.name||item.fileName }}
+                </span> -->
+                <span v-else>
+                  {{ item.name||item.fileName }}
+                </span>
+              </div>
               <div class="item-info">
                 <span class="item-date">{{ formatDate(item.createTime) }}</span>
                 <el-dropdown v-if="!isBatchMode" @command="handleCommand($event, item)">
@@ -144,7 +154,7 @@ import { get, post, del ,put} from '../utils/request'
 import { downloadFile } from '../utils'
 
 // 状态变量
-const currentType = ref('all')
+const currentType = ref('ai.answer')
 const searchText = ref('')
 const loading = ref(false)
 const favorites = ref([])
@@ -428,14 +438,14 @@ const handleItemClick = async (item) => {
     switch (item.relatedType) {
       case 'ai.answer':
         // router.push(`/ai-chat?historyId=${item.id}`);
-        window.location.href = `/ai-chat?historyId=${item.id}`
+        window.location.href = `/ai-chat?historyId=${item.relatedId}`
         break;
       case 'scheme':
         router.push(`/solution?historyId=${item.id}`);
         break;
       case 'file.download':
         try {
-          await downloadFile(item.fileUrl, item.fileName)
+          await downloadFile(item.fileUrl, item.fileName||item.name)
           // 添加文件下载记录
           await post('/admin/userHistoryLog/saveFileDownloadLog', {
             relatedId: item.relatedId