import React from 'react' ; import axios , { Method } from 'axios' ; import { message as antdMessage , notification } from 'antd' ; import Config from '@/config' ; import ErrorCode from '@/config/error-code' ; import { commitMutation } from '@/store' ; import { loginOut } from '@/store/mutations/app.mutations' ; import { getToken , saveAccessToken } from '@/lin/utils/token' ; const config = { baseURL : Config . baseURL || process . env . apiUrl || '' , timeout : 10 * 1000 , crossDomain : true , validateStatus ( status ) { return status >= 200 && status < 500 ; } } ; const _axios = axios . create ( config ) ; _axios . interceptors . request . use ( originConfig => { const reqConfig = { ... originConfig } ; if ( ! reqConfig . url ) { console . error ( 'request need url' ) ; throw new Error ( { source : 'axiosInterceptors' , message : 'request need url' } as any ) ; } if ( ! reqConfig . method ) { reqConfig . method = 'get' ; } reqConfig . method = reqConfig . method . toLowerCase ( ) as Method ; if ( reqConfig . method === 'get' ) { if ( ! reqConfig . params ) { reqConfig . params = reqConfig . data || { } ; } } else if ( reqConfig . method === 'post' ) { if ( ! reqConfig . data ) { reqConfig . data = reqConfig . params || { } ; } let hasFile = false ; Object . keys ( reqConfig . data ) . forEach ( key => { if ( typeof reqConfig . data [ key ] === 'object' ) { const item = reqConfig . data [ key ] ; if ( item instanceof FileList || item instanceof File || item instanceof Blob ) { hasFile = true ; } } } ) ; if ( hasFile ) { const formData = new FormData ( ) ; Object . keys ( reqConfig . data ) . forEach ( key => { formData . append ( key , reqConfig . data [ key ] ) ; } ) ; reqConfig . data = formData ; } } else { console . warn ( `其他请求类型: ${ reqConfig . method }, 暂无自动处理` ) ; } if ( reqConfig . url === 'cms/user/refresh' ) { const refreshToken = getToken ( 'refresh_token' ) ; if ( refreshToken ) { reqConfig . headers . Authorization = refreshToken ; } } else { const accessToken = getToken ( 'access_token' ) ; if ( accessToken ) { reqConfig . headers . Authorization = accessToken ; } } return reqConfig ; } , ( error ) => { Promise . reject ( error ) ; } ) ; _axios . interceptors . response . use ( async ( res ) => { let { code , message , } = res . data ; if ( res . status . toString ( ) . charAt ( 0 ) === '2' ) { return res . data ; } return new Promise ( async ( resolve , reject ) => { const { url , handleError , showBackend } = res . config as any ; if ( code === 10000 || code === 10100 ) { setTimeout ( ( ) => { commitMutation ( loginOut ( ) ) ; const { origin } = window . location ; window . location . href = origin ; } , 1500 ) ; return resolve ( null ) ; } if ( code === 10040 || code === 10041 || code === 10050 || code === 10051 ) { const cache = { } as any ; if ( cache . url !== url ) { cache . url = url ; const refreshResult = await _axios ( 'cms/user/refresh' ) ; saveAccessToken ( ( refreshResult as any ) . access_token ) ; const result = await _axios ( res . config ) ; return resolve ( result ) ; } } if ( handleError ) { return reject ( res ) ; } console . log ( 'message' , message ) ; if ( Config . useFrontEndErrorMsg && ! showBackend ) { const errorArr = Object . entries ( ErrorCode ) . filter ( v => v [ 0 ] === code . toString ( ) ) ; if ( errorArr . length > 0 && errorArr [ 0 ] [ 1 ] !== '' ) { message = errorArr [ 0 ] [ 1 ] ; } else { message = ErrorCode [ '777' ] ; } } antdMessage . error ( message ) ; reject ( ) ; } ) ; } , ( error ) => { if ( ! error . response ) { notification . error ( { message : 'Network Error' , description : React . createElement ( 'strong' , { className : 'my-notify' } , '请检查 API 是否异常' ) } ) ; console . log ( 'error' , error ) ; } if ( error . code === 'ECONNABORTED' && error . message . indexOf ( 'timeout' ) !== - 1 ) { antdMessage . warning ( '请求超时' ) ; } return Promise . reject ( error ) ; } ) ; param { string } url param { object } data param { object } params export function post ( url , data = { } , params = { } ) { return _axios ( { method : 'post' , url , data , params } ) ; } param { string } url param { object } params export function get ( url , params = { } ) { return _axios ( { method : 'get' , url , params } ) ; } param { string } url param { object } data param { object } params export function put ( url , data = { } , params = { } ) { return _axios ( { method : 'put' , url , params , data } ) ; } param { string } url param { object } params export function _delete ( url , params = { } ) { return _axios ( { method : 'delete' , url , params } ) ; } export default _axios ;