|
|
|
@@ -3,7 +3,14 @@ |
|
|
|
* @Date: 2024-03-25 13:52:54 |
|
|
|
* @Description: 网络请求配置,详情请参考 https://umijs.org/docs/max/request |
|
|
|
*/ |
|
|
|
import type { AxiosRequestConfig, AxiosResponse, RequestConfig, RequestOptions } from '@umijs/max'; |
|
|
|
import type { |
|
|
|
AxiosError, |
|
|
|
AxiosRequestConfig, |
|
|
|
AxiosResponse, |
|
|
|
RequestConfig, |
|
|
|
RequestError, |
|
|
|
RequestOptions, |
|
|
|
} from '@umijs/max'; |
|
|
|
import { message } from 'antd'; |
|
|
|
import { clearSessionToken, getAccessToken } from './access'; |
|
|
|
import { setRemoteMenu } from './services/session'; |
|
|
|
@@ -12,13 +19,12 @@ import { gotoLoginPage } from './utils/ui'; |
|
|
|
|
|
|
|
// [antd: Notification] You are calling notice in render which will break in React 18 concurrent mode. Please trigger in effect instead. |
|
|
|
const popupError = (error: string, skipErrorHandler: boolean | undefined = false) => { |
|
|
|
if (skipErrorHandler) { |
|
|
|
return; |
|
|
|
if (!skipErrorHandler) { |
|
|
|
// 直接调用 message.error 有时候不弹出来 |
|
|
|
setTimeout(() => { |
|
|
|
message.error(error); |
|
|
|
}, 100); |
|
|
|
} |
|
|
|
// 直接调用 message.error 有时候不弹出来 |
|
|
|
setTimeout(() => { |
|
|
|
message.error(error); |
|
|
|
}, 100); |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
@@ -32,22 +38,29 @@ export const requestConfig: RequestConfig = { |
|
|
|
const headers = options.headers ?? {}; |
|
|
|
const authHeader = headers['Authorization']; |
|
|
|
const isToken = headers['isToken']; |
|
|
|
const skipLoading = (options as RequestOptions)?.skipLoading; |
|
|
|
if (!authHeader && isToken !== false) { |
|
|
|
const accessToken = getAccessToken(); |
|
|
|
if (accessToken) { |
|
|
|
headers['Authorization'] = `Bearer ${accessToken}`; |
|
|
|
} |
|
|
|
} |
|
|
|
Loading.show(); |
|
|
|
if (!skipLoading) { |
|
|
|
Loading.show(); |
|
|
|
} |
|
|
|
return { url, options }; |
|
|
|
}, |
|
|
|
], |
|
|
|
responseInterceptors: [ |
|
|
|
[ |
|
|
|
(response: AxiosResponse) => { |
|
|
|
Loading.hide(); |
|
|
|
const { status, data, config } = response || {}; |
|
|
|
const skipErrorHandler = (config as RequestOptions)?.skipErrorHandler; |
|
|
|
const options = config as RequestOptions; |
|
|
|
const skipErrorHandler = options?.skipErrorHandler; |
|
|
|
const skipLoading = options?.skipLoading; |
|
|
|
if (!skipLoading) { |
|
|
|
Loading.hide(); |
|
|
|
} |
|
|
|
if (status >= 200 && status < 300) { |
|
|
|
if (data && (data instanceof Blob || data.code === 200)) { |
|
|
|
return response; |
|
|
|
@@ -66,9 +79,14 @@ export const requestConfig: RequestConfig = { |
|
|
|
return Promise.reject(response); |
|
|
|
} |
|
|
|
}, |
|
|
|
(error: Error) => { |
|
|
|
Loading.hide(); |
|
|
|
popupError(error.message ?? '请求失败'); |
|
|
|
(error: RequestError) => { |
|
|
|
const options = (error as AxiosError).config as RequestOptions; |
|
|
|
const skipErrorHandler = options?.skipErrorHandler; |
|
|
|
const skipLoading = options?.skipLoading; |
|
|
|
if (!skipLoading) { |
|
|
|
Loading.hide(); |
|
|
|
} |
|
|
|
popupError(error.message ?? '请求失败', skipErrorHandler); |
|
|
|
return Promise.reject(error); |
|
|
|
}, |
|
|
|
], |
|
|
|
|