page.js 19 KB

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