index.vue 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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="subjectId"
  75. key="subjectId"
  76. width="150px"
  77. :customRender="subjectFormatter"
  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, defineAsyncComponent, shallowRef } from 'vue'
  156. import { useExamStore } from '@/store/exam'
  157. import tQuestionApi from '@/api/exam/question/tQuestionApi'
  158. import customPagination from '@/components/customPagination.vue'
  159. import { StarFilled, StarOutlined } from '@ant-design/icons-vue'
  160. import { Modal, message } from 'ant-design-vue'
  161. import { parseTime } from '@/utils/exam'
  162. const bankTypeEnum = computed(() => examStore.getBankTypeEnum)
  163. const examStore = useExamStore()
  164. const queryParam = reactive({
  165. id: null,
  166. questionType: null,
  167. // level: null,
  168. // subjectId: null,
  169. pageIndex: 1,
  170. pageSize: 10,
  171. bankType: null
  172. })
  173. const subjectFilter = ref([])
  174. const listLoading = ref(false)
  175. const tableData = ref([])
  176. const total = ref(0)
  177. const questionShow = reactive({
  178. qType: 0,
  179. dialog: false,
  180. question: null,
  181. loading: false
  182. })
  183. const levelEnum = computed(() => examStore.levelEnum)
  184. const questionTypeEnum = computed(() => examStore.questionTypeEnum)
  185. const editUrlEnum = computed(() => examStore.exam.question.editUrlEnum)
  186. const subjects = computed(() => examStore.subjects)
  187. const enumFormat = examStore.enumFormat
  188. const subjectEnumFormat = examStore.subjectEnumFormat
  189. const currentComponentId = ref(null)
  190. // 动态抽屉相关
  191. const drawerOpen = ref(false)
  192. const currentComponent = shallowRef(null)
  193. const openDrawer = (componentPath) => {
  194. currentComponentId.value = 0
  195. currentComponent.value = defineAsyncComponent(() => import(/* @vite-ignore */ componentPath))
  196. drawerOpen.value = true
  197. }
  198. const closeDrawer = () => {
  199. drawerOpen.value = false
  200. currentComponent.value = null
  201. }
  202. const onDrawerSuccess = () => {
  203. closeDrawer()
  204. search()
  205. }
  206. onMounted(async () => {
  207. await examStore.initSubject()
  208. subjectFilter.value = subjects.value
  209. search()
  210. })
  211. const submitForm = () => {
  212. queryParam.pageIndex = 1
  213. search()
  214. }
  215. const search = () => {
  216. listLoading.value = true
  217. const params = {
  218. ...queryParam,
  219. current: queryParam.pageIndex,
  220. size: queryParam.pageSize
  221. }
  222. delete params.pageIndex
  223. delete params.pageSize
  224. tQuestionApi
  225. .pageList(params)
  226. .then((data) => {
  227. const re = data
  228. tableData.value = re.records
  229. total.value = re.total
  230. queryParam.pageIndex = re.current
  231. listLoading.value = false
  232. })
  233. .catch(() => {
  234. listLoading.value = false
  235. })
  236. }
  237. const levelChange = () => {
  238. queryParam.subjectId = null
  239. subjectFilter.value = subjects.value.filter((data) => data.level === queryParam.level)
  240. }
  241. const showQuestion = (row) => {
  242. questionShow.dialog = true
  243. questionShow.loading = true
  244. tQuestionApi.select(row.id).then((re) => {
  245. questionShow.qType = re.questionType
  246. questionShow.question = re
  247. questionShow.loading = false
  248. })
  249. }
  250. const editQuestion = (row) => {
  251. const url = enumFormat(editUrlEnum.value, row.questionType)
  252. currentComponent.value = defineAsyncComponent(() => import(/* @vite-ignore */ url))
  253. drawerOpen.value = true
  254. currentComponentId.value = row.id
  255. }
  256. const deleteQuestion = (row) => {
  257. Modal.confirm({
  258. title: '确认删除该题目吗?',
  259. onOk: () => {
  260. tQuestionApi.deleteQuestion(row.id).then((re) => {
  261. search()
  262. })
  263. }
  264. })
  265. }
  266. const questionTypeFormatter = ({ text }) => {
  267. return enumFormat(questionTypeEnum.value, text)
  268. }
  269. const subjectFormatter = ({ text }) => {
  270. return subjectEnumFormat(text)
  271. }
  272. const onPageChange = (page) => {
  273. queryParam.pageIndex = page
  274. search()
  275. }
  276. const onPageSizeChange = (page, size) => {
  277. queryParam.pageIndex = page
  278. queryParam.pageSize = size
  279. search()
  280. }
  281. // 处理Excel导入
  282. const handleImportExcel = (options) => {
  283. const { file } = options
  284. const loading = message.loading('正在导入中...', 0)
  285. tQuestionApi
  286. .importExcel(file)
  287. .then((res) => {
  288. loading()
  289. message.success('导入成功')
  290. search() // 刷新列表
  291. })
  292. .catch((error) => {
  293. loading()
  294. message.error('导入失败,请检查Excel文件格式是否正确')
  295. console.error('导入失败:', error)
  296. })
  297. }
  298. </script>
  299. <style lang="less" scoped>
  300. .app-container {
  301. background: #fff;
  302. padding: 24px;
  303. border-radius: 8px;
  304. }
  305. .link-left {
  306. margin-left: 8px;
  307. }
  308. </style>
  309. <style>
  310. .ant-modal div[aria-hidden='true'] {
  311. display: none !important;
  312. }
  313. </style>