index.tsx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 subChannelColumns:TableProps['columns'] = [
  78. {
  79. title: '日期',
  80. dataIndex: 'dateStr',
  81. key: 'dateStr',
  82. },
  83. {
  84. title: '子渠道',
  85. dataIndex: 'subChannel',
  86. key: 'subChannel',
  87. },
  88. {
  89. title: '小程序访问人数',
  90. dataIndex: 'firstLevel',
  91. key: 'firstLevel',
  92. },
  93. {
  94. title: '传播得分',
  95. dataIndex: 'score',
  96. key: 'score',
  97. },
  98. ];
  99. const fetchQwData = async (page = 1, pageSize = 10, type = 0) => {
  100. try {
  101. setLoading(true);
  102. const res = await http.post<QwDataResponse>(qwDataList, {
  103. pageNum: page,
  104. pageSize: pageSize,
  105. type: type
  106. });
  107. if (res.success && res.data) {
  108. setDataSource(res.data.objs || []);
  109. setPagination({
  110. current: res.data.currentPage,
  111. pageSize: res.data.pageSize,
  112. total: res.data.totalSize
  113. });
  114. }
  115. } catch (error) {
  116. console.error('获取企微数据失败:', error);
  117. } finally {
  118. setLoading(false);
  119. }
  120. };
  121. useEffect(() => {
  122. fetchQwData(1, 10, 0);
  123. }, []);
  124. const onChange = (key: string) => {
  125. console.log(key);
  126. // 先清空数据源,避免显示上一个tab的数据
  127. setDataSource([]);
  128. fetchQwData(1, 10, Number(key));
  129. };
  130. const handleTableChange = (pagination: any) => {
  131. const { current, pageSize } = pagination;
  132. // 先清空数据源
  133. setDataSource([]);
  134. fetchQwData(current, pageSize, Number(activeKey));
  135. };
  136. const handleDownload = async () => {
  137. try {
  138. setDownloadLoading(true);
  139. // 根据当前选中的标签页设置type参数
  140. const type = Number(activeKey);
  141. // 使用当前分页信息
  142. const response = await http.post(qwDataExport, {
  143. pageNum: pagination.current,
  144. pageSize: pagination.pageSize,
  145. type: type
  146. });
  147. if (response.success && response.data) {
  148. window.open(response.data as string);
  149. } else {
  150. message.error('下载失败');
  151. }
  152. } catch {
  153. message.error('下载失败');
  154. } finally {
  155. setDownloadLoading(false);
  156. }
  157. };
  158. return (
  159. <>
  160. <div className={"flex mb-[10px]"}>
  161. <div className={"flex-1 leading-[32px]"}>企微数据统计</div>
  162. </div>
  163. <Tabs
  164. defaultActiveKey="0"
  165. type="card"
  166. tabBarExtraContent={{
  167. right: <Button
  168. type="link"
  169. icon={<DownloadOutlined />}
  170. loading={downloadLoading}
  171. onClick={handleDownload}
  172. >
  173. 下载数据
  174. </Button>
  175. }}
  176. onChange={(key) => {
  177. setActiveKey(key);
  178. onChange(key);
  179. }}
  180. items={[
  181. {
  182. key: "0",
  183. label: "总体",
  184. children: (
  185. <Table
  186. dataSource={dataSource}
  187. columns={columns}
  188. rowKey="dateStr"
  189. loading={loading}
  190. pagination={{
  191. current: pagination.current,
  192. pageSize: pagination.pageSize,
  193. total: pagination.total,
  194. showSizeChanger: true,
  195. showTotal: (total) => `共 ${total} 条`,
  196. }}
  197. onChange={handleTableChange}
  198. />
  199. ),
  200. },
  201. {
  202. key: "1",
  203. label: "群发",
  204. children: (
  205. <Table
  206. dataSource={dataSource}
  207. columns={columns}
  208. rowKey="dateStr"
  209. loading={loading}
  210. pagination={{
  211. current: pagination.current,
  212. pageSize: pagination.pageSize,
  213. total: pagination.total,
  214. showSizeChanger: true,
  215. showTotal: (total) => `共 ${total} 条`,
  216. }}
  217. onChange={handleTableChange}
  218. />
  219. ),
  220. },
  221. {
  222. key: "2",
  223. label: "私发",
  224. children: (
  225. <Table
  226. dataSource={dataSource}
  227. columns={columns}
  228. rowKey="dateStr"
  229. loading={loading}
  230. pagination={{
  231. current: pagination.current,
  232. pageSize: pagination.pageSize,
  233. total: pagination.total,
  234. showSizeChanger: true,
  235. showTotal: (total) => `共 ${total} 条`,
  236. }}
  237. onChange={handleTableChange}
  238. />
  239. ),
  240. },
  241. {
  242. key: "3",
  243. label: "自动回复",
  244. children: (
  245. <Table
  246. dataSource={dataSource}
  247. columns={columns}
  248. rowKey="dateStr"
  249. loading={loading}
  250. pagination={{
  251. current: pagination.current,
  252. pageSize: pagination.pageSize,
  253. total: pagination.total,
  254. showSizeChanger: true,
  255. showTotal: (total) => `共 ${total} 条`,
  256. }}
  257. onChange={handleTableChange}
  258. />
  259. ),
  260. },
  261. {
  262. key: "4",
  263. label: "分链接",
  264. children: (
  265. <Table
  266. dataSource={dataSource}
  267. columns={specialColumns}
  268. rowKey="dateStr"
  269. loading={loading}
  270. pagination={{
  271. current: pagination.current,
  272. pageSize: pagination.pageSize,
  273. total: pagination.total,
  274. showSizeChanger: true,
  275. showTotal: (total) => `共 ${total} 条`,
  276. }}
  277. onChange={handleTableChange}
  278. />
  279. ),
  280. },
  281. {
  282. key: "5",
  283. label: "分子渠道",
  284. children: (
  285. <Table
  286. dataSource={dataSource}
  287. columns={subChannelColumns}
  288. rowKey="dateStr"
  289. loading={loading}
  290. pagination={{
  291. current: pagination.current,
  292. pageSize: pagination.pageSize,
  293. total: pagination.total,
  294. showSizeChanger: true,
  295. showTotal: (total) => `共 ${total} 条`,
  296. }}
  297. onChange={handleTableChange}
  298. />
  299. ),
  300. },
  301. ]}
  302. />
  303. </>
  304. )
  305. }
  306. export default Qw