single-choice.vue 7.2 KB

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