fix: 修复AI

This commit is contained in:
Shu Guang 2025-05-17 20:48:20 +08:00
parent ffabdcb595
commit 5249bdcd75
2 changed files with 49 additions and 31 deletions

View File

@ -335,6 +335,17 @@ export function AnyncChart(data) {
}); });
} }
export function AiChat(data) {
return request({
url: '/api/ai-chat/chat',
method: 'post',
data,
headers: {
'Content-Type': 'application/json'
}
});
}
// 搜索知识库 // 搜索知识库
export function searchKnowledgeBase(query) { export function searchKnowledgeBase(query) {
return request({ return request({
@ -353,6 +364,7 @@ export function getAIResponse(data) {
}); });
} }
// 获取所有教师 // 获取所有教师
export const getTeacherCount = params => request.get('/user/getTeacherCount', { params }); export const getTeacherCount = params => request.get('/user/getTeacherCount', { params });
// 获取所有学生 // 获取所有学生
export const getStudentCount = params => request.get('/user/getStudentCount', { params }); export const getStudentCount = params => request.get('/user/getStudentCount', { params });

View File

@ -24,10 +24,15 @@
:class="['message', message.type]" :class="['message', message.type]"
> >
<div class="message-avatar"> <div class="message-avatar">
<a-avatar :src="message.type === 'user' ? userAvatar : aiAvatar" /> <a-avatar
:src="message.type === 'user' ? userAvatar : aiAvatar"
/>
</div> </div>
<div class="message-content"> <div class="message-content">
<div class="message-text" v-html="formatMessage(message.content)"></div> <div
class="message-text"
v-html="formatMessage(message.content)"
></div>
<div class="message-time">{{ message.time }}</div> <div class="message-time">{{ message.time }}</div>
</div> </div>
</div> </div>
@ -48,42 +53,43 @@
</template> </template>
<script> <script>
import * as monaco from 'monaco-editor'; import * as monaco from "monaco-editor";
import hljs from 'highlight.js'; import hljs from "highlight.js";
import 'highlight.js/styles/github.css'; import "highlight.js/styles/github.css";
import marked from 'marked'; import { marked } from "marked";
import { AiChat } from "@/api";
export default { export default {
name: 'AiAssistant', name: "AiAssistant",
data() { data() {
return { return {
code: '// 在这里输入你的代码\n', code: "// 在这里输入你的代码\n",
inputMessage: '', inputMessage: "",
loading: false, loading: false,
chatHistory: [], chatHistory: [],
editor: null, editor: null,
userAvatar: 'https://avatars.githubusercontent.com/u/1?v=4', userAvatar: "https://avatars.githubusercontent.com/u/1?v=4",
aiAvatar: 'https://avatars.githubusercontent.com/u/2?v=4', aiAvatar: "https://avatars.githubusercontent.com/u/2?v=4",
}; };
}, },
mounted() { mounted() {
this.initEditor(); this.initEditor();
window.addEventListener('resize', this.handleResize); window.addEventListener("resize", this.handleResize);
}, },
beforeDestroy() { beforeDestroy() {
if (this.editor) { if (this.editor) {
this.editor.dispose(); this.editor.dispose();
} }
window.removeEventListener('resize', this.handleResize); window.removeEventListener("resize", this.handleResize);
}, },
methods: { methods: {
initEditor() { initEditor() {
this.editor = monaco.editor.create(this.$refs.editorContainer, { this.editor = monaco.editor.create(this.$refs.editorContainer, {
value: this.code, value: this.code,
language: 'javascript', language: "javascript",
theme: 'vs-dark', theme: "vs-dark",
fontSize: 14, fontSize: 14,
lineNumbers: 'on', lineNumbers: "on",
minimap: { enabled: false }, minimap: { enabled: false },
scrollBeyondLastLine: false, scrollBeyondLastLine: false,
automaticLayout: true, automaticLayout: true,
@ -106,7 +112,7 @@ export default {
return hljs.highlight(code, { language: lang }).value; return hljs.highlight(code, { language: lang }).value;
} }
return hljs.highlightAuto(code).value; return hljs.highlightAuto(code).value;
} },
}); });
}, },
scrollToBottom() { scrollToBottom() {
@ -117,7 +123,7 @@ export default {
} }
}); });
}, },
addMessage(content, type = 'user') { addMessage(content, type = "user") {
this.chatHistory.push({ this.chatHistory.push({
content, content,
type, type,
@ -127,42 +133,42 @@ export default {
}, },
async sendMessage() { async sendMessage() {
if (!this.code.trim()) return; if (!this.code.trim()) return;
this.loading = true; this.loading = true;
this.addMessage('```javascript\n' + this.code + '\n```'); this.addMessage("```javascript\n" + this.code + "\n```");
try { try {
const response = await this.mockAiResponse(this.code); const response = await this.mockAiResponse(this.code);
this.addMessage(response, 'ai'); this.addMessage(response, "ai");
} catch (error) { } catch (error) {
console.error('AI 响应错误:', error); console.error("AI 响应错误:", error);
} finally { } finally {
this.loading = false; this.loading = false;
} }
}, },
async sendTextMessage() { async sendTextMessage() {
if (!this.inputMessage.trim()) return; if (!this.inputMessage.trim()) return;
this.loading = true; this.loading = true;
this.addMessage(this.inputMessage); this.addMessage(this.inputMessage);
try { try {
const response = await this.mockAiResponse(this.inputMessage); const response = await this.mockAiResponse(this.inputMessage);
this.addMessage(response, 'ai'); this.addMessage(response, "ai");
this.inputMessage = ''; this.inputMessage = "";
} catch (error) { } catch (error) {
console.error('AI 响应错误:', error); console.error("AI 响应错误:", error);
} finally { } finally {
this.loading = false; this.loading = false;
} }
}, },
async mockAiResponse(input) { async mockAiResponse(input) {
await new Promise(resolve => setTimeout(resolve, 1000)); await new Promise((resolve) => setTimeout(resolve, 1000));
return '这是 AI 助手的回复,实际开发时替换为真实的 AI 接口调用。\n\n```javascript\nconsole.log("Hello from AI");\n```'; return '这是 AI 助手的回复,实际开发时替换为真实的 AI 接口调用。\n\n```javascript\nconsole.log("Hello from AI");\n```';
}, },
clearCode() { clearCode() {
if (this.editor) { if (this.editor) {
this.editor.setValue('// 在这里输入你的代码\n'); this.editor.setValue("// 在这里输入你的代码\n");
} }
}, },
}, },
@ -268,4 +274,4 @@ export default {
:deep(.ant-card-head-title) { :deep(.ant-card-head-title) {
color: white; color: white;
} }
</style> </style>