page.js 6.4 KB

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