From 6ef3449ab07299b34f7ad1e6e4c3d3da4b3d2d1b Mon Sep 17 00:00:00 2001
From: gaoluyang <2820782392@qq.com>
Date: 星期四, 11 六月 2026 10:57:18 +0800
Subject: [PATCH] pro 1.bom样式添加半成品
---
src/views/collaborativeApproval/knowledgeBase/index.vue | 757 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 files changed, 736 insertions(+), 21 deletions(-)
diff --git a/src/views/collaborativeApproval/knowledgeBase/index.vue b/src/views/collaborativeApproval/knowledgeBase/index.vue
index f7a1ef3..f50d06b 100644
--- a/src/views/collaborativeApproval/knowledgeBase/index.vue
+++ b/src/views/collaborativeApproval/knowledgeBase/index.vue
@@ -1,6 +1,6 @@
<template>
<div class="app-container">
- <div class="search_form">
+ <div class="search_form" style="margin-bottom: 20px;">
<div>
<span class="search_title">鐭ヨ瘑鏍囬锛�</span>
<el-input
@@ -225,18 +225,167 @@
</div>
</div>
</FormDialog>
+
+ <!-- 鏂囦欢绠$悊寮圭獥 -->
+ <FormDialog
+ v-model="filesDialogVisible"
+ title="鏂囦欢绠$悊"
+ :width="'900px'"
+ @close="closeFilesDialog"
+ @confirm="closeFilesDialog"
+ @cancel="closeFilesDialog"
+ >
+ <div class="file-manager">
+ <!-- 鏂囦欢涓婁紶 -->
+ <div class="upload-section">
+ <el-upload
+ :action="uploadUrl"
+ :headers="uploadHeaders"
+ :on-success="handleUploadSuccess"
+ :on-error="handleUploadError"
+ :before-upload="beforeUpload"
+ name="files"
+ multiple
+ :show-file-list="false"
+ accept=".txt,.md,.docx,.xlsx,.xls,.pdf"
+ >
+ <el-button type="primary">涓婁紶鏂囦欢</el-button>
+ </el-upload>
+ <el-button
+ type="success"
+ @click="saveFiles"
+ :disabled="uploadedBlobIds.length === 0"
+ :loading="savingFiles"
+ style="margin-left: 10px"
+ >
+ 淇濆瓨鏂囦欢鍏宠仈
+ </el-button>
+ <el-button
+ v-if="uploadedBlobIds.length > 0"
+ type="text"
+ @click="clearUploadedFiles"
+ style="margin-left: 10px"
+ >
+ 娓呯┖寰呬繚瀛樺垪琛�
+ </el-button>
+ </div>
+
+ <!-- 寰呬繚瀛樼殑鏂囦欢鍒楄〃 -->
+ <div v-if="uploadedBlobIds.length > 0" class="uploaded-list">
+ <div class="uploaded-tip">
+ <el-icon style="color: #409eff"><InfoFilled /></el-icon>
+ <span>宸蹭笂浼� {{ uploadedBlobIds.length }} 涓枃浠�,璇风偣鍑�"淇濆瓨鏂囦欢鍏宠仈"鎸夐挳瑙﹀彂鍚戦噺鍖栧鐞�</span>
+ </div>
+ </div>
+
+ <!-- 鏂囦欢鍒楄〃涓庡悜閲忓寲鐘舵�� -->
+ <el-table :data="fileList" style="margin-top: 20px" border>
+ <el-table-column prop="fileName" label="鏂囦欢鍚�" show-overflow-tooltip />
+ <el-table-column prop="fileType" label="鏂囦欢绫诲瀷" width="100" />
+ <el-table-column label="鍚戦噺鍖栫姸鎬�" width="120">
+ <template #default="{ row }">
+ <el-tag :type="getStatusType(row.vectorStatus)">
+ {{ getStatusText(row.vectorStatus) }}
+ </el-tag>
+ </template>
+ </el-table-column>
+ <el-table-column prop="chunkCount" label="鍒囩墖鏁�" width="100" align="center" />
+ <el-table-column label="閿欒淇℃伅" width="200" show-overflow-tooltip>
+ <template #default="{ row }">
+ <span v-if="row.vectorError" style="color: #f56c6c">{{ row.vectorError }}</span>
+ <span v-else style="color: #909399">-</span>
+ </template>
+ </el-table-column>
+ <el-table-column prop="createTime" label="涓婁紶鏃堕棿" width="180" />
+ <el-table-column label="鎿嶄綔" width="150" align="center">
+ <template #default="{ row }">
+ <el-button
+ v-if="row.vectorStatus === 3"
+ type="text"
+ @click="reprocessFile(row)"
+ >
+ 閲嶆柊澶勭悊
+ </el-button>
+ <el-button type="text" @click="deleteFile(row)" style="color: #f56c6c">
+ 鍒犻櫎
+ </el-button>
+ </template>
+ </el-table-column>
+ </el-table>
+ </div>
+ </FormDialog>
+
+ <!-- 鐭ヨ瘑搴撻棶绛斿脊绐� -->
+ <FormDialog
+ v-model="chatDialogVisible"
+ title="鐭ヨ瘑搴撻棶绛�"
+ :width="'800px'"
+ @close="closeChatDialog"
+ @confirm="closeChatDialog"
+ @cancel="closeChatDialog"
+ >
+ <div class="knowledge-chat">
+ <div class="chat-header">
+ <el-tag type="success">褰撳墠鐭ヨ瘑搴�: {{ currentKnowledgeBase?.title }}</el-tag>
+ </div>
+
+ <!-- 瀵硅瘽鍖哄煙 -->
+ <div class="chat-messages" ref="chatMessagesRef">
+ <div
+ v-for="(msg, index) in messages"
+ :key="index"
+ :class="['message', msg.role]"
+ >
+ <div class="message-role">{{ msg.role === 'user' ? '鎴�' : 'AI鍔╂墜' }}</div>
+ <div class="message-content">{{ msg.content }}</div>
+ </div>
+ <div v-if="chatLoading" class="message assistant">
+ <div class="message-role">AI鍔╂墜</div>
+ <div class="message-content typing">姝e湪鎬濊�冧腑...</div>
+ </div>
+ </div>
+
+ <!-- 杈撳叆妗� -->
+ <div class="chat-input">
+ <el-input
+ v-model="inputQuestion"
+ placeholder="璇疯緭鍏ラ棶棰�,鎸夊洖杞﹀彂閫�(Ctrl+Enter蹇嵎鍙戦��)"
+ @keyup.enter="sendMessage"
+ :disabled="chatLoading"
+ >
+ <template #append>
+ <el-button @click="sendMessage" :loading="chatLoading">鍙戦��</el-button>
+ </template>
+ </el-input>
+ <div class="chat-actions">
+ <el-button type="text" size="small" @click="clearMessages">娓呯┖瀵硅瘽</el-button>
+ </div>
+ </div>
+ </div>
+ </FormDialog>
</div>
</template>
<script setup>
-import { Search } from "@element-plus/icons-vue";
-import { onMounted, ref, reactive, toRefs, getCurrentInstance, computed, watch } from "vue";
+import { Search, InfoFilled } from "@element-plus/icons-vue";
+import { onMounted, ref, reactive, toRefs, getCurrentInstance, computed, watch, nextTick } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import PIMTable from "@/components/PIMTable/PIMTable.vue";
import FormDialog from '@/components/Dialog/FormDialog.vue';
-import { listKnowledgeBase, delKnowledgeBase,addKnowledgeBase,updateKnowledgeBase } from "@/api/collaborativeApproval/knowledgeBase.js";
+import {
+ listKnowledgeBase,
+ delKnowledgeBase,
+ addKnowledgeBase,
+ updateKnowledgeBase,
+ getVectorStatus,
+ reprocessVector,
+ saveKnowledgeBaseFiles,
+ deleteKnowledgeBaseFile,
+ knowledgeChat
+} from "@/api/collaborativeApproval/knowledgeBase.js";
import useUserStore from '@/store/modules/user';
import { userListNoPageByTenantId } from '@/api/system/user.js';
+import { getToken } from "@/utils/auth";
// 琛ㄥ崟楠岃瘉瑙勫垯
const rules = {
@@ -283,7 +432,18 @@
dialogTitle: "",
dialogType: "add",
viewDialogVisible: false,
- currentKnowledge: {}
+ currentKnowledge: {},
+ filesDialogVisible: false,
+ currentKnowledgeBase: null,
+ fileList: [],
+ uploadedBlobIds: [],
+ savingFiles: false,
+ vectorStatusTimer: null, // 鍚戦噺鍖栫姸鎬佽疆璇㈠畾鏃跺櫒
+ chatDialogVisible: false,
+ messages: [],
+ inputQuestion: "",
+ chatLoading: false,
+ memoryId: ""
});
const {
@@ -297,7 +457,18 @@
dialogTitle,
dialogType,
viewDialogVisible,
- currentKnowledge
+ currentKnowledge,
+ filesDialogVisible,
+ currentKnowledgeBase,
+ fileList,
+ uploadedBlobIds,
+ savingFiles,
+ vectorStatusTimer,
+ chatDialogVisible,
+ messages,
+ inputQuestion,
+ chatLoading,
+ memoryId
} = toRefs(data);
// 琛ㄥ崟寮曠敤
@@ -305,6 +476,12 @@
// 鐢ㄦ埛鐩稿叧
const userStore = useUserStore();
const userList = ref([]);
+// 鑱婂ぉ娑堟伅瀹瑰櫒寮曠敤
+const chatMessagesRef = ref();
+
+// 鏂囦欢涓婁紶鐩稿叧
+const uploadUrl = import.meta.env.VITE_APP_BASE_API + "/common/upload";
+const uploadHeaders = { Authorization: "Bearer " + getToken() };
// 琛ㄦ牸鍒楅厤缃�
const tableColumn = ref([
@@ -352,6 +529,18 @@
}
},
{
+ label: "鏂囦欢鏁伴噺",
+ prop: "fileCount",
+ width: 100,
+ align: "center"
+ },
+ {
+ label: "鍒囩墖鏁伴噺",
+ prop: "totalChunkCount",
+ width: 100,
+ align: "center"
+ },
+ {
label: "浣跨敤娆℃暟",
prop: "usageCount",
width: 100,
@@ -382,7 +571,21 @@
}
},
{
- name: "鏌ョ湅",
+ name: "鏂囦欢",
+ type: "text",
+ clickFun: (row) => {
+ openFilesDialog(row);
+ }
+ },
+ {
+ name: "闂瓟",
+ type: "text",
+ clickFun: (row) => {
+ openChatDialog(row);
+ }
+ },
+ {
+ name: "璇︽儏",
type: "text",
clickFun: (row) => {
viewKnowledge(row);
@@ -422,21 +625,43 @@
const getList = () => {
tableLoading.value = true;
- listKnowledgeBase({...page.value, ...searchForm.value})
+
+ // 鉁� GET璇锋眰浣跨敤params浼犲弬
+ listKnowledgeBase({
+ current: page.value.current,
+ size: page.value.size,
+ title: searchForm.value.title,
+ type: searchForm.value.type
+ })
.then(res => {
tableLoading.value = false;
- tableData.value = res.data.records
page.value.total = res.data.total;
- }).catch(err => {
- tableLoading.value = false;
+
+ // 濡傛灉褰撳墠椤垫暟瓒呰繃鎬婚〉鏁�,閲嶇疆鍒扮1椤靛苟閲嶆柊鏌ヨ
+ const maxPage = Math.ceil(res.data.total / page.value.size) || 1;
+ if (page.value.current > maxPage && maxPage > 0) {
+ page.value.current = 1;
+ return getList();
+ }
+
+ tableData.value = res.data.records;
})
+ .catch(err => {
+ tableLoading.value = false;
+ console.error("鏌ヨ鐭ヨ瘑搴撳垪琛ㄥけ璐�:", err);
+ });
};
// 鍒嗛〉澶勭悊
const pagination = (obj) => {
+ const oldSize = page.value.size;
page.value.current = obj.page;
page.value.size = obj.limit;
- handleQuery();
+ // 濡傛灉 size 鏀瑰彉浜嗭紝閲嶇疆鍒扮1椤碉紝閬垮厤褰撳墠椤佃秴鍑鸿寖鍥�
+ if (oldSize !== obj.limit) {
+ page.value.current = 1;
+ }
+ getList();
};
// 閫夋嫨鍙樺寲澶勭悊
@@ -599,27 +824,47 @@
const submitForm = async () => {
try {
await formRef.value.validate();
+
+ // 鉁� POST璇锋眰浣跨敤data浼犲弬,鏄庣‘鍙傛暟缁撴瀯
+ const formData = {
+ title: form.value.title,
+ type: form.value.type,
+ scenario: form.value.scenario || "",
+ efficiency: form.value.efficiency || "",
+ problem: form.value.problem,
+ solution: form.value.solution,
+ keyPoints: form.value.keyPoints || "",
+ creator: form.value.creator || "",
+ usageCount: form.value.usageCount || 0
+ };
+
if (dialogType.value === "add") {
// 鏂板鐭ヨ瘑
- addKnowledgeBase({...form.value}).then(res => {
+ addKnowledgeBase(formData).then(res => {
if(res.code == 200){
ElMessage.success("娣诲姞鎴愬姛");
closeKnowledgeDialog();
getList();
}
}).catch(err => {
- ElMessage.error(err.msg);
- })
+ console.error("娣诲姞鐭ヨ瘑搴撳け璐�:", err);
+ ElMessage.error(err.msg || "娣诲姞澶辫触");
+ });
} else {
- updateKnowledgeBase({...form.value}).then(res => {
+ // 鏇存柊鐭ヨ瘑 - 娣诲姞id鍙傛暟
+ updateKnowledgeBase({
+ id: form.value.id,
+ ...formData
+ }).then(res => {
if(res.code == 200){
ElMessage.success("鏇存柊鎴愬姛");
closeKnowledgeDialog();
getList();
}
}).catch(err => {
- ElMessage.error(err.msg);
- })
+ console.error("鏇存柊鐭ヨ瘑搴撳け璐�:", err);
+ ElMessage.error(err.msg || "鏇存柊澶辫触");
+ });
}
} catch (error) {
console.error("琛ㄥ崟楠岃瘉澶辫触:", error);
@@ -633,19 +878,22 @@
return;
}
- ElMessageBox.confirm("閫変腑鐨勫唴瀹瑰皢琚垹闄わ紝鏄惁纭鍒犻櫎锛�", "鍒犻櫎", {
+ ElMessageBox.confirm("閫変腑鐨勫唴瀹瑰皢琚垹闄�,鏄惁纭鍒犻櫎?", "鍒犻櫎", {
confirmButtonText: "纭",
cancelButtonText: "鍙栨秷",
type: "warning",
}).then(() => {
- // console.log(selectedIds.value);
+ // 鉁� DELETE璇锋眰浣跨敤data浼犻�扞D鏁扮粍
delKnowledgeBase(selectedIds.value).then(res => {
if(res.code == 200){
ElMessage.success("鍒犻櫎鎴愬姛");
selectedIds.value = [];
getList();
}
- })
+ }).catch(err => {
+ console.error("鍒犻櫎鐭ヨ瘑搴撳け璐�:", err);
+ ElMessage.error(err.msg || "鍒犻櫎澶辫触");
+ });
}).catch(() => {
// 鐢ㄦ埛鍙栨秷
});
@@ -668,6 +916,364 @@
const handleExport = () => {
proxy.download('/knowledgeBase/export', { ...searchForm.value }, '鐭ヨ瘑搴�.xlsx')
}
+
+// ============ 鏂囦欢绠$悊鐩稿叧 ============
+
+// 鎵撳紑鏂囦欢绠$悊寮圭獥
+const openFilesDialog = (row) => {
+ currentKnowledgeBase.value = row;
+ filesDialogVisible.value = true;
+ loadFileList();
+};
+
+// 鍔犺浇鏂囦欢鍒楄〃
+const loadFileList = async () => {
+ if (!currentKnowledgeBase.value?.id) return;
+
+ try {
+ const res = await getVectorStatus(currentKnowledgeBase.value.id);
+ fileList.value = res.data || [];
+
+ // 妫�鏌ユ槸鍚︽湁澶勭悊涓殑鏂囦欢,濡傛灉鏈夊垯鍚姩杞
+ const hasProcessing = res.data.some(item => item.vectorStatus === 1);
+ if (hasProcessing && !vectorStatusTimer.value) {
+ startVectorStatusPolling();
+ } else if (!hasProcessing && vectorStatusTimer.value) {
+ stopVectorStatusPolling();
+ }
+ } catch (error) {
+ console.error("鍔犺浇鏂囦欢鍒楄〃澶辫触:", error);
+ ElMessage.error("鍔犺浇鏂囦欢鍒楄〃澶辫触");
+ }
+};
+
+// 寮�濮嬭疆璇㈠悜閲忓寲鐘舵��
+const startVectorStatusPolling = () => {
+ vectorStatusTimer.value = setInterval(async () => {
+ try {
+ const res = await getVectorStatus(currentKnowledgeBase.value.id);
+ fileList.value = res.data || [];
+
+ // 妫�鏌ユ槸鍚﹁繕鏈夊鐞嗕腑鐨勬枃浠�
+ const hasProcessing = res.data.some(item => item.vectorStatus === 1);
+ if (!hasProcessing) {
+ stopVectorStatusPolling();
+ ElMessage.success("鎵�鏈夋枃浠跺悜閲忓寲澶勭悊瀹屾垚");
+ }
+ } catch (error) {
+ console.error("杞鍚戦噺鍖栫姸鎬佸け璐�:", error);
+ stopVectorStatusPolling();
+ }
+ }, 3000); // 姣�3绉掕疆璇竴娆�
+};
+
+// 鍋滄杞鍚戦噺鍖栫姸鎬�
+const stopVectorStatusPolling = () => {
+ if (vectorStatusTimer.value) {
+ clearInterval(vectorStatusTimer.value);
+ vectorStatusTimer.value = null;
+ }
+};
+
+// 涓婁紶鍓嶆牎楠�
+const beforeUpload = (file) => {
+ const allowedTypes = ['.txt', '.md', '.docx', '.xlsx', '.xls', '.pdf'];
+ const fileName = file.name.toLowerCase();
+ const isAllowed = allowedTypes.some(type => fileName.endsWith(type));
+
+ if (!isAllowed) {
+ ElMessage.error('鍙敮鎸� txt銆乵d銆乨ocx銆亁lsx銆亁ls銆乸df 鏍煎紡鐨勬枃浠�');
+ return false;
+ }
+
+ const isLt50M = file.size / 1024 / 1024 < 50;
+ if (!isLt50M) {
+ ElMessage.error('鏂囦欢澶у皬涓嶈兘瓒呰繃 50MB');
+ return false;
+ }
+
+ return true;
+};
+
+// 涓婁紶鎴愬姛
+const handleUploadSuccess = (response, file) => {
+ console.log("涓婁紶鍝嶅簲:", response); // 璋冭瘯鏃ュ織
+
+ if (response.code === 200) {
+ // 鉁� 鍚庣杩斿洖鐨勬槸 List<StorageBlobVO>,鎵�浠ata鏄暟缁�
+ if (Array.isArray(response.data) && response.data.length > 0) {
+ // 鍙栨暟缁勭涓�涓厓绱犵殑id
+ const blobId = response.data[0].id;
+ if (blobId) {
+ uploadedBlobIds.value.push(blobId);
+ ElMessage.success(`鏂囦欢 ${file.name} 涓婁紶鎴愬姛`);
+ } else {
+ console.error("涓婁紶鍝嶅簲涓湭鎵惧埌id:", response.data[0]);
+ ElMessage.error("涓婁紶澶辫触: 鏈幏鍙栧埌鏂囦欢ID");
+ }
+ } else {
+ console.error("涓婁紶鍝嶅簲鏍煎紡閿欒:", response);
+ ElMessage.error("涓婁紶澶辫触: 鍝嶅簲鏍煎紡閿欒");
+ }
+ } else {
+ ElMessage.error(response.msg || "涓婁紶澶辫触");
+ }
+};
+
+// 涓婁紶澶辫触
+const handleUploadError = (error, file) => {
+ ElMessage.error(`鏂囦欢 ${file.name} 涓婁紶澶辫触`);
+};
+
+// 淇濆瓨鏂囦欢鍏宠仈
+const saveFiles = async () => {
+ // 鍙傛暟鏍¢獙
+ if (!currentKnowledgeBase.value?.id) {
+ ElMessage.error("鐭ヨ瘑搴撲俊鎭紓甯�");
+ return;
+ }
+
+ if (uploadedBlobIds.value.length === 0) {
+ ElMessage.warning("璇峰厛涓婁紶鏂囦欢");
+ return;
+ }
+
+ savingFiles.value = true;
+
+ try {
+ // 鉁� POST璇锋眰浣跨敤data浼犲弬,鏄庣‘鍙傛暟缁撴瀯
+ await saveKnowledgeBaseFiles({
+ knowledgeBaseId: currentKnowledgeBase.value.id, // 鐭ヨ瘑搴揑D
+ storageBlobIds: uploadedBlobIds.value // 鏂囦欢blob ID鏁扮粍
+ });
+
+ ElMessage.success("鏂囦欢鍏宠仈淇濆瓨鎴愬姛,姝e湪鍚庡彴澶勭悊鍚戦噺鍖�");
+ uploadedBlobIds.value = [];
+
+ // 寤惰繜鍒锋柊鏂囦欢鍒楄〃,缁欏悗鍙板鐞嗘椂闂�
+ setTimeout(() => {
+ loadFileList();
+ }, 1000);
+ } catch (error) {
+ console.error("淇濆瓨鏂囦欢鍏宠仈澶辫触:", error);
+ ElMessage.error("淇濆瓨鏂囦欢鍏宠仈澶辫触");
+ } finally {
+ savingFiles.value = false;
+ }
+};
+
+// 閲嶆柊澶勭悊鍚戦噺鍖栫殑鏂囦欢
+const reprocessFile = async (row) => {
+ try {
+ await reprocessVector(row.id);
+ ElMessage.success("宸查噸鏂版彁浜ゅ悜閲忓寲浠诲姟");
+ // 寤惰繜鍒锋柊
+ setTimeout(() => {
+ loadFileList();
+ }, 1000);
+ } catch (error) {
+ console.error("閲嶆柊澶勭悊澶辫触:", error);
+ ElMessage.error("閲嶆柊澶勭悊澶辫触");
+ }
+};
+
+// 娓呯┖寰呬繚瀛樼殑鏂囦欢鍒楄〃
+const clearUploadedFiles = () => {
+ uploadedBlobIds.value = [];
+ ElMessage.success("宸叉竻绌哄緟淇濆瓨鏂囦欢鍒楄〃");
+};
+
+// 鍒犻櫎鏂囦欢
+const deleteFile = async (row) => {
+ try {
+ await ElMessageBox.confirm(
+ "纭畾瑕佸垹闄よ鏂囦欢鍚�?鍒犻櫎鍚庡皢鏃犳硶鎭㈠鍚戦噺鏁版嵁",
+ "鍒犻櫎纭",
+ {
+ confirmButtonText: "纭畾",
+ cancelButtonText: "鍙栨秷",
+ type: "warning"
+ }
+ );
+
+ // 鉁� DELETE璇锋眰浣跨敤data浼犻�扞D鏁扮粍
+ await deleteKnowledgeBaseFile([row.id]); // 娉ㄦ剰: row.id鏄悜閲忚褰旾D,涓嶆槸storageBlobId
+ ElMessage.success("鍒犻櫎鎴愬姛");
+ loadFileList();
+ } catch (error) {
+ if (error !== 'cancel') {
+ console.error("鍒犻櫎鏂囦欢澶辫触:", error);
+ ElMessage.error("鍒犻櫎鏂囦欢澶辫触");
+ }
+ }
+};
+
+// 鐘舵�佹枃鏈槧灏�
+const getStatusText = (status) => {
+ const map = {
+ 0: '寰呭鐞�',
+ 1: '澶勭悊涓�',
+ 2: '宸插畬鎴�',
+ 3: '澶辫触'
+ };
+ return map[status] || '鏈煡';
+};
+
+// 鐘舵�佹爣绛剧被鍨嬫槧灏�
+const getStatusType = (status) => {
+ const map = {
+ 0: 'info',
+ 1: 'warning',
+ 2: 'success',
+ 3: 'danger'
+ };
+ return map[status] || 'info';
+};
+
+// 鍏抽棴鏂囦欢绠$悊寮圭獥
+const closeFilesDialog = () => {
+ filesDialogVisible.value = false;
+ currentKnowledgeBase.value = null;
+ fileList.value = [];
+ uploadedBlobIds.value = [];
+ stopVectorStatusPolling(); // 鍋滄杞
+ getList(); // 鍒锋柊涓诲垪琛�,鏇存柊鏂囦欢鏁伴噺
+};
+
+// ============ 鐭ヨ瘑搴撻棶绛旂浉鍏� ============
+
+// 鐢熸垚UUID鐨刦allback鏂规
+const generateUUID = () => {
+ if (typeof crypto !== 'undefined' && crypto.randomUUID) {
+ return crypto.randomUUID();
+ }
+ // fallback: 鍏煎涓嶆敮鎸� crypto.randomUUID 鐨勭幆澧�
+ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
+ const r = Math.random() * 16 | 0;
+ const v = c === 'x' ? r : (r & 0x3 | 0x8);
+ return v.toString(16);
+ });
+};
+
+// 鎵撳紑闂瓟寮圭獥
+const openChatDialog = (row) => {
+ currentKnowledgeBase.value = row;
+ chatDialogVisible.value = true;
+ memoryId.value = generateUUID();
+ messages.value = [];
+ inputQuestion.value = "";
+};
+
+// 鍙戦�佹秷鎭�
+const sendMessage = async () => {
+ // 鍙傛暟鏍¢獙
+ if (!inputQuestion.value.trim()) {
+ ElMessage.warning("璇疯緭鍏ラ棶棰�");
+ return;
+ }
+
+ if (!currentKnowledgeBase.value?.id) {
+ ElMessage.error("鐭ヨ瘑搴撲俊鎭紓甯�");
+ return;
+ }
+
+ const question = inputQuestion.value.trim();
+
+ // 娣诲姞鐢ㄦ埛娑堟伅
+ messages.value.push({
+ role: 'user',
+ content: question
+ });
+
+ inputQuestion.value = "";
+ chatLoading.value = true;
+
+ // 婊氬姩鍒板簳閮�
+ await nextTick();
+ scrollToBottom();
+
+ try {
+ // 鉁� 娴佸紡璇锋眰浣跨敤Fetch API
+ const response = await knowledgeChat({
+ knowledgeBaseId: currentKnowledgeBase.value.id, // 鐭ヨ瘑搴揑D
+ memoryId: memoryId.value, // 浼氳瘽ID
+ question: question // 鐢ㄦ埛闂
+ });
+
+ if (!response.ok) {
+ const errorText = await response.text();
+ throw new Error(errorText || '璇锋眰澶辫触');
+ }
+
+ // 鉁� 鍚庣杩斿洖 text/stream;charset=utf-8
+ const reader = response.body.getReader();
+ const decoder = new TextDecoder();
+ let aiContent = '';
+
+ messages.value.push({ role: 'assistant', content: '' });
+
+ while (true) {
+ const { done, value } = await reader.read();
+ if (done) break;
+
+ const text = decoder.decode(value, { stream: true }); // 鉁� 娣诲姞stream閫夐」
+ aiContent += text;
+ messages.value[messages.value.length - 1].content = aiContent;
+
+ // 婊氬姩鍒板簳閮�
+ await nextTick();
+ scrollToBottom();
+ }
+
+ // 濡傛灉AI杩斿洖绌哄唴瀹�,鏄剧ず鎻愮ず
+ if (!aiContent.trim()) {
+ messages.value[messages.value.length - 1].content = '鎶辨瓑,鐭ヨ瘑搴撲腑鏈壘鍒扮浉鍏冲唴瀹�,璇峰皾璇曞叾浠栭棶棰樸��';
+ }
+ } catch (error) {
+ console.error("闂瓟璇锋眰澶辫触:", error);
+ ElMessage.error("闂瓟璇锋眰澶辫触,璇风◢鍚庨噸璇�");
+ messages.value.push({
+ role: 'assistant',
+ content: '鎶辨瓑,鍙戠敓浜嗛敊璇�,璇风◢鍚庨噸璇�'
+ });
+ } finally {
+ chatLoading.value = false;
+ }
+};
+
+// 娓呯┖瀵硅瘽
+const clearMessages = () => {
+ ElMessageBox.confirm(
+ "纭畾瑕佹竻绌烘墍鏈夊璇濊褰曞悧?",
+ "娓呯┖纭",
+ {
+ confirmButtonText: "纭畾",
+ cancelButtonText: "鍙栨秷",
+ type: "warning"
+ }
+ ).then(() => {
+ messages.value = [];
+ memoryId.value = generateUUID(); // 閲嶆柊鐢熸垚浼氳瘽ID
+ ElMessage.success("瀵硅瘽宸叉竻绌�");
+ }).catch(() => {
+ // 鐢ㄦ埛鍙栨秷
+ });
+};
+
+// 婊氬姩鍒板簳閮�
+const scrollToBottom = () => {
+ if (chatMessagesRef.value) {
+ chatMessagesRef.value.scrollTop = chatMessagesRef.value.scrollHeight;
+ }
+};
+
+// 鍏抽棴闂瓟寮圭獥
+const closeChatDialog = () => {
+ chatDialogVisible.value = false;
+ currentKnowledgeBase.value = null;
+ messages.value = [];
+ inputQuestion.value = "";
+};
</script>
<style scoped>
@@ -743,4 +1349,113 @@
font-size: 14px;
color: #909399;
}
+
+/* 鏂囦欢绠$悊鏍峰紡 */
+.file-manager {
+ padding: 20px 0;
+}
+
+.upload-section {
+ display: flex;
+ align-items: center;
+}
+
+.uploaded-list {
+ margin-top: 16px;
+ padding: 12px;
+ background: #f0f9ff;
+ border-radius: 6px;
+ border: 1px solid #b3d8ff;
+}
+
+.uploaded-tip {
+ display: flex;
+ align-items: center;
+ gap: 8px;
+ color: #409eff;
+ font-size: 14px;
+}
+
+/* 鐭ヨ瘑搴撻棶绛旀牱寮� */
+.knowledge-chat {
+ display: flex;
+ flex-direction: column;
+ height: 500px;
+}
+
+.chat-header {
+ margin-bottom: 16px;
+}
+
+.chat-messages {
+ flex: 1;
+ overflow-y: auto;
+ padding: 16px;
+ background: #f5f7fa;
+ border-radius: 8px;
+ margin-bottom: 16px;
+}
+
+.message {
+ margin-bottom: 16px;
+ max-width: 80%;
+}
+
+.message.user {
+ margin-left: auto;
+ text-align: right;
+}
+
+.message.assistant {
+ margin-right: auto;
+}
+
+.message-role {
+ font-size: 12px;
+ color: #909399;
+ margin-bottom: 4px;
+}
+
+.message-content {
+ display: inline-block;
+ padding: 10px 14px;
+ border-radius: 8px;
+ line-height: 1.6;
+ word-wrap: break-word;
+ white-space: pre-wrap;
+}
+
+.message.user .message-content {
+ background: #409eff;
+ color: white;
+}
+
+.message.assistant .message-content {
+ background: white;
+ color: #303133;
+ box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
+}
+
+.typing {
+ animation: typing 1.5s infinite;
+}
+
+@keyframes typing {
+ 0%, 50%, 100% {
+ opacity: 1;
+ }
+ 25%, 75% {
+ opacity: 0.5;
+ }
+}
+
+.chat-input {
+ margin-top: auto;
+}
+
+.chat-actions {
+ margin-top: 8px;
+ text-align: right;
+}
+
</style>
--
Gitblit v1.9.3