Skip to content

Let's use AI to Earn!

feat(数据统计): 接入数据API,并且分拣数据

gaoSir9ca48ef15da7
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 16ec35ea..15b0dd4d 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -4,20 +4,14 @@
<option name="autoReloadType" value="NONE" />
</component>
<component name="ChangeListManager">
- <list default="true" id="8ce961ad-7c9e-494c-bc53-9ecde4358af8" name="更改" comment="feat(数据统计): 顶部账号完成">
- <change afterPath="$PROJECT_DIR$/src/app/[lng]/dataStatistics/components/DataStatisticsDetails/dataStatisticsDetails.module.scss" afterDir="false" />
- <change afterPath="$PROJECT_DIR$/src/app/[lng]/dataStatistics/components/DataStatisticsDetails/index.tsx" afterDir="false" />
- <change afterPath="$PROJECT_DIR$/src/app/[lng]/dataStatistics/svgs/collectCount.svg" afterDir="false" />
- <change afterPath="$PROJECT_DIR$/src/app/[lng]/dataStatistics/svgs/fansCount.svg" afterDir="false" />
- <change afterPath="$PROJECT_DIR$/src/app/[lng]/dataStatistics/svgs/forwardCount.svg" afterDir="false" />
- <change afterPath="$PROJECT_DIR$/src/app/[lng]/dataStatistics/useDataStatistics.tsx" afterDir="false" />
+ <list default="true" id="8ce961ad-7c9e-494c-bc53-9ecde4358af8" name="更改" comment="feat(数据统计): 数据明细完成">
+ <change afterPath="$PROJECT_DIR$/src/api/dataStatistics.ts" afterDir="false" />
+ <change afterPath="$PROJECT_DIR$/src/api/types/dataStatistics.ts" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/app/[lng]/dataStatistics/DataStatisticsCore.tsx" beforeDir="false" afterPath="$PROJECT_DIR$/src/app/[lng]/dataStatistics/DataStatisticsCore.tsx" afterDir="false" />
- <change beforePath="$PROJECT_DIR$/src/app/[lng]/dataStatistics/components/DataStatisticsHeader/dataStatisticsHeader.module.scss" beforeDir="false" afterPath="$PROJECT_DIR$/src/app/[lng]/dataStatistics/components/DataStatisticsHeader/dataStatisticsHeader.module.scss" afterDir="false" />
- <change beforePath="$PROJECT_DIR$/src/app/[lng]/dataStatistics/components/DataStatisticsHeader/index.tsx" beforeDir="false" afterPath="$PROJECT_DIR$/src/app/[lng]/dataStatistics/components/DataStatisticsHeader/index.tsx" afterDir="false" />
- <change beforePath="$PROJECT_DIR$/src/app/[lng]/dataStatistics/dataStatistics.module.scss" beforeDir="false" afterPath="$PROJECT_DIR$/src/app/[lng]/dataStatistics/dataStatistics.module.scss" afterDir="false" />
- <change beforePath="$PROJECT_DIR$/src/app/[lng]/dataStatistics/useDataStatistics.ts" beforeDir="false" />
- <change beforePath="$PROJECT_DIR$/src/app/styles/mixin.scss" beforeDir="false" afterPath="$PROJECT_DIR$/src/app/styles/mixin.scss" afterDir="false" />
+ <change beforePath="$PROJECT_DIR$/src/app/[lng]/dataStatistics/components/DataStatisticsDetails/dataStatisticsDetails.module.scss" beforeDir="false" afterPath="$PROJECT_DIR$/src/app/[lng]/dataStatistics/components/DataStatisticsDetails/dataStatisticsDetails.module.scss" afterDir="false" />
+ <change beforePath="$PROJECT_DIR$/src/app/[lng]/dataStatistics/components/DataStatisticsDetails/index.tsx" beforeDir="false" afterPath="$PROJECT_DIR$/src/app/[lng]/dataStatistics/components/DataStatisticsDetails/index.tsx" afterDir="false" />
+ <change beforePath="$PROJECT_DIR$/src/app/[lng]/dataStatistics/useDataStatistics.tsx" beforeDir="false" afterPath="$PROJECT_DIR$/src/app/[lng]/dataStatistics/useDataStatistics.tsx" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@@ -215,15 +209,7 @@
<workItem from="1757646405605" duration="66000" />
<workItem from="1757646484870" duration="466000" />
<workItem from="1757647784882" duration="2426000" />
- <workItem from="1757656251307" duration="21340000" />
- </task>
- <task id="LOCAL-00001" summary="feat(CalendarTiming): 优化发布记录列表展示&#10;- 调整日历记录项的样式和布局&#10;- 优化 RecordCore组件,添加平台图标和时间显示&#10;- 修改 CalendarTimingItem 组件,移除不必要的 Fragment&#10;-调整 index.tsx 中的日期格式化逻辑">
- <option name="closed" value="true" />
- <created>1751978595304</created>
- <option name="number" value="00001" />
- <option name="presentableId" value="LOCAL-00001" />
- <option name="project" value="LOCAL" />
- <updated>1751978595304</updated>
+ <workItem from="1757656251307" duration="24213000" />
</task>
<task id="LOCAL-00002" summary="feat(CalendarTiming): 优化发布记录列表展示&#10;- 调整日历记录项的样式和布局&#10;- 优化 RecordCore组件,添加平台图标和时间显示&#10;- 调整 index.tsx 中的日期格式化逻辑">
<option name="closed" value="true" />
@@ -609,7 +595,15 @@
<option name="project" value="LOCAL" />
<updated>1757676377923</updated>
</task>
- <option name="localTasksCounter" value="50" />
+ <task id="LOCAL-00050" summary="feat(数据统计): 数据明细完成">
+ <option name="closed" value="true" />
+ <created>1757683195377</created>
+ <option name="number" value="00050" />
+ <option name="presentableId" value="LOCAL-00050" />
+ <option name="project" value="LOCAL" />
+ <updated>1757683195377</updated>
+ </task>
+ <option name="localTasksCounter" value="51" />
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
@@ -660,13 +654,6 @@
</list>
</value>
</entry>
- <entry key="structure">
- <value>
- <list>
- <option value="file:$PROJECT_DIR$/next.config.mjs" />
- </list>
- </value>
- </entry>
</map>
</option>
</State>
@@ -679,7 +666,6 @@
<option name="CHECK_CODE_SMELLS_BEFORE_PROJECT_COMMIT" value="false" />
<option name="CHECK_NEW_TODO" value="false" />
<option name="ADD_EXTERNAL_FILES_SILENTLY" value="true" />
- <MESSAGE value="fix(日历): 删除最佳时间" />
<MESSAGE value="chore: 测试静态站点" />
<MESSAGE value="选择用户弹框恢复" />
<MESSAGE value="feat: 恢复选择账户弹框" />
@@ -704,6 +690,7 @@
<MESSAGE value="feat(数据统计): 顶部筛选完成" />
<MESSAGE value="feat(数据统计): 1. 找不到空间的账号更改为默认账号的ID。 2. 完善swiper。 3. 添加过滤后的账号" />
<MESSAGE value="feat(数据统计): 顶部账号完成" />
- <option name="LAST_COMMIT_MESSAGE" value="feat(数据统计): 顶部账号完成" />
+ <MESSAGE value="feat(数据统计): 数据明细完成" />
+ <option name="LAST_COMMIT_MESSAGE" value="feat(数据统计): 数据明细完成" />
</component>
</project>
\ No newline at end of file
diff --git a/src/api/dataStatistics.ts b/src/api/dataStatistics.ts
new file mode 100644
index 00000000..47a2d1d2
--- /dev/null
+++ b/src/api/dataStatistics.ts
@@ -0,0 +1,13 @@
+import http from "@/utils/request";
+import {
+ StatisticsPeriodApiParams,
+ StatisticsPeriodModel,
+} from "@/api/types/dataStatistics";
+
+// 获取数据统计信息
+export const getStatisticsPeriodApi = (data: StatisticsPeriodApiParams) => {
+ return http.post<StatisticsPeriodModel>(
+ "statistics/channels/period-batch",
+ data,
+ );
+};
diff --git a/src/api/types/dataStatistics.ts b/src/api/types/dataStatistics.ts
new file mode 100644
index 00000000..222cc1d4
--- /dev/null
+++ b/src/api/types/dataStatistics.ts
@@ -0,0 +1,70 @@
+// getStatisticsPeriodApi 入参
+export interface StatisticsPeriodApiParams {
+ startDate: string;
+ endDate: string;
+ queries: {
+ platform: string;
+ uid: string;
+ }[];
+}
+
+export interface StatisticsPeriodModel {
+ items?: StatisticsPeriodItems[];
+ queryCount: number;
+ successCount: number;
+ groupedByDate: GroupedByDate[];
+}
+
+export interface StatisticsPeriodItems {
+ platform: string;
+ uid: string;
+ items?: StatisticsPeriodItems2[];
+}
+
+export interface StatisticsPeriodItems2 {
+ id: string;
+ platform: string;
+ snapshotDate: string;
+ uid: string;
+ accountId: string;
+ commentCount?: number;
+ createdAt: string;
+ followingCount?: number;
+ likeCount?: number;
+ status: number;
+ updatedAt: string;
+ userId: string;
+ snapshotDateAsDate: string;
+ fansCount: number;
+ readCount?: number;
+ collectCount?: number;
+ forwardCount?: number;
+ workCount: number;
+}
+
+export interface GroupedByDate {
+ date: string;
+ records: Records[];
+ count: number;
+}
+
+export interface Records {
+ id: string;
+ platform: string;
+ snapshotDate: string;
+ uid: string;
+ accountId: string;
+ commentCount: number;
+ createdAt: string;
+ followingCount: number;
+ likeCount: number;
+ status: number;
+ updatedAt: string;
+ userId: string;
+ snapshotDateAsDate: string;
+ fansCount: number;
+ readCount: number;
+ collectCount: number;
+ forwardCount: number;
+ workCount: number;
+}
diff --git a/src/app/[lng]/dataStatistics/DataStatisticsCore.tsx b/src/app/[lng]/dataStatistics/DataStatisticsCore.tsx
index 70b911dc..26ca3e17 100644
--- a/src/app/[lng]/dataStatistics/DataStatisticsCore.tsx
+++ b/src/app/[lng]/dataStatistics/DataStatisticsCore.tsx
@@ -9,15 +9,29 @@ import { useAccountStore } from "@/store/account";
import DataStatisticsDetails from "@/app/[lng]/dataStatistics/components/DataStatisticsDetails";
export const DataStatisticsCore = () => {
- const { setChoosedGroupIds, setFilteredAccountList, choosedGroupIds, init } =
- useDataStatisticsStore(
- useShallow((state) => ({
- setChoosedGroupIds: state.setChoosedGroupIds,
- setFilteredAccountList: state.setFilteredAccountList,
- choosedGroupIds: state.choosedGroupIds,
- init: state.init,
- })),
- );
+ const {
+ setChoosedGroupIds,
+ setFilteredAccountList,
+ choosedGroupIds,
+ getStatistics,
+ timeRangeValue,
+ init,
+ filteredAccountList,
+ currentDetailType,
+ sortingData,
+ } = useDataStatisticsStore(
+ useShallow((state) => ({
+ setChoosedGroupIds: state.setChoosedGroupIds,
+ setFilteredAccountList: state.setFilteredAccountList,
+ choosedGroupIds: state.choosedGroupIds,
+ init: state.init,
+ getStatistics: state.getStatistics,
+ timeRangeValue: state.timeRangeValue,
+ filteredAccountList: state.filteredAccountList,
+ currentDetailType: state.currentDetailType,
+ sortingData: state.sortingData,
+ })),
+ );
const { accountGroupList, accountList } = useAccountStore(
useShallow((state) => ({
accountGroupList: state.accountGroupList,
@@ -29,6 +43,18 @@ export const DataStatisticsCore = () => {
init();
}, []);
+ // 数据明细切换,重新分拣数据
+ useEffect(() => {
+ sortingData();
+ }, [currentDetailType, sortingData]);
+
+ // 获取数据统计
+ useEffect(() => {
+ if (filteredAccountList.length == 0) return;
+ getStatistics();
+ }, [timeRangeValue, getStatistics, filteredAccountList]);
+
+ // 初始化选择所有账户组
useEffect(() => {
if (accountGroupList.length > 0) {
setChoosedGroupIds(accountGroupList.map((group) => group.id));
diff --git a/src/app/[lng]/dataStatistics/components/DataStatisticsDetails/dataStatisticsDetails.module.scss b/src/app/[lng]/dataStatistics/components/DataStatisticsDetails/dataStatisticsDetails.module.scss
index 122f561c..baa39908 100644
--- a/src/app/[lng]/dataStatistics/components/DataStatisticsDetails/dataStatisticsDetails.module.scss
+++ b/src/app/[lng]/dataStatistics/components/DataStatisticsDetails/dataStatisticsDetails.module.scss
@@ -1,19 +1,24 @@
.dataStatisticsDetails {
:global {
- h3 {
+ .dataStatisticsDetails-head {
+ display: flex;
+ justify-content: space-between;
margin: 30px 0;
- position: relative;
- padding-left: 20px;
- &:after {
- content: "";
- display: block;
- position: absolute;
- top: 53%;
- transform: translateY(-50%);
- left: 0;
- width: 2px;
- height: 60%;
- background: var(--colorPrimary5);
+
+ h3 {
+ position: relative;
+ padding-left: 20px;
+ &:after {
+ content: "";
+ display: block;
+ position: absolute;
+ top: 45%;
+ transform: translateY(-50%);
+ left: 0;
+ width: 2px;
+ height: 50%;
+ background: var(--colorPrimary5);
+ }
}
}
diff --git a/src/app/[lng]/dataStatistics/components/DataStatisticsDetails/index.tsx b/src/app/[lng]/dataStatistics/components/DataStatisticsDetails/index.tsx
index 81a3ce39..032e68f9 100644
--- a/src/app/[lng]/dataStatistics/components/DataStatisticsDetails/index.tsx
+++ b/src/app/[lng]/dataStatistics/components/DataStatisticsDetails/index.tsx
@@ -3,6 +3,8 @@ import styles from "./dataStatisticsDetails.module.scss";
import { useDataStatisticsStore } from "@/app/[lng]/dataStatistics/useDataStatistics";
import { useShallow } from "zustand/react/shallow";
import Icon from "@ant-design/icons";
+import { DatePicker } from "antd";
+import { Dayjs } from "dayjs";
export interface IDataStatisticsDetailsRef {}
@@ -14,18 +16,38 @@ const DataStatisticsDetails = memo(
{}: IDataStatisticsDetailsProps,
ref: ForwardedRef<IDataStatisticsDetailsRef>,
) => {
- const { currentDetailType, dataDetails, setCurrentDetailType } =
- useDataStatisticsStore(
- useShallow((state) => ({
- dataDetails: state.dataDetails,
- currentDetailType: state.currentDetailType,
- setCurrentDetailType: state.setCurrentDetailType,
- })),
- );
+ const {
+ currentDetailType,
+ dataDetails,
+ setTimeRangeValue,
+ setCurrentDetailType,
+ timeRangeValue,
+ } = useDataStatisticsStore(
+ useShallow((state) => ({
+ dataDetails: state.dataDetails,
+ currentDetailType: state.currentDetailType,
+ setCurrentDetailType: state.setCurrentDetailType,
+ setTimeRangeValue: state.setTimeRangeValue,
+ timeRangeValue: state.timeRangeValue,
+ })),
+ );
return (
<div className={styles.dataStatisticsDetails}>
- <h3>数据明细</h3>
+ <div className="dataStatisticsDetails-head">
+ <h3>数据明细</h3>
+ <div className="dataStatisticsDetails-head-rangePicker">
+ <label>时间范围:</label>
+ <DatePicker.RangePicker
+ value={timeRangeValue}
+ allowClear={false}
+ onChange={(e) => {
+ const dates = e as [Dayjs, Dayjs];
+ setTimeRangeValue(dates);
+ }}
+ />
+ </div>
+ </div>
<div className="dataStatisticsDetails-content">
{dataDetails.map((detail) => (
diff --git a/src/app/[lng]/dataStatistics/useDataStatistics.tsx b/src/app/[lng]/dataStatistics/useDataStatistics.tsx
index 50f29255..7e1b1716 100644
--- a/src/app/[lng]/dataStatistics/useDataStatistics.tsx
+++ b/src/app/[lng]/dataStatistics/useDataStatistics.tsx
@@ -9,6 +9,10 @@ import {
VideoCameraFilled,
MessageFilled,
} from "@ant-design/icons";
+import dayjs, { Dayjs } from "dayjs";
+import { getStatisticsPeriodApi } from "@/api/dataStatistics";
+import { StatisticsPeriodModel } from "@/api/types/dataStatistics";
+import { message } from "antd";
export interface IDataStatisticsStore {
// 当前选择的账户组IDs
@@ -27,6 +31,23 @@ export interface IDataStatisticsStore {
}[];
// 当前选择的明细类型
currentDetailType: string;
+ // 日期范围
+ timeRangeValue: [Dayjs, Dayjs];
+ // 源数据
+ originData?: StatisticsPeriodModel;
+ // loading
+ loading: boolean;
+ // echart 数据
+ echartData: {
+ // 平台,格式:['抖音', '微信', '快手', '小红书']
+ platforms: string[];
+ // 日期,格式:['2023-10-01', '2023-10-02', ...]
+ dates: string[];
+ // 数据,格式:{'抖音': [100, 200, ...], '微信': [150, 250, ...], ...}
+ data: {
+ [key: string]: number[];
+ };
+ };
}
const state: IDataStatisticsStore = {
@@ -77,6 +98,14 @@ const state: IDataStatisticsStore = {
},
],
currentDetailType: "",
+ timeRangeValue: [dayjs().subtract(7, "day"), dayjs()],
+ loading: false,
+ originData: undefined,
+ echartData: {
+ platforms: [],
+ dates: [],
+ data: {},
+ },
};
export const useDataStatisticsStore = create(
@@ -86,12 +115,119 @@ export const useDataStatisticsStore = create(
},
(set, get) => {
const methods = {
- init() {
+ async init() {
set({
currentDetailType: get().dataDetails[0].value,
});
},
+ // 获取数据统计
+ async getStatistics() {
+ set({
+ loading: true,
+ });
+ const res = await getStatisticsPeriodApi({
+ startDate: get().timeRangeValue[0].format("YYYY-MM-DD"),
+ endDate: get().timeRangeValue[1].format("YYYY-MM-DD"),
+ queries: get().filteredAccountList.map((account) => ({
+ platform: account.type,
+ uid: account.uid,
+ })),
+ });
+ set({
+ loading: false,
+ });
+
+ if (!res || !res.data) {
+ message.error("获取数据统计失败,请稍后重试");
+ return;
+ }
+ set({
+ originData: res.data,
+ });
+
+ methods.sortingData();
+ },
+
+ // 分拣数据
+ sortingData() {
+ const data = get().originData;
+ if (!data) return;
+
+ // 1. 汇总各指标总数
+ const metrics = get().dataDetails.map((e) => e.value);
+ const totals: Record<string, number> = {};
+ metrics.forEach((m) => (totals[m] = 0));
+
+ data.groupedByDate.forEach((day) => {
+ day.records.forEach((rec) => {
+ metrics.forEach((m) => {
+ // @ts-ignore
+ totals[m] += rec[m] ?? 0;
+ });
+ });
+ });
+
+ set({
+ dataDetails: get().dataDetails.map((item) => ({
+ ...item,
+ total: totals[item.value] ?? 0,
+ })),
+ });
+
+ // 2. 生成折线图数据
+ const currentField = get().currentDetailType;
+
+ // 日期
+ const dates = Array.from(
+ new Set(data.groupedByDate.map((e) => e.date)),
+ ).sort();
+ const dateIndex: Record<string, number> = {};
+ dates.forEach((d, i) => (dateIndex[d] = i));
+
+ // 平台
+ const platformSet = new Set<string>();
+ data.groupedByDate.forEach((g) =>
+ g.records.forEach((r) => platformSet.add(r.platform)),
+ );
+ const platforms = Array.from(platformSet).sort();
+
+ // 初始化矩阵
+ const chartData: Record<string, number[]> = {};
+ platforms.forEach(
+ (p) => (chartData[p] = Array(dates.length).fill(0)),
+ );
+
+ // 填充数据
+ data.groupedByDate.forEach((g) => {
+ const dIdx = dateIndex[g.date];
+ g.records.forEach((r) => {
+ const p = r.platform;
+ if (chartData[p]) {
+ // @ts-ignore
+ chartData[p][dIdx] = r[currentField] ?? 0;
+ }
+ });
+ });
+
+ set({
+ echartData: {
+ platforms,
+ dates,
+ data: chartData,
+ },
+ });
+
+ console.log(get().echartData);
+ },
+
+ // 设置 timeRangeValue
+ setTimeRangeValue(timeRangeValue: [Dayjs, Dayjs]) {
+ set({
+ timeRangeValue,
+ });
+ },
+
// 设置 currentDetailType
setCurrentDetailType(currentDetailType: string) {
set({