page.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import React from 'react';
  2. import { Link } from 'react-router-dom';
  3. import './index.less';
  4. import Page from '@src/containers/Page';
  5. import Footer from '../../../components/Footer';
  6. import { Contact } from '../../../components/Other';
  7. import Select from '../../../components/Select';
  8. import UserTable from '../../../components/UserTable';
  9. import { Textbook } from '../../../stores/textbook';
  10. import { Main } from '../../../stores/main';
  11. import { TextbookSubject, TextbookQuality, TextbookType } from '../../../../Constant';
  12. import { getMap, formatDate } from '../../../../../src/services/Tools';
  13. const TextbookSubjectMap = getMap(TextbookSubject, 'value', 'label');
  14. const TextbookQualityMap = getMap(TextbookQuality, 'value', 'label');
  15. const TextbookTypeMap = getMap(TextbookType, 'value', 'label');
  16. export default class extends Page {
  17. initState() {
  18. return {
  19. subject: TextbookSubject[0].value,
  20. textbookSubject: TextbookSubject.map(row => {
  21. return {
  22. title: row.label,
  23. key: row.value,
  24. };
  25. }),
  26. textbookQuality: TextbookQuality.map(row => {
  27. return {
  28. title: row.label,
  29. key: row.value,
  30. };
  31. }),
  32. textbookType: TextbookType.map(row => {
  33. return {
  34. title: row.label,
  35. key: row.value,
  36. };
  37. }),
  38. defaultSortMap: { no: 'desc' },
  39. };
  40. }
  41. init() {
  42. this.baseColumns = [
  43. {
  44. title: '文章编号',
  45. key: 'no',
  46. sort: true,
  47. render: (text) => {
  48. return <Link to={`/textbook/topic/detail/${this.params.subject}?no=${text}`}>{text}</Link>;
  49. },
  50. },
  51. { title: '关键词', key: 'keyword', render: (text) => (this.params.subject === 'quant' ? TextbookTypeMap[text] : text) },
  52. { title: '机经质量', key: 'quality', render: (text) => TextbookQualityMap[text] || '' },
  53. ];
  54. this.quantColumns = [
  55. {
  56. title: '题号',
  57. key: 'no',
  58. sort: true,
  59. render: (text) => {
  60. return <Link to={`/textbook/topic/detail/${this.params.subject}?no=${text}`}>{text}</Link>;
  61. },
  62. },
  63. { title: '题型', key: 'keyword', render: (text) => (this.params.subject === 'quant' ? TextbookTypeMap[text] : text) },
  64. { title: '机经质量', key: 'quality', render: (text) => TextbookQualityMap[text] || '' },
  65. ];
  66. Main.getBase()
  67. .then(result => {
  68. this.setState({ base: result });
  69. });
  70. Textbook.getInfo()
  71. .then(result => {
  72. if (!result.hasService) {
  73. linkTo('/textbook');
  74. }
  75. this.setState({ data: result });
  76. });
  77. }
  78. initData() {
  79. const { subject } = this.params;
  80. const data = Object.assign(this.state, this.state.search);
  81. console.log(data);
  82. data.filterMap = this.state.search;
  83. if (data.order) {
  84. data.sortMap = { [data.order]: data.direction };
  85. } else if (data.order !== null && data.order !== '') {
  86. data.sortMap = Object.assign({}, this.state.defaultSortMap);
  87. data.order = 'no';
  88. data.direction = 'desc';
  89. }
  90. this.setState(data);
  91. Textbook.listTopic(Object.assign({ latest: true, subject, order: data.order, direction: data.direction }, this.state.search, { isOld: data.month === '2' ? true : (data.month === '1' ? false : null) }))
  92. .then(result => {
  93. if (this.state.search.page === 1) {
  94. result.list = result.list.map(row => {
  95. row.new = '';
  96. return row;
  97. });
  98. }
  99. this.setState({ list: result.list, total: result.total });
  100. });
  101. }
  102. filter(data) {
  103. data.page = 1;
  104. this.search(data);
  105. }
  106. onSort(value) {
  107. const keys = Object.keys(value);
  108. const { sortMap, defaultSortMap } = this.state;
  109. const index = keys.length > 1 && sortMap[keys[0]] ? 1 : 0;
  110. let order = keys.length && value[keys[index]] ? keys[index] : null;
  111. let direction = keys.length ? value[keys[index]] : null;
  112. if (order == null) {
  113. const [prevOrder] = Object.keys(sortMap);
  114. console.log(prevOrder, defaultSortMap[prevOrder]);
  115. if (!defaultSortMap[prevOrder]) {
  116. [order] = Object.keys(defaultSortMap);
  117. direction = defaultSortMap[order];
  118. }
  119. }
  120. this.search({ order, direction }, false);
  121. this.initData();
  122. }
  123. onChangePage(page) {
  124. this.search({ page });
  125. }
  126. changeSubject(subject) {
  127. linkTo(`/textbook/topic/list/${subject}`);
  128. }
  129. renderView() {
  130. const { subject } = this.params;
  131. const { base = {}, textbookSubject, textbookQuality, textbookType, keyword, quality, month, data = {}, list = [], page, total, sortMap } = this.state;
  132. const { latest = {} } = data;
  133. return (
  134. <div>
  135. <div className="top content t-8">
  136. <Link to="/textbook">机经</Link> > 本期机经 > <span className="t-1">{TextbookSubjectMap[subject]}</span>
  137. <Select className="f-r m-t-1" size="small" theme="white" value={subject} list={textbookSubject} onChange={({ key }) => this.changeSubject(key)} />
  138. </div>
  139. <div className="center content">
  140. <div className="t-1 t-s-18 m-b-1">
  141. 【{TextbookSubjectMap[subject]}】{latest.startDate && formatDate(latest.startDate, 'MMDD')} 起{TextbookSubjectMap[subject]}机经整理
  142. <Select className="f-r m-l-1" size="small" theme="default" value={quality} placeholder={'机经质量'} list={textbookQuality} onChange={({ key }) => this.filter({ quality: key })} />
  143. <Select
  144. className="f-r m-l-1"
  145. size="small"
  146. theme="default"
  147. value={month}
  148. list={[{ title: '全部', key: '' }, { title: '本月', key: '1' }, { title: '考古', key: '2' }]}
  149. onChange={({ key }) => this.filter({ month: key })}
  150. />
  151. {subject === 'quant' && <Select
  152. className="f-r m-l-1"
  153. size="small"
  154. theme="default"
  155. value={keyword}
  156. list={textbookType}
  157. placeholder={'题型'}
  158. onChange={({ key }) => this.filter({ keyword: key })}
  159. />}
  160. </div>
  161. <UserTable
  162. size="small"
  163. data={list}
  164. sortMap={sortMap}
  165. onSort={v => this.onSort(v)}
  166. current={page}
  167. pageSize={this.state.size}
  168. total={total}
  169. onChange={p => this.onChangePage(p)}
  170. jump
  171. columns={subject === 'quant' ? this.quantColumns : this.baseColumns}
  172. />
  173. </div>
  174. <Contact data={base.contact} />
  175. <Footer />
  176. </div>
  177. );
  178. }
  179. }