short-answer.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. <template>
  2. <div class="app-container">
  3. <a-form :model="form" ref="formRef" :rules="rules" layout="vertical">
  4. <!-- <a-form-item label="年级:" name="gradeLevel" required>
  5. <a-select v-model:value="form.gradeLevel" placeholder="年级" @change="levelChange">
  6. <a-select-option v-for="item in levelEnum" :key="item.key" :value="item.key">
  7. {{ item.value }}
  8. </a-select-option>
  9. </a-select>
  10. </a-form-item>
  11. <a-form-item label="学科:" name="subjectId" required>
  12. <a-select v-model:value="form.subjectId" placeholder="学科">
  13. <a-select-option v-for="item in subjectFilter" :key="item.id" :value="item.id">
  14. {{ item.name + ' ( ' + item.levelName + ' )' }}
  15. </a-select-option>
  16. </a-select>
  17. </a-form-item> -->
  18. <a-form-item label="学期" name="semesterId">
  19. <a-select v-model:value="form.semesterId" placeholder="请选择学期" allowClear @change="handleSemesterChange">
  20. <a-select-option v-for="item in semesterList" :key="item.id" :value="item.id">
  21. {{ item.name }}
  22. </a-select-option>
  23. </a-select>
  24. </a-form-item>
  25. <!-- <a-form-item label="专业" name="majorId">
  26. <a-select v-model:value="form.majorId" placeholder="请选择专业" allowClear @change="handleMajorChange">
  27. <a-select-option v-for="item in majorList" :key="item.id" :value="item.id">
  28. {{ item.majorName }}
  29. </a-select-option>
  30. </a-select>
  31. </a-form-item> -->
  32. <a-form-item label="课程" name="courseId">
  33. <a-select v-model:value="form.courseId" placeholder="请选择课程" allowClear :disabled="!form.semesterId">
  34. <a-select-option v-for="item in courseList" :key="item.courseId" :value="item.courseId">
  35. {{ item.courseName }}
  36. </a-select-option>
  37. </a-select>
  38. </a-form-item>
  39. <a-form-item label="题库类型:" name="bankType" required>
  40. <a-select v-model:value="form.bankType" placeholder="题库类型">
  41. <a-select-option v-for="item in bankTypeEnum" :key="item.key" :value="item.key">
  42. {{ item.value }}
  43. </a-select-option>
  44. </a-select>
  45. </a-form-item>
  46. <a-form-item label="题干:" name="title" required>
  47. <div v-if="form.title" class="rich-text-preview" v-html="form.title" @click="inputClick(form, 'title')"></div>
  48. <a-input
  49. v-else
  50. v-model:value="form.title"
  51. readonly
  52. placeholder="点击编辑题干内容"
  53. @click="inputClick(form, 'title')"
  54. />
  55. </a-form-item>
  56. <a-form-item label="答案:" name="correct" required>
  57. <div
  58. v-if="form.correct"
  59. class="rich-text-preview"
  60. v-html="form.correct"
  61. @click="inputClick(form, 'correct')"
  62. ></div>
  63. <a-input
  64. v-else
  65. v-model:value="form.correct"
  66. readonly
  67. placeholder="点击编辑答案内容"
  68. @click="inputClick(form, 'correct')"
  69. />
  70. </a-form-item>
  71. <a-form-item label="解析:" name="analyze" required>
  72. <div
  73. v-if="form.analyze"
  74. class="rich-text-preview"
  75. v-html="form.analyze"
  76. @click="inputClick(form, 'analyze')"
  77. ></div>
  78. <a-input
  79. v-else
  80. v-model:value="form.analyze"
  81. readonly
  82. placeholder="点击编辑解析内容"
  83. @click="inputClick(form, 'analyze')"
  84. />
  85. </a-form-item>
  86. <a-form-item v-if="form.bankType !== '2'" label="分数:" name="score" required>
  87. <a-input-number v-model:value="form.score" :precision="1" :step="1" :max="100" />
  88. </a-form-item>
  89. <a-form-item label="难度:" required>
  90. <a-rate v-model:value="form.difficult" class="question-item-rate" />
  91. </a-form-item>
  92. <a-form-item>
  93. <a-button type="primary" @click="submitForm" :loading="formLoading">提交</a-button>
  94. <a-button @click="resetForm">重置</a-button>
  95. <a-button type="success" @click="showQuestion">预览</a-button>
  96. </a-form-item>
  97. </a-form>
  98. <a-modal
  99. v-model:visible="richEditor.dialogVisible"
  100. width="70%"
  101. :footer="null"
  102. :closable="false"
  103. centered
  104. destroy-on-close
  105. >
  106. <Editor v-model="richEditorContent" :toolbar="toolbar" :height="300" />
  107. <div style="text-align: right; margin-top: 16px">
  108. <a-button type="primary" @click="editorConfirm">确定</a-button>
  109. <a-button @click="richEditor.dialogVisible = false">取消</a-button>
  110. </div>
  111. </a-modal>
  112. <a-modal v-model:visible="questionShow.dialog" width="800px" :footer="null" :bodyStyle="{ padding: '24px' }">
  113. <QuestionShow :qType="questionShow.qType" :question="questionShow.question" :qLoading="questionShow.loading" />
  114. </a-modal>
  115. </div>
  116. </template>
  117. <script setup>
  118. import { ref, reactive, computed, onMounted } from 'vue'
  119. import { message } from 'ant-design-vue'
  120. import { useExamStore } from '@/store/exam'
  121. import tQuestionApi from '@/api/exam/question/tQuestionApi'
  122. import QuestionShow from '../components/Show.vue'
  123. import Editor from '@/components/Editor/index.vue'
  124. import '../style/common.less'
  125. import resourceAuditApi from '@/api/resourceAudit.js'
  126. const bankTypeEnum = computed(() => examStore.getBankTypeEnum)
  127. const toolbar = computed(() => examStore.toolbar)
  128. const examStore = useExamStore()
  129. const props = defineProps({
  130. id: {
  131. type: Number,
  132. default: 0
  133. }
  134. })
  135. const emit = defineEmits(['successful'])
  136. const formRef = ref()
  137. const semesterList = ref([])
  138. const majorList = ref([])
  139. const courseList = ref([])
  140. const form = reactive({
  141. id: null,
  142. questionType: 5,
  143. // gradeLevel: null,
  144. // subjectId: null,
  145. title: '',
  146. items: [],
  147. analyze: '',
  148. correct: '',
  149. score: '',
  150. difficult: 0,
  151. bankType: null,
  152. courseId: null,
  153. semesterId: null,
  154. majorId: null
  155. })
  156. const subjectFilter = ref([])
  157. const formLoading = ref(false)
  158. const rules = {
  159. bankType: [{ required: true, message: '请选择题库类型', trigger: 'change' }],
  160. gradeLevel: [{ required: true, message: '请选择年级', trigger: 'change' }],
  161. subjectId: [{ required: true, message: '请选择学科', trigger: 'change' }],
  162. title: [{ required: true, message: '请输入题干', trigger: 'blur' }],
  163. correct: [{ required: true, message: '请输入答案', trigger: 'blur' }],
  164. analyze: [{ required: true, message: '请输入解析', trigger: 'blur' }],
  165. score: [{ required: true, message: '请输入分数', trigger: 'blur' }]
  166. }
  167. const richEditor = reactive({
  168. dialogVisible: false,
  169. object: null,
  170. parameterName: '',
  171. instance: null
  172. })
  173. const richEditorContent = ref('')
  174. const questionShow = reactive({
  175. qType: 0,
  176. dialog: false,
  177. question: null,
  178. loading: false
  179. })
  180. const levelEnum = computed(() => examStore.levelEnum)
  181. const subjects = computed(() => examStore.subjects)
  182. onMounted(async () => {
  183. await examStore.initSubject()
  184. subjectFilter.value = subjects.value
  185. const id = props.id
  186. if (id && parseInt(id) !== 0) {
  187. formLoading.value = true
  188. tQuestionApi.select(id).then((re) => {
  189. Object.assign(form, re)
  190. formLoading.value = false
  191. // 如果是编辑模式,需要根据已有数据加载相应的课程列表
  192. if (re.semesterId) {
  193. // 直接加载课程列表
  194. loadCourseList()
  195. }
  196. })
  197. }
  198. // 加载学期列表
  199. const semesterLoading = message.loading('正在加载学期列表...', 0)
  200. resourceAuditApi
  201. .semesterDownList()
  202. .then((res) => {
  203. if (res.code === 200) {
  204. semesterList.value = res.data
  205. } else {
  206. message.error('加载学期列表失败:' + (res.msg || '未知错误'))
  207. }
  208. })
  209. .catch((err) => {
  210. message.error('加载学期列表失败:' + (err.message || '网络错误'))
  211. })
  212. .finally(() => {
  213. semesterLoading()
  214. })
  215. // 加载专业
  216. // const majorLoading = message.loading('正在加载专业列表...', 0)
  217. // resourceAuditApi
  218. // .majordownList()
  219. // .then((res) => {
  220. // if (res.code === 200) {
  221. // majorList.value = res.data
  222. // } else {
  223. // message.error('加载专业列表失败:' + (res.msg || '未知错误'))
  224. // }
  225. // })
  226. // .catch((err) => {
  227. // message.error('加载专业列表失败:' + (err.message || '网络错误'))
  228. // })
  229. // .finally(() => {
  230. // majorLoading()
  231. // })
  232. })
  233. // 学期变更处理函数
  234. const handleSemesterChange = (value) => {
  235. form.courseId = null
  236. courseList.value = []
  237. // 如果学期和专业都已选择,则查询课程列表
  238. if (value) {
  239. loadCourseList()
  240. }
  241. }
  242. // 专业变更处理函数
  243. // const handleMajorChange = (value) => {
  244. // form.courseId = null
  245. // courseList.value = []
  246. // // 如果学期和专业都已选择,则查询课程列表
  247. // if (value && form.semesterId) {
  248. // loadCourseList()
  249. // }
  250. // }
  251. // 加载课程列表
  252. const loadCourseList = () => {
  253. // 添加加载状态
  254. const courseLoading = message.loading('正在加载课程列表...', 0)
  255. // 根据选择的学期和专业加载课程列表
  256. resourceAuditApi
  257. .courseAllList({
  258. semesterId: form.semesterId,
  259. majorId: form.majorId
  260. })
  261. .then((res) => {
  262. if (res.code === 200) {
  263. courseList.value = res.data
  264. } else {
  265. message.error('加载课程列表失败:' + (res.msg || '未知错误'))
  266. }
  267. })
  268. .catch((err) => {
  269. message.error('加载课程列表失败:' + (err.message || '网络错误'))
  270. })
  271. .finally(() => {
  272. courseLoading()
  273. })
  274. }
  275. function inputClick(object, parameterName) {
  276. richEditor.object = object
  277. richEditor.parameterName = parameterName
  278. richEditorContent.value = object[parameterName] || ''
  279. richEditor.dialogVisible = true
  280. }
  281. function editorConfirm() {
  282. richEditor.object[richEditor.parameterName] = richEditorContent.value
  283. richEditor.dialogVisible = false
  284. }
  285. function submitForm() {
  286. formRef.value.validate().then((valid) => {
  287. if (valid) {
  288. formLoading.value = true
  289. tQuestionApi
  290. .edit(form)
  291. .then(() => {
  292. emit('successful')
  293. formLoading.value = false
  294. })
  295. .catch(() => {
  296. formLoading.value = false
  297. })
  298. }
  299. })
  300. }
  301. function resetForm() {
  302. const lastId = form.id
  303. formRef.value.resetFields()
  304. Object.assign(form, {
  305. id: null,
  306. questionType: 5,
  307. // gradeLevel: null,
  308. // subjectId: null,
  309. semesterId: null,
  310. majorId: null,
  311. courseId: null,
  312. title: '',
  313. items: [],
  314. analyze: '',
  315. correct: '',
  316. score: '',
  317. bankType: null,
  318. difficult: 0
  319. })
  320. form.id = lastId
  321. majorList.value = [] // 清空专业列表
  322. courseList.value = [] // 清空课程列表
  323. }
  324. function levelChange() {
  325. form.subjectId = null
  326. subjectFilter.value = subjects.value.filter((data) => data.level === form.gradeLevel)
  327. }
  328. function showQuestion() {
  329. questionShow.dialog = true
  330. questionShow.qType = form.questionType
  331. questionShow.question = { ...form }
  332. }
  333. </script>
  334. <style lang="less" scoped>
  335. .app-container {
  336. background: #fff;
  337. padding: 24px;
  338. border-radius: 8px;
  339. }
  340. </style>