import axios , { AxiosInstance , AxiosError , AxiosRequestConfig , AxiosResponse , Method } from 'axios' ; import https from 'https' ; import { message , Modal } from 'antd' ; import { signOut } from '@/utils/app' ; const DEFAULT_ERROR_MSG = '服务器繁忙，请稍后再试~' ; class Axios { config : AxiosRequestConfig ; axios : AxiosInstance ; constructor ( config ) { this . config = config ; this . axios = this . createAxios ( config ) ; } createAxios ( config ) { const { timeout = 30000 , ... requestData } = config ; const service = axios . create ( { timeout , httpsAgent : new https . Agent ( { rejectUnauthorized : false } ) , ... requestData } ) ; service . interceptors . request . use ( ( config ) => config , ( error ) => Promise . reject ( error ) ) ; service . interceptors . response . use ( ( response ) => response , ( error ) => Promise . reject ( error ) ) ; return service ; } request < T extends ResponseData > ( method , url , data , config ) { const q = method === 'GET' ? 'params' : 'data' ; return this . axios ( { method , url , [ q ] : data , headers : { 'Content-Type' : 'application/json' , ... config } } ) . then ( this . checkStatus ) . then ( this . checkCode ) . catch ( this . onError ) ; } checkStatus < T > ( res ) { if ( res . status === 200 || res . status === 304 ) { return res . data ; } throw DEFAULT_ERROR_MSG ; } checkCode < T extends ResponseData > ( res ) { if ( res . code !== 0 ) { if ( res . message ) { message . error ( res . message ) ; throw res . message ; } } return res ; } onError ( error ) { if ( error . response ?. status === 401 ) { Modal . warning ( { title : '退出登录' , content : 'token过期' , okText : '重新登录' , onOk : ( ) => { signOut ( ) ; } } ) ; return ; } else { throw error ; } } get < T extends ResponseData > ( url , data ? , config = { } ) { return this . request < T > ( 'GET' , url , data , config ) ; } post < T extends ResponseData > ( url , data ? , config = { } ) { return this . request < T > ( 'POST' , url , data , config ) ; } put < T extends ResponseData > ( url , data ? , config = { } ) { return this . request < T > ( 'PUT' , url , data , config ) ; } delete < T extends ResponseData > ( url , data ? , config = { } ) { return this . request < T > ( 'DELETE' , url , data , config ) ; } patch < T extends ResponseData > ( url , data ? , config = { } ) { return this . request < T > ( 'PATCH' , url , data , config ) ; } } export default Axios ;