index.tsx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 specialColumns:TableProps['columns'] = [
  56. {
  57. title: '日期',
  58. dataIndex: 'dateStr',
  59. key: 'dateStr',
  60. },
  61. {
  62. title: '视频标题',
  63. dataIndex: 'title',
  64. key: 'title',
  65. },
  66. {
  67. title: '视频ID',
  68. dataIndex: 'videoId',
  69. key: 'videoId',
  70. },
  71. {
  72. title: '传播得分',
  73. dataIndex: 'score',
  74. key: 'score',
  75. },
  76. ];
  77. const fetchQwData = async (page = 1, pageSize = 10, type = 0) => {
  78. try {
  79. setLoading(true);
  80. const res = await http.post<QwDataResponse>(qwDataList, {
  81. pageNum: page,
  82. pageSize: pageSize,
  83. type: type
  84. });
  85. if (res.success && res.data) {
  86. setDataSource(res.data.objs || []);
  87. setPagination({
  88. current: res.data.currentPage,
  89. pageSize: res.data.pageSize,
  90. total: res.data.totalSize
  91. });
  92. }
  93. } catch (error) {
  94. console.error('获取企微数据失败:', error);
  95. } finally {
  96. setLoading(false);
  97. }
  98. };
  99. useEffect(() => {
  100. fetchQwData(1, 10, 0);
  101. }, []);
  102. const onChange = (key: string) => {
  103. console.log(key);
  104. fetchQwData(1, 10, Number(key));
  105. };
  106. const handleTableChange = (pagination: any) => {
  107. const { current, pageSize } = pagination;
  108. fetchQwData(current, pageSize, Number(activeKey));
  109. };
  110. const handleDownload = async () => {
  111. try {
  112. setDownloadLoading(true);
  113. // 根据当前选中的标签页设置type参数
  114. const type = Number(activeKey);
  115. // 使用当前分页信息
  116. const response = await http.post(qwDataExport, {
  117. pageNum: pagination.current,
  118. pageSize: pagination.pageSize,
  119. type: type
  120. }, { responseType: 'blob' });
  121. if (response.success && response.data) {
  122. message.success('下载成功');
  123. } else {
  124. message.error('下载失败');
  125. }
  126. } catch {
  127. message.error('下载失败');
  128. } finally {
  129. setDownloadLoading(false);
  130. }
  131. };
  132. return (
  133. <>
  134. <div className={"flex mb-[10px]"}>
  135. <div className={"flex-1 leading-[32px]"}>企微数据统计</div>
  136. </div>
  137. <Tabs
  138. defaultActiveKey="0"
  139. type="card"
  140. tabBarExtraContent={{
  141. right: <Button
  142. type="link"
  143. icon={<DownloadOutlined />}
  144. loading={downloadLoading}
  145. onClick={handleDownload}
  146. >
  147. 下载数据
  148. </Button>
  149. }}
  150. onChange={(key) => {
  151. setActiveKey(key);
  152. onChange(key);
  153. }}
  154. items={[
  155. {
  156. key: "0",
  157. label: "总体",
  158. children: (
  159. <Table
  160. dataSource={dataSource}
  161. columns={columns}
  162. rowKey="dateStr"
  163. loading={loading}
  164. pagination={{
  165. current: pagination.current,
  166. pageSize: pagination.pageSize,
  167. total: pagination.total,
  168. showSizeChanger: true,
  169. showTotal: (total) => `共 ${total} 条`,
  170. }}
  171. onChange={handleTableChange}
  172. />
  173. ),
  174. },
  175. {
  176. key: "1",
  177. label: "群发",
  178. children: (
  179. <Table
  180. dataSource={dataSource}
  181. columns={columns}
  182. rowKey="dateStr"
  183. loading={loading}
  184. pagination={{
  185. current: pagination.current,
  186. pageSize: pagination.pageSize,
  187. total: pagination.total,
  188. showSizeChanger: true,
  189. showTotal: (total) => `共 ${total} 条`,
  190. }}
  191. onChange={handleTableChange}
  192. />
  193. ),
  194. },
  195. {
  196. key: "2",
  197. label: "私发",
  198. children: (
  199. <Table
  200. dataSource={dataSource}
  201. columns={columns}
  202. rowKey="dateStr"
  203. loading={loading}
  204. pagination={{
  205. current: pagination.current,
  206. pageSize: pagination.pageSize,
  207. total: pagination.total,
  208. showSizeChanger: true,
  209. showTotal: (total) => `共 ${total} 条`,
  210. }}
  211. onChange={handleTableChange}
  212. />
  213. ),
  214. },
  215. {
  216. key: "3",
  217. label: "自动回复",
  218. children: (
  219. <Table
  220. dataSource={dataSource}
  221. columns={columns}
  222. rowKey="dateStr"
  223. loading={loading}
  224. pagination={{
  225. current: pagination.current,
  226. pageSize: pagination.pageSize,
  227. total: pagination.total,
  228. showSizeChanger: true,
  229. showTotal: (total) => `共 ${total} 条`,
  230. }}
  231. onChange={handleTableChange}
  232. />
  233. ),
  234. },
  235. {
  236. key: "4",
  237. label: "分链接",
  238. children: (
  239. <Table
  240. dataSource={dataSource}
  241. columns={specialColumns}
  242. rowKey="dateStr"
  243. loading={loading}
  244. pagination={{
  245. current: pagination.current,
  246. pageSize: pagination.pageSize,
  247. total: pagination.total,
  248. showSizeChanger: true,
  249. showTotal: (total) => `共 ${total} 条`,
  250. }}
  251. onChange={handleTableChange}
  252. />
  253. ),
  254. },
  255. ]}
  256. />
  257. </>
  258. )
  259. }
  260. export default Qw