119 lines
3.3 KiB
Vue
119 lines
3.3 KiB
Vue
<template>
|
|
<el-dialog v-model="examStore.dialogVisible" title="考试配置"
|
|
:width="appStore.isMobile ? '80%' : '30%'" center destroy-on-close>
|
|
<el-form ref="examFormRef" :model="examStore.config" :rules="rules" label-width="auto">
|
|
<el-form-item label="考试学科" prop="subject">
|
|
<el-select
|
|
v-model="examStore.config.subject"
|
|
placeholder="请选择考试学科"
|
|
>
|
|
<el-option
|
|
v-for="(item, index) in subjectList"
|
|
:key="index" :label="item" :value="item"
|
|
/>
|
|
</el-select>
|
|
</el-form-item>
|
|
|
|
<el-form-item label="题目数量" prop="total">
|
|
<el-input-number
|
|
v-model="examStore.config.total"
|
|
:min="10"
|
|
:max="50"
|
|
/>
|
|
</el-form-item>
|
|
</el-form>
|
|
|
|
<template #footer>
|
|
<el-button @click="examStore.dialogVisible = false">取消</el-button>
|
|
<el-button type="primary" @click="confirmClick(examFormRef)">确认</el-button>
|
|
</template>
|
|
</el-dialog>
|
|
</template>
|
|
|
|
<script setup lang="ts">
|
|
import { ElInputNumber, ElLoading, ElMessage, ElOption, ElSelect, type FormInstance } from 'element-plus'
|
|
import { useExamStore } from '@/stores/exam.ts'
|
|
import { useLibraryStore } from '@/stores/library.ts'
|
|
import { useQuestionStore } from '@/stores/question.ts'
|
|
import { useAppStore } from '@/stores/app.ts'
|
|
import { computed, ref } from 'vue'
|
|
|
|
const examStore = useExamStore()
|
|
const libraryStore = useLibraryStore()
|
|
const questionStore = useQuestionStore()
|
|
const appStore = useAppStore()
|
|
|
|
const examFormRef = ref<FormInstance>()
|
|
|
|
const subjectList = computed(() => {
|
|
return [...new Set(libraryStore.fileList.map((item) => item.subject))]
|
|
})
|
|
|
|
const rules = {
|
|
subject: [
|
|
{ required: true, message: '请选择考试学科', trigger: 'blur' }
|
|
],
|
|
total: [
|
|
{ required: true, message: '请选择题目数量', trigger: 'blur' }
|
|
]
|
|
}
|
|
|
|
const confirmClick = async (formEl: FormInstance | undefined) => {
|
|
if (!formEl) return
|
|
await formEl.validate(async (valid, fields) => {
|
|
if (valid) {
|
|
questionStore.type = 'Exam'
|
|
await generateExam()
|
|
} else {
|
|
ElMessage.error('请先输入信息')
|
|
}
|
|
})
|
|
}
|
|
|
|
const generateExam = async () => {
|
|
questionStore.questionList = []
|
|
|
|
const loading = ElLoading.service({
|
|
lock: true,
|
|
text: '题库生成中 请等待',
|
|
background: 'rgba(0, 0, 0, 0.7)'
|
|
})
|
|
|
|
try {
|
|
const response = await fetch('/study-api/agent/exam', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify({
|
|
subject: examStore.config.subject,
|
|
total: examStore.config.total
|
|
})
|
|
})
|
|
|
|
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
|
|
questionStore.questionContent += decoder.decode(value, { stream: true })
|
|
}
|
|
|
|
ElMessage.success('生成完成')
|
|
|
|
questionStore.questionList = JSON.parse(questionStore.questionContent)
|
|
examStore.dialogVisible = false
|
|
} catch (error) {
|
|
console.log(error)
|
|
ElMessage.error('生成失败 请联系管理员')
|
|
} finally {
|
|
loading.close()
|
|
}
|
|
}
|
|
</script>
|