true-false.vue 6.5 KB

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