gap-filling.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 in form.items" :key="item.prefix" class="question-item-label">
  30. <a-input
  31. v-model:value="item.content"
  32. readonly
  33. @click="inputClick(item, 'content')"
  34. class="question-item-content-input"
  35. style="width: 80%"
  36. />
  37. <span class="question-item-span">分数:</span>
  38. <a-input-number v-model:value="item.score" :precision="1" :step="1" :max="100" />
  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>
  51. <a-button type="primary" @click="submitForm" :loading="formLoading">提交</a-button>
  52. <a-button @click="resetForm">重置</a-button>
  53. <a-button type="success" @click="showQuestion">预览</a-button>
  54. </a-form-item>
  55. </a-form>
  56. <a-modal
  57. v-model:visible="richEditor.dialogVisible"
  58. width="800px"
  59. :footer="null"
  60. :closable="false"
  61. centered
  62. destroy-on-close
  63. >
  64. <Editor v-model="richEditorContent" :height="300" />
  65. <div style="text-align: right; margin-top: 16px">
  66. <a-button type="primary" @click="editorConfirm">确定</a-button>
  67. <a-button @click="richEditor.dialogVisible = false">取消</a-button>
  68. </div>
  69. </a-modal>
  70. <a-modal v-model:visible="questionShow.dialog" width="800px" :footer="null" :bodyStyle="{ padding: '24px' }">
  71. <QuestionShow :qType="questionShow.qType" :question="questionShow.question" :qLoading="questionShow.loading" />
  72. </a-modal>
  73. </div>
  74. </template>
  75. <script setup>
  76. import { ref, reactive, computed, onMounted } from 'vue'
  77. import { message } from 'ant-design-vue'
  78. import { useExamStore } from '@/store/exam'
  79. import tQuestionApi from '@/api/exam/question/tQuestionApi'
  80. import QuestionShow from '../components/Show.vue'
  81. import Editor from '@/components/Editor/index.vue'
  82. const bankTypeEnum = computed(() => examStore.getBankTypeEnum)
  83. const examStore = useExamStore()
  84. const props = defineProps({
  85. id: {
  86. type: Number,
  87. default: 0
  88. }
  89. })
  90. const emit = defineEmits(['successful'])
  91. const formRef = ref()
  92. const form = reactive({
  93. id: null,
  94. questionType: 4,
  95. // gradeLevel: null,
  96. // subjectId: null,
  97. title: '',
  98. items: [],
  99. analyze: '',
  100. correct: '',
  101. score: '',
  102. difficult: 0,
  103. bankType: 1
  104. })
  105. const subjectFilter = ref([])
  106. const formLoading = ref(false)
  107. const rules = {
  108. bankType: [{ required: true, message: '请选择题库类型', trigger: 'change' }],
  109. gradeLevel: [{ required: true, message: '请选择年级', trigger: 'change' }],
  110. subjectId: [{ required: true, message: '请选择学科', trigger: 'change' }],
  111. title: [{ required: true, message: '请输入题干', trigger: 'blur' }],
  112. analyze: [{ required: true, message: '请输入解析', trigger: 'blur' }],
  113. score: [{ required: true, message: '请输入分数', trigger: 'blur' }]
  114. }
  115. const richEditor = reactive({
  116. dialogVisible: false,
  117. object: null,
  118. parameterName: '',
  119. instance: null
  120. })
  121. const richEditorContent = ref('')
  122. const questionShow = reactive({
  123. qType: 0,
  124. dialog: false,
  125. question: null,
  126. loading: false
  127. })
  128. const levelEnum = computed(() => examStore.levelEnum)
  129. const subjects = computed(() => examStore.subjects)
  130. onMounted(async () => {
  131. await examStore.initSubject()
  132. subjectFilter.value = subjects.value
  133. const id = props.id
  134. if (id && parseInt(id) !== 0) {
  135. formLoading.value = true
  136. tQuestionApi.select(id).then((re) => {
  137. Object.assign(form, re)
  138. formLoading.value = false
  139. })
  140. }
  141. })
  142. function inputClick(object, parameterName) {
  143. richEditor.object = object
  144. richEditor.parameterName = parameterName
  145. richEditorContent.value = object[parameterName] || ''
  146. richEditor.dialogVisible = true
  147. }
  148. function editorConfirm() {
  149. if (richEditor.parameterName === 'title') {
  150. if (questionItemReset(richEditorContent.value)) {
  151. richEditor.object[richEditor.parameterName] = richEditorContent.value
  152. richEditor.dialogVisible = false
  153. }
  154. } else {
  155. richEditor.object[richEditor.parameterName] = richEditorContent.value
  156. richEditor.dialogVisible = false
  157. }
  158. }
  159. function questionItemReset(content) {
  160. const spanRegex = /<span class="gapfilling-span (.*?)">(.*?)<\/span>/g
  161. const newFormItem = []
  162. const gapfillingItems = content.match(spanRegex)
  163. if (gapfillingItems === null) {
  164. message.error('请插入填空')
  165. return false
  166. }
  167. gapfillingItems.forEach((span) => {
  168. const pairRegex = /<span class="gapfilling-span (.*?)">(.*?)<\/span>/
  169. pairRegex.test(span)
  170. newFormItem.push({ id: null, itemUuid: RegExp.$1, prefix: RegExp.$2, content: '', score: '0' })
  171. })
  172. newFormItem.forEach((item) => {
  173. form.items.some((oldItem) => {
  174. if (oldItem.itemUuid === item.itemUuid) {
  175. item.content = oldItem.content
  176. item.id = oldItem.id
  177. item.score = oldItem.score
  178. return true
  179. }
  180. })
  181. })
  182. form.items = newFormItem
  183. return true
  184. }
  185. function submitForm() {
  186. formRef.value.validate().then((valid) => {
  187. if (valid) {
  188. formLoading.value = true
  189. tQuestionApi
  190. .edit(form)
  191. .then(() => {
  192. emit('successful')
  193. formLoading.value = false
  194. })
  195. .catch(() => {
  196. formLoading.value = false
  197. })
  198. }
  199. })
  200. }
  201. function levelChange() {
  202. form.subjectId = null
  203. subjectFilter.value = subjects.value.filter((data) => data.level === form.gradeLevel)
  204. }
  205. function showQuestion() {
  206. questionShow.dialog = true
  207. questionShow.qType = form.questionType
  208. questionShow.question = { ...form }
  209. }
  210. function resetForm() {
  211. const lastId = form.id
  212. formRef.value.resetFields()
  213. Object.assign(form, {
  214. id: null,
  215. questionType: 4,
  216. // gradeLevel: null,
  217. // subjectId: null,
  218. title: '',
  219. items: [],
  220. analyze: '',
  221. correct: '',
  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-content-input {
  240. margin-left: 8px;
  241. width: 60%;
  242. height: 20px;
  243. }
  244. .question-item-span {
  245. vertical-align: middle;
  246. font-size: 14px;
  247. color: #606266;
  248. font-weight: 700;
  249. box-sizing: border-box;
  250. margin-left: 10px;
  251. }
  252. .question-item-rate {
  253. line-height: 2.5;
  254. }
  255. </style>