page.js 8.3 KB

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