multiple-choice.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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="bankType" required>
  19. <a-select v-model:value="form.bankType" placeholder="题库类型">
  20. <a-select-option v-for="item in bankTypeEnum" :key="item.key" :value="item.key">
  21. {{ item.value }}
  22. </a-select-option>
  23. </a-select>
  24. </a-form-item>
  25. <a-form-item label="题干:" name="title" required>
  26. <a-input v-model:value="form.title" readonly @click="inputClick(form, 'title')" />
  27. </a-form-item>
  28. <a-form-item label="选项:" required>
  29. <div v-for="(item, index) in form.items" :key="item.prefix" class="question-item-label">
  30. <a-input v-model:value="item.prefix" style="width: 50px; margin-right: 8px" />
  31. <a-input
  32. v-model:value="item.content"
  33. readonly
  34. @click="inputClick(item, 'content')"
  35. class="question-item-content-input"
  36. style="width: 60%"
  37. />
  38. <a-button danger size="small" class="question-item-remove" @click="questionItemRemove(index)">删除</a-button>
  39. </div>
  40. </a-form-item>
  41. <a-form-item label="解析:" name="analyze" required>
  42. <a-input v-model:value="form.analyze" readonly @click="inputClick(form, 'analyze')" />
  43. </a-form-item>
  44. <a-form-item label="分数:" name="score" required>
  45. <a-input-number v-model:value="form.score" :precision="1" :step="1" :max="100" />
  46. </a-form-item>
  47. <a-form-item label="难度:" required>
  48. <a-rate v-model:value="form.difficult" class="question-item-rate" />
  49. </a-form-item>
  50. <a-form-item label="正确答案:" name="correctArray" required>
  51. <a-checkbox-group v-model:value="form.correctArray">
  52. <a-checkbox v-for="item in form.items" :value="item.prefix" :key="item.prefix">{{ item.prefix }}</a-checkbox>
  53. </a-checkbox-group>
  54. </a-form-item>
  55. <a-form-item>
  56. <a-button type="primary" @click="submitForm" :loading="formLoading">提交</a-button>
  57. <a-button @click="resetForm">重置</a-button>
  58. <a-button type="success" @click="questionItemAdd">添加选项</a-button>
  59. <a-button type="success" @click="showQuestion">预览</a-button>
  60. </a-form-item>
  61. </a-form>
  62. <a-modal
  63. v-model:visible="richEditor.dialogVisible"
  64. width="800px"
  65. :footer="null"
  66. :closable="false"
  67. centered
  68. destroy-on-close
  69. >
  70. <Editor v-model="richEditorContent" :height="300" />
  71. <div style="text-align: right; margin-top: 16px">
  72. <a-button type="primary" @click="editorConfirm">确定</a-button>
  73. <a-button @click="richEditor.dialogVisible = false">取消</a-button>
  74. </div>
  75. </a-modal>
  76. <a-modal v-model:visible="questionShow.dialog" width="800px" :footer="null" :bodyStyle="{ padding: '24px' }">
  77. <QuestionShow :qType="questionShow.qType" :question="questionShow.question" :qLoading="questionShow.loading" />
  78. </a-modal>
  79. </div>
  80. </template>
  81. <script setup>
  82. import { ref, reactive, computed, onMounted } from 'vue'
  83. import { message } from 'ant-design-vue'
  84. import { useExamStore } from '@/store/exam'
  85. import tQuestionApi from '@/api/exam/question/tQuestionApi'
  86. import QuestionShow from '../components/Show.vue'
  87. import Editor from '@/components/Editor/index.vue'
  88. const bankTypeEnum = computed(() => examStore.getBankTypeEnum)
  89. const examStore = useExamStore()
  90. const props = defineProps({
  91. id: {
  92. type: Number,
  93. default: 0
  94. }
  95. })
  96. const emit = defineEmits(['successful'])
  97. const formRef = ref()
  98. const form = reactive({
  99. id: null,
  100. questionType: 2,
  101. // gradeLevel: null,
  102. // subjectId: null,
  103. title: '',
  104. items: [
  105. { id: null, prefix: 'A', content: '' },
  106. { id: null, prefix: 'B', content: '' },
  107. { id: null, prefix: 'C', content: '' },
  108. { id: null, prefix: 'D', content: '' }
  109. ],
  110. analyze: '',
  111. correct: '',
  112. correctArray: [],
  113. score: '',
  114. difficult: 0,
  115. bankType: 1
  116. })
  117. const subjectFilter = ref([])
  118. const formLoading = ref(false)
  119. const rules = {
  120. bankType: [{ required: true, message: '请选择题库类型', trigger: 'change' }],
  121. gradeLevel: [{ required: true, message: '请选择年级', trigger: 'change' }],
  122. subjectId: [{ required: true, message: '请选择学科', trigger: 'change' }],
  123. title: [{ required: true, message: '请输入题干', trigger: 'blur' }],
  124. analyze: [{ required: true, message: '请输入解析', trigger: 'blur' }],
  125. score: [{ required: true, message: '请输入分数', trigger: 'blur' }],
  126. correctArray: [{ required: true, message: '请选择正确答案', trigger: 'change' }]
  127. }
  128. const richEditor = reactive({
  129. dialogVisible: false,
  130. object: null,
  131. parameterName: '',
  132. instance: null
  133. })
  134. const richEditorContent = ref('')
  135. const questionShow = reactive({
  136. qType: 0,
  137. dialog: false,
  138. question: null,
  139. loading: false
  140. })
  141. const levelEnum = computed(() => examStore.levelEnum)
  142. const subjects = computed(() => examStore.subjects)
  143. onMounted(async () => {
  144. await examStore.initSubject()
  145. subjectFilter.value = subjects.value
  146. const id = props.id
  147. if (id && parseInt(id) !== 0) {
  148. formLoading.value = true
  149. tQuestionApi.select(id).then((re) => {
  150. Object.assign(form, re)
  151. formLoading.value = false
  152. })
  153. }
  154. })
  155. function inputClick(object, parameterName) {
  156. richEditor.object = object
  157. richEditor.parameterName = parameterName
  158. richEditorContent.value = object[parameterName] || ''
  159. richEditor.dialogVisible = true
  160. }
  161. function editorConfirm() {
  162. richEditor.object[richEditor.parameterName] = richEditorContent.value
  163. richEditor.dialogVisible = false
  164. }
  165. function questionItemRemove(index) {
  166. form.items.splice(index, 1)
  167. }
  168. function questionItemAdd() {
  169. const items = form.items
  170. let newLastPrefix
  171. if (items.length > 0) {
  172. let last = items[items.length - 1]
  173. newLastPrefix = String.fromCharCode(last.prefix.charCodeAt() + 1)
  174. } else {
  175. newLastPrefix = 'A'
  176. }
  177. items.push({ id: null, prefix: newLastPrefix, content: '' })
  178. }
  179. function submitForm() {
  180. formRef.value.validate().then((valid) => {
  181. if (valid) {
  182. formLoading.value = true
  183. tQuestionApi
  184. .edit(form)
  185. .then((re) => {
  186. emit('successful')
  187. formLoading.value = false
  188. })
  189. .catch(() => {
  190. formLoading.value = false
  191. })
  192. }
  193. })
  194. }
  195. function levelChange() {
  196. form.subjectId = null
  197. subjectFilter.value = subjects.value.filter((data) => data.level === form.gradeLevel)
  198. }
  199. function showQuestion() {
  200. questionShow.dialog = true
  201. questionShow.qType = form.questionType
  202. questionShow.question = { ...form }
  203. }
  204. function resetForm() {
  205. const lastId = form.id
  206. formRef.value.resetFields()
  207. Object.assign(form, {
  208. id: null,
  209. questionType: 2,
  210. // gradeLevel: null,
  211. // subjectId: null,
  212. title: '',
  213. items: [
  214. { id: null, prefix: 'A', content: '' },
  215. { id: null, prefix: 'B', content: '' },
  216. { id: null, prefix: 'C', content: '' },
  217. { id: null, prefix: 'D', content: '' }
  218. ],
  219. analyze: '',
  220. correct: '',
  221. correctArray: [],
  222. score: '',
  223. difficult: 0,
  224. bankType: 1
  225. })
  226. form.id = lastId
  227. }
  228. </script>
  229. <style lang="less" scoped>
  230. .app-container {
  231. background: #fff;
  232. padding: 24px;
  233. border-radius: 8px;
  234. }
  235. .question-item-label {
  236. margin-top: 10px;
  237. margin-bottom: 10px !important;
  238. }
  239. .question-item-remove {
  240. margin-left: 20px;
  241. }
  242. .question-item-content-input {
  243. margin-left: 8px;
  244. width: 60%;
  245. height: 20px;
  246. }
  247. .question-item-rate {
  248. line-height: 2.5;
  249. }
  250. </style>