index.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <template>
  2. <div class="app-container">
  3. <!-- 查询表单 -->
  4. <a-form :model="queryParam" layout="inline" class="search-form">
  5. <a-form-item label="题目ID:">
  6. <a-input v-model:value="queryParam.id" placeholder="请输入题目ID" allow-clear />
  7. </a-form-item>
  8. <a-form-item label="年级:">
  9. <a-select
  10. v-model:value="queryParam.level"
  11. placeholder="请选择年级"
  12. @change="levelChange"
  13. allow-clear
  14. style="width: 120px"
  15. >
  16. <a-select-option v-for="item in levelEnum" :key="item.key" :value="item.key">
  17. {{ item.value }}
  18. </a-select-option>
  19. </a-select>
  20. </a-form-item>
  21. <a-form-item label="学科:">
  22. <a-select v-model:value="queryParam.subjectId" placeholder="请选择学科" allow-clear style="width: 200px">
  23. <a-select-option v-for="item in subjectFilter" :key="item.id" :value="item.id">
  24. {{ item.name }} ( {{ item.levelName }} )
  25. </a-select-option>
  26. </a-select>
  27. </a-form-item>
  28. <a-form-item>
  29. <a-button type="primary" @click="submitForm">查询</a-button>
  30. <a-button type="primary" @click="openDrawer('add')" style="margin-left: 8px">添加</a-button>
  31. </a-form-item>
  32. </a-form>
  33. <!-- 数据表格 -->
  34. <a-table
  35. :loading="listLoading"
  36. :data-source="tableData"
  37. :columns="columns"
  38. :pagination="false"
  39. row-key="id"
  40. class="data-table"
  41. :locale="{ emptyText: '暂无数据' }"
  42. >
  43. <template #bodyCell="{ column, record }">
  44. <template v-if="column.key === 'action'">
  45. <a-button size="small" @click="openDrawer('edit', record.id)">编辑</a-button>
  46. <a-button size="small" type="primary" danger @click="deletePaper(record)" style="margin-left: 8px">
  47. 删除
  48. </a-button>
  49. </template>
  50. </template>
  51. </a-table>
  52. <!-- 分页 -->
  53. <a-pagination
  54. v-if="total > 0"
  55. :current="queryParam.pageIndex"
  56. :page-size="queryParam.pageSize"
  57. :total="total"
  58. :show-size-changer="true"
  59. :show-quick-jumper="true"
  60. :show-total="(total, range) => `第 ${range[0]}-${range[1]} 条/共 ${total} 条`"
  61. @change="handlePageChange"
  62. @show-size-change="handlePageSizeChange"
  63. class="pagination"
  64. />
  65. <!-- 编辑/添加 抽屉 -->
  66. <a-drawer
  67. :visible="drawerVisible"
  68. :title="drawerTitle"
  69. placement="right"
  70. width="900"
  71. @close="closeDrawer"
  72. destroyOnClose
  73. >
  74. <FormEdit v-if="drawerVisible" :id="editId" @success="onEditSuccess" />
  75. </a-drawer>
  76. </div>
  77. </template>
  78. <script setup>
  79. import { ref, reactive, computed, onMounted } from 'vue'
  80. import { message, Modal } from 'ant-design-vue'
  81. import { useExamStore } from '@/store/exam'
  82. import examPaperApi from '@/api/exam/paper/examPaperApi'
  83. import FormEdit from './form.vue'
  84. const examStore = useExamStore()
  85. // 响应式数据
  86. const queryParam = reactive({
  87. id: null,
  88. level: null,
  89. subjectId: null,
  90. current: 1,
  91. size: 10
  92. })
  93. const subjectFilter = ref([])
  94. const listLoading = ref(true)
  95. const tableData = ref([])
  96. const total = ref(0)
  97. // Drawer 控制
  98. const drawerVisible = ref(false)
  99. const drawerTitle = ref('')
  100. const editId = ref(null)
  101. function openDrawer(type, id = null) {
  102. if (type === 'add') {
  103. drawerTitle.value = '添加试卷'
  104. editId.value = null
  105. } else {
  106. drawerTitle.value = '编辑试卷'
  107. editId.value = id
  108. }
  109. drawerVisible.value = true
  110. }
  111. function closeDrawer() {
  112. drawerVisible.value = false
  113. }
  114. function onEditSuccess() {
  115. closeDrawer()
  116. search()
  117. }
  118. // 表格列配置
  119. const columns = [
  120. {
  121. title: 'Id',
  122. dataIndex: 'id',
  123. key: 'id',
  124. width: 90
  125. },
  126. {
  127. title: '学科',
  128. dataIndex: 'subjectId',
  129. key: 'subjectId',
  130. width: 200,
  131. customRender: ({ text }) => examStore.subjectEnumFormat(text)
  132. },
  133. {
  134. title: '名称',
  135. dataIndex: 'name',
  136. key: 'name'
  137. },
  138. {
  139. title: '创建时间',
  140. dataIndex: 'createTime',
  141. key: 'createTime',
  142. width: 160
  143. },
  144. {
  145. title: '操作',
  146. key: 'action',
  147. width: 160,
  148. align: 'center'
  149. }
  150. ]
  151. // 计算属性
  152. const levelEnum = computed(() => examStore.getLevelEnum)
  153. // 方法
  154. const submitForm = () => {
  155. queryParam.pageIndex = 1
  156. search()
  157. }
  158. const search = async () => {
  159. listLoading.value = true
  160. try {
  161. const response = await examPaperApi.pageList(queryParam)
  162. if (response) {
  163. const data = response
  164. tableData.value = data.records || []
  165. total.value = data.total || 0
  166. queryParam.pageIndex = data.current || 1
  167. } else {
  168. message.error(response.message || '获取数据失败')
  169. }
  170. } catch (error) {
  171. console.error('获取试卷列表失败:', error)
  172. message.error('获取数据失败')
  173. } finally {
  174. listLoading.value = false
  175. }
  176. }
  177. const deletePaper = async (row) => {
  178. try {
  179. // 显示确认对话框
  180. const confirmed = await new Promise((resolve) => {
  181. Modal.confirm({
  182. title: '确认删除',
  183. content: `确定要删除试卷"${row.name}"吗?`,
  184. okText: '确定',
  185. cancelText: '取消',
  186. onOk: () => resolve(true),
  187. onCancel: () => resolve(false)
  188. })
  189. })
  190. if (!confirmed) return
  191. await examPaperApi.deletePaper(row.id)
  192. search()
  193. } catch (error) {
  194. console.error('删除试卷失败:', error)
  195. message.error('删除失败')
  196. }
  197. }
  198. const levelChange = () => {
  199. queryParam.subjectId = null
  200. subjectFilter.value = examStore.subjects.filter((data) => data.level === queryParam.level)
  201. }
  202. const handlePageChange = (page, pageSize) => {
  203. queryParam.pageIndex = page
  204. queryParam.pageSize = pageSize
  205. search()
  206. }
  207. const handlePageSizeChange = (current, size) => {
  208. queryParam.pageIndex = 1
  209. queryParam.pageSize = size
  210. search()
  211. }
  212. // 生命周期
  213. onMounted(async () => {
  214. examStore.initSubject(search)
  215. })
  216. </script>
  217. <style lang="less" scoped>
  218. .app-container {
  219. padding: 24px;
  220. background: #fff;
  221. .search-form {
  222. margin-bottom: 24px;
  223. padding: 24px;
  224. background: #fafafa;
  225. border-radius: 6px;
  226. }
  227. .data-table {
  228. margin-bottom: 24px;
  229. }
  230. .pagination {
  231. text-align: right;
  232. }
  233. }
  234. </style>