page.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. import React from 'react';
  2. import './index.less';
  3. import { Drawer, ListView } from 'antd-mobile';
  4. import Page from '@src/containers/Page';
  5. import Assets from '@src/components/Assets';
  6. import { formatTreeData } from '@src/services/Tools';
  7. import ListData from '@src/services/ListData';
  8. import { DataBlock } from '../../../components/Block';
  9. import { SpecialRadioGroup } from '../../../components/Radio';
  10. import Switch from '../../../components/Switch';
  11. import Button from '../../../components/Button';
  12. import Checkbox from '../../../components/CheckBox';
  13. import { Course } from '../../../stores/course';
  14. import { Main } from '../../../stores/main';
  15. import { DataType } from '../../../../Constant';
  16. export default class extends Page {
  17. initState() {
  18. return {
  19. listMap: {},
  20. };
  21. }
  22. init() {
  23. Main.dataStruct().then(list => {
  24. const structTree = formatTreeData(
  25. list.map(row => {
  26. row.title = `${row.titleZh} ${row.titleEn}`;
  27. row.label = row.title;
  28. row.key = row.id;
  29. return row;
  30. }),
  31. 'id',
  32. 'title',
  33. 'parentId',
  34. );
  35. this.setState({ structTree });
  36. });
  37. const { search } = this.state;
  38. if (!search.order) search.order = 'saleNumber';
  39. this.setState({ search });
  40. }
  41. initData() {
  42. return this.initListKeys(['data']).then(() => {
  43. return this.getList('data', 1);
  44. });
  45. }
  46. initListKeys(keys) {
  47. const { listMap = {} } = this.state;
  48. keys.forEach(key => {
  49. listMap[key] = new ListData();
  50. });
  51. this.setState({ listMap });
  52. return Promise.resolve();
  53. }
  54. getList(key, page) {
  55. Course.listData(Object.assign({ page }, this.state.search)).then(result => {
  56. const { listMap = {} } = this.state;
  57. if (page === 1) {
  58. // todo 是否重新读取第一页为刷新所有数据
  59. listMap[key] = new ListData();
  60. }
  61. listMap[key].get(page, result, this.state.search.size);
  62. this.setState({ listMap });
  63. });
  64. }
  65. changeOrder(order, direction) {
  66. const { search = {} } = this.state;
  67. search.order = order;
  68. search.direction = direction;
  69. this.setState({ search });
  70. this.refresh();
  71. }
  72. changeNovice() {
  73. const { search = {} } = this.state;
  74. search.isNovice = !search.isNovice;
  75. this.setState({ search });
  76. }
  77. changeOriginal() {
  78. const { search = {} } = this.state;
  79. search.isOriginal = !search.isOriginal;
  80. this.setState({ search });
  81. }
  82. changeDataType(value) {
  83. const { search = {} } = this.state;
  84. search.dataType = value;
  85. this.setState({ search });
  86. }
  87. changeStruct(value) {
  88. const { search = {} } = this.state;
  89. search.structId = value;
  90. this.setState({ search });
  91. }
  92. renderView() {
  93. const { filter, search = {} } = this.state;
  94. return (
  95. <Drawer
  96. style={{ minHeight: document.documentElement.clientHeight }}
  97. position="right"
  98. open={filter}
  99. sidebar={this.renderFilter()}
  100. onOpenChange={isOpen => this.setState({ filter: isOpen })}
  101. >
  102. <div className="top">
  103. <div
  104. className={search.order === 'saleNumber' ? 'tab active' : 'tab'}
  105. onClick={() => {
  106. if (search.order !== 'saleNumber') this.changeOrder('saleNumber', 'desc');
  107. }}
  108. >
  109. 销量
  110. </div>
  111. <div
  112. className={search.order === 'updateTime' ? 'tab active' : 'tab'}
  113. onClick={() => {
  114. if (search.order !== 'updateTime') this.changeOrder('updateTime', 'desc');
  115. }}
  116. >
  117. 更新时间
  118. </div>
  119. <div className="right" onClick={() => this.setState({ filter: true })}>
  120. 筛选
  121. <Assets name="screen" />
  122. </div>
  123. </div>
  124. {this.renderList()}
  125. </Drawer>
  126. );
  127. }
  128. renderRow(rowData) {
  129. return <DataBlock data={rowData} />;
  130. }
  131. renderList() {
  132. const { listMap } = this.state;
  133. const { data = {} } = listMap;
  134. const { dataSource = {}, bottom, loading, finish, maxSectionId = 1, total } = data;
  135. if (total === 0) return this.renderEmpty();
  136. return (
  137. <ListView
  138. className="list"
  139. ref={el => {
  140. this.lv = el;
  141. }}
  142. dataSource={dataSource}
  143. renderFooter={() => (
  144. <div style={{ padding: 30, textAlign: 'center' }}>{loading ? '加载中...' : bottom ? '没有更多了' : ''}</div>
  145. )}
  146. renderRow={(rowData, sectionID, rowID) => this.renderRow(rowData, sectionID, rowID)}
  147. style={{
  148. height: this.state.height,
  149. overflow: 'auto',
  150. }}
  151. pageSize={this.state.search.size}
  152. scrollRenderAheadDistance={500}
  153. onEndReached={() => {
  154. if (loading) return;
  155. if (bottom) {
  156. if (!finish) {
  157. data.finish = true;
  158. // this.setState({ time: new Date() })
  159. }
  160. return;
  161. }
  162. this.getList('data', maxSectionId + 1);
  163. }}
  164. onEndReachedThreshold={10}
  165. />
  166. );
  167. }
  168. renderEmpty() {
  169. return <div />;
  170. }
  171. renderFilter() {
  172. const { search, structTree = [] } = this.state;
  173. return (
  174. <div className="filter">
  175. <div className="body">
  176. <div className="item">
  177. <div className="label left">适合新手</div>
  178. <div className="value right">
  179. <Switch
  180. checked={search.isNovice}
  181. onClick={() => {
  182. this.changeNovice();
  183. }}
  184. />
  185. </div>
  186. </div>
  187. <div className="item">
  188. <div className="label left">原创资料</div>
  189. <div className="value right">
  190. <Switch
  191. checked={search.isOriginal}
  192. onClick={() => {
  193. this.changeOriginal();
  194. }}
  195. />
  196. </div>
  197. </div>
  198. <div className="item">
  199. <div className="label left">资料形式</div>
  200. <div className="value right">
  201. <SpecialRadioGroup
  202. list={DataType}
  203. value={search.dataType}
  204. onChange={value => {
  205. this.changeDataType(value);
  206. }}
  207. />
  208. </div>
  209. </div>
  210. <div className="sub">
  211. <div className="title">筛选学科</div>
  212. {structTree.map(row => {
  213. return (
  214. <div className="item">
  215. <div className={row.children.length > 0 ? 'label' : 'label left'}>{row.title}</div>
  216. {row.children.length > 0 && (
  217. <div className="value">
  218. <SpecialRadioGroup
  219. list={row.children}
  220. value={search.structId}
  221. onChange={value => {
  222. this.changeStruct(value);
  223. }}
  224. />
  225. </div>
  226. )}
  227. {row.children.length === 0 && (
  228. <div className="value right">
  229. <Checkbox
  230. checked={search.structId === row.id}
  231. onClick={() => {
  232. this.changeStruct(row.id);
  233. }}
  234. />
  235. </div>
  236. )}
  237. </div>
  238. );
  239. })}
  240. </div>
  241. </div>
  242. <div className="footer">
  243. <Button
  244. radius
  245. width={90}
  246. onClick={() => {
  247. this.getList('data', 1);
  248. }}
  249. >
  250. 确定
  251. </Button>
  252. </div>
  253. </div>
  254. );
  255. }
  256. }