page.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. import React from 'react';
  2. import './index.less';
  3. import { Link } from 'react-router-dom';
  4. import { Anchor, Icon } from 'antd';
  5. import Assets from '@src/components/Assets';
  6. import Page from '@src/containers/Page';
  7. import Select from '../../../components/Select';
  8. import Button from '../../../components/Button';
  9. import QAList from '../../../components/QAList';
  10. import Tabs from '../../../components/Tabs';
  11. import { SuppleModal, SuppleFinishModal } from '../../../components/OtherModal';
  12. import { Main } from '../../../stores/main';
  13. import { User } from '../../../stores/user';
  14. import { getMap, formatTreeData } from '../../../../../src/services/Tools';
  15. export default class extends Page {
  16. initState() {
  17. this.areaMap = {};
  18. this.categoryMap = {};
  19. this.loadArticleMap = {};
  20. this.dataList = null;
  21. return {
  22. readTab: '1',
  23. // load: false,
  24. // list: [
  25. // {
  26. // key: '1',
  27. // title: '了解GMAT',
  28. // children: true,
  29. // },
  30. // { key: '2', title: '推荐阅读' },
  31. // { key: '3', title: '考场查询' },
  32. // { key: '4', title: '熟悉备考资料', children: true },
  33. // ],
  34. // current: '1',
  35. // scrollCurrent: '1-1',
  36. // result: [{}, {}, {}],
  37. // data: [
  38. // { key: '1', id: 'data1', title: '官方', children: [{}, {}, {}] },
  39. // { key: '2', id: 'data2', title: '非官方', children: [{}, {}, {}] },
  40. // ],
  41. // page: [
  42. // {
  43. // key: '1-1',
  44. // title: '考试功能',
  45. // id: '#test1',
  46. // content:
  47. // '11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111',
  48. // },
  49. // {
  50. // key: '1-2',
  51. // title: '出题原理',
  52. // id: '#test2',
  53. // content:
  54. // '11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111',
  55. // },
  56. // {
  57. // key: '1-3',
  58. // title: '出题原理',
  59. // id: '#test3',
  60. // content:
  61. // '11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111',
  62. // },
  63. // {
  64. // key: '1-4',
  65. // title: '出题原理',
  66. // id: '#test4',
  67. // content:
  68. // '11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111',
  69. // },
  70. // {
  71. // key: '1-5',
  72. // title: '出题原理',
  73. // id: '#test5',
  74. // content:
  75. // '11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111',
  76. // },
  77. // {
  78. // key: '1-6',
  79. // title: '出题原理',
  80. // id: '#test6',
  81. // content:
  82. // '11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111',
  83. // },
  84. // {
  85. // key: '1-7',
  86. // title: '出题原理',
  87. // id: '#test7',
  88. // content:
  89. // '11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111',
  90. // },
  91. // {
  92. // key: '1-8',
  93. // title: '出题原理',
  94. // id: '#test8',
  95. // content:
  96. // '11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111',
  97. // },
  98. // {
  99. // key: '1-9',
  100. // title: '出题原理',
  101. // id: '#test9',
  102. // content:
  103. // '11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111',
  104. // },
  105. // {
  106. // key: '1-10',
  107. // title: '出题原理',
  108. // id: '#test10',
  109. // content:
  110. // '11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111',
  111. // },
  112. // {
  113. // key: '1-11',
  114. // title: '出题原理',
  115. // id: '#test11',
  116. // content:
  117. // '11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111',
  118. // },
  119. // ],
  120. };
  121. }
  122. init() {
  123. Main.readyInfo()
  124. .then(result => {
  125. // read, category, area
  126. this.areaMap = getMap(result.area, 'id', 'title');
  127. result.category = result.category.map(row => {
  128. row.key = row.id;
  129. return row;
  130. });
  131. result.category.push({
  132. id: 'read',
  133. key: 'read',
  134. title: '推荐阅读',
  135. isRead: true,
  136. });
  137. result.category.push({
  138. id: 'faq',
  139. key: 'faq',
  140. title: 'FAQs',
  141. isFaq: true,
  142. });
  143. this.categoryMap = getMap(result.category, 'id');
  144. const list = formatTreeData(result.category, 'id', 'title', 'parentId');
  145. this.setState({ list });
  146. this.changePage(list[0].id);
  147. });
  148. }
  149. initData() {
  150. }
  151. refreshRoom() {
  152. const { isOverseas, keyword } = this.state;
  153. Main.listRoom({ page: 1, size: 10, keyword, isOverseas })
  154. .then(result => {
  155. result.list = result.list.map(row => {
  156. if (row.isOverseas) return row;
  157. row.area = this.areaMap[row.areaId];
  158. return row;
  159. });
  160. this.setState({ rooms: result.list });
  161. });
  162. }
  163. refreshData(item) {
  164. if (!this.dataList) {
  165. Main.allData()
  166. .then(result => {
  167. const official = result.filter(row => row.isOfficial);
  168. const unofficial = result.filter(row => !row.isOfficial);
  169. item.children.forEach(row => {
  170. if (row.isOfficial) {
  171. row.children = official;
  172. } else {
  173. row.children = unofficial;
  174. }
  175. });
  176. this.dataList = result;
  177. this.setState({ datas: item.children });
  178. this.readyScroll(item.children);
  179. });
  180. } else {
  181. this.setState({ datas: item.children });
  182. this.readyScroll(item.children);
  183. }
  184. }
  185. refreshList(item) {
  186. const { key } = item;
  187. if (!this.loadArticleMap[key]) {
  188. Main.listArticle(key)
  189. .then(result => {
  190. this.loadArticleMap[key] = result;
  191. this.setState({ articles: result });
  192. this.readyScroll(item.children);
  193. });
  194. } else {
  195. const list = this.loadArticleMap[key];
  196. this.setState({ articles: list });
  197. this.readyScroll(item.children);
  198. }
  199. }
  200. refreshRead() {
  201. if (!this.readList) {
  202. Main.listRead({ page: 1, size: 1000, plate: 'getready' })
  203. .then(result => {
  204. this.readList = result.list;
  205. this.setState({ reads: this.readList });
  206. });
  207. } else {
  208. this.setState({ reads: this.readList });
  209. }
  210. }
  211. refreshFaq() {
  212. if (!this.faqList) {
  213. Main.listFaq({ page: 1, size: 1000, channel: 'getready' })
  214. .then(result => {
  215. this.faqList = result.list;
  216. this.setState({ faqs: this.faqList });
  217. });
  218. } else {
  219. this.setState({ faqs: this.faqList });
  220. }
  221. }
  222. readyScroll(list) {
  223. if (this.scrollTimeKey) clearTimeout(this.scrollTimeKey);
  224. this.initScroll(list);
  225. }
  226. initScroll(list) {
  227. this.load = false;
  228. try {
  229. list.forEach(item => {
  230. item.top = document.getElementById(item.id).offsetTop - 100;
  231. return item;
  232. });
  233. this.load = true;
  234. } catch (e) {
  235. this.scrollTimeKey = setTimeout(() => {
  236. this.initScroll(list);
  237. }, 500);
  238. }
  239. }
  240. scrollListener(e) {
  241. if (!this.load) return;
  242. this.onScroll(e);
  243. }
  244. inPage() {
  245. document.getElementById('root').addEventListener('scroll', this.scrollListener.bind(this));
  246. }
  247. outPage() {
  248. document.getElementById('root').removeEventListener('scroll', this.scrollListener.bind(this));
  249. }
  250. onScroll(e) {
  251. const t = e.target.scrollTop;
  252. const { current, scrollCurrent } = this.state;
  253. const _list = this.categoryMap[current].children;
  254. let _current = null;
  255. for (let i = 0; i < _list.length; i += 1) {
  256. if (!_current) _current = _list[i].key;
  257. if (t < _list[i].top) break;
  258. _current = _list[i].key;
  259. }
  260. if (_current !== scrollCurrent) {
  261. this.setState({ scrollCurrent: _current });
  262. }
  263. }
  264. scrollTo(top) {
  265. document.getElementById('root').scrollTo({ top, behavior: 'smooth' });
  266. }
  267. changePage(key) {
  268. const item = this.categoryMap[key];
  269. if (item.isRoom) {
  270. this.refreshRoom();
  271. } else if (item.isData) {
  272. this.refreshData(item);
  273. } else if (item.isRead) {
  274. this.refreshRead();
  275. } else if (item.isFaq) {
  276. this.refreshFaq();
  277. } else {
  278. this.refreshList(item);
  279. }
  280. this.setState({ current: key });
  281. }
  282. onChangeReadTab(key) {
  283. this.setState({ readTab: key });
  284. }
  285. renderView() {
  286. const { list, current, scrollCurrent } = this.state;
  287. let detail = {};
  288. return (
  289. <div>
  290. <div className="content">
  291. <div className="left">
  292. <Anchor offsetTop={90} getContainer={() => document.getElementById('root')}>
  293. {list.map(item => {
  294. if (item.key === current) detail = item;
  295. return (
  296. <div
  297. className={`page-item ${item.key === current ? 'active' : ''}`}
  298. onClick={() => this.changePage(item.key)}
  299. >
  300. {item.children && item.children.length > 0 && <Assets name="dropdown_hover" className="up" />}
  301. {item.children && item.children.length > 0 && <Assets name="dropdown_normal" className="down" />}
  302. <div className="page-item-title">{item.title}</div>
  303. {current === item.key &&
  304. item.children &&
  305. item.children.map(child => {
  306. return (
  307. <div
  308. style={{ margin: 3 }}
  309. className={`t-s-12 t-2 c-p ${child.key === scrollCurrent ? 't-4' : ''}`}
  310. onClick={(e) => {
  311. e.stopPropagation();
  312. this.scrollTo(child.top);
  313. }}
  314. title={child.title}
  315. >
  316. {child.title}
  317. </div>
  318. );
  319. })}
  320. </div>
  321. );
  322. })}
  323. </Anchor>
  324. </div>
  325. <div className="center">
  326. <div className="t-1 t-s-24 m-b-2">{detail.title}</div>
  327. {!detail.isData && !detail.isRoom && !detail.isRead && !detail.isFaq && this.renderList()}
  328. {!!detail.isRoom && this.renderRoom()}
  329. {!!detail.isData && this.renderData()}
  330. {!!detail.isRead && this.renderRead()}
  331. {!!detail.isFaq && this.renderFaq()}
  332. </div>
  333. </div>
  334. </div >
  335. );
  336. }
  337. renderRoom() {
  338. const { rooms = [], isOverseas, keyword, showFinish, showSupple, supple, room } = this.state;
  339. return (
  340. <div className="query-layout">
  341. <div className="search-wrapper">
  342. <Select theme="white" value={isOverseas} list={[{ title: '中国', key: false }, { title: '海外', key: true }]} onChange={({ key }) => this.setState({ isOverseas: key })} />
  343. <input value={keyword} onChange={(e) => this.setState({ keyword: e.target.value })} />
  344. <Button width={150} onClick={() => this.refreshRoom()}>
  345. <Icon className="m-r-5" type="search" />
  346. 搜索考场
  347. </Button>
  348. </div>
  349. {rooms.map((item, index) => {
  350. return (
  351. <div className="m-b-2">
  352. <div className="t-1 t-s-18">{index + 1}.{item.title}</div>
  353. <div className="t-1">{item.address}</div>
  354. <div className="t-1">
  355. 温馨提示: {item.description} <span className="t-4 c-p" onClick={() => User.needLogin().then(() => this.setState({ showSupple: true, supple: { roomId: item.id }, room: item }))}>我要补充</span>
  356. </div>
  357. </div>
  358. );
  359. })}
  360. <SuppleModal show={showSupple} defaultData={supple} info={room} onConfirm={() => this.setState({ showSupple: false, showFinish: true })} onCancel={() => this.setState({ showSupple: false })} />
  361. <SuppleFinishModal show={showFinish} onConfirm={() => this.setState({ showFinish: false })} onCancel={() => this.setState({ showFinish: false })} />
  362. </div>
  363. );
  364. }
  365. renderData() {
  366. const { datas = [], dataId } = this.state;
  367. return datas.map(item => {
  368. return (
  369. <div className="p-t-1 m-b-2" id={item.id}>
  370. <div className="t-1 t-s-18 m-b-1">{item.title}</div>
  371. <div className="data-list">
  372. {item.children.map(child => {
  373. if (child.id === dataId) {
  374. return <div style={{ width: 240, height: 310, overflowY: 'auto', display: 'inline-block' }} className="ws-p data-item" onClick={() => this.setState({ dataId: null })}>{child.content}</div>;
  375. }
  376. return <Assets width={240} height={310} src={child.cover} className="data-item" onClick={() => this.setState({ dataId: child.id })} />;
  377. })}
  378. </div>
  379. </div>
  380. );
  381. });
  382. }
  383. renderList() {
  384. const { articles = [] } = this.state;
  385. return articles.map(item => {
  386. return (
  387. <div className="p-t-1 m-b-2" id={item.categoryId}>
  388. <div className="t-1 t-s-18 m-b-1">{item.title}</div>
  389. <div className="t-2" dangerouslySetInnerHTML={{ __html: item.content }} />
  390. </div>
  391. );
  392. });
  393. }
  394. renderRead() {
  395. const { readTab, reads = [] } = this.state;
  396. return <div className='read-layout'>
  397. <Tabs
  398. className='m-b-2'
  399. type="division"
  400. theme="gray"
  401. space={2.5}
  402. active={readTab}
  403. tabs={[{ title: '单项攻略', key: '1' }, { title: '模考&PACE', key: '2' }, { title: '换库&机经', key: '3' }, { title: '经验分享', key: '4' }, { title: '申请相关', key: '5' }]}
  404. onChange={key => this.onChangeReadTab(key)}
  405. />
  406. <div className='m-b-2'>
  407. <Button className='m-r-1'>CAT模考 ></Button>
  408. <Button>非CAT模考 ></Button>
  409. </div>
  410. {reads.map((item) => {
  411. return <div className='m-b-5'>
  412. <Link to={item.path}>{item.title}</Link>
  413. </div>;
  414. })}
  415. </div>;
  416. }
  417. renderFaq() {
  418. const { faqs = [] } = this.state;
  419. return <div className='faq-layout'>
  420. <Button>我要提问 ></Button>
  421. <QAList data={faqs} />
  422. </div>;
  423. }
  424. }