import { createContext , ReactNode } from 'react' ; import Axios , { AxiosInstance , AxiosTransformer } from 'axios' ; import { notification } from 'antd' ; import { useContext } from 'react' ; import { createBrowserHistory } from 'history' ; import { useQuery , useMutation , useQueryClient } from 'react-query' ; import qs from 'qs' ; import { result } from 'cypress/types/lodash' ; const history = createBrowserHistory ( ) ; console . log ( 'baseurl:' , import . meta . env . VITE_BASE_URL ) ; const axios = Axios . create ( { baseURL : import . meta . env . VITE_BASE_URL + '' , timeout : 1000 , headers : { 'Content-Type' : 'application/json' , } , } ) ; axios . interceptors . request . use ( ( config ) => { const token = localStorage . getItem ( 'token' ) ; if ( token ) { config . headers . Authorization = `Bearer ${ token }` ; } return config ; } ) ; axios . interceptors . response . use ( ( response ) => { const data = response . data ; console . log ( 'response:' , response ) ; if ( response . status === 200 ) { return data ; } notification . error ( { message : `请求错误 ${ response . statusText }: ${ response }` , description : data || response . statusText || 'Error' , } ) ; if ( response . status === 401 ) { history . push ( '/auth/login' ) ; } return Promise . reject ( new Error ( response . statusText || 'Error' ) ) ; } , ( error ) => { console . log ( 'err:' , error , error . response ) ; let msg = "请求错误" ; if ( error . response && error . response . status ) { switch ( error . response . status ) { case 401 : history . push ( '/auth/login' ) ; break ; case 403 : history . push ( '/auth/login' ) ; break ; case 404 : notification . error ( { message : `请求不存在` , description : error . response . data ?. msg || 'Error' , } ) ; break ; case 406 : notification . error ( { message : `请求参数有误` , description : error . response . data ?. msg || 'Error' , } ) ; break ; default : notification . error ( { message : `请求错误` , description : error . response . data ?. msg || 'Error' , } ) ; } } return Promise . reject ( error ) ; } ) ; export const AxiosContext = createContext < AxiosInstance > ( new Proxy ( axios , { apply : ( ) => { throw new Error ( 'You must wrap your component in an AxiosProvider' ) ; } , get : ( ) => { throw new Error ( 'You must wrap your component in an AxiosProvider' ) ; } , } ) ) ; export const useAxios = ( ) => { return useContext ( AxiosContext ) ; } ; const transformPagination = ( pagination ) => { if ( ! pagination ) return ; const current = pagination . current ? pagination . current : pagination . defaultCurrent ; const pageSize = pagination . pageSize ? pagination . pageSize : pagination . defaultPageSize ; let offset = 0 ; if ( current && pageSize ) { offset = ( current - 1 ) * pageSize ; } return { offset , limit : pageSize , } ; } ; const transformFilters = ( filters ) => { if ( ! filters ) return ; let result = [ ] ; for ( let key in filters ) { if ( ! filters [ key ] || filters [ key ] === null ) continue ; result = [ ... result , [ key + ':eq:' + filters [ key ] ] ] ; } return result ; } ; const transformSorter = ( sorter ) => { if ( ! sorter ) return ; let result = '' ; if ( sorter . field && sorter . order ) { let order = 'desc' ; if ( sorter . order === 'ascend' ) order = 'asc' ; result = sorter . field + ' ' + order ; ; } return result ; } ; type listParams = { limit ? : number ; offset ? : number ; filter ? : string [ ] ; order ? : string ; } ; const useGetList = < T > ( key , url , pagination ? , filters ? , sorter ? ) => { const axios = useAxios ( ) ; const service = async ( ) => { let params = { } ; params = { ... transformPagination ( pagination ) } ; params . filter = transformFilters ( filters ) ; params . order = transformSorter ( sorter ) ; const transformRequest = ( data , headers ) => { } ; console . log ( 'params: ' , params ) ; const data = await axios . get ( `${ url }` , { params , paramsSerializer : params => { return qs . stringify ( params , { arrayFormat : 'repeat' } ) ; } , transformRequest , } ) ; return data ; } ; return useQuery ( key , ( ) => service ( ) ) ; } ; const useGetOne = < T > ( key , url , params ? ) => { const axios = useAxios ( ) ; const service = async ( ) => { const data = await axios . get ( `${ url }` , params ) ; return data ; } ; return useQuery ( key , ( ) => service ( ) ) ; } ; const useCreate = < T , U > ( url ) => { const axios = useAxios ( ) ; const queryClient = useQueryClient ( ) ; return useMutation ( async ( params ) => { const data = await axios . post ( `${ url }` , params ) ; return data ; } ) ; } ; const useUpdate = < T > ( url ) => { const axios = useAxios ( ) ; const queryClient = useQueryClient ( ) ; return useMutation ( async ( item ) => { const data = await axios . patch ( `${ url }` , item ) ; return data ; } ) ; } ; const useDelete = < T > ( url ) => { const axios = useAxios ( ) ; const queryClient = useQueryClient ( ) ; return useMutation ( async ( id ) => { const data = await axios . delete ( `${ url }?id=${ id }` ) ; return data ; } ) ; } ; const useBatch = ( url ) => { const axios = useAxios ( ) ; const queryClient = useQueryClient ( ) ; return useMutation ( async ( ids ) => { const data = await axios . post ( `${ url }` , { idList : ids } ) ; return data ; } ) ; } ; export { useGetOne , useGetList , useUpdate , useCreate , useDelete , useBatch , } ; export default axios ;