page.js 21 KB

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