page.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import React from 'react';
  2. import { Badge, ListView } from 'antd-mobile';
  3. import './index.less';
  4. import Page from '@src/containers/Page';
  5. import Assets from '@src/components/Assets';
  6. import { formatDate } from '@src/services/Tools';
  7. import ListData from '@src/services/ListData';
  8. import { My } from '../../../stores/my';
  9. export default class extends Page {
  10. initState() {
  11. return {
  12. listMap: {},
  13. };
  14. }
  15. initData() {
  16. return this.initListKeys(['message'])
  17. .then(() => {
  18. return this.getList('message', 1);
  19. });
  20. }
  21. initListKeys(keys) {
  22. const { listMap } = this.state;
  23. keys.forEach((key) => {
  24. listMap[key] = new ListData();
  25. });
  26. this.setState({ listMap });
  27. return Promise.resolve();
  28. }
  29. getList(key, page) {
  30. My.message(Object.assign({ page }, this.state.search))
  31. .then((result) => {
  32. const { listMap } = this.state;
  33. if (page === 1) { // todo 是否重新读取第一页为刷新所有数据
  34. listMap[key] = new ListData();
  35. }
  36. listMap[key].get(page, result, this.state.search.size);
  37. this.setState({ listMap });
  38. });
  39. }
  40. readAll() {
  41. My.readAllMessage()
  42. .then(() => {
  43. this.refresh();
  44. });
  45. }
  46. renderView() {
  47. return (
  48. <div>
  49. <div className="all" onClick={() => {
  50. this.readAll();
  51. }}>
  52. <Assets name="clean" />
  53. 全部已读2
  54. </div>
  55. {this.renderList()}
  56. </div>
  57. );
  58. }
  59. renderRow(rowData) {
  60. return <div className="item">
  61. <div className="detail">
  62. <div className="title">
  63. {rowData.title}
  64. {rowData.isRead > 0 && <Badge dot />}
  65. </div>
  66. <div className="desc">{rowData.description}</div>
  67. {rowData.link && <a href={rowData.link}>查看详情</a>}
  68. </div>
  69. <div className="date-time">
  70. <div className="date">{formatDate(rowData.createTime, 'YYYY-MM-DD')}</div>
  71. <div className="time">{formatDate(rowData.createTime, 'HH:mm:ss')}</div>
  72. </div>
  73. </div>;
  74. }
  75. renderList() {
  76. const { listMap } = this.state;
  77. const { message = {} } = listMap;
  78. const { dataSource = {}, bottom, loading, finish, maxSectionId = 1, total } = message;
  79. if (total === 0) return this.renderEmpty();
  80. return <ListView
  81. className="list"
  82. ref={el => { this.lv = el; }}
  83. dataSource={dataSource}
  84. renderFooter={() => (<div style={{ padding: 30, textAlign: 'center' }}>
  85. {loading ? '加载中...' : (bottom ? '没有更多了' : '')}
  86. </div>)}
  87. renderRow={(rowData, sectionID, rowID) => this.renderRow(rowData, sectionID, rowID)}
  88. style={{
  89. height: this.state.height,
  90. overflow: 'auto',
  91. }}
  92. pageSize={this.state.search.size}
  93. scrollRenderAheadDistance={500}
  94. onEndReached={() => {
  95. if (loading) return;
  96. if (bottom) {
  97. if (!finish) {
  98. message.finish = true;
  99. // this.setState({ time: new Date() })
  100. }
  101. return;
  102. }
  103. this.getList('message', maxSectionId + 1);
  104. }}
  105. onEndReachedThreshold={10}
  106. />;
  107. }
  108. renderEmpty() {
  109. return <div />;
  110. }
  111. }