Browse Source

fix: 请求添加skipLoading配置,禁止弹出loading

pull/132/head
cp3hnu 1 year ago
parent
commit
aa9ae2c304
2 changed files with 34 additions and 15 deletions
  1. +31
    -13
      react-ui/src/requestConfig.ts
  2. +3
    -2
      react-ui/src/services/experiment/index.js

+ 31
- 13
react-ui/src/requestConfig.ts View File

@@ -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);
},
],


+ 3
- 2
react-ui/src/services/experiment/index.js View File

@@ -103,7 +103,7 @@ export function putExperiment(data) {
});
}

// 启动tensorBoard
// 启动 tensorBoard
export function runTensorBoardReq(data) {
return request(`/api/mmp/tensorBoard/run`, {
method: 'POST',
@@ -111,11 +111,12 @@ export function runTensorBoardReq(data) {
});
}

// 启动tensorBoard
// 获取 tensorBoard 状态
export function getTensorBoardStatusReq(data) {
return request(`/api/mmp/tensorBoard/getStatus`, {
method: 'POST',
data,
skipLoading: true,
});
}



Loading…
Cancel
Save