index.tsx 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. import { useState, useEffect } from 'react';
  2. import { Tabs, Table, Button, message } from "antd";
  3. import type { TableProps } from 'antd';
  4. import { DownloadOutlined } from '@ant-design/icons';
  5. import http from '@src/http';
  6. import { gzhDataList, gzhDataExport } from '@src/http/api.ts';
  7. interface GzhDataItem {
  8. dateStr: string;
  9. fansIncreaseCount: number;
  10. firstLevel: number;
  11. name: string;
  12. openRate: number;
  13. score: number;
  14. }
  15. interface GzhDataResponse {
  16. curPageFirstRecNum: number;
  17. curPageLastRecNum: number;
  18. currentPage: number;
  19. nextPage: number;
  20. obj: GzhDataItem;
  21. objs: GzhDataItem[];
  22. offset: number;
  23. pageSize: number;
  24. prePage: number;
  25. totalPage: number;
  26. totalSize: number;
  27. }
  28. const Gzh: React.FC = () => {
  29. const [allDataSource, setAllDataSource] = useState<GzhDataItem[]>([]);
  30. const [separateDataSource, setSeparateDataSource] = useState<GzhDataItem[]>([]);
  31. const [loading, setLoading] = useState<boolean>(false);
  32. const [downloadLoading, setDownloadLoading] = useState<boolean>(false);
  33. const [pagination, setPagination] = useState({
  34. current: 1,
  35. pageSize: 10,
  36. total: 0
  37. });
  38. const [activeKey, setActiveKey] = useState("0");
  39. const columns:TableProps['columns'] = [
  40. {
  41. title: '日期',
  42. dataIndex: 'dateStr',
  43. key: 'dateStr',
  44. },
  45. {
  46. title: '新增粉丝',
  47. dataIndex: 'fansIncreaseCount',
  48. key: 'fansIncreaseCount',
  49. },
  50. {
  51. title: '小程序访问人数',
  52. dataIndex: 'firstLevel',
  53. key: 'firstLevel',
  54. },
  55. {
  56. title: '打开率',
  57. dataIndex: 'openRate',
  58. key: 'openRate',
  59. render: (text) => `${(Number(text) * 100).toFixed(2)}%`
  60. },
  61. {
  62. title: '本渠道裂变率',
  63. dataIndex: 'score',
  64. key: 'score',
  65. },
  66. ];
  67. const separateColumns:TableProps['columns'] = [
  68. {
  69. title: '日期',
  70. dataIndex: 'dateStr',
  71. key: 'dateStr',
  72. },
  73. {
  74. title: '公众号',
  75. dataIndex: 'name',
  76. key: 'name',
  77. },
  78. {
  79. title: '新增粉丝',
  80. dataIndex: 'fansIncreaseCount',
  81. key: 'fansIncreaseCount',
  82. },
  83. {
  84. title: '小程序访问人数',
  85. dataIndex: 'firstLevel',
  86. key: 'firstLevel',
  87. },
  88. {
  89. title: '打开率',
  90. dataIndex: 'openRate',
  91. key: 'openRate',
  92. render: (text) => `${(Number(text) * 100).toFixed(2)}%`
  93. },
  94. {
  95. title: '本渠道裂变率',
  96. dataIndex: 'score',
  97. key: 'score',
  98. },
  99. ];
  100. const fetchGzhData = async (page = 1, pageSize = 10, type = 0) => {
  101. try {
  102. setLoading(true);
  103. const res = await http.post<GzhDataResponse>(gzhDataList, {
  104. pageNum: page,
  105. pageSize: pageSize,
  106. type: type
  107. });
  108. if (res.success && res.data) {
  109. if (type === 0 || type === 2) {
  110. setAllDataSource(res.data.objs || []);
  111. } else {
  112. setSeparateDataSource(res.data.objs || []);
  113. }
  114. setPagination({
  115. current: res.data.currentPage,
  116. pageSize: res.data.pageSize,
  117. total: res.data.totalSize
  118. });
  119. }
  120. } catch (error) {
  121. console.error('获取公众号数据失败:', error);
  122. } finally {
  123. setLoading(false);
  124. }
  125. };
  126. useEffect(() => {
  127. fetchGzhData(1, 10, 0);
  128. }, []);
  129. const onChange = (key: string) => {
  130. console.log(key);
  131. fetchGzhData(1, 10, Number(key));
  132. };
  133. const handleTableChange = (pagination: any) => {
  134. const { current, pageSize } = pagination;
  135. fetchGzhData(current, pageSize, Number(activeKey));
  136. };
  137. const handleDownload = async () => {
  138. try {
  139. setDownloadLoading(true);
  140. // 根据当前选中的标签页设置type参数
  141. const type = Number(activeKey);
  142. // 使用当前分页信息
  143. const response = await http.post(gzhDataExport, {
  144. pageNum: pagination.current,
  145. pageSize: pagination.pageSize,
  146. type: type
  147. });
  148. if (response.success && response.data) {
  149. window.open(response.data as string);
  150. } else {
  151. message.error('下载失败');
  152. }
  153. } catch {
  154. message.error('下载失败');
  155. } finally {
  156. setDownloadLoading(false);
  157. }
  158. };
  159. return (
  160. <>
  161. <div className={"flex mb-[10px]"}>
  162. <div className={"flex-1 leading-[32px]"}>公众号数据统计</div>
  163. </div>
  164. <Tabs
  165. defaultActiveKey="0"
  166. type="card"
  167. tabBarExtraContent={{
  168. right: <Button
  169. type="link"
  170. icon={<DownloadOutlined />}
  171. loading={downloadLoading}
  172. onClick={handleDownload}
  173. >
  174. 下载数据
  175. </Button>
  176. }}
  177. onChange={(key) => {
  178. setActiveKey(key);
  179. onChange(key);
  180. }}
  181. items={[
  182. {
  183. key: "0",
  184. label: "自动回复总计",
  185. children: (
  186. <Table
  187. dataSource={allDataSource}
  188. columns={columns}
  189. rowKey="dateStr"
  190. loading={loading}
  191. pagination={{
  192. current: pagination.current,
  193. pageSize: pagination.pageSize,
  194. total: pagination.total,
  195. showSizeChanger: true,
  196. showTotal: (total) => `共 ${total} 条`,
  197. }}
  198. onChange={handleTableChange}
  199. />
  200. ),
  201. },
  202. {
  203. key: "1",
  204. label: "自动回复分账号",
  205. children: (
  206. <Table
  207. dataSource={separateDataSource}
  208. columns={separateColumns}
  209. rowKey="dateStr"
  210. loading={loading}
  211. pagination={{
  212. current: pagination.current,
  213. pageSize: pagination.pageSize,
  214. total: pagination.total,
  215. showSizeChanger: true,
  216. showTotal: (total) => `共 ${total} 条`,
  217. }}
  218. onChange={handleTableChange}
  219. />
  220. ),
  221. },
  222. {
  223. key: "2",
  224. label: "服务号总计",
  225. children: (
  226. <Table
  227. dataSource={allDataSource}
  228. columns={columns}
  229. rowKey="dateStr"
  230. loading={loading}
  231. pagination={{
  232. current: pagination.current,
  233. pageSize: pagination.pageSize,
  234. total: pagination.total,
  235. showSizeChanger: true,
  236. showTotal: (total) => `共 ${total} 条`,
  237. }}
  238. onChange={handleTableChange}
  239. />
  240. ),
  241. },
  242. {
  243. key: "3",
  244. label: "服务号分账号",
  245. children: (
  246. <Table
  247. dataSource={separateDataSource}
  248. columns={separateColumns}
  249. rowKey="dateStr"
  250. loading={loading}
  251. pagination={{
  252. current: pagination.current,
  253. pageSize: pagination.pageSize,
  254. total: pagination.total,
  255. showSizeChanger: true,
  256. showTotal: (total) => `共 ${total} 条`,
  257. }}
  258. onChange={handleTableChange}
  259. />
  260. ),
  261. },
  262. ]}
  263. />
  264. </>
  265. )
  266. }
  267. export default Gzh