page.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. inPage() {
  233. document.getElementById('root').addEventListener('scroll', e => {
  234. if (!this.load) return;
  235. this.onScroll(e);
  236. });
  237. }
  238. outPage() {
  239. document.getElementById('root').removeEventListener('scroll');
  240. }
  241. onScroll(e) {
  242. const t = e.target.scrollTop;
  243. const { current, scrollCurrent } = this.state;
  244. const _list = this.categoryMap[current].children;
  245. let _current = null;
  246. for (let i = 0; i < _list.length; i += 1) {
  247. if (!_current) _current = _list[i].key;
  248. if (t < _list[i].top) break;
  249. _current = _list[i].key;
  250. }
  251. if (_current !== scrollCurrent) {
  252. this.setState({ scrollCurrent: _current });
  253. }
  254. }
  255. scrollTo(top) {
  256. document.getElementById('root').scrollTo({ top, behavior: 'smooth' });
  257. }
  258. changePage(key) {
  259. const item = this.categoryMap[key];
  260. if (item.isRoom) {
  261. this.refreshRoom();
  262. } else if (item.isData) {
  263. this.refreshData(item);
  264. } else if (item.isRead) {
  265. this.refreshRead();
  266. } else if (item.isFaq) {
  267. this.refreshFaq();
  268. } else {
  269. this.refreshList(item);
  270. }
  271. this.setState({ current: key });
  272. }
  273. renderView() {
  274. const { list, current, scrollCurrent } = this.state;
  275. let detail = {};
  276. return (
  277. <div>
  278. <div className="content">
  279. <div className="left">
  280. <Anchor offsetTop={90} getContainer={() => document.getElementById('root')}>
  281. {list.map(item => {
  282. if (item.key === current) detail = item;
  283. return (
  284. <div
  285. className={`page-item ${item.key === current ? 'active' : ''}`}
  286. onClick={() => this.changePage(item.key)}
  287. >
  288. {item.children && item.children.length > 0 && <Assets name="dropdown_hover" className="up" />}
  289. {item.children && item.children.length > 0 && <Assets name="dropdown_normal" className="down" />}
  290. <div className="page-item-title">{item.title}</div>
  291. {current === item.key &&
  292. item.children &&
  293. item.children.map(child => {
  294. return (
  295. <div
  296. style={{ margin: 3 }}
  297. className={`t-s-12 t-2 c-p ${child.key === scrollCurrent ? 't-4' : ''}`}
  298. onClick={(e) => {
  299. e.stopPropagation();
  300. this.scrollTo(child.top);
  301. }}
  302. title={child.title}
  303. >
  304. {child.title}
  305. </div>
  306. );
  307. })}
  308. </div>
  309. );
  310. })}
  311. </Anchor>
  312. </div>
  313. <div className="center">
  314. <div className="t-1 t-s-24 m-b-2">{detail.title}</div>
  315. {!detail.isData && !detail.isRoom && !detail.isRead && !detail.isFaq && this.renderList()}
  316. {!!detail.isRoom && this.renderRoom()}
  317. {!!detail.isData && this.renderData()}
  318. {!!detail.isRead && this.renderRead()}
  319. {!!detail.isFaq && this.renderFaq()}
  320. </div>
  321. </div>
  322. </div >
  323. );
  324. }
  325. renderRoom() {
  326. const { rooms = [], isOverseas, keyword, showFinish, showSupple, supple, room } = this.state;
  327. return (
  328. <div className="query-layout">
  329. <div className="search-wrapper">
  330. <Select theme="white" value={isOverseas} list={[{ title: '中国', key: false }, { title: '海外', key: true }]} onChange={({ key }) => this.setState({ isOverseas: key })} />
  331. <input value={keyword} onChange={(e) => this.setState({ keyword: e.target.value })} />
  332. <Button width={150} onClick={() => this.refreshRoom()}>
  333. <Icon className="m-r-5" type="search" />
  334. 搜索考场
  335. </Button>
  336. </div>
  337. {rooms.map((item, index) => {
  338. return (
  339. <div className="m-b-2">
  340. <div className="t-1 t-s-18">{index + 1}.{item.title}</div>
  341. <div className="t-1">{item.address}</div>
  342. <div className="t-1">
  343. 温馨提示: {item.description} <span className="t-4" onClick={() => User.needLogin().then(() => this.setState({ showSupple: true, supple: { roomId: item.id }, room: item }))}>我要补充</span>
  344. </div>
  345. </div>
  346. );
  347. })}
  348. <SuppleModal show={showSupple} defaultData={supple} info={room} onConfirm={() => this.setState({ showSupple: false, showFinish: true })} onCancel={() => this.setState({ showSupple: false })} />
  349. <SuppleFinishModal show={showFinish} onConfirm={() => this.setState({ showFinish: false })} onCancel={() => this.setState({ showFinish: false })} />
  350. </div>
  351. );
  352. }
  353. renderData() {
  354. const { datas = [], dataId } = this.state;
  355. return datas.map(item => {
  356. return (
  357. <div className="p-t-1 m-b-2" id={item.id}>
  358. <div className="t-1 t-s-18 m-b-1">{item.title}</div>
  359. <div className="data-list">
  360. {item.children.map(child => {
  361. if (child.id === dataId) {
  362. 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>;
  363. }
  364. return <Assets width={240} height={310} src={child.cover} className="data-item" onClick={() => this.setState({ dataId: child.id })} />;
  365. })}
  366. </div>
  367. </div>
  368. );
  369. });
  370. }
  371. renderList() {
  372. const { articles = [] } = this.state;
  373. return articles.map(item => {
  374. return (
  375. <div className="p-t-1 m-b-2" id={item.categoryId}>
  376. <div className="t-1 t-s-18 m-b-1">{item.title}</div>
  377. <div className="t-2" dangerouslySetInnerHTML={{ __html: item.content }} />
  378. </div>
  379. );
  380. });
  381. }
  382. renderRead() {
  383. return <div />;
  384. }
  385. renderFaq() {
  386. return <div />;
  387. }
  388. }