diff --git a/src/hooks/modules/usePagination.ts b/src/hooks/modules/usePagination.ts index c69d022..6661445 100644 --- a/src/hooks/modules/usePagination.ts +++ b/src/hooks/modules/usePagination.ts @@ -3,11 +3,12 @@ import { useBreakpoint } from '@/hooks' type Callback = () => void -type Options = { +export type Options = { defaultPageSize: number + defaultSizeOptions: number[] } -export function usePagination(callback: Callback, options: Options = { defaultPageSize: 10 }) { +export function usePagination(callback: Callback, options: Options = { defaultPageSize: 10, defaultSizeOptions: [10, 20, 30, 40, 50] }) { const { breakpoint } = useBreakpoint() const pagination = reactive({ @@ -15,6 +16,7 @@ export function usePagination(callback: Callback, options: Options = { defaultPa showTotal: true, current: 1, pageSize: options.defaultPageSize, + pageSizeOptions: options.defaultSizeOptions, total: 0, simple: false, onChange: (size: number) => { diff --git a/src/hooks/modules/useTable.ts b/src/hooks/modules/useTable.ts index f631c79..d3b48cf 100644 --- a/src/hooks/modules/useTable.ts +++ b/src/hooks/modules/useTable.ts @@ -1,5 +1,6 @@ import type { TableData, TableInstance } from '@arco-design/web-vue' import { Message, Modal } from '@arco-design/web-vue' +import type { Options as paginationOptions } from './usePagination' import { usePagination } from '@/hooks' interface Options { @@ -7,6 +8,7 @@ interface Options { onSuccess?: () => void immediate?: boolean rowKey?: keyof T + paginationOption: paginationOptions } type PaginationParams = { page: number, size: number } @@ -14,7 +16,7 @@ type Api = (params: PaginationParams) => Promise>> | Prom export function useTable(api: Api, options?: Options) { const { formatResult, onSuccess, immediate, rowKey } = options || {} - const { pagination, setTotal } = usePagination(() => getTableData()) + const { pagination, setTotal } = usePagination(() => getTableData(), options?.paginationOption) const loading = ref(false) const tableData = ref([]) diff --git a/src/views/system/file/main/FileMain/index.vue b/src/views/system/file/main/FileMain/index.vue index 267764e..9468838 100644 --- a/src/views/system/file/main/FileMain/index.vue +++ b/src/views/system/file/main/FileMain/index.vue @@ -17,13 +17,7 @@ - + @@ -104,6 +84,7 @@ import { } from '../../components/index' import FileGrid from './FileGrid.vue' import useFileManage from './useFileManage' +import { useTable } from '@/hooks' import { type FileItem, type FilePageQuery, type FileQuery, deleteFile, listFile, uploadFile } from '@/apis' import { ImageTypes } from '@/constant/file' import 'viewerjs/dist/viewer.css' @@ -118,48 +99,17 @@ const queryForm = reactive({ type: route.query.type?.toString() || undefined, sort: ['updateTime,desc'] }) -const pagination = reactive({ - page: 1, - size: 30 +const paginationOption = reactive({ + defaultPageSize: 30, + defaultSizeOptions: [30, 40, 50, 100, 120] }) - -const fileList = ref([]) const isBatchMode = ref(false) -const loading = ref(false) -const timer = ref() -// 查询文件列表 -const getFileList = async (query: FilePageQuery = { ...queryForm, page: pagination.page, size: pagination.size }) => { - try { - loading.value = true - isBatchMode.value = false - query.type = query.type === '0' ? undefined : query.type - const res = await listFile(query) - if (res.data.list) { - if (query.page === 1) { - fileList.value = res.data.list - timer.value = setTimeout(() => { - clearTimeout(timer.value) - timer.value = null - }, 1000) - } else { - fileList.value = [...fileList.value, ...res.data.list] - } - } else { - --pagination.page - } - } catch (error) { - --pagination.page - return error - } finally { - loading.value = false - } -} - -// 查询 -const search = () => { - pagination.page = 1 - getFileList() -} +const { + tableData: fileList, + loading, + pagination, + search +} = useTable((page) => listFile({ ...queryForm, ...page }), { immediate: false, paginationOption }) // 点击文件 const handleClickFile = (item: FileItem) => { @@ -236,20 +186,20 @@ const handleMulDelete = () => { // 上传 const handleUpload = (options: RequestOption) => { const controller = new AbortController() - ;(async function requestWrap() { - const { onProgress, onError, onSuccess, fileItem, name = 'file' } = options - onProgress(20) - const formData = new FormData() - formData.append(name as string, fileItem.file as Blob) - try { - const res = await uploadFile(formData) - Message.success('上传成功') - onSuccess(res) - search() - } catch (error) { - onError(error) - } - })() + ; (async function requestWrap() { + const { onProgress, onError, onSuccess, fileItem, name = 'file' } = options + onProgress(20) + const formData = new FormData() + formData.append(name as string, fileItem.file as Blob) + try { + const res = await uploadFile(formData) + Message.success('上传成功') + onSuccess(res) + search() + } catch (error) { + onError(error) + } + })() return { abort() { controller.abort() @@ -257,30 +207,6 @@ const handleUpload = (options: RequestOption) => { } } -const handleScroll = (event) => { - const { clientHeight, scrollHeight, scrollTop } = event.target - const scrollLinerHeight = (clientHeight / scrollHeight) * scrollHeight - if (!timer.value) { - timer.value = setTimeout(() => { - if ((scrollTop + scrollLinerHeight) / scrollHeight >= 0.9 && !loading.value) { - ++pagination.page - getFileList() - } - timer.value = null - }, 1000) - } -} - -onMounted(() => { - const fileMainDom = document.getElementById('fileMain') - fileMainDom.addEventListener('scrool', handleScroll) -}) -onUnmounted(() => { - // 移除事件监听 - const fileMainDom = document.getElementById('fileMain') - fileMainDom.removeEventListener('scroll', handleScroll) -}) - onBeforeRouteUpdate((to) => { if (!to.query.type) return queryForm.type = to.query.type?.toString() @@ -315,5 +241,13 @@ onMounted(() => { display: flex; flex-direction: column; } + + .pagination { + margin: 10px 0; + + :deep(.arco-pagination) { + justify-content: end; + } + } }