page.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. import React from 'react';
  2. import './index.less';
  3. import Page from '@src/containers/Page';
  4. import { asyncSMessage } from '@src/services/AsyncTools';
  5. import { formatTreeData, formatPercent, formatDate } from '@src/services/Tools';
  6. import Panel, { WaitPanel, BuyPanel, SmallPanel, SmallWaitPanel, SmallBuyPanel } from '../../../components/Panel';
  7. import Tabs from '../../../components/Tabs';
  8. import Module from '../../../components/Module';
  9. import Division from '../../../components/Division';
  10. import QAList from '../../../components/QAList';
  11. import { Main } from '../../../stores/main';
  12. // import { My } from '../../../stores/my';
  13. import { Question } from '../../../stores/question';
  14. import { Textbook } from '../../../stores/textbook';
  15. // import { User } from '../../../stores/user';
  16. // import { CourseModuleShow, CourseModule } from '../../../../Constant';
  17. import { Order } from '../../../stores/order';
  18. import { User } from '../../../stores/user';
  19. const TEXTBOOK = 'textbook';
  20. export default class extends Page {
  21. initState() {
  22. this.examinationProgress = {};
  23. this.textbookProgress = {};
  24. this.inited = false;
  25. return {
  26. tab1: '',
  27. tab2: '',
  28. tabs: [],
  29. };
  30. }
  31. init() {
  32. Main.getExamination().then(result => {
  33. const list = result.filter(row => row.level === 1).map(row => {
  34. row.title = `${row.titleZh}${row.titleEn}`;
  35. row.key = row.extend;
  36. return row;
  37. });
  38. const tabs = formatTreeData(list, 'id', 'title', 'parentId');
  39. tabs.push({ key: TEXTBOOK, name: '数学机经' });
  40. this.setState({
  41. tabs,
  42. });
  43. this.inited = true;
  44. this.refreshData();
  45. });
  46. }
  47. initData() {
  48. const data = Object.assign(this.state, this.state.search);
  49. if (!data.tab1) {
  50. data.tab1 = 'cat';
  51. }
  52. this.setState(data);
  53. if (this.inited) this.refreshData();
  54. }
  55. refreshData(tab) {
  56. const { tab1 } = this.state;
  57. switch (tab || tab1) {
  58. case TEXTBOOK:
  59. this.refreshTextbook();
  60. break;
  61. default:
  62. this.refreshExamination(tab || tab1);
  63. }
  64. }
  65. refreshTextbook() {
  66. Textbook.progress().then(result => {
  67. // const exerciseProgress = getMap(r, 'id');
  68. result = result.map(row => {
  69. row.info = [
  70. {
  71. title: '已做',
  72. number: row.userNumber || '-',
  73. unit: '题',
  74. },
  75. {
  76. title: '剩余',
  77. number: row.userNumber ? row.questionNumber - row.userNumber : '-',
  78. unit: '题',
  79. },
  80. {
  81. title: '正确率',
  82. number: row.userNumber ? formatPercent(row.userStat.userCorrect, row.userStat.userNumber, false) : '-%',
  83. unit: '',
  84. },
  85. {
  86. title: '全站',
  87. number: row.userNumber ? formatPercent(row.stat.totalCorrect, row.stat.totalNumber, false) : '-%',
  88. unit: '题',
  89. },
  90. ];
  91. row.progress = formatPercent(row.questionNumber - row.userNumber || 0, row.questionNumber);
  92. row.totalCorrect = formatPercent(row.stat.totalCorrect, row.stat.totalNumber, false);
  93. row.children = row.children.map(r => {
  94. r.title = r.title || r.titleZh;
  95. r.progress = formatPercent(r.userNumber, r.questionNumber);
  96. return r;
  97. });
  98. row.pieValue = formatPercent(row.userNumber, row.questionNumber);
  99. row.pieText = formatPercent(row.userNumber, row.questionNumber, false);
  100. row.pieSubText = `共${row.questionNumber}`;
  101. if (row.isLatest) {
  102. const day = parseInt((new Date().getTime() - new Date(row.startDate).getTime()) / 86400000, 10);
  103. row.desc = [`最近换库:${formatDate(row.startDate, 'YYYY-MM-DD')} ,已换库${day}天`, `最后更新:${formatDate(row.updateTime)}`];
  104. }
  105. return row;
  106. });
  107. this.setState({ textbookProgress: result });
  108. });
  109. Main.listFaq({ page: 1, size: 100, channel: 'textbook' }).then(result => {
  110. this.setState({ faqs: result.list });
  111. });
  112. }
  113. refreshExamination(tab) {
  114. const { tabs, tab1 } = this.state;
  115. if (!tabs) {
  116. // 等待数据加载
  117. return;
  118. }
  119. const [subject] = tabs.filter(row => row.key === tab || row.key === tab1);
  120. Question.getExaminationProgress(subject.id).then(result => {
  121. // const exerciseProgress = getMap(r, 'id');
  122. result = result.map(row => {
  123. row.title = `${row.titleZh}${row.titleEn}`;
  124. row.info = [
  125. {
  126. title: '已做',
  127. number: row.userNumber || '-',
  128. unit: '套',
  129. },
  130. {
  131. title: '剩余',
  132. number: row.userNumber ? row.paperNumber - row.userNumber : '-',
  133. unit: '套',
  134. },
  135. ];
  136. row.pieValue = formatPercent(row.userNumber, row.paperNumber);
  137. row.pieText = formatPercent(row.userNumber, row.paperNumber, false);
  138. row.pieSubText = `共${row.paperNumber}套`;
  139. return row;
  140. });
  141. this.setState({ examinationProgress: result });
  142. });
  143. Main.listFaq({ page: 1, size: 100, channel: `examination-${subject.extend}` }).then(result => {
  144. this.setState({ faqs: result.list });
  145. });
  146. }
  147. onChangeTab(level, tab) {
  148. const { tab1 } = this.state;
  149. const data = {};
  150. if (level > 1) {
  151. data.tab1 = tab1;
  152. data.tab2 = tab;
  153. } else {
  154. data.tab1 = tab;
  155. }
  156. // this.refreshData(tab);
  157. this.refreshQuery(data);
  158. }
  159. onTextbook() {
  160. const { tab1, tab2, struct } = this.state;
  161. const data = {
  162. tab1, tab2, struct,
  163. };
  164. this.refreshQuery(data);
  165. }
  166. // 开通模考或者机经
  167. open(recordId) {
  168. Order.useRecord(recordId).then(() => {
  169. asyncSMessage('开通成功');
  170. this.refresh();
  171. });
  172. }
  173. examinationList(item) {
  174. User.needLogin().then(() => {
  175. linkTo(`/examination/list/${item.id}`);
  176. });
  177. }
  178. textbookList(item, isLatest) {
  179. User.needLogin().then(() => {
  180. linkTo(`/textbook/list?logic=${encodeURIComponent(item.logic)}&latest=${isLatest ? 1 : 0}`);
  181. });
  182. }
  183. buyTextbook() {
  184. User.needLogin()
  185. .then(() => {
  186. });
  187. }
  188. buyQxCat() {
  189. User.needLogin()
  190. .then(() => {
  191. });
  192. }
  193. renderView() {
  194. const { tab1, tab2, tabs } = this.state;
  195. const [subject] = tabs.filter(row => row.key === tab1);
  196. const children = (subject && subject.children) ? subject.children : [];
  197. return (
  198. <div>
  199. <div className="content">
  200. <Module className="m-t-2">
  201. <Tabs
  202. type="card"
  203. active={tab1}
  204. tabs={tabs}
  205. onChange={key => {
  206. this.onChangeTab(1, key);
  207. }}
  208. />
  209. {children && children.length > 1 && (
  210. <Tabs active={tab2} tabs={children} onChange={key => this.onChangeTab(2, key)} />
  211. )}
  212. </Module>
  213. {tab1 !== TEXTBOOK && this.renderExamination()}
  214. {tab1 === TEXTBOOK && this.renderTextbook()}
  215. {this.state.faqs && <QAList data={this.state.faqs} active={'faq'} tabs={[{ key: 'faq', name: 'FAQs' }]} />}
  216. </div>
  217. </div>
  218. );
  219. }
  220. renderTextbook() {
  221. const { textbookProgress = [] } = this.state;
  222. return (
  223. <div>
  224. <Division col={2}>
  225. {(textbookProgress || []).map(struct => {
  226. if (struct.needService && !struct.hasService) {
  227. if (struct.unUseRecord) {
  228. return <WaitPanel
  229. title={struct.isLatest ? '最新' : '往期'}
  230. col="3"
  231. data={struct}
  232. onOpen={() => {
  233. this.open(struct.unUseRecord.id);
  234. }}
  235. />;
  236. }
  237. return <BuyPanel
  238. title={struct.isLatest ? '最新' : '往期'}
  239. onBuy={() => {
  240. this.buyTextbook();
  241. }}
  242. />;
  243. }
  244. return <Panel
  245. title={struct.isLatest ? '最新' : '往期'}
  246. col="3"
  247. data={struct}
  248. onClick={(item) => {
  249. this.textbookList(item, struct.isLatest);
  250. }}
  251. />;
  252. })}
  253. </Division>
  254. </div>
  255. );
  256. }
  257. renderExamination() {
  258. const { examinationProgress = [] } = this.state;
  259. return (
  260. <div>
  261. <Division col={3} type="compact">
  262. {(examinationProgress || []).map(struct => {
  263. if (struct.hasService) {
  264. return <SmallPanel
  265. title={struct.title}
  266. data={struct}
  267. onClick={() => {
  268. this.examinationList(struct);
  269. }}
  270. />;
  271. } if (struct.unUseRecord) {
  272. return <SmallWaitPanel
  273. title={struct.title}
  274. data={struct}
  275. onOpen={() => {
  276. this.open(struct.unUseRecord.id);
  277. }}
  278. />;
  279. }
  280. return <SmallBuyPanel
  281. title={struct.title}
  282. data={struct}
  283. onBuy={() => {
  284. this.buyQxCat();
  285. }}
  286. />;
  287. })}
  288. </Division>
  289. </div>
  290. );
  291. }
  292. }