gap-filling.vue 7.2 KB

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