page.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import React from 'react';
  2. import './index.less';
  3. import { ListView } from 'antd-mobile';
  4. import Page from '@src/containers/Page';
  5. import ListData from '@src/services/ListData';
  6. import { BuyBlock } from '../../../components/Block';
  7. import { Order } from '../../../stores/order';
  8. export default class extends Page {
  9. initState() {
  10. return {
  11. listMap: {},
  12. };
  13. }
  14. init() {
  15. }
  16. initData() {
  17. return this.initListKeys(['data']).then(() => {
  18. return this.getList('data', 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. Order.listRecord(Object.assign({ page }, this.state.search)).then(result => {
  31. const { listMap = {} } = this.state;
  32. if (page === 1) {
  33. // todo 是否重新读取第一页为刷新所有数据
  34. listMap[key] = new ListData();
  35. }
  36. listMap[key].get(page, result, this.state.search.size);
  37. this.setState({ listMap });
  38. });
  39. }
  40. renderView() {
  41. return (
  42. this.renderList()
  43. );
  44. }
  45. renderRow(rowData) {
  46. let theme = 'default';
  47. if (!rowData.isUsed) {
  48. theme = 'not';
  49. } else if (rowData.useEndTime && new Date(rowData.useEndTime).getTime() < new Date().getTime()) {
  50. theme = 'end';
  51. }
  52. return <BuyBlock data={rowData} theme={theme} onOpen={() => {
  53. linkTo(`/open/${rowData.id}`);
  54. }} onBuy={() => {
  55. linkTo(`/product/service/${rowData.service}`);
  56. }} onRead={() => {
  57. if (rowData.service === 'textbook') {
  58. linkTo('/textbook');
  59. } else {
  60. openLink(rowData.data.resource);
  61. }
  62. }} />;
  63. }
  64. renderList() {
  65. const { listMap } = this.state;
  66. const { data = {} } = listMap;
  67. const { dataSource = {}, bottom, loading, finish, maxSectionId = 1, total } = data;
  68. if (total === 0) return this.renderEmpty();
  69. return (
  70. <ListView
  71. className="list"
  72. ref={el => {
  73. this.lv = el;
  74. }}
  75. dataSource={dataSource}
  76. renderFooter={() => (
  77. <div style={{ padding: 30, textAlign: 'center' }}>{loading ? '加载中...' : bottom ? '没有更多了' : ''}</div>
  78. )}
  79. renderRow={(rowData, sectionID, rowID) => this.renderRow(rowData, sectionID, rowID)}
  80. style={{
  81. height: this.state.height,
  82. overflow: 'auto',
  83. }}
  84. pageSize={this.state.search.size}
  85. scrollRenderAheadDistance={500}
  86. onEndReached={() => {
  87. if (loading) return;
  88. if (bottom) {
  89. if (!finish) {
  90. data.finish = true;
  91. // this.setState({ time: new Date() })
  92. }
  93. return;
  94. }
  95. this.getList('data', maxSectionId + 1);
  96. }}
  97. onEndReachedThreshold={10}
  98. />
  99. );
  100. }
  101. renderEmpty() {
  102. return <div />;
  103. }
  104. }