index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. <template>
  2. <div class="app-container">
  3. <a-form :model="queryParam" layout="inline" @submit.prevent>
  4. <a-form-item label="题目ID:">
  5. <a-input v-model:value="queryParam.id" allow-clear />
  6. </a-form-item>
  7. <a-form-item label="题目内容:">
  8. <a-input v-model:value="queryParam.content" allow-clear />
  9. </a-form-item>
  10. <!-- <a-form-item label="年级:">
  11. <a-select
  12. v-model:value="queryParam.level"
  13. placeholder="年级"
  14. allow-clear
  15. @change="levelChange"
  16. style="width: 100px"
  17. >
  18. <a-select-option v-for="item in levelEnum" :key="item.key" :value="item.key">{{
  19. item.value
  20. }}</a-select-option>
  21. </a-select>
  22. </a-form-item>
  23. <a-form-item label="学科:">
  24. <a-select v-model:value="queryParam.subjectId" allow-clear style="width: 100px">
  25. <a-select-option v-for="item in subjectFilter" :key="item.id" :value="item.id">
  26. {{ item.name + ' ( ' + item.levelName + ' )' }}
  27. </a-select-option>
  28. </a-select>
  29. </a-form-item> -->
  30. <a-form-item label="题库类型:">
  31. <a-select v-model:value="queryParam.bankType" placeholder="题库类型" allow-clear style="width: 100px">
  32. <a-select-option v-for="item in bankTypeEnum" :key="item.key" :value="item.key">{{
  33. item.value
  34. }}</a-select-option>
  35. </a-select>
  36. </a-form-item>
  37. <a-form-item label="题型:">
  38. <a-select v-model:value="queryParam.questionType" allow-clear style="width: 100px">
  39. <a-select-option v-for="item in questionTypeEnum" :key="item.key" :value="item.key">{{
  40. item.value
  41. }}</a-select-option>
  42. </a-select>
  43. </a-form-item>
  44. <a-form-item>
  45. <a-button type="primary" @click="submitForm">查询</a-button>
  46. <a-dropdown>
  47. <a-button type="primary" class="link-left">添加</a-button>
  48. <template #overlay>
  49. <a-menu>
  50. <a-menu-item v-for="item in editUrlEnum" :key="item.key" @click="openDrawer(item.value)">
  51. {{ item.name }}
  52. </a-menu-item>
  53. </a-menu>
  54. </template>
  55. </a-dropdown>
  56. <a-upload :customRequest="handleImportExcel" :showUploadList="false" accept=".xlsx,.xls" class="link-left">
  57. <a-button type="primary">导入Excel</a-button>
  58. </a-upload>
  59. </a-form-item>
  60. </a-form>
  61. <a-table
  62. :loading="listLoading"
  63. :data-source="tableData"
  64. :pagination="false"
  65. row-key="id"
  66. bordered
  67. style="margin-top: 16px"
  68. tableLayout="fixed"
  69. :scroll="{ x: 'max-content' }"
  70. >
  71. <a-table-column title="Id" data-index="id" key="id" width="90px" />
  72. <a-table-column
  73. title="题库类型"
  74. data-index="bankType"
  75. key="bankType"
  76. width="150px"
  77. :customRender="bankTypeFormatter"
  78. />
  79. <a-table-column
  80. title="题型"
  81. data-index="questionType"
  82. key="questionType"
  83. width="90px"
  84. :customRender="questionTypeFormatter"
  85. />
  86. <a-table-column title="题干" data-index="shortTitle" key="shortTitle" ellipsis width="350px" />
  87. <a-table-column title="分数" data-index="score" key="score" width="90px" />
  88. <a-table-column title="难度" data-index="difficult" key="difficult" width="110px">
  89. <template #default="{ text }">
  90. <StarFilled v-for="n in Number(text)" :key="n" style="color: #faad14" />
  91. <StarOutlined v-for="n in 5 - Number(text)" :key="'empty' + n" style="color: #e8e8e8" />
  92. </template>
  93. </a-table-column>
  94. <a-table-column title="创建时间" data-index="createTime" key="createTime" width="180px">
  95. <template #default="{ text }">
  96. {{ parseTime(text, '{y}-{m}-{d} {h}:{i}:{s}') }}
  97. </template>
  98. </a-table-column>
  99. <a-table-column title="操作" key="action" width="200px">
  100. <template #default="{ record }">
  101. <a-button size="small" style="margin-right: 8px" @click="showQuestion(record)">预览</a-button>
  102. <a-button size="small" style="margin-right: 8px" @click="editQuestion(record)">编辑</a-button>
  103. <a-button size="small" danger @click="deleteQuestion(record)">删除</a-button>
  104. </template>
  105. </a-table-column>
  106. </a-table>
  107. <custom-pagination
  108. v-show="total > 0"
  109. :total="total"
  110. :page-size="queryParam.pageSize"
  111. :current="queryParam.pageIndex"
  112. :showSizeChanger="true"
  113. @change="onPageChange"
  114. @showSizeChange="onPageSizeChange"
  115. style="margin-top: 16px; text-align: right; display: flex; justify-content: center"
  116. />
  117. <a-modal v-model:visible="questionShow.dialog" width="800px" :footer="null" :bodyStyle="{ padding: '24px' }">
  118. <div v-if="questionShow.loading" style="text-align: center; padding: 40px 0">
  119. <a-spin />
  120. </div>
  121. <div v-else>
  122. <!-- 题目预览内容,Show.vue 为空可先简单展示 -->
  123. <div v-if="questionShow.question">
  124. <h3>题型:{{ enumFormat(questionTypeEnum, questionShow.qType) }}</h3>
  125. <div v-html="questionShow.question.title || questionShow.question.shortTitle"></div>
  126. <div v-if="questionShow.question.items">
  127. <div v-for="item in questionShow.question.items" :key="item.prefix">
  128. <b>{{ item.prefix }}:</b><span v-html="item.content"></span>
  129. </div>
  130. </div>
  131. <div>解析:<span v-html="questionShow.question.analyze"></span></div>
  132. <div>分数:{{ questionShow.question.score }}</div>
  133. <div>难度:{{ questionShow.question.difficult }}</div>
  134. </div>
  135. </div>
  136. </a-modal>
  137. <a-drawer
  138. :visible="drawerOpen"
  139. title="添加题目"
  140. placement="right"
  141. width="900"
  142. :destroyOnClose="true"
  143. @close="closeDrawer"
  144. >
  145. <component
  146. :is="currentComponent"
  147. v-if="currentComponent"
  148. :id="currentComponentId"
  149. @successful="onDrawerSuccess"
  150. />
  151. </a-drawer>
  152. </div>
  153. </template>
  154. <script setup>
  155. import { ref, reactive, computed, onMounted, shallowRef } from 'vue'
  156. import SingleChoice from './edit/single-choice.vue'
  157. import MultipleChoice from './edit/multiple-choice.vue'
  158. import TrueFalse from './edit/true-false.vue'
  159. import GapFilling from './edit/gap-filling.vue'
  160. import ShortAnswer from './edit/short-answer.vue'
  161. import { useExamStore } from '@/store/exam'
  162. import tQuestionApi from '@/api/exam/question/tQuestionApi'
  163. import customPagination from '@/components/customPagination.vue'
  164. import { StarFilled, StarOutlined } from '@ant-design/icons-vue'
  165. import { Modal, message } from 'ant-design-vue'
  166. import { parseTime } from '@/utils/exam'
  167. const bankTypeEnum = computed(() => examStore.getBankTypeEnum)
  168. const examStore = useExamStore()
  169. const queryParam = reactive({
  170. id: null,
  171. questionType: null,
  172. // level: null,
  173. // subjectId: null,
  174. pageIndex: 1,
  175. pageSize: 10,
  176. bankType: null
  177. })
  178. const subjectFilter = ref([])
  179. const listLoading = ref(false)
  180. const tableData = ref([])
  181. const total = ref(0)
  182. const questionShow = reactive({
  183. qType: 0,
  184. dialog: false,
  185. question: null,
  186. loading: false
  187. })
  188. const levelEnum = computed(() => examStore.levelEnum)
  189. const questionTypeEnum = computed(() => examStore.questionTypeEnum)
  190. const editUrlEnum = computed(() => examStore.exam.question.editUrlEnum)
  191. const subjects = computed(() => examStore.subjects)
  192. const enumFormat = examStore.enumFormat
  193. const subjectEnumFormat = examStore.subjectEnumFormat
  194. const currentComponentId = ref(null)
  195. // 动态抽屉相关
  196. const drawerOpen = ref(false)
  197. const currentComponent = shallowRef(null)
  198. const componentMap = {
  199. 'single-choice': SingleChoice,
  200. 'multiple-choice': MultipleChoice,
  201. 'true-false': TrueFalse,
  202. 'gap-filling': GapFilling,
  203. 'short-answer': ShortAnswer
  204. }
  205. const openDrawer = (componentName) => {
  206. currentComponentId.value = 0
  207. currentComponent.value = componentMap[componentName]
  208. drawerOpen.value = true
  209. }
  210. const closeDrawer = () => {
  211. drawerOpen.value = false
  212. currentComponent.value = null
  213. }
  214. const onDrawerSuccess = () => {
  215. closeDrawer()
  216. search()
  217. }
  218. onMounted(async () => {
  219. await examStore.initSubject()
  220. subjectFilter.value = subjects.value
  221. search()
  222. })
  223. const submitForm = () => {
  224. queryParam.pageIndex = 1
  225. search()
  226. }
  227. const bankTypeFormatter = ({ text }) => {
  228. const key = Number(text)
  229. return enumFormat(bankTypeEnum.value, key)
  230. }
  231. const search = () => {
  232. listLoading.value = true
  233. const params = {
  234. ...queryParam,
  235. current: queryParam.pageIndex,
  236. size: queryParam.pageSize
  237. }
  238. delete params.pageIndex
  239. delete params.pageSize
  240. tQuestionApi
  241. .pageList(params)
  242. .then((data) => {
  243. const re = data
  244. tableData.value = re.records
  245. total.value = re.total
  246. queryParam.pageIndex = re.current
  247. listLoading.value = false
  248. })
  249. .catch(() => {
  250. listLoading.value = false
  251. })
  252. }
  253. const levelChange = () => {
  254. queryParam.subjectId = null
  255. subjectFilter.value = subjects.value.filter((data) => data.level === queryParam.level)
  256. }
  257. const showQuestion = (row) => {
  258. questionShow.dialog = true
  259. questionShow.loading = true
  260. tQuestionApi.select(row.id).then((re) => {
  261. questionShow.qType = re.questionType
  262. questionShow.question = re
  263. questionShow.loading = false
  264. })
  265. }
  266. const editQuestion = (row) => {
  267. const componentName = enumFormat(editUrlEnum.value, row.questionType)
  268. currentComponent.value = componentMap[componentName]
  269. drawerOpen.value = true
  270. currentComponentId.value = row.id
  271. }
  272. const deleteQuestion = (row) => {
  273. Modal.confirm({
  274. title: '确认删除该题目吗?',
  275. onOk: () => {
  276. tQuestionApi.deleteQuestion(row.id).then((re) => {
  277. search()
  278. })
  279. }
  280. })
  281. }
  282. const questionTypeFormatter = ({ text }) => {
  283. return enumFormat(questionTypeEnum.value, text)
  284. }
  285. const subjectFormatter = ({ text }) => {
  286. return subjectEnumFormat(text)
  287. }
  288. const onPageChange = (page) => {
  289. queryParam.pageIndex = page
  290. search()
  291. }
  292. const onPageSizeChange = (page, size) => {
  293. queryParam.pageIndex = page
  294. queryParam.pageSize = size
  295. search()
  296. }
  297. // 处理Excel导入
  298. const handleImportExcel = (options) => {
  299. const { file } = options
  300. // const loading = message.loading('正在检查文件...', 0)
  301. // 先调用检查接口
  302. // tQuestionApi
  303. // .checkExcel(file)
  304. // .then((checkRes) => {
  305. // loading()
  306. // // 检查通过后,再调用导入接口
  307. const importLoading = message.loading('正在导入中...', 0)
  308. tQuestionApi
  309. .importExcel(file)
  310. .then((res) => {
  311. importLoading()
  312. message.success('导入成功')
  313. search() // 刷新列表
  314. })
  315. .catch((error) => {
  316. importLoading()
  317. message.error('导入失败,请检查Excel文件格式是否正确')
  318. console.error('导入失败:', error)
  319. throw error // 继续抛出错误以便外层catch捕获
  320. })
  321. // })
  322. // .catch((error) => {
  323. // loading()
  324. // message.error('文件检查失败,请确保Excel格式正确')
  325. // console.error('检查失败:', error)
  326. // })
  327. }
  328. </script>
  329. <style lang="less" scoped>
  330. .app-container {
  331. background: #fff;
  332. padding: 24px;
  333. border-radius: 8px;
  334. }
  335. .link-left {
  336. margin-left: 8px;
  337. }
  338. </style>
  339. <style>
  340. .ant-modal div[aria-hidden='true'] {
  341. display: none !important;
  342. }
  343. </style>