import axios , { AxiosRequestConfig , AxiosError , AxiosResponse } from 'axios' ; import { message , Modal } from 'antd' ; import AdminConfig from '../config' ; import { getToken } from '../utils/cookie' ; import store from '../store/index' ; import { logout } from '../store/module/user' ; import { clearSideBarRoutes } from '../store/module/app' ; interface ResponseData < T > { code : number ; data : T ; msg : string ; } axios . defaults . headers = { 'Content-Type' : 'application/json;charset=utf-8' , } ; axios . defaults . baseURL = process . env . NODE_ENV === 'production' ? AdminConfig . API_URL : '' ; axios . interceptors . request . use ( ( config ) => { const token = getToken ( ) ; if ( token ) { config . headers . token = token ; } return config ; } , ( error ) => Promise . reject ( error ) ) ; axios . interceptors . response . use ( ( response ) => { if ( ! response . data ) { return Promise . resolve ( response ) ; } if ( response . data . code === AdminConfig . LOGIN_EXPIRE ) { Modal . confirm ( { title : '系统提示' , content : response . data . msg , okText : '重新登录' , onOk ( ) { store . dispatch ( clearSideBarRoutes ( ) ) ; store . dispatch ( logout ( ) ) ; window . location . href = `${ window . location . origin }/react-ant-admin/system/login?redirectURL=${ encodeURIComponent ( window . location . href ) }` ; } , onCancel ( ) { } , } ) ; return Promise . reject ( new Error ( response . data . msg ) ) ; } if ( response . data . code === AdminConfig . SUCCESS_CODE ) { return response . data as any ; } message . error ( response . data . msg ) ; return Promise . reject ( new Error ( response . data . msg ) ) ; } , ( error ) => { message . error ( error . message ) ; return Promise . reject ( error ) ; } ) ; export function request < T > ( options ) { return axios . request < T > ( options ) ; }