PostData.jsx 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import React, { useEffect, useMemo, useRef } from 'react';
  2. import { connect,history } from 'umi';
  3. import moment from 'moment';
  4. import { PageContainer } from '@ant-design/pro-layout';
  5. import ProTable from '@ant-design/pro-table';
  6. import { queryTable } from '@/utils/request';
  7. import {Space} from 'antd'
  8. const PostData = (props) => {
  9. const tableRef = useRef()
  10. const changeQueryParam = (params) => {
  11. const dt = params.date
  12. const startDate = dt ? dt[0] : undefined
  13. const endDate = dt ? dt[1] : undefined
  14. return {
  15. ...params,
  16. startDate: startDate ? moment(startDate).format('YYYY-MM-DD') : undefined,
  17. endDate: endDate ? moment(endDate).format('YYYY-MM-DD') : undefined,
  18. date: undefined,
  19. }
  20. }
  21. const typeDict = useMemo(() => {
  22. return (props.typeList || []).reduce((acc, item) => {
  23. return {
  24. ...acc,
  25. [item.typeId]: { text: item.name },
  26. };
  27. }, {});
  28. }, [props.typeList]);
  29. const columns = [
  30. {
  31. title: '日期',
  32. key: 'date',
  33. hideInTable: true,
  34. valueType: 'dateRange',
  35. },
  36. {
  37. title: '名称',
  38. key: 'name',
  39. hideInTable: true,
  40. },
  41. {
  42. title: '排名',
  43. key: 'topN',
  44. hideInSearch: true,
  45. render: (_, $, index) => index + 1,
  46. },
  47. {
  48. title: '科普知识',
  49. key: 'postId',
  50. dataIndex: 'postId',
  51. hideInSearch: true,
  52. render: (_, row) => (
  53. <div>
  54. <h3>{row.name}</h3>
  55. <p>
  56. <small>{`作者: ${row.author}`}</small>
  57. </p>
  58. <p>
  59. <small>{row.isVideo ? '视频' : '图文'}</small>
  60. </p>
  61. </div>
  62. ),
  63. },
  64. {
  65. title: '分类',
  66. key: 'typeId',
  67. dataIndex: 'typeId',
  68. valueType: 'select',
  69. valueEnum: typeDict,
  70. },
  71. {
  72. title: '阅读人数',
  73. key: 'uv',
  74. dataIndex: 'uv',
  75. hideInSearch: true,
  76. sorter: true,
  77. render: (text, row) => (
  78. <Space size="middle">
  79. {text?<a onClick={() => history.push(`/report/student?postId=${row.postId}`)}>{text}</a>:0}
  80. </Space>
  81. ),
  82. },
  83. {
  84. title: '浏览量',
  85. key: 'pv',
  86. dataIndex: 'pv',
  87. hideInSearch: true,
  88. sorter: true,
  89. },
  90. {
  91. title: '收藏量',
  92. key: 'savedNum',
  93. dataIndex: 'savedNum',
  94. hideInSearch: true,
  95. sorter: true,
  96. },
  97. ];
  98. useEffect(() => {
  99. if (!props.typeList || !props.typeList.length) {
  100. props.dispatch({
  101. type: 'post/getTypeList',
  102. payload: { pageSize: 999 },
  103. });
  104. }
  105. }, [props]);
  106. return (
  107. <PageContainer>
  108. <ProTable
  109. actionRef={tableRef}
  110. columns={columns}
  111. request={queryTable('/statis/post-data')}
  112. rowKey="postId"
  113. search={{
  114. labelWidth: '4em',
  115. }}
  116. options={false}
  117. beforeSearchSubmit={changeQueryParam}
  118. />
  119. </PageContainer>
  120. );
  121. };
  122. export default connect((s) => ({
  123. typeList: s.post.typeList,
  124. }))(PostData);