page.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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').map((row) => {
  147. if (row.isRoom) row.children = [];
  148. return row;
  149. });
  150. const readPlates = ((result.read || {}).plates || []).map((row, index) => {
  151. row.title = row.plate;
  152. row.key = `${index + 1}`;
  153. return row;
  154. });
  155. this.readPlateMap = getMap(readPlates, 'key');
  156. this.setState({ list, readPlates });
  157. this.changePage(list[0].id);
  158. });
  159. }
  160. initData() {
  161. }
  162. refreshRoom() {
  163. const { isOverseas, keyword } = this.state;
  164. if (!keyword) {
  165. return;
  166. }
  167. Main.listRoom({ page: 1, size: 10, keyword, isOverseas })
  168. .then(result => {
  169. result.list = result.list.map(row => {
  170. if (row.isOverseas) return row;
  171. row.area = this.areaMap[row.areaId];
  172. return row;
  173. });
  174. this.setState({ rooms: result.list });
  175. });
  176. }
  177. refreshData(item) {
  178. if (!this.dataList) {
  179. Main.allData()
  180. .then(result => {
  181. const official = result.filter(row => row.isOfficial);
  182. const unofficial = result.filter(row => !row.isOfficial);
  183. item.children.forEach(row => {
  184. if (row.isOfficial) {
  185. row.children = official;
  186. } else {
  187. row.children = unofficial;
  188. }
  189. });
  190. this.dataList = result;
  191. this.setState({ datas: item.children });
  192. this.readyScroll(item.children);
  193. });
  194. } else {
  195. this.setState({ datas: item.children });
  196. this.readyScroll(item.children);
  197. }
  198. }
  199. refreshList(item) {
  200. const { key } = item;
  201. if (!this.loadArticleMap[key]) {
  202. Main.listArticle(key)
  203. .then(result => {
  204. this.loadArticleMap[key] = result;
  205. this.setState({ articles: result });
  206. this.readyScroll(item.children);
  207. });
  208. } else {
  209. const list = this.loadArticleMap[key];
  210. this.setState({ articles: list });
  211. this.readyScroll(item.children);
  212. }
  213. }
  214. refreshFaq() {
  215. if (!this.faqList) {
  216. Main.listFaq({ page: 1, size: 1000, channel: 'getready' })
  217. .then(result => {
  218. this.faqList = result.list;
  219. this.setState({ faqs: this.faqList });
  220. });
  221. } else {
  222. this.setState({ faqs: this.faqList });
  223. }
  224. }
  225. readyScroll(list) {
  226. if (this.scrollTimeKey) clearTimeout(this.scrollTimeKey);
  227. this.initScroll(list);
  228. }
  229. initScroll(list) {
  230. this.load = false;
  231. try {
  232. list.forEach(item => {
  233. item.top = document.getElementById(item.id).offsetTop - 100;
  234. return item;
  235. });
  236. this.load = true;
  237. } catch (e) {
  238. this.scrollTimeKey = setTimeout(() => {
  239. this.initScroll(list);
  240. }, 500);
  241. }
  242. }
  243. scrollListener(e) {
  244. if (!this.load) return;
  245. this.onScroll(e);
  246. }
  247. inPage() {
  248. document.getElementById('root').addEventListener('scroll', this.scrollListener.bind(this));
  249. }
  250. outPage() {
  251. document.getElementById('root').removeEventListener('scroll', this.scrollListener.bind(this));
  252. }
  253. onScroll(e) {
  254. const t = e.target.scrollTop;
  255. const { current, scrollCurrent } = this.state;
  256. const _list = this.categoryMap[current].children;
  257. let _current = null;
  258. for (let i = 0; i < _list.length; i += 1) {
  259. if (!_current) _current = _list[i].key;
  260. if (t < _list[i].top) break;
  261. _current = _list[i].key;
  262. }
  263. if (_current !== scrollCurrent) {
  264. this.setState({ scrollCurrent: _current });
  265. }
  266. }
  267. scrollTo(top) {
  268. document.getElementById('root').scrollTo({ top, behavior: 'smooth' });
  269. }
  270. changePage(key) {
  271. const item = this.categoryMap[key];
  272. if (item.isRoom) {
  273. this.refreshRoom();
  274. } else if (item.isData) {
  275. this.refreshData(item);
  276. } else if (item.isRead) {
  277. this.refreshRead();
  278. } else if (item.isFaq) {
  279. this.refreshFaq();
  280. } else {
  281. this.refreshList(item);
  282. }
  283. this.setState({ current: key, open: true });
  284. }
  285. refreshRead() {
  286. // init
  287. const { readPlates } = this.state;
  288. this.onChangeReadTab(readPlates[0].key);
  289. }
  290. onChangeReadTab(key) {
  291. const plate = this.readPlateMap[key];
  292. this.setState({ readTab: key, plate, reads: [] });
  293. if (!this.loadReadMap[key]) {
  294. Main.listRead({ page: 1, size: 100, plate: key })
  295. .then(result => {
  296. this.loadReadMap[key] = result.list;
  297. this.setState({ reads: result.list });
  298. });
  299. } else {
  300. const list = this.loadReadMap[key];
  301. this.setState({ reads: list });
  302. }
  303. }
  304. renderView() {
  305. const { list, current, scrollCurrent, open } = this.state;
  306. let detail = {};
  307. return (
  308. <div>
  309. <div className="content">
  310. <div className="left">
  311. <Anchor offsetTop={90} getContainer={() => document.getElementById('root')}>
  312. {list.map(item => {
  313. if (item.key === current) detail = item;
  314. return (
  315. <div
  316. className={`page-item ${item.key === current && open ? 'active' : ''}`}
  317. onClick={() => {
  318. if (item.key === current) {
  319. this.setState({ open: !open });
  320. } else {
  321. this.changePage(item.key);
  322. }
  323. }}
  324. >
  325. {item.children && item.children.length > 0 && <Assets name="dropdown_hover" className="up" />}
  326. {<Assets name="dropdown_normal" className="down" />}
  327. {(!item.children || item.children.length === 0) && <Assets name="dropdown_hover" className="change" />}
  328. <div className="page-item-title">{item.title}</div>
  329. {current === item.key && open &&
  330. item.children &&
  331. item.children.map(child => {
  332. return (
  333. <div
  334. style={{ margin: 3 }}
  335. className={`t-s-12 t-2 c-p ${child.key === scrollCurrent ? 't-4' : ''}`}
  336. onClick={(e) => {
  337. e.stopPropagation();
  338. this.scrollTo(child.top);
  339. }}
  340. title={child.title}
  341. >
  342. {child.title}
  343. </div>
  344. );
  345. })}
  346. </div>
  347. );
  348. })}
  349. </Anchor>
  350. </div>
  351. <div className="center">
  352. <div className="t-1 t-s-24 m-b-2">{detail.title}</div>
  353. {!detail.isData && !detail.isRoom && !detail.isRead && !detail.isFaq && this.renderList()}
  354. {!!detail.isRoom && this.renderRoom()}
  355. {!!detail.isData && this.renderData()}
  356. {!!detail.isRead && this.renderRead()}
  357. {!!detail.isFaq && this.renderFaq()}
  358. </div>
  359. </div>
  360. </div>
  361. );
  362. }
  363. renderRoom() {
  364. const { rooms = [], isOverseas, keyword, showFinish, showSupple, supple, room } = this.state;
  365. return (
  366. <div className="query-layout">
  367. <div className="search-wrapper">
  368. <Select theme="white" value={isOverseas} list={[{ title: '中国', key: false }, { title: '海外', key: true }]} onChange={({ key }) => this.setState({ isOverseas: key })} />
  369. <input value={keyword} placeholder='请输入城市名称' onChange={(e) => this.setState({ keyword: e.target.value })} />
  370. <Button width={150} onClick={() => this.refreshRoom()}>
  371. <Icon className="m-r-5" type="search" />
  372. 搜索考场
  373. </Button>
  374. </div>
  375. {rooms.map((item) => {
  376. return (
  377. <div className="m-b-2">
  378. <div className="t-1 t-s-18">{item.title}</div>
  379. <div className="t-1">{item.address}</div>
  380. <div className="t-1">
  381. 温馨提示: {item.description} <span className="t-4 c-p" onClick={() => User.needLogin().then(() => this.setState({ showSupple: true, supple: { roomId: item.id }, room: item }))}>我要补充</span>
  382. </div>
  383. </div>
  384. );
  385. })}
  386. <SuppleModal show={showSupple} defaultData={supple} info={room} onConfirm={() => this.setState({ showSupple: false, showFinish: true })} onCancel={() => this.setState({ showSupple: false })} />
  387. <SuppleFinishModal show={showFinish} onConfirm={() => this.setState({ showFinish: false })} onCancel={() => this.setState({ showFinish: false })} />
  388. </div>
  389. );
  390. }
  391. renderData() {
  392. const { datas = [], dataId } = this.state;
  393. return datas.map(item => {
  394. return (
  395. <div className="p-t-1 m-b-2" id={item.id}>
  396. <div className="t-1 t-s-18 m-b-1">{item.title}</div>
  397. <div className="data-list">
  398. {item.children.map(child => {
  399. if (child.id === dataId) {
  400. 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>;
  401. }
  402. return <Assets width={240} height={310} src={child.cover} className="data-item" onClick={() => this.setState({ dataId: child.id })} />;
  403. })}
  404. </div>
  405. </div>
  406. );
  407. });
  408. }
  409. renderList() {
  410. const { articles = [], scrollCurrent } = this.state;
  411. return articles.map(item => {
  412. return (
  413. <div className="p-t-1 m-b-2" id={item.categoryId}>
  414. <div className="t-1 t-s-18 m-b-1">{scrollCurrent === item.categoryId ? <span className="t-4">{item.title}</span> : item.title}</div>
  415. <div className="t-2" dangerouslySetInnerHTML={{ __html: item.content }} />
  416. </div>
  417. );
  418. });
  419. }
  420. renderRead() {
  421. const { readTab, reads = [], readPlates, plate = {} } = this.state;
  422. return <div className='read-layout'>
  423. <Tabs
  424. className='m-b-2'
  425. type="division"
  426. theme="gray"
  427. space={2.5}
  428. active={readTab}
  429. tabs={readPlates}
  430. onChange={key => this.onChangeReadTab(key)}
  431. />
  432. {!!plate.jump && <div className='m-b-2'>
  433. <Button className='m-r-1' onClick={() => openLink(plate.link)}>{plate.text}</Button>
  434. </div>}
  435. {reads.map((item) => {
  436. return <div className='m-b-5'>
  437. <a href={item.link || ''} target="_blank">{item.title}</a>
  438. </div>;
  439. })}
  440. </div>;
  441. }
  442. renderFaq() {
  443. const { faqs = [], showFaq, showFinish, faq = {} } = this.state;
  444. return <div className='faq-layout'>
  445. <Button onClick={() => User.needLogin().then(() => this.setState({ showFaq: true, faq: { channel: 'getready' } }))}>我要提问 ></Button>
  446. <QAList data={faqs} />
  447. <FaqModal show={showFaq} defaultData={faq} onCancel={() => this.setState({ showFaq: false, faq: {} })} onConfirm={() => this.setState({ showFaq: false, faq: {}, showFinish: true })} />
  448. <FinishModal
  449. show={showFinish}
  450. onConfirm={() => this.setState({ showFinish: false })}
  451. />
  452. </div>;
  453. }
  454. }