list.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import React, {PureComponent} from "react";
  2. import {MainWrapper, TagsTop, TagsWrapper} from "./style";
  3. import axios from "axios";
  4. import {connect} from "react-redux";
  5. import CatList from "../../components/List";
  6. import PagInation from '../../components/PagInation';
  7. const getrand = (m, n) => {
  8. return Math.floor(Math.random() * (n - m + 1)) + m;
  9. };
  10. class TagList extends PureComponent {
  11. constructor(props) {
  12. super(props);
  13. this.state = {
  14. id: props.match.params.id,
  15. timg: '',
  16. finished: false,
  17. loading: true,
  18. page: 1,
  19. list: []
  20. };
  21. this.getList = this.getList.bind(this);
  22. }
  23. render() {
  24. const {timg, list, page, finished, loading, id} = this.state;
  25. return (
  26. <TagsWrapper>
  27. <div className='pattern-center-blank'/>
  28. <TagsTop>
  29. <div className='pattern-attachment-img'>
  30. <img className='lazyload' src={timg} alt=""/>
  31. </div>
  32. <div className='pattern-header '>
  33. <h1>标签墙</h1>
  34. </div>
  35. </TagsTop>
  36. <MainWrapper>
  37. <CatList list={list}/>
  38. <PagInation page={page} id={id} finished={finished} loading={loading} getList={this.getList}/>
  39. </MainWrapper>
  40. </TagsWrapper>
  41. )
  42. }
  43. componentDidMount() {
  44. this.getList(1, this.state.id, true);
  45. this.getTimg();
  46. }
  47. getList(page, id, override) {
  48. this.setState({loading: true});
  49. axios.get('/posts/posts/v1/list', {
  50. params: {
  51. page: page,
  52. size: 10,
  53. postsTagsId: id
  54. }
  55. }).then((res) => {
  56. if (res.success === 1) {
  57. let current = res.pageInfo.page * res.pageInfo.size;
  58. let total = res.pageInfo.total;
  59. const data = res.models;
  60. const Img = this.props.ListImg;
  61. let arr = [];
  62. data.forEach((item) => {
  63. arr.push({
  64. id: item.id,
  65. title: item.title,
  66. thumbnail: item.thumbnail || Img[getrand(0, Img.length - 1)].img,
  67. comments: item.comments,
  68. status: item.status,
  69. summary: item.summary,
  70. views: item.views,
  71. createTime: item.createTime,
  72. syncStatus: item.syncStatus,
  73. author: item.author,
  74. categoryName: item.categoryName
  75. })
  76. });
  77. this.setState((prevState) => {
  78. return {
  79. list: override ? arr : [...prevState.list, ...arr],
  80. page: page + 1,
  81. loading: false
  82. }
  83. });
  84. if (current > total) {
  85. this.setState({
  86. finished: true
  87. })
  88. }
  89. }
  90. });
  91. }
  92. getTimg() {
  93. const list = this.props.topImg;
  94. const num = getrand(0, list.length - 1);
  95. this.setState({timg: list[num].img});
  96. }
  97. }
  98. const mapState = (state) => {
  99. return {
  100. topImg: state.getIn(['image', 'topImg']),
  101. ListImg: state.getIn(['image', 'ListImg']),
  102. }
  103. };
  104. export default connect(mapState)(TagList)