fix(ui): 修复登录和并发请求并统一页面展示
调整登录流程和请求处理,统一各中心的表格交互、主题样式和页面展示。
This commit is contained in:
+24
-32
@@ -19,6 +19,7 @@ For commercial licensing, please contact support@quantumnous.com
|
||||
|
||||
import {
|
||||
getUserIdFromLocalStorage,
|
||||
redirectToExpiredLogin,
|
||||
showError,
|
||||
formatMessageForAPI,
|
||||
isValidMessage,
|
||||
@@ -29,6 +30,7 @@ import i18next from 'i18next';
|
||||
import { MESSAGE_ROLES } from '../constants/playground.constants';
|
||||
import { getServerURL } from './config';
|
||||
import { buildGetRequestKey, getHeaderValue } from './requestDedupe';
|
||||
import { installRequestLifecycle } from './requestLifecycle';
|
||||
|
||||
function createAPIInstance() {
|
||||
const serverURL = getServerURL();
|
||||
@@ -41,14 +43,20 @@ function createAPIInstance() {
|
||||
});
|
||||
}
|
||||
|
||||
export let API = createAPIInstance();
|
||||
export let API;
|
||||
let retireAPI;
|
||||
|
||||
function patchAPIInstance(instance) {
|
||||
const originalGet = instance.get.bind(instance);
|
||||
const inFlightGetRequests = new Map();
|
||||
|
||||
instance.get = (url, config = {}) => {
|
||||
if (config?.disableDuplicate) {
|
||||
if (
|
||||
config?.disableDuplicate ||
|
||||
config?.signal ||
|
||||
config?.cancelToken ||
|
||||
config?.requestGroup
|
||||
) {
|
||||
return originalGet(url, config);
|
||||
}
|
||||
|
||||
@@ -56,12 +64,13 @@ function patchAPIInstance(instance) {
|
||||
url.startsWith('/api/org/') && typeof localStorage !== 'undefined'
|
||||
? localStorage.getItem('current_org_id') || ''
|
||||
: '';
|
||||
const key = buildGetRequestKey(
|
||||
const requestKey = buildGetRequestKey(
|
||||
url,
|
||||
config,
|
||||
fallbackOrgId,
|
||||
getUserIdFromLocalStorage(),
|
||||
);
|
||||
const key = `${requestKey}#error=${!config?.skipErrorHandler}#session=${config?.sessionBound !== false}`;
|
||||
if (inFlightGetRequests.has(key)) {
|
||||
return inFlightGetRequests.get(key);
|
||||
}
|
||||
@@ -75,23 +84,6 @@ function patchAPIInstance(instance) {
|
||||
};
|
||||
}
|
||||
|
||||
patchAPIInstance(API);
|
||||
|
||||
// 添加 response interceptor 的函数
|
||||
function addResponseInterceptor(instance) {
|
||||
instance.interceptors.response.use(
|
||||
(response) => response,
|
||||
(error) => {
|
||||
// 如果请求配置中显式要求跳过全局错误处理,则不弹出默认错误提示
|
||||
if (error.config && error.config.skipErrorHandler) {
|
||||
return Promise.reject(error);
|
||||
}
|
||||
showError(error);
|
||||
return Promise.reject(error);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
// 添加 request interceptor:自动为 /api/org/ 请求注入 X-Org-Id 头
|
||||
function addOrgHeaderInterceptor(instance) {
|
||||
instance.interceptors.request.use((config) => {
|
||||
@@ -107,19 +99,21 @@ function addOrgHeaderInterceptor(instance) {
|
||||
});
|
||||
}
|
||||
|
||||
// 初始化时添加 interceptor
|
||||
addResponseInterceptor(API);
|
||||
addOrgHeaderInterceptor(API);
|
||||
|
||||
export function updateAPI() {
|
||||
API = createAPIInstance();
|
||||
|
||||
patchAPIInstance(API);
|
||||
// 重新添加 interceptors
|
||||
addResponseInterceptor(API);
|
||||
addOrgHeaderInterceptor(API);
|
||||
const instance = createAPIInstance();
|
||||
patchAPIInstance(instance);
|
||||
addOrgHeaderInterceptor(instance);
|
||||
const previous = retireAPI;
|
||||
retireAPI = installRequestLifecycle(instance, {
|
||||
onError: (error) => showError(error, { redirectOnUnauthorized: false }),
|
||||
onSessionExpired: redirectToExpiredLogin,
|
||||
});
|
||||
API = instance;
|
||||
previous?.();
|
||||
}
|
||||
|
||||
updateAPI();
|
||||
|
||||
// playground
|
||||
|
||||
// 构建API请求负载
|
||||
@@ -310,8 +304,6 @@ export const processGroupsData = (data, userGroup) => {
|
||||
return groupOptions;
|
||||
};
|
||||
|
||||
// 原来components中的utils.js
|
||||
|
||||
export async function getOAuthState() {
|
||||
let path = '/api/oauth/state';
|
||||
let affCode = localStorage.getItem('aff');
|
||||
|
||||
Reference in New Issue
Block a user