1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- import { CombineService, Mutate } from '@ahooksjs/use-request/lib/types'
- import e from '@umijs/deps/compiled/express'
- import { message } from 'antd'
- import moment from 'moment'
- import { useRequest } from 'umi'
- interface Options {
- manual?: boolean,
- pollingInterval?: number,
- pollingWhenHidden?: boolean,
- debounceInterval?: number,
- throttleInterval?: number,
- cacheKey?: string,
- onSuccess?: (data: any, params: any[]) => void,
- onError?: (error: Error, params: any[]) => void,
- formatResult?: boolean,
- initialData?: any,
- msgNmae?: string,
- }
- export interface FnAjax<T> {
- loading: boolean,
- data: T,
- run: (...args: any) => Promise<any>,
- refresh: () => Promise<any>,
- mutate: Mutate<any>
- }
- export function useAjax(fnc: CombineService<any, any>, options?: Options) {
- const ajax = useRequest(fnc, {
- manual: options?.manual || true,
- cacheKey: options?.cacheKey,
- pollingInterval: options?.pollingInterval || undefined,
- debounceInterval: options?.debounceInterval || 0,
- throttleInterval: options?.throttleInterval || 0,
- formatResult: (res) => {
- let reqTime = moment().format('YYYY-MM-DD HH:mm:ss')
- res['reqTime'] = reqTime
- if(res.data){
- return options?.formatResult ? { ...res, reqTime } : res.data
- }else{
- console.log('res===>',res)
- return res
- }
- },
- onSuccess: (res) => {
- if (res) {
- options?.msgNmae && message.success(options?.msgNmae + '成功!', 2)
- }
- return null
- },
- onError: (err) => {
- }
- })
- return ajax
- }
|