short-answer.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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="semesterId" :rules="rules.semesterId">
  19. <a-select v-model:value="form.semesterId" placeholder="请选择学期" allowClear @change="handleSemesterChange">
  20. <a-select-option v-for="item in semesterList" :key="item.id" :value="item.id">
  21. {{ item.name }}
  22. </a-select-option>
  23. </a-select>
  24. </a-form-item>
  25. <!-- <a-form-item label="专业" name="majorId">
  26. <a-select v-model:value="form.majorId" placeholder="请选择专业" allowClear @change="handleMajorChange">
  27. <a-select-option v-for="item in majorList" :key="item.id" :value="item.id">
  28. {{ item.majorName }}
  29. </a-select-option>
  30. </a-select>
  31. </a-form-item> -->
  32. <a-form-item label="课程" name="courseId" :rules="rules.courseId">
  33. <a-select v-model:value="form.courseId" placeholder="请选择课程" allowClear :disabled="!form.semesterId">
  34. <a-select-option v-for="item in courseList" :key="item.courseId" :value="item.courseId">
  35. {{ item.courseName }}
  36. </a-select-option>
  37. </a-select>
  38. </a-form-item>
  39. <a-form-item label="题库类型:" name="bankType" required>
  40. <a-select v-model:value="form.bankType" placeholder="题库类型">
  41. <a-select-option v-for="item in bankTypeEnum" :key="item.key" :value="item.key">
  42. {{ item.value }}
  43. </a-select-option>
  44. </a-select>
  45. </a-form-item>
  46. <a-form-item label="题干:" name="title" required>
  47. <div v-if="form.title" class="rich-text-preview" v-html="form.title" @click="inputClick(form, 'title')"></div>
  48. <a-input
  49. v-else
  50. v-model:value="form.title"
  51. readonly
  52. placeholder="点击编辑题干内容"
  53. @click="inputClick(form, 'title')"
  54. />
  55. </a-form-item>
  56. <a-form-item label="答案:" name="correct" required>
  57. <div
  58. v-if="form.correct"
  59. class="rich-text-preview"
  60. v-html="form.correct"
  61. @click="inputClick(form, 'correct')"
  62. ></div>
  63. <a-input
  64. v-else
  65. v-model:value="form.correct"
  66. readonly
  67. placeholder="点击编辑答案内容"
  68. @click="inputClick(form, 'correct')"
  69. />
  70. </a-form-item>
  71. <a-form-item label="解析:" name="analyze" required>
  72. <div
  73. v-if="form.analyze"
  74. class="rich-text-preview"
  75. v-html="form.analyze"
  76. @click="inputClick(form, 'analyze')"
  77. ></div>
  78. <a-input
  79. v-else
  80. v-model:value="form.analyze"
  81. readonly
  82. placeholder="点击编辑解析内容"
  83. @click="inputClick(form, 'analyze')"
  84. />
  85. </a-form-item>
  86. <a-form-item v-if="form.bankType !== '2'" label="分数:" name="score" required>
  87. <a-input-number v-model:value="form.score" :precision="1" :step="1" :max="100" :min="0"/>
  88. </a-form-item>
  89. <a-form-item label="难度:" required>
  90. <a-rate v-model:value="form.difficult" class="question-item-rate" />
  91. </a-form-item>
  92. <a-form-item>
  93. <a-button type="primary" @click="submitForm" :loading="formLoading">提交</a-button>
  94. <a-button @click="resetForm">重置</a-button>
  95. <a-button type="success" @click="showQuestion">预览</a-button>
  96. </a-form-item>
  97. </a-form>
  98. <a-modal
  99. v-model:visible="richEditor.dialogVisible"
  100. width="70%"
  101. :footer="null"
  102. :closable="false"
  103. centered
  104. destroy-on-close
  105. >
  106. <Editor v-model="richEditorContent" :toolbar="toolbar" :height="300" />
  107. <div style="text-align: right; margin-top: 16px">
  108. <a-button type="primary" @click="editorConfirm">确定</a-button>
  109. <a-button @click="richEditor.dialogVisible = false">取消</a-button>
  110. </div>
  111. </a-modal>
  112. <a-modal v-model:visible="questionShow.dialog" width="800px" :footer="null" :bodyStyle="{ padding: '24px' }">
  113. <QuestionShow :qType="questionShow.qType" :question="questionShow.question" :qLoading="questionShow.loading" />
  114. </a-modal>
  115. </div>
  116. </template>
  117. <script setup>
  118. import { ref, reactive, computed, onMounted } from 'vue'
  119. import { message } from 'ant-design-vue'
  120. import { useExamStore } from '@/store/exam'
  121. import tQuestionApi from '@/api/exam/question/tQuestionApi'
  122. import QuestionShow from '../components/Show.vue'
  123. import Editor from '@/components/Editor/index.vue'
  124. import '../style/common.less'
  125. import resourceAuditApi from '@/api/resourceAudit.js'
  126. const bankTypeEnum = computed(() => examStore.getBankTypeEnum)
  127. const toolbar = computed(() => examStore.toolbar)
  128. const examStore = useExamStore()
  129. const props = defineProps({
  130. id: {
  131. type: Number,
  132. default: 0
  133. }
  134. })
  135. const emit = defineEmits(['successful'])
  136. const formRef = ref()
  137. const semesterList = ref([])
  138. const majorList = ref([])
  139. const courseList = ref([])
  140. const form = reactive({
  141. id: null,
  142. questionType: 5,
  143. // gradeLevel: null,
  144. // subjectId: null,
  145. title: '',
  146. items: [],
  147. analyze: '',
  148. correct: '',
  149. score: '',
  150. difficult: 0,
  151. bankType: null,
  152. courseId: null,
  153. semesterId: null,
  154. majorId: null
  155. })
  156. const subjectFilter = ref([])
  157. const formLoading = ref(false)
  158. const rules = {
  159. bankType: [{ required: true, message: '请选择题库类型', trigger: 'change' }],
  160. gradeLevel: [{ required: true, message: '请选择年级', trigger: 'change' }],
  161. subjectId: [{ required: true, message: '请选择学科', trigger: 'change' }],
  162. title: [{ required: true, message: '请输入题干', trigger: 'blur' }],
  163. correct: [{ required: true, message: '请输入答案', trigger: 'blur' }],
  164. analyze: [{ required: true, message: '请输入解析', trigger: 'blur' }],
  165. score: [{ required: true, message: '请输入分数', trigger: 'blur' }],
  166. semesterId: [{ required: true, message: '请选择学期', trigger: 'change' }],
  167. courseId: [{ required: true, message: '请选择课程', trigger: 'change' }]
  168. }
  169. const richEditor = reactive({
  170. dialogVisible: false,
  171. object: null,
  172. parameterName: '',
  173. instance: null
  174. })
  175. const richEditorContent = ref('')
  176. const questionShow = reactive({
  177. qType: 0,
  178. dialog: false,
  179. question: null,
  180. loading: false
  181. })
  182. const levelEnum = computed(() => examStore.levelEnum)
  183. const subjects = computed(() => examStore.subjects)
  184. onMounted(async () => {
  185. await examStore.initSubject()
  186. subjectFilter.value = subjects.value
  187. const id = props.id
  188. if (id && parseInt(id) !== 0) {
  189. formLoading.value = true
  190. tQuestionApi.select(id).then((re) => {
  191. Object.assign(form, re)
  192. formLoading.value = false
  193. // 如果是编辑模式,需要根据已有数据加载相应的课程列表
  194. if (re.semesterId) {
  195. // 直接加载课程列表
  196. loadCourseList()
  197. }
  198. })
  199. }
  200. // 加载学期列表
  201. const semesterLoading = message.loading('正在加载学期列表...', 0)
  202. resourceAuditApi
  203. .semesterDownList()
  204. .then((res) => {
  205. if (res.code === 200) {
  206. semesterList.value = res.data
  207. } else {
  208. message.error('加载学期列表失败:' + (res.msg || '未知错误'))
  209. }
  210. })
  211. .catch((err) => {
  212. message.error('加载学期列表失败:' + (err.message || '网络错误'))
  213. })
  214. .finally(() => {
  215. semesterLoading()
  216. })
  217. // 加载专业
  218. // const majorLoading = message.loading('正在加载专业列表...', 0)
  219. // resourceAuditApi
  220. // .majordownList()
  221. // .then((res) => {
  222. // if (res.code === 200) {
  223. // majorList.value = res.data
  224. // } else {
  225. // message.error('加载专业列表失败:' + (res.msg || '未知错误'))
  226. // }
  227. // })
  228. // .catch((err) => {
  229. // message.error('加载专业列表失败:' + (err.message || '网络错误'))
  230. // })
  231. // .finally(() => {
  232. // majorLoading()
  233. // })
  234. })
  235. // 学期变更处理函数
  236. const handleSemesterChange = (value) => {
  237. form.courseId = null
  238. courseList.value = []
  239. // 如果学期和专业都已选择,则查询课程列表
  240. if (value) {
  241. loadCourseList()
  242. }
  243. }
  244. // 专业变更处理函数
  245. // const handleMajorChange = (value) => {
  246. // form.courseId = null
  247. // courseList.value = []
  248. // // 如果学期和专业都已选择,则查询课程列表
  249. // if (value && form.semesterId) {
  250. // loadCourseList()
  251. // }
  252. // }
  253. // 加载课程列表
  254. const loadCourseList = () => {
  255. // 添加加载状态
  256. const courseLoading = message.loading('正在加载课程列表...', 0)
  257. // 根据选择的学期和专业加载课程列表
  258. resourceAuditApi
  259. .courseAllList({
  260. semesterId: form.semesterId,
  261. majorId: form.majorId
  262. })
  263. .then((res) => {
  264. if (res.code === 200) {
  265. courseList.value = res.data
  266. } else {
  267. message.error('加载课程列表失败:' + (res.msg || '未知错误'))
  268. }
  269. })
  270. .catch((err) => {
  271. message.error('加载课程列表失败:' + (err.message || '网络错误'))
  272. })
  273. .finally(() => {
  274. courseLoading()
  275. })
  276. }
  277. function inputClick(object, parameterName) {
  278. richEditor.object = object
  279. richEditor.parameterName = parameterName
  280. richEditorContent.value = object[parameterName] || ''
  281. richEditor.dialogVisible = true
  282. }
  283. function editorConfirm() {
  284. richEditor.object[richEditor.parameterName] = richEditorContent.value
  285. richEditor.dialogVisible = false
  286. }
  287. function submitForm() {
  288. formRef.value.validate().then((valid) => {
  289. if (valid) {
  290. formLoading.value = true
  291. tQuestionApi
  292. .edit(form)
  293. .then(() => {
  294. emit('successful')
  295. formLoading.value = false
  296. })
  297. .catch(() => {
  298. formLoading.value = false
  299. })
  300. }
  301. })
  302. }
  303. function resetForm() {
  304. const lastId = form.id
  305. formRef.value.resetFields()
  306. Object.assign(form, {
  307. id: null,
  308. questionType: 5,
  309. // gradeLevel: null,
  310. // subjectId: null,
  311. semesterId: null,
  312. majorId: null,
  313. courseId: null,
  314. title: '',
  315. items: [],
  316. analyze: '',
  317. correct: '',
  318. score: '',
  319. bankType: null,
  320. difficult: 0
  321. })
  322. form.id = lastId
  323. majorList.value = [] // 清空专业列表
  324. courseList.value = [] // 清空课程列表
  325. }
  326. function levelChange() {
  327. form.subjectId = null
  328. subjectFilter.value = subjects.value.filter((data) => data.level === form.gradeLevel)
  329. }
  330. function showQuestion() {
  331. questionShow.dialog = true
  332. questionShow.qType = form.questionType
  333. questionShow.question = { ...form }
  334. }
  335. </script>
  336. <style lang="less" scoped>
  337. .app-container {
  338. background: #fff;
  339. padding: 24px;
  340. border-radius: 8px;
  341. }
  342. </style>