import Axios , { AxiosResponse } from 'axios' ; import { message } from 'antd' ; import createBrowserHistory from 'history/createBrowserHistory' ; import { IRequestRes , IOpts } from '@/utils/request.interface' ; const proxyName = '/share-api' ; const defaultopts = { timeout : 20000 , } ; interface IOAuthData { casLoginUrl ? : string ; appSecurityUrl ? : string ; appRedirectParameter ? : string ; casServiceParameter ? : string ; } Axios . interceptors . response . use ( ( res ) => { const data = res . data ; switch ( data . code ) { case 401 : const { casLoginUrl , appSecurityUrl , appRedirectParameter , casServiceParameter } = data . data ; const encodeUrl = `${ appSecurityUrl }?${ appRedirectParameter }=${ encodeURIComponent ( window . location . origin ) }` ; const loginUrl = `${ casLoginUrl }?${ casServiceParameter }=${ encodeURIComponent ( encodeUrl ) }` ; window . location . href = loginUrl ; return res ; break ; case 403 : createBrowserHistory ( ) . push ( '/noauthorize' ) ; return res ; break ; case 441 : window . location . reload ( ) ; break ; } return res ; } ) ; const Request = < T > ( opts , code ? ) : Promise < T > => { opts . method = ( opts . method || 'GET' ) . toLocaleUpperCase ( ) ; if ( ! opts . notBasePath ) { opts . url = proxyName + opts . url ; } opts . whitecode = code || null ; opts = { ... defaultopts , ... opts } ; if ( opts . method !== 'GET' ) { opts . data = opts . params ; delete opts . params ; } return new Promise ( ( resolve , reject ) => { Axios ( opts ) . then ( ( res ) => { const allData = JSON . parse ( JSON . stringify ( res . data ) ) as T ; resolve ( allData ) ; } ) . catch ( ( err ) => { message . error ( '网络繁忙，请稍后再试！' ) ; reject ( err ) ; } ) ; } ) ; } ; export default Request ;