single-choice.vue 8.1 KB

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