feat:增加AI生成资料功能

This commit is contained in:
2026-08-02 09:47:02 +08:00
parent 280ea9f7d0
commit fefd329ea8

View File

@@ -33,7 +33,7 @@
<el-form-item label="名称" prop="name">
<el-input v-model="libraryStore.currentFile.name"
show-word-limit :maxlength="20"
show-word-limit :maxlength="20" clearable
placeholder="请输入资料名称">
</el-input>
</el-form-item>
@@ -51,11 +51,13 @@
</template>
</el-upload>
</el-form-item>
<el-form-item label="AI生成">
<el-button type="primary" :icon="MagicStick" @click="generateClick">AI生成</el-button>
</el-form-item>
</el-form>
<QuillEditor v-if="libraryStore.activeStep === 2"
:content="libraryStore.currentFile.content"
contentType="html" theme="snow"/>
<MdEditor v-if="libraryStore.activeStep === 2" v-model="libraryStore.currentFile.content" />
<template #footer>
<el-button @click="libraryStore.fileDialogVisible = false">取消</el-button>
@@ -73,12 +75,13 @@
import { ref } from 'vue'
import { useLibraryStore } from '@/stores/library.ts'
import { ElLoading, ElMessage, type FormInstance, type UploadRequestOptions } from 'element-plus'
import { Document, Upload, UploadFilled } from '@element-plus/icons-vue'
import { QuillEditor } from '@vueup/vue-quill'
import { Document, Upload, UploadFilled, MagicStick } from '@element-plus/icons-vue'
import '@vueup/vue-quill/dist/vue-quill.snow.css'
import { uploadLibraryFileApi } from '@/apis/library.ts'
import { getModuleOptions } from '@/utils/libraryUtil.ts'
import { getFileExtension, getFileHashName } from '@/utils/fileUtil.ts'
import { MdEditor } from 'md-editor-v3'
import 'md-editor-v3/lib/style.css'
const libraryStore = useLibraryStore()
const libraryFileFormRef = ref<FormInstance>()
@@ -147,6 +150,56 @@ const upload = async (options: UploadRequestOptions) => {
}
}
const generateClick = async () => {
const { subject, module, name } = libraryStore.currentFile
if (subject && module && name) {
libraryStore.currentFile.content = ''
libraryStore.activeStep++
const loading = ElLoading.service({
lock: true,
text: '资料生成中 请等待',
background: 'rgba(0, 0, 0, 0.7)'
})
try {
const response = await fetch('/study-api/library/knowledge', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
subject,
module,
name
})
})
if (!response.body) {
throw new Error('未获取到响应流')
}
const reader = response.body.getReader()
const decoder = new TextDecoder('utf-8')
while (true) {
const { value, done } = await reader.read()
if (done) break
libraryStore.currentFile.content += decoder.decode(value, { stream: true })
}
ElMessage.success('生成完成')
} catch (error) {
console.log(error)
ElMessage.error('生成失败 请联系管理员')
} finally {
loading.close()
}
} else {
ElMessage.error('请先填写信息')
}
}
const submitForm = async () => {
if (libraryStore.currentFile.content) {
@@ -164,7 +217,7 @@ const submitForm = async () => {
flex-direction: column;
gap: 16px;
.ql-container {
.md-editor {
height: auto;
}