page.js 17 KB

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