2aa9d73325
集中保留原版权声明,移除文件头检查和全局测试忽略规则。验证:全量单测、受影响用例复测及静态检查通过。
125 lines
3.9 KiB
JavaScript
125 lines
3.9 KiB
JavaScript
import { useState, useCallback, useEffect, useRef } from 'react';
|
|
import { API } from '../../helpers';
|
|
|
|
/**
|
|
* 企业经营决策报表数据 Hook(单接口 /api/analytics/org-consumption/report,服务端基于汇总表计算)
|
|
*
|
|
* 参数 active:是否激活。false 时不发起任何请求(用于 Tab 懒加载)。
|
|
* 首次切换为 true 时自动加载一次;之后切回保持现有数据,不重复拉。
|
|
*
|
|
* 自带 period / dateRange 状态,与运营看板(useAnalyticsData)相互独立。
|
|
*/
|
|
export const useOrgConsumptionData = (active = true) => {
|
|
const [loading, setLoading] = useState(false);
|
|
const [report, setReport] = useState(null);
|
|
const [error, setError] = useState(false);
|
|
const [period, setPeriodState] = useState('week');
|
|
const [dateRange, setDateRangeState] = useState([null, null]);
|
|
// 只采纳最后一次请求的结果,避免快速切换时间段时旧响应覆盖新数据
|
|
const requestSeq = useRef(0);
|
|
|
|
// 与 useAnalyticsData 保持一致:DatePicker 选的本地日期分量当作 CST 日期处理
|
|
const toCST = useCallback((date, isEndOfDay = false) => {
|
|
const d = new Date(date);
|
|
const cstMidnightMs =
|
|
Date.UTC(d.getFullYear(), d.getMonth(), d.getDate(), 0, 0, 0, 0) -
|
|
8 * 3600 * 1000;
|
|
if (isEndOfDay) {
|
|
return Math.floor((cstMidnightMs + 24 * 3600 * 1000) / 1000);
|
|
}
|
|
return Math.floor(cstMidnightMs / 1000);
|
|
}, []);
|
|
|
|
const buildQueryParams = useCallback(
|
|
(currentPeriod, currentDateRange) => {
|
|
const params = new URLSearchParams();
|
|
const usePeriod = currentPeriod || period;
|
|
const useRange = currentDateRange || dateRange;
|
|
|
|
if (usePeriod === 'custom' && useRange[0] && useRange[1]) {
|
|
params.append('start_time', toCST(useRange[0], false));
|
|
params.append('end_time', toCST(useRange[1], true));
|
|
} else if (usePeriod !== 'custom') {
|
|
params.append('period', usePeriod);
|
|
}
|
|
return params.toString();
|
|
},
|
|
[period, dateRange, toCST],
|
|
);
|
|
|
|
const refreshAll = useCallback(
|
|
async (currentPeriod, currentDateRange) => {
|
|
const seq = ++requestSeq.current;
|
|
setLoading(true);
|
|
try {
|
|
const queryParams = buildQueryParams(currentPeriod, currentDateRange);
|
|
const res = await API.get(
|
|
`/api/analytics/org-consumption/report?${queryParams}`,
|
|
{ skipErrorHandler: true },
|
|
);
|
|
if (seq !== requestSeq.current) return;
|
|
if (res.data?.success) {
|
|
setReport(res.data.data);
|
|
setError(false);
|
|
} else {
|
|
// 清掉旧时间段的数据,避免失败时把上一时段结果当成当前结果展示
|
|
setReport(null);
|
|
setError(true);
|
|
}
|
|
} catch (err) {
|
|
if (seq !== requestSeq.current) return;
|
|
console.error('获取企业经营报表失败:', err);
|
|
setReport(null);
|
|
setError(true);
|
|
} finally {
|
|
if (seq === requestSeq.current) setLoading(false);
|
|
}
|
|
},
|
|
[buildQueryParams],
|
|
);
|
|
|
|
const setPeriod = useCallback(
|
|
(newPeriod) => {
|
|
setPeriodState(newPeriod);
|
|
if (newPeriod !== 'custom') {
|
|
setDateRangeState([null, null]);
|
|
refreshAll(newPeriod, [null, null]);
|
|
}
|
|
},
|
|
[refreshAll],
|
|
);
|
|
|
|
const setDateRange = useCallback(
|
|
(newRange) => {
|
|
setDateRangeState(newRange);
|
|
if (newRange[0] && newRange[1]) {
|
|
setPeriodState('custom');
|
|
refreshAll('custom', newRange);
|
|
}
|
|
},
|
|
[refreshAll],
|
|
);
|
|
|
|
// 懒加载:仅在 active 首次为 true 时拉数据
|
|
const loadedRef = useRef(false);
|
|
useEffect(() => {
|
|
if (!active || loadedRef.current) return;
|
|
loadedRef.current = true;
|
|
refreshAll(period, dateRange);
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, [active]);
|
|
|
|
return {
|
|
loading,
|
|
report,
|
|
error,
|
|
period,
|
|
dateRange,
|
|
setPeriod,
|
|
setDateRange,
|
|
refreshAll,
|
|
};
|
|
};
|
|
|
|
export default useOrgConsumptionData;
|