Initial import of weiyuan frontend
This commit is contained in:
@@ -0,0 +1,306 @@
|
||||
/*
|
||||
Copyright (C) 2025 QuantumNous
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
For commercial licensing, please contact support@quantumnous.com
|
||||
*/
|
||||
import { useState, useCallback, useEffect, useRef } from 'react';
|
||||
import { API, showError } from '../../helpers';
|
||||
|
||||
/**
|
||||
* 运营看板数据获取Hook
|
||||
*
|
||||
* 参数 active:是否激活。false 时不发起任何请求(用于 Tab 切换的懒加载)。
|
||||
* 首次切换为 true 时自动加载一次;之后再切回保持现有数据,不重复拉。
|
||||
*/
|
||||
export const useAnalyticsData = (active = true) => {
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [overview, setOverview] = useState(null);
|
||||
const [userTrend, setUserTrend] = useState([]);
|
||||
const [financeTrend, setFinanceTrend] = useState([]);
|
||||
const [channelConsumption, setChannelConsumption] = useState([]);
|
||||
const [userRanking, setUserRanking] = useState([]);
|
||||
const [topupChannel, setTopupChannel] = useState([]);
|
||||
const [dailyStats, setDailyStats] = useState([]);
|
||||
const [userTopupRanking, setUserTopupRanking] = useState([]);
|
||||
const [period, setPeriod] = useState('today'); // today, week, month, quarter, custom
|
||||
const [dateRange, setDateRange] = useState([null, null]); // 自定义日期范围 [startDate, endDate]
|
||||
|
||||
// 将 DatePicker 选择的日期转换为 UTC+8 当天零点/末尾的 Unix 时间戳(秒)
|
||||
// DatePicker 返回的 Date 对象,其本地日期分量即用户选的日期,直接当作 CST 日期处理
|
||||
const toCST = useCallback((date, isEndOfDay = false) => {
|
||||
const d = new Date(date);
|
||||
// 取本地日期分量(即用户在日历上选择的日期)
|
||||
const year = d.getFullYear();
|
||||
const month = d.getMonth();
|
||||
const day = d.getDate();
|
||||
|
||||
// 构造该日期在 CST (UTC+8) 零点的 UTC 时间戳
|
||||
// CST 零点 = UTC 同日零点 - 8小时
|
||||
const cstMidnightMs =
|
||||
Date.UTC(year, month, day, 0, 0, 0, 0) - 8 * 3600 * 1000;
|
||||
if (isEndOfDay) {
|
||||
// 当天结束 → 次日零点 CST
|
||||
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]) {
|
||||
// 自定义日期范围:统一使用 UTC+8 时区的天边界
|
||||
const startTime = toCST(useRange[0], false);
|
||||
const endTime = toCST(useRange[1], true);
|
||||
params.append('start_time', startTime);
|
||||
params.append('end_time', endTime);
|
||||
} else if (usePeriod !== 'custom') {
|
||||
params.append('period', usePeriod);
|
||||
}
|
||||
return params.toString();
|
||||
},
|
||||
[period, dateRange, toCST],
|
||||
);
|
||||
|
||||
// 获取概览数据。后端对概览结果做了 60s 缓存,force=true 时穿透(用户主动点刷新)
|
||||
const fetchOverview = useCallback(async (force = false) => {
|
||||
try {
|
||||
const res = await API.get(
|
||||
`/api/analytics/overview${force ? '?refresh=true' : ''}`,
|
||||
);
|
||||
if (res.data.success) {
|
||||
setOverview(res.data.data);
|
||||
} else {
|
||||
showError(res.data.message);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取概览数据失败:', error);
|
||||
}
|
||||
}, []);
|
||||
|
||||
// 获取用户趋势
|
||||
const fetchUserTrend = useCallback(
|
||||
async (currentPeriod, currentDateRange) => {
|
||||
try {
|
||||
const queryParams = buildQueryParams(currentPeriod, currentDateRange);
|
||||
const res = await API.get(`/api/analytics/user-trend?${queryParams}`);
|
||||
if (res.data.success) {
|
||||
setUserTrend(res.data.data || []);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取用户趋势失败:', error);
|
||||
}
|
||||
},
|
||||
[buildQueryParams],
|
||||
);
|
||||
|
||||
// 获取收支趋势
|
||||
const fetchFinanceTrend = useCallback(
|
||||
async (currentPeriod, currentDateRange) => {
|
||||
try {
|
||||
const queryParams = buildQueryParams(currentPeriod, currentDateRange);
|
||||
const res = await API.get(
|
||||
`/api/analytics/finance-trend?${queryParams}`,
|
||||
);
|
||||
if (res.data.success) {
|
||||
setFinanceTrend(res.data.data || []);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取收支趋势失败:', error);
|
||||
}
|
||||
},
|
||||
[buildQueryParams],
|
||||
);
|
||||
|
||||
// 获取渠道消耗排行
|
||||
const fetchChannelConsumption = useCallback(
|
||||
async (currentPeriod, currentDateRange) => {
|
||||
try {
|
||||
const queryParams = buildQueryParams(currentPeriod, currentDateRange);
|
||||
const separator = queryParams ? '&' : '';
|
||||
const res = await API.get(
|
||||
`/api/analytics/channel-consumption?${queryParams}${separator}limit=30`,
|
||||
);
|
||||
if (res.data.success) {
|
||||
setChannelConsumption(res.data.data || []);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取渠道消耗排行失败:', error);
|
||||
}
|
||||
},
|
||||
[buildQueryParams],
|
||||
);
|
||||
|
||||
// 获取用户排行
|
||||
const fetchUserRanking = useCallback(
|
||||
async (currentPeriod, currentDateRange) => {
|
||||
try {
|
||||
const queryParams = buildQueryParams(currentPeriod, currentDateRange);
|
||||
const separator = queryParams ? '&' : '';
|
||||
const res = await API.get(
|
||||
`/api/analytics/user-ranking?${queryParams}${separator}limit=100`,
|
||||
);
|
||||
if (res.data.success) {
|
||||
setUserRanking(res.data.data || []);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取用户排行失败:', error);
|
||||
}
|
||||
},
|
||||
[buildQueryParams],
|
||||
);
|
||||
|
||||
// 获取充值渠道分布
|
||||
const fetchTopupChannel = useCallback(
|
||||
async (currentPeriod, currentDateRange) => {
|
||||
try {
|
||||
const queryParams = buildQueryParams(currentPeriod, currentDateRange);
|
||||
const res = await API.get(
|
||||
`/api/analytics/topup-channel?${queryParams}`,
|
||||
);
|
||||
if (res.data.success) {
|
||||
setTopupChannel(res.data.data || []);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取充值渠道分布失败:', error);
|
||||
}
|
||||
},
|
||||
[buildQueryParams],
|
||||
);
|
||||
|
||||
// 获取每日统计
|
||||
const fetchDailyStats = useCallback(
|
||||
async (currentPeriod, currentDateRange) => {
|
||||
try {
|
||||
const queryParams = buildQueryParams(currentPeriod, currentDateRange);
|
||||
const res = await API.get(`/api/analytics/daily-stats?${queryParams}`);
|
||||
if (res.data.success) {
|
||||
setDailyStats(res.data.data || []);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取每日统计失败:', error);
|
||||
}
|
||||
},
|
||||
[buildQueryParams],
|
||||
);
|
||||
|
||||
// 获取用户充值排行
|
||||
const fetchUserTopupRanking = useCallback(
|
||||
async (currentPeriod, currentDateRange) => {
|
||||
try {
|
||||
const queryParams = buildQueryParams(currentPeriod, currentDateRange);
|
||||
const separator = queryParams ? '&' : '';
|
||||
const res = await API.get(
|
||||
`/api/analytics/user-topup-ranking?${queryParams}${separator}limit=50`,
|
||||
);
|
||||
if (res.data.success) {
|
||||
setUserTopupRanking(res.data.data || []);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取用户充值排行失败:', error);
|
||||
}
|
||||
},
|
||||
[buildQueryParams],
|
||||
);
|
||||
|
||||
// 刷新所有数据
|
||||
const refreshAll = useCallback(
|
||||
async (currentPeriod, currentDateRange, force = false) => {
|
||||
setLoading(true);
|
||||
try {
|
||||
await Promise.all([
|
||||
fetchOverview(force),
|
||||
fetchUserTrend(currentPeriod, currentDateRange),
|
||||
fetchFinanceTrend(currentPeriod, currentDateRange),
|
||||
fetchChannelConsumption(currentPeriod, currentDateRange),
|
||||
fetchUserRanking(currentPeriod, currentDateRange),
|
||||
fetchTopupChannel(currentPeriod, currentDateRange),
|
||||
fetchDailyStats(currentPeriod, currentDateRange),
|
||||
fetchUserTopupRanking(currentPeriod, currentDateRange),
|
||||
]);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
},
|
||||
[
|
||||
fetchOverview,
|
||||
fetchUserTrend,
|
||||
fetchFinanceTrend,
|
||||
fetchChannelConsumption,
|
||||
fetchUserRanking,
|
||||
fetchTopupChannel,
|
||||
fetchDailyStats,
|
||||
fetchUserTopupRanking,
|
||||
],
|
||||
);
|
||||
|
||||
// 切换时间周期
|
||||
const handlePeriodChange = useCallback(
|
||||
(newPeriod) => {
|
||||
setPeriod(newPeriod);
|
||||
if (newPeriod !== 'custom') {
|
||||
setDateRange([null, null]);
|
||||
refreshAll(newPeriod, [null, null]);
|
||||
}
|
||||
},
|
||||
[refreshAll],
|
||||
);
|
||||
|
||||
// 更新自定义日期范围
|
||||
const handleDateRangeChange = useCallback(
|
||||
(newRange) => {
|
||||
setDateRange(newRange);
|
||||
if (newRange[0] && newRange[1]) {
|
||||
setPeriod('custom');
|
||||
refreshAll('custom', newRange);
|
||||
}
|
||||
},
|
||||
[refreshAll],
|
||||
);
|
||||
|
||||
// 懒加载:仅在 active 第一次为 true 时拉数据。
|
||||
// 由调用方传入 activeTab === 'operations',避免在 audit / risk Tab 时白白发起 8 个 API。
|
||||
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,
|
||||
overview,
|
||||
userTrend,
|
||||
financeTrend,
|
||||
channelConsumption,
|
||||
userRanking,
|
||||
topupChannel,
|
||||
dailyStats,
|
||||
userTopupRanking,
|
||||
period,
|
||||
dateRange,
|
||||
setPeriod: handlePeriodChange,
|
||||
setDateRange: handleDateRangeChange,
|
||||
refreshAll,
|
||||
};
|
||||
};
|
||||
|
||||
export default useAnalyticsData;
|
||||
Reference in New Issue
Block a user