page.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. import React from 'react';
  2. import './index.less';
  3. import Assets from '@src/components/Assets';
  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 Modal from '../../../components/Modal';
  9. import { Button } from '../../../components/Button';
  10. import { Textbook } from '../../../stores/textbook';
  11. import { My } from '../../../stores/my';
  12. import { Main } from '../../../stores/main';
  13. import { User } from '../../../stores/user';
  14. import { TextbookSubject, TextbookQuality, TextbookType } from '../../../../Constant';
  15. import { getMap, formatDate } from '../../../../../src/services/Tools';
  16. const TextbookSubjectMap = getMap(TextbookSubject, 'value', 'label');
  17. const TextbookQualityMap = getMap(TextbookQuality, 'value', 'label');
  18. const TextbookTypeMap = getMap(TextbookType, 'value', 'label');
  19. export default class extends Page {
  20. constructor(props) {
  21. super(props);
  22. this.keyMap = {};
  23. window.onkeydown = this.onKeydown.bind(this);
  24. window.onkeyup = this.onKeyup.bind(this);
  25. }
  26. initState() {
  27. const { info = {} } = this.props.user;
  28. return {
  29. open: false,
  30. showTip: !info.textbookTips,
  31. subject: TextbookSubject[0].value,
  32. textbookSubject: TextbookSubject.map(row => {
  33. return {
  34. title: row.label,
  35. key: row.value,
  36. };
  37. }),
  38. };
  39. }
  40. init() {
  41. Main.getBase()
  42. .then(result => {
  43. this.setState({ base: result });
  44. });
  45. }
  46. initData() {
  47. Textbook.getInfo()
  48. .then(result => {
  49. if (!result.hasService) {
  50. linkTo('/textbook');
  51. }
  52. this.setState({ data: result });
  53. console.log(this.state);
  54. this.refreshItem(this.state.search.no || 1);
  55. });
  56. }
  57. refreshItem(no) {
  58. const { subject } = this.params;
  59. const { data } = this.state;
  60. Textbook.noTopic(data.latest.id, subject, no)
  61. .then(result => {
  62. this.setState({ item: result });
  63. });
  64. }
  65. onKeydown(e) {
  66. let active = false;
  67. if (this.keyMap[e.keyCode]) return false;
  68. switch (e.keyCode) {
  69. case 32:
  70. active = true;
  71. break;
  72. case 37:
  73. active = true;
  74. break;
  75. case 39:
  76. active = true;
  77. break;
  78. default:
  79. break;
  80. }
  81. if (active) {
  82. this.keyMap[e.keyCode] = true;
  83. return false;
  84. }
  85. return true;
  86. }
  87. onKeyup(e) {
  88. let active = false;
  89. switch (e.keyCode) {
  90. case 32:
  91. active = true;
  92. this.onOpen();
  93. break;
  94. case 37:
  95. active = true;
  96. this.onPrev();
  97. break;
  98. case 39:
  99. active = true;
  100. this.onNext();
  101. break;
  102. default:
  103. break;
  104. }
  105. if (active) {
  106. this.keyMap[e.keyCode] = false;
  107. return false;
  108. }
  109. return true;
  110. }
  111. onOpen() {
  112. this.setState({ open: !this.state.open });
  113. }
  114. onNext() {
  115. const { subject } = this.params;
  116. const { item, data } = this.state;
  117. const no = item.no + 1;
  118. if (no > data.latest[`${subject}Number`]) return;
  119. this.refreshItem(no);
  120. }
  121. onPrev() {
  122. const { item } = this.state;
  123. const no = item.no - 1;
  124. if (no === 0) return;
  125. this.refreshItem(no);
  126. }
  127. closeTips() {
  128. this.setState({ showTip: false });
  129. My.textbookTips()
  130. .then(() => {
  131. const { info } = this.props.user;
  132. info.textbookTips = 1;
  133. User.infoHandle(info);
  134. });
  135. }
  136. changeSubject(subject) {
  137. linkTo(`/textbook/topic/list/${subject}`);
  138. }
  139. renderView() {
  140. const { showTip, base = {}, subject, data = {}, textbookSubject } = this.state;
  141. const { latest = {} } = data;
  142. return (
  143. <div>
  144. <div className="top content t-8">
  145. 机经 > 本期机经 > <span className="t-1">{TextbookSubjectMap[subject]}</span>
  146. <Select className="f-r m-t-1" size="small" theme="white" value={subject} list={textbookSubject} onChange={({ key }) => this.changeSubject(key)} />
  147. </div>
  148. <div className="center content">
  149. 【{TextbookSubjectMap[subject]}】{latest.startDate && formatDate(latest.startDate, 'MMDD')} 起{TextbookSubjectMap[subject]}机经整理
  150. {this.renderDetail()}
  151. <Assets className="prev" name="footer_previous_highlight_1" onClick={() => this.onPrev()} />
  152. <Assets className="next" name="footer_next_highlight_1" onClick={() => this.onNext()} />
  153. </div>
  154. <Contact data={base.contact} />
  155. <Footer />
  156. <Modal
  157. show={showTip}
  158. title="提示"
  159. onConfirm={() => this.closeTips()}
  160. confirmText="好的,知道了"
  161. btnAlign="center"
  162. >
  163. <div className="t-2 t-s-18">敲击键盘← →可翻页;</div>
  164. <div className="t-2 t-s-18">敲击空格键第一次查看解析,敲击空格键第二次收起解析。</div>
  165. </Modal>
  166. </div>
  167. );
  168. }
  169. renderDetail() {
  170. const { open, subject, item = {} } = this.state;
  171. return (
  172. <div className="detail">
  173. <div className="m-b-1">
  174. <span className="t-1 t-s-18 m-r-1">NO.{item.no} {subject === 'quant' ? TextbookTypeMap[item.keyword] : item.keyword}</span>
  175. <span className="t-3 t-s-12 m-r-1">{TextbookQualityMap[item.quality]}</span>
  176. <span className="t-3 t-s-12 m-r-1">{item.createTime && formatDate(item.createTime, 'YYYY-MM-DD HH:mm:ss')}</span>
  177. <Button radius className="f-r" onClick={() => this.onOpen()}>
  178. {open ? '收起' : '展开'}解析
  179. </Button>
  180. </div>
  181. <div className="t-2 t-s-16 m-b-2" dangerouslySetInnerHTML={{ __html: item.detail }} />
  182. <div hidden={!open} className="p-20 b-c-3">
  183. <div className="t t-1 t-s-16 f-w-b m-b-5">官方解析</div>
  184. <div className="t-1 t-s-16" dangerouslySetInnerHTML={{ __html: item.content }} />
  185. </div>
  186. </div>
  187. );
  188. }
  189. }