index.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. import { parseTime } from '@/utils/exam'
  85. const examStore = useExamStore()
  86. // 响应式数据
  87. const queryParam = reactive({
  88. id: null,
  89. // level: null,
  90. // subjectId: null,
  91. current: 1,
  92. size: 10
  93. })
  94. const subjectFilter = ref([])
  95. const listLoading = ref(true)
  96. const tableData = ref([])
  97. const total = ref(0)
  98. // Drawer 控制
  99. const drawerVisible = ref(false)
  100. const drawerTitle = ref('')
  101. const editId = ref(null)
  102. function openDrawer(type, id = null) {
  103. if (type === 'add') {
  104. drawerTitle.value = '添加试卷'
  105. editId.value = null
  106. } else {
  107. drawerTitle.value = '编辑试卷'
  108. editId.value = id
  109. }
  110. drawerVisible.value = true
  111. }
  112. function closeDrawer() {
  113. drawerVisible.value = false
  114. }
  115. function onEditSuccess() {
  116. closeDrawer()
  117. search()
  118. }
  119. // 表格列配置
  120. const columns = [
  121. {
  122. title: 'Id',
  123. dataIndex: 'id',
  124. key: 'id',
  125. width: 90
  126. },
  127. // {
  128. // title: '学科',
  129. // dataIndex: 'subjectId',
  130. // key: 'subjectId',
  131. // width: 200,
  132. // customRender: ({ text }) => examStore.subjectEnumFormat(text)
  133. // },
  134. {
  135. title: '名称',
  136. dataIndex: 'name',
  137. key: 'name'
  138. },
  139. {
  140. title: '创建时间',
  141. dataIndex: 'createTime',
  142. key: 'createTime',
  143. width: 200,
  144. customRender: ({ text }) => parseTime(text, '{y}-{m}-{d} {h}:{i}:{s}')
  145. },
  146. {
  147. title: '操作',
  148. key: 'action',
  149. width: 160,
  150. align: 'center'
  151. }
  152. ]
  153. // 计算属性
  154. const levelEnum = computed(() => examStore.getLevelEnum)
  155. // 方法
  156. const submitForm = () => {
  157. queryParam.pageIndex = 1
  158. search()
  159. }
  160. const search = async () => {
  161. listLoading.value = true
  162. try {
  163. const response = await examPaperApi.pageList(queryParam)
  164. if (response) {
  165. const data = response
  166. tableData.value = data.records || []
  167. total.value = data.total || 0
  168. queryParam.pageIndex = data.current || 1
  169. } else {
  170. message.error(response.message || '获取数据失败')
  171. }
  172. } catch (error) {
  173. console.error('获取试卷列表失败:', error)
  174. message.error('获取数据失败')
  175. } finally {
  176. listLoading.value = false
  177. }
  178. }
  179. const deletePaper = async (row) => {
  180. try {
  181. // 显示确认对话框
  182. const confirmed = await new Promise((resolve) => {
  183. Modal.confirm({
  184. title: '确认删除',
  185. content: `确定要删除试卷"${row.name}"吗?`,
  186. okText: '确定',
  187. cancelText: '取消',
  188. onOk: () => resolve(true),
  189. onCancel: () => resolve(false)
  190. })
  191. })
  192. if (!confirmed) return
  193. await examPaperApi.deletePaper(row.id)
  194. search()
  195. } catch (error) {
  196. console.error('删除试卷失败:', error)
  197. message.error('删除失败')
  198. }
  199. }
  200. const levelChange = () => {
  201. queryParam.subjectId = null
  202. subjectFilter.value = examStore.subjects.filter((data) => data.level === queryParam.level)
  203. }
  204. const handlePageChange = (page, pageSize) => {
  205. queryParam.pageIndex = page
  206. queryParam.pageSize = pageSize
  207. search()
  208. }
  209. const handlePageSizeChange = (current, size) => {
  210. queryParam.pageIndex = 1
  211. queryParam.pageSize = size
  212. search()
  213. }
  214. // 生命周期
  215. onMounted(async () => {
  216. examStore.initSubject(search)
  217. })
  218. </script>
  219. <style lang="less" scoped>
  220. .app-container {
  221. padding: 24px;
  222. background: #fff;
  223. .search-form {
  224. margin-bottom: 24px;
  225. padding: 24px;
  226. background: #fafafa;
  227. border-radius: 6px;
  228. }
  229. .data-table {
  230. margin-bottom: 24px;
  231. }
  232. .pagination {
  233. text-align: right;
  234. }
  235. }
  236. </style>