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) {
return request({
@ -353,6 +364,7 @@ export function getAIResponse(data) {
});
}
// 获取所有教师
export const getTeacherCount = params => request.get('/user/getTeacherCount', { params });
// 获取所有学生
export const getStudentCount = params => request.get('/user/getStudentCount', { params });

View File

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