single-choice.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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="correct" required>
  51. <a-radio-group v-model:value="form.correct">
  52. <a-radio v-for="item in form.items" :value="item.prefix" :key="item.prefix">{{ item.prefix }}</a-radio>
  53. </a-radio-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: 1,
  101. // gradeLevel: null,
  102. // subjectId: null,
  103. title: '',
  104. items: [
  105. { prefix: 'A', content: '' },
  106. { prefix: 'B', content: '' },
  107. { prefix: 'C', content: '' },
  108. { prefix: 'D', content: '' }
  109. ],
  110. analyze: '',
  111. correct: '',
  112. score: '',
  113. difficult: 0,
  114. bankType: 1
  115. })
  116. const subjectFilter = ref([])
  117. const formLoading = ref(false)
  118. const rules = {
  119. bankType: [{ required: true, message: '请选择题库类型', trigger: 'change' }],
  120. gradeLevel: [{ required: true, message: '请选择年级', trigger: 'change' }],
  121. subjectId: [{ required: true, message: '请选择学科', trigger: 'change' }],
  122. title: [{ required: true, message: '请输入题干', trigger: 'blur' }],
  123. analyze: [{ required: true, message: '请输入解析', trigger: 'blur' }],
  124. score: [{ required: true, message: '请输入分数', trigger: 'blur' }],
  125. correct: [{ required: true, message: '请选择正确答案', trigger: 'change' }]
  126. }
  127. const richEditor = reactive({
  128. dialogVisible: false,
  129. object: null,
  130. parameterName: '',
  131. instance: null
  132. })
  133. const richEditorContent = ref('')
  134. const questionShow = reactive({
  135. qType: 0,
  136. dialog: false,
  137. question: null,
  138. loading: false
  139. })
  140. const levelEnum = computed(() => examStore.levelEnum)
  141. const subjects = computed(() => examStore.subjects)
  142. onMounted(async () => {
  143. await examStore.initSubject()
  144. subjectFilter.value = subjects.value
  145. const id = props.id
  146. if (id && parseInt(id) !== 0) {
  147. formLoading.value = true
  148. tQuestionApi.select(id).then((re) => {
  149. Object.assign(form, re)
  150. formLoading.value = false
  151. })
  152. }
  153. })
  154. function inputClick(object, parameterName) {
  155. richEditor.object = object
  156. richEditor.parameterName = parameterName
  157. richEditorContent.value = object[parameterName] || ''
  158. richEditor.dialogVisible = true
  159. }
  160. function editorConfirm() {
  161. richEditor.object[richEditor.parameterName] = richEditorContent.value
  162. richEditor.dialogVisible = false
  163. }
  164. function questionItemRemove(index) {
  165. form.items.splice(index, 1)
  166. }
  167. function questionItemAdd() {
  168. const items = form.items
  169. let newLastPrefix
  170. if (items.length > 0) {
  171. let last = items[items.length - 1]
  172. newLastPrefix = String.fromCharCode(last.prefix.charCodeAt() + 1)
  173. } else {
  174. newLastPrefix = 'A'
  175. }
  176. items.push({ prefix: newLastPrefix, content: '' })
  177. }
  178. function submitForm() {
  179. formRef.value.validate().then((valid) => {
  180. if (valid) {
  181. formLoading.value = true
  182. tQuestionApi
  183. .edit(form)
  184. .then((re) => {
  185. emit('successful')
  186. formLoading.value = false
  187. })
  188. .catch(() => {
  189. formLoading.value = false
  190. })
  191. }
  192. })
  193. }
  194. function resetForm() {
  195. const lastId = form.id
  196. formRef.value.resetFields()
  197. Object.assign(form, {
  198. id: null,
  199. questionType: 1,
  200. // gradeLevel: null,
  201. // subjectId: null,
  202. title: '',
  203. items: [
  204. { prefix: 'A', content: '' },
  205. { prefix: 'B', content: '' },
  206. { prefix: 'C', content: '' },
  207. { prefix: 'D', content: '' }
  208. ],
  209. analyze: '',
  210. correct: '',
  211. score: '',
  212. difficult: 0,
  213. bankType: 1
  214. })
  215. form.id = lastId
  216. }
  217. function levelChange() {
  218. form.subjectId = null
  219. subjectFilter.value = subjects.value.filter((data) => data.level === form.gradeLevel)
  220. }
  221. function showQuestion() {
  222. questionShow.dialog = true
  223. questionShow.qType = form.questionType
  224. questionShow.question = { ...form }
  225. }
  226. </script>
  227. <style lang="less" scoped>
  228. .app-container {
  229. background: #fff;
  230. padding: 24px;
  231. border-radius: 8px;
  232. }
  233. .question-item-label {
  234. margin-top: 10px;
  235. margin-bottom: 10px !important;
  236. }
  237. .question-item-remove {
  238. margin-left: 20px;
  239. }
  240. .question-item-content-input {
  241. margin-left: 8px;
  242. width: 60%;
  243. height: 20px;
  244. }
  245. .question-item-rate {
  246. line-height: 2.5;
  247. }
  248. </style>