index.tsx 7.2 KB

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