|
|
@@ -3,7 +3,7 @@ import { FetchTypes, CommonFetchOptions, CommonResult } from '@/services/http/ty
|
|
|
import { useDataTable } from '../datatable'
|
|
|
|
|
|
interface RequestOptions<T extends FetchTypes> {
|
|
|
- params?: {
|
|
|
+ defaultParams?: {
|
|
|
pageNum?: number; // 当前页码
|
|
|
pageSize?: number; // 每页条数
|
|
|
} & T['request'];
|
|
|
@@ -18,12 +18,12 @@ export function useRequest<T extends FetchTypes>(fetcher: (options: CommonFetchO
|
|
|
type RequestType = T['request']
|
|
|
type ResponseType = T['response'] extends (infer U)[] ? U : T['response'] // 如果 U 是数组,取 U 的类型
|
|
|
|
|
|
- const { params, localPagination, manual, onSuccess, onError, onFinally } = options ?? {}
|
|
|
+ const { defaultParams, localPagination, manual, onSuccess, onError, onFinally } = options ?? {}
|
|
|
|
|
|
const { dataList, filters, total, pageIndex, pageSize, pageCount } = useDataTable<ResponseType>({
|
|
|
localPagination,
|
|
|
- pageIndex: params?.pageNum,
|
|
|
- pageSize: params?.pageSize,
|
|
|
+ pageIndex: defaultParams?.pageNum,
|
|
|
+ pageSize: defaultParams?.pageSize,
|
|
|
})
|
|
|
|
|
|
const data = ref<ResponseType>()
|
|
|
@@ -31,15 +31,15 @@ export function useRequest<T extends FetchTypes>(fetcher: (options: CommonFetchO
|
|
|
const controller = new AbortController()
|
|
|
|
|
|
// 异步函数
|
|
|
- const runAsync = async (payload: Partial<RequestType> = {}) => {
|
|
|
- if (params && params.pageSize) {
|
|
|
- params.pageNum = pageIndex.value
|
|
|
- params.pageSize = pageSize.value
|
|
|
+ const runAsync = async (params: Partial<RequestType> = {}) => {
|
|
|
+ if (defaultParams && defaultParams.pageSize) {
|
|
|
+ defaultParams.pageNum = pageIndex.value
|
|
|
+ defaultParams.pageSize = pageSize.value
|
|
|
}
|
|
|
|
|
|
const res = await fetcher({
|
|
|
controller,
|
|
|
- data: typeof params === 'object' ? { ...(params as object), ...payload } : payload
|
|
|
+ data: typeof defaultParams === 'object' ? { ...(defaultParams as object), ...params } : params
|
|
|
})
|
|
|
|
|
|
total.value = res.total
|
|
|
@@ -54,9 +54,9 @@ export function useRequest<T extends FetchTypes>(fetcher: (options: CommonFetchO
|
|
|
}
|
|
|
|
|
|
// 同步函数
|
|
|
- const run = (payload: Partial<RequestType> = {}) => {
|
|
|
+ const run = (params: Partial<RequestType> = {}) => {
|
|
|
loading.value = true
|
|
|
- runAsync(payload).then((res) => {
|
|
|
+ runAsync(params).then((res) => {
|
|
|
onSuccess && onSuccess(res)
|
|
|
}).catch((err) => {
|
|
|
onError && onError(err)
|