fix(console): 修复看板与作品详情加载并限制只读请求等待
This commit is contained in:
@@ -8,6 +8,16 @@
|
||||
|
||||
**记录规则**:每条写“范围、变更、影响、回滚、关联”五项;回滚优先 `git revert <提交>`,提交含多项改动时按文件还原(`git checkout <提交>^ -- <文件>`),不整仓 reset。本文不作为规则依据。
|
||||
|
||||
## 2026-11-25
|
||||
|
||||
### 修复 · 控制台数据看板持续加载
|
||||
|
||||
- **范围**:控制台 `/console/analytics` 运营看板、创作作品详情抽屉及公共 API 的只读请求。
|
||||
- **变更**:看板首轮查询取消后可重新发起,作品详情的当前副作用接管加载状态。只读请求默认 60 秒超时,运营看板核心请求设置为 30 秒。
|
||||
- **影响**:两处首次加载可正常结束。只读接口持续无响应时返回超时错误,页面可按原有失败流程结束加载。
|
||||
- **回滚**:还原运营看板和作品详情的加载处理,以及公共 API 的只读超时。
|
||||
- **关联**:`src/hooks/analytics/useAnalyticsData.js`、`src/components/creation/WorkFeed/WorkDetailDrawer.jsx`、`src/helpers/api.js`。
|
||||
|
||||
## 2026-11-24
|
||||
|
||||
### 修复 · 控制台会员列表与弹窗
|
||||
|
||||
@@ -121,6 +121,7 @@
|
||||
- `API` 已提供:`New-API-User` 请求头;`/api/org/*` 自动注入 `X-Org-Id`(显式传入优先);并发相同 GET 去重(`disableDuplicate: true` 关闭,带取消信号或请求组的 GET 不共享);HTTP 错误统一提示(`skipErrorHandler: true` 跳过并由页面自行处理)。受会话约束的接口首次返回 401 时取消该会话未完成的只读请求,阻止该会话继续发请求,并仅清理、跳转 `/org/login?expired=true` 一次。公开接口或显式 `sessionBound: false` 的请求不参与会话联动。
|
||||
- 同一批必须全部成功的只读请求,可传入同一个 `requestGroup: new AbortController()`;首次拒绝的 HTTP 4xx/5xx 取消同组剩余读取。普通失败不取消无关请求,正常并发不限流;已发送的写入不由请求组批量取消。调用方仍须处理 Promise 拒绝,并在 `finally` 清理加载状态。
|
||||
- 查询参数用 `params` 对象,不手拼查询字符串(手拼不会编码 `&`、`#`、中文)。
|
||||
- `API` 的 GET、HEAD、OPTIONS 默认在 60 秒后超时,调用方可以通过 `timeout` 覆盖。保持在 `finally` 回收加载状态,并让过期请求与当前请求的状态互不干扰。
|
||||
- 响应按 `{ success, message, data }` 处理,先判断 `success`;具体字段以接口契约为准。
|
||||
- 分页参数 `p` 从 1 开始,`page_size` 默认 `ITEMS_PER_PAGE`(10,须与后端一致);后端对 `page_size > 100` 静默截断为 100,需要全量数据时用 `collectPagedItems(fetchPage)`(`src/helpers/paging.js`)。
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ import { useNavigate } from 'react-router-dom';
|
||||
import { ERROR_ACTIONS } from '../../../helpers/creationError';
|
||||
import { copy, renderQuota, showSuccess } from '../../../helpers';
|
||||
import { useIsMobile } from '../../../hooks/common/useIsMobile';
|
||||
import { useEffectEvent } from '../../../hooks/common/useEffectEvent';
|
||||
import { adapterOf } from '../../../hooks/creation/creationForm';
|
||||
import {
|
||||
elapsedSeconds,
|
||||
@@ -101,24 +102,18 @@ const WorkDetailDrawer = ({
|
||||
}, [taskId]);
|
||||
|
||||
// 打开时拉取一次详情(快照 / 播放地址 / regenerate_supported)
|
||||
const fetchedRef = useRef('');
|
||||
const loadDetail = useEffectEvent(() => fetchDetail(task));
|
||||
useEffect(() => {
|
||||
if (!visible || !task || fetchedRef.current === task.id) return;
|
||||
fetchedRef.current = task.id;
|
||||
if (!visible || !taskId) return;
|
||||
let alive = true;
|
||||
setLoadingDetail(true);
|
||||
fetchDetail(task).finally(() => {
|
||||
loadDetail().finally(() => {
|
||||
if (alive) setLoadingDetail(false);
|
||||
});
|
||||
return () => {
|
||||
alive = false;
|
||||
};
|
||||
// 只按作品 id 触发
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [visible, taskId]);
|
||||
useEffect(() => {
|
||||
if (!visible) fetchedRef.current = '';
|
||||
}, [visible]);
|
||||
}, [visible, taskId, loadDetail]);
|
||||
|
||||
if (!task) return null;
|
||||
|
||||
|
||||
+9
-2
@@ -11,13 +11,18 @@ import i18next from 'i18next';
|
||||
import { MESSAGE_ROLES } from '../constants/playground.constants';
|
||||
import { getServerURL } from './config';
|
||||
import { buildGetRequestKey, getHeaderValue } from './requestDedupe';
|
||||
import { installRequestLifecycle } from './requestLifecycle';
|
||||
import {
|
||||
DEFAULT_READ_TIMEOUT_MS,
|
||||
installRequestLifecycle,
|
||||
} from './requestLifecycle';
|
||||
|
||||
function createAPIInstance() {
|
||||
const serverURL = getServerURL();
|
||||
return axios.create({
|
||||
baseURL: serverURL,
|
||||
withCredentials: serverURL !== '',
|
||||
// Let the lifecycle apply a read default while preserving explicit timeouts.
|
||||
timeout: null,
|
||||
headers: {
|
||||
'Cache-Control': 'no-store',
|
||||
},
|
||||
@@ -51,7 +56,9 @@ function patchAPIInstance(instance) {
|
||||
fallbackOrgId,
|
||||
getUserIdFromLocalStorage(),
|
||||
);
|
||||
const key = `${requestKey}#error=${!config?.skipErrorHandler}#session=${config?.sessionBound !== false}`;
|
||||
const timeout =
|
||||
config?.timeout ?? instance.defaults.timeout ?? DEFAULT_READ_TIMEOUT_MS;
|
||||
const key = `${requestKey}#error=${!config?.skipErrorHandler}#session=${config?.sessionBound !== false}#timeout=${timeout}`;
|
||||
if (inFlightGetRequests.has(key)) {
|
||||
return inFlightGetRequests.get(key);
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ import { CanceledError, isCancel } from 'axios';
|
||||
const PUBLIC_API_PATH =
|
||||
/^\/api\/(?:status|setup|i18n|notice|about|privacy_policy|user_agreement|home_page_content|customer-service|verification|reset_password|oauth|user\/(?:login|register|reset|passkey\/login)|org\/(?:login|register))(?:\/|$)/;
|
||||
const READ_METHODS = new Set(['get', 'head', 'options']);
|
||||
export const DEFAULT_READ_TIMEOUT_MS = 60_000;
|
||||
|
||||
function isSessionBound(config) {
|
||||
const pathname = config?.url?.split(/[?#]/, 1)[0];
|
||||
@@ -40,6 +41,9 @@ export function installRequestLifecycle(
|
||||
instance.interceptors.request.use((config) => {
|
||||
const sessionBound = isSessionBound(config);
|
||||
const read = READ_METHODS.has(config.method);
|
||||
if (read && config.timeout == null) {
|
||||
config.timeout = DEFAULT_READ_TIMEOUT_MS;
|
||||
}
|
||||
const groupSignal = read ? config.requestGroup?.signal : undefined;
|
||||
if ((sessionBound && session.signal.aborted) || groupSignal?.aborted) {
|
||||
throw new CanceledError(undefined, config);
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
import { useState, useCallback, useEffect, useRef } from 'react';
|
||||
import { API, showError } from '../../helpers';
|
||||
import { useEffectEvent } from '../common/useEffectEvent';
|
||||
import { useFilterSearch } from '../common/useFilterSearch';
|
||||
|
||||
const ANALYTICS_TIMEOUT_MS = 30_000;
|
||||
|
||||
/**
|
||||
* 运营看板数据获取Hook
|
||||
*
|
||||
@@ -24,7 +27,11 @@ export const useAnalyticsData = (active = true) => {
|
||||
const [dateRange, setDateRange] = useState([null, null]); // 自定义日期范围 [startDate, endDate]
|
||||
|
||||
const activeRequest = useRef(null);
|
||||
const cancelQuery = useCallback(() => activeRequest.current?.abort(), []);
|
||||
const loadedRef = useRef(false);
|
||||
const cancelQuery = useCallback(() => {
|
||||
activeRequest.current?.abort();
|
||||
activeRequest.current = null;
|
||||
}, []);
|
||||
useEffect(() => cancelQuery, [cancelQuery]);
|
||||
|
||||
// 将 DatePicker 选择的日期转换为 UTC+8 当天零点/末尾的 Unix 时间戳(秒)
|
||||
@@ -74,7 +81,7 @@ export const useAnalyticsData = (active = true) => {
|
||||
try {
|
||||
const res = await API.get(
|
||||
`/api/analytics/overview${force ? '?refresh=true' : ''}`,
|
||||
{ signal },
|
||||
{ signal, timeout: ANALYTICS_TIMEOUT_MS },
|
||||
);
|
||||
if (signal.aborted) return;
|
||||
if (res.data.success) {
|
||||
@@ -96,6 +103,7 @@ export const useAnalyticsData = (active = true) => {
|
||||
const queryParams = buildQueryParams(currentPeriod, currentDateRange);
|
||||
const res = await API.get(`/api/analytics/user-trend?${queryParams}`, {
|
||||
signal,
|
||||
timeout: ANALYTICS_TIMEOUT_MS,
|
||||
});
|
||||
if (signal.aborted) return;
|
||||
if (res.data.success) {
|
||||
@@ -115,7 +123,7 @@ export const useAnalyticsData = (active = true) => {
|
||||
const queryParams = buildQueryParams(currentPeriod, currentDateRange);
|
||||
const res = await API.get(
|
||||
`/api/analytics/finance-trend?${queryParams}`,
|
||||
{ signal },
|
||||
{ signal, timeout: ANALYTICS_TIMEOUT_MS },
|
||||
);
|
||||
if (signal.aborted) return;
|
||||
if (res.data.success) {
|
||||
@@ -136,7 +144,7 @@ export const useAnalyticsData = (active = true) => {
|
||||
const separator = queryParams ? '&' : '';
|
||||
const res = await API.get(
|
||||
`/api/analytics/channel-consumption?${queryParams}${separator}limit=30`,
|
||||
{ signal },
|
||||
{ signal, timeout: ANALYTICS_TIMEOUT_MS },
|
||||
);
|
||||
if (signal.aborted) return;
|
||||
if (res.data.success) {
|
||||
@@ -157,7 +165,7 @@ export const useAnalyticsData = (active = true) => {
|
||||
const separator = queryParams ? '&' : '';
|
||||
const res = await API.get(
|
||||
`/api/analytics/user-ranking?${queryParams}${separator}limit=100`,
|
||||
{ signal },
|
||||
{ signal, timeout: ANALYTICS_TIMEOUT_MS },
|
||||
);
|
||||
if (signal.aborted) return;
|
||||
if (res.data.success) {
|
||||
@@ -180,6 +188,7 @@ export const useAnalyticsData = (active = true) => {
|
||||
const queryParams = buildQueryParams(currentPeriod, currentDateRange);
|
||||
const res = await API.get(`/api/analytics/daily-stats?${queryParams}`, {
|
||||
signal,
|
||||
timeout: ANALYTICS_TIMEOUT_MS,
|
||||
});
|
||||
if (signal.aborted) return;
|
||||
if (res.data.success) {
|
||||
@@ -200,7 +209,7 @@ export const useAnalyticsData = (active = true) => {
|
||||
const separator = queryParams ? '&' : '';
|
||||
const res = await API.get(
|
||||
`/api/analytics/user-topup-ranking?${queryParams}${separator}limit=50`,
|
||||
{ signal },
|
||||
{ signal, timeout: ANALYTICS_TIMEOUT_MS },
|
||||
);
|
||||
if (signal.aborted) return;
|
||||
if (res.data.success) {
|
||||
@@ -238,6 +247,10 @@ export const useAnalyticsData = (active = true) => {
|
||||
if (!signal.aborted) setLoading(false);
|
||||
}
|
||||
await overviewTask;
|
||||
if (activeRequest.current === controller) {
|
||||
activeRequest.current = null;
|
||||
loadedRef.current = true;
|
||||
}
|
||||
},
|
||||
[
|
||||
cancelQuery,
|
||||
@@ -283,15 +296,16 @@ export const useAnalyticsData = (active = true) => {
|
||||
void refreshAll('today', [null, null]);
|
||||
};
|
||||
|
||||
// 懒加载:仅在 active 第一次为 true 时拉数据。
|
||||
// 由调用方传入 activeTab === 'operations',避免在 audit / risk Tab 时白白发起 8 个 API。
|
||||
const loadedRef = useRef(false);
|
||||
const loadInitialData = useEffectEvent(() => {
|
||||
if (!loadedRef.current && !activeRequest.current) {
|
||||
void refreshAll(period, dateRange);
|
||||
}
|
||||
});
|
||||
|
||||
// StrictMode 会取消首次副作用,只有完整结束的请求批次才算加载过。
|
||||
useEffect(() => {
|
||||
if (!active || loadedRef.current) return;
|
||||
loadedRef.current = true;
|
||||
refreshAll(period, dateRange);
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [active]);
|
||||
if (active) loadInitialData();
|
||||
}, [active, loadInitialData]);
|
||||
|
||||
return {
|
||||
loading,
|
||||
|
||||
Reference in New Issue
Block a user