short-answer.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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="答案:" name="correct" required>
  22. <a-input v-model:value="form.correct" readonly @click="inputClick(form, 'correct')" />
  23. </a-form-item>
  24. <a-form-item label="解析:" name="analyze" required>
  25. <a-input v-model:value="form.analyze" readonly @click="inputClick(form, 'analyze')" />
  26. </a-form-item>
  27. <a-form-item label="分数:" name="score" required>
  28. <a-input-number v-model:value="form.score" :precision="1" :step="1" :max="100" />
  29. </a-form-item>
  30. <a-form-item label="难度:" required>
  31. <a-rate v-model:value="form.difficult" class="question-item-rate" />
  32. </a-form-item>
  33. <a-form-item>
  34. <a-button type="primary" @click="submitForm" :loading="formLoading">提交</a-button>
  35. <a-button @click="resetForm">重置</a-button>
  36. <a-button type="success" @click="showQuestion">预览</a-button>
  37. </a-form-item>
  38. </a-form>
  39. <a-modal
  40. v-model:visible="richEditor.dialogVisible"
  41. width="800px"
  42. :footer="null"
  43. :closable="false"
  44. centered
  45. destroy-on-close
  46. >
  47. <Editor v-model="richEditorContent" :height="300" />
  48. <div style="text-align: right; margin-top: 16px">
  49. <a-button type="primary" @click="editorConfirm">确定</a-button>
  50. <a-button @click="richEditor.dialogVisible = false">取消</a-button>
  51. </div>
  52. </a-modal>
  53. <a-modal v-model:visible="questionShow.dialog" width="800px" :footer="null" :bodyStyle="{ padding: '24px' }">
  54. <QuestionShow :qType="questionShow.qType" :question="questionShow.question" :qLoading="questionShow.loading" />
  55. </a-modal>
  56. </div>
  57. </template>
  58. <script setup>
  59. import { ref, reactive, computed, onMounted } from 'vue'
  60. import { message } from 'ant-design-vue'
  61. import { useExamStore } from '@/store/exam'
  62. import tQuestionApi from '@/api/exam/question/tQuestionApi'
  63. import QuestionShow from '../components/Show.vue'
  64. import Editor from '@/components/Editor/index.vue'
  65. const examStore = useExamStore()
  66. const props = defineProps({
  67. id: {
  68. type: Number,
  69. default: 0
  70. }
  71. })
  72. const emit = defineEmits(['successful'])
  73. const formRef = ref()
  74. const form = reactive({
  75. id: null,
  76. questionType: 5,
  77. gradeLevel: null,
  78. subjectId: null,
  79. title: '',
  80. items: [],
  81. analyze: '',
  82. correct: '',
  83. score: '',
  84. difficult: 0
  85. })
  86. const subjectFilter = ref([])
  87. const formLoading = ref(false)
  88. const rules = {
  89. gradeLevel: [{ required: true, message: '请选择年级', trigger: 'change' }],
  90. subjectId: [{ required: true, message: '请选择学科', trigger: 'change' }],
  91. title: [{ required: true, message: '请输入题干', trigger: 'blur' }],
  92. correct: [{ required: true, message: '请输入答案', trigger: 'blur' }],
  93. analyze: [{ required: true, message: '请输入解析', trigger: 'blur' }],
  94. score: [{ required: true, message: '请输入分数', trigger: 'blur' }]
  95. }
  96. const richEditor = reactive({
  97. dialogVisible: false,
  98. object: null,
  99. parameterName: '',
  100. instance: null
  101. })
  102. const richEditorContent = ref('')
  103. const questionShow = reactive({
  104. qType: 0,
  105. dialog: false,
  106. question: null,
  107. loading: false
  108. })
  109. const levelEnum = computed(() => examStore.levelEnum)
  110. const subjects = computed(() => examStore.subjects)
  111. onMounted(async () => {
  112. await examStore.initSubject()
  113. subjectFilter.value = subjects.value
  114. const id = props.id
  115. if (id && parseInt(id) !== 0) {
  116. formLoading.value = true
  117. tQuestionApi.select(id).then((re) => {
  118. Object.assign(form, re)
  119. formLoading.value = false
  120. })
  121. }
  122. })
  123. function inputClick(object, parameterName) {
  124. richEditor.object = object
  125. richEditor.parameterName = parameterName
  126. richEditorContent.value = object[parameterName] || ''
  127. richEditor.dialogVisible = true
  128. }
  129. function editorConfirm() {
  130. richEditor.object[richEditor.parameterName] = richEditorContent.value
  131. richEditor.dialogVisible = false
  132. }
  133. function submitForm() {
  134. formRef.value.validate().then((valid) => {
  135. if (valid) {
  136. formLoading.value = true
  137. tQuestionApi
  138. .edit(form)
  139. .then(() => {
  140. emit('successful')
  141. formLoading.value = false
  142. })
  143. .catch(() => {
  144. formLoading.value = false
  145. })
  146. }
  147. })
  148. }
  149. function resetForm() {
  150. const lastId = form.id
  151. formRef.value.resetFields()
  152. Object.assign(form, {
  153. id: null,
  154. questionType: 5,
  155. gradeLevel: null,
  156. subjectId: null,
  157. title: '',
  158. items: [],
  159. analyze: '',
  160. correct: '',
  161. score: '',
  162. difficult: 0
  163. })
  164. form.id = lastId
  165. }
  166. function levelChange() {
  167. form.subjectId = null
  168. subjectFilter.value = subjects.value.filter((data) => data.level === form.gradeLevel)
  169. }
  170. function showQuestion() {
  171. questionShow.dialog = true
  172. questionShow.qType = form.questionType
  173. questionShow.question = { ...form }
  174. }
  175. </script>
  176. <style lang="less" scoped>
  177. .app-container {
  178. background: #fff;
  179. padding: 24px;
  180. border-radius: 8px;
  181. }
  182. .question-item-rate {
  183. line-height: 2.5;
  184. }
  185. </style>