single-choice.vue 8.2 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 v-if="form.bankType !== '2'" 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" :toolbar="toolbar" :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 toolbar = computed(() => examStore.toolbar)
  119. const examStore = useExamStore()
  120. const props = defineProps({
  121. id: {
  122. type: Number,
  123. default: 0
  124. }
  125. })
  126. const emit = defineEmits(['successful'])
  127. const formRef = ref()
  128. const form = reactive({
  129. id: null,
  130. questionType: 1,
  131. // gradeLevel: null,
  132. // subjectId: null,
  133. title: '',
  134. items: [
  135. { prefix: 'A', content: '' },
  136. { prefix: 'B', content: '' },
  137. { prefix: 'C', content: '' },
  138. { prefix: 'D', content: '' }
  139. ],
  140. analyze: '',
  141. correct: '',
  142. score: '',
  143. difficult: 0,
  144. bankType: null
  145. })
  146. const subjectFilter = ref([])
  147. const formLoading = ref(false)
  148. const rules = {
  149. bankType: [{ required: true, message: '请选择题库类型', trigger: 'change' }],
  150. gradeLevel: [{ required: true, message: '请选择年级', trigger: 'change' }],
  151. subjectId: [{ required: true, message: '请选择学科', trigger: 'change' }],
  152. title: [{ required: true, message: '请输入题干', trigger: 'blur' }],
  153. analyze: [{ required: true, message: '请输入解析', trigger: 'blur' }],
  154. score: [{ required: true, message: '请输入分数', trigger: 'blur' }],
  155. correct: [{ required: true, message: '请选择正确答案', trigger: 'change' }]
  156. }
  157. const richEditor = reactive({
  158. dialogVisible: false,
  159. object: null,
  160. parameterName: '',
  161. instance: null
  162. })
  163. const richEditorContent = ref('')
  164. const questionShow = reactive({
  165. qType: 0,
  166. dialog: false,
  167. question: null,
  168. loading: false
  169. })
  170. const levelEnum = computed(() => examStore.levelEnum)
  171. const subjects = computed(() => examStore.subjects)
  172. onMounted(async () => {
  173. await examStore.initSubject()
  174. subjectFilter.value = subjects.value
  175. const id = props.id
  176. if (id && parseInt(id) !== 0) {
  177. formLoading.value = true
  178. tQuestionApi.select(id).then((re) => {
  179. Object.assign(form, re)
  180. formLoading.value = false
  181. })
  182. }
  183. })
  184. function inputClick(object, parameterName) {
  185. richEditor.object = object
  186. richEditor.parameterName = parameterName
  187. richEditorContent.value = object[parameterName] || ''
  188. richEditor.dialogVisible = true
  189. }
  190. function editorConfirm() {
  191. richEditor.object[richEditor.parameterName] = richEditorContent.value
  192. richEditor.dialogVisible = false
  193. }
  194. function questionItemRemove(index) {
  195. form.items.splice(index, 1)
  196. }
  197. function questionItemAdd() {
  198. const items = form.items
  199. let newLastPrefix
  200. if (items.length > 0) {
  201. let last = items[items.length - 1]
  202. newLastPrefix = String.fromCharCode(last.prefix.charCodeAt() + 1)
  203. } else {
  204. newLastPrefix = 'A'
  205. }
  206. items.push({ prefix: newLastPrefix, content: '' })
  207. }
  208. function submitForm() {
  209. formRef.value.validate().then((valid) => {
  210. if (valid) {
  211. formLoading.value = true
  212. tQuestionApi
  213. .edit(form)
  214. .then((re) => {
  215. emit('successful')
  216. formLoading.value = false
  217. })
  218. .catch(() => {
  219. formLoading.value = false
  220. })
  221. }
  222. })
  223. }
  224. function resetForm() {
  225. const lastId = form.id
  226. formRef.value.resetFields()
  227. Object.assign(form, {
  228. id: null,
  229. questionType: 1,
  230. // gradeLevel: null,
  231. // subjectId: null,
  232. title: '',
  233. items: [
  234. { prefix: 'A', content: '' },
  235. { prefix: 'B', content: '' },
  236. { prefix: 'C', content: '' },
  237. { prefix: 'D', content: '' }
  238. ],
  239. analyze: '',
  240. correct: '',
  241. score: '',
  242. difficult: 0,
  243. bankType: null
  244. })
  245. form.id = lastId
  246. }
  247. function levelChange() {
  248. form.subjectId = null
  249. subjectFilter.value = subjects.value.filter((data) => data.level === form.gradeLevel)
  250. }
  251. function showQuestion() {
  252. questionShow.dialog = true
  253. questionShow.qType = form.questionType
  254. questionShow.question = { ...form }
  255. }
  256. </script>
  257. <style lang="less" scoped>
  258. .app-container {
  259. background: #fff;
  260. padding: 24px;
  261. border-radius: 8px;
  262. }
  263. </style>