page.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. row.pieValue = formatPercent(row.userNumber, row.questionNumber);
  98. row.pieText = formatPercent(row.userNumber, row.questionNumber, false);
  99. row.pieSubText = `共${row.questionNumber}`;
  100. if (row.isLatest) {
  101. const day = parseInt((new Date().getTime() - new Date(row.startDate).getTime()) / 86400000, 10);
  102. row.desc = [`最近换库:${formatDate(row.startDate, 'YYYY-MM-DD')} ,已换库${day}天`, `最后更新:${formatDate(row.updateTime)}`];
  103. }
  104. return row;
  105. });
  106. this.setState({ textbookProgress: result });
  107. });
  108. }
  109. refreshExamination(tab) {
  110. const { tabs, tab1 } = this.state;
  111. if (!tabs) {
  112. // 等待数据加载
  113. return;
  114. }
  115. const [subject] = tabs.filter(row => row.key === tab || row.key === tab1);
  116. Question.getExaminationProgress(subject.id).then(result => {
  117. // const exerciseProgress = getMap(r, 'id');
  118. result = result.map(row => {
  119. row.title = `${row.titleZh}${row.titleEn}`;
  120. row.info = [
  121. {
  122. title: '已做',
  123. number: row.userNumber || '-',
  124. unit: '套',
  125. },
  126. {
  127. title: '剩余',
  128. number: row.userNumber ? row.questionNumber - row.userNumber : '-',
  129. unit: '套',
  130. },
  131. ];
  132. row.pieValue = formatPercent(row.userNumber, row.questionNumber);
  133. row.pieText = formatPercent(row.userNumber, row.questionNumber, false);
  134. row.pieSubText = `共${row.questionNumber}套`;
  135. return row;
  136. });
  137. this.setState({ examinationProgress: result });
  138. });
  139. }
  140. onChangeTab(level, tab) {
  141. const { tab1 } = this.state;
  142. const data = {};
  143. if (level > 1) {
  144. data.tab1 = tab1;
  145. data.tab2 = tab;
  146. } else {
  147. data.tab1 = tab;
  148. }
  149. // this.refreshData(tab);
  150. this.refreshQuery(data);
  151. }
  152. onTextbook() {
  153. const { tab1, tab2, struct } = this.state;
  154. const data = {
  155. tab1, tab2, struct,
  156. };
  157. this.refreshQuery(data);
  158. }
  159. // 开通模考或者机经
  160. open(recordId) {
  161. Order.useRecord(recordId).then(() => {
  162. asyncSMessage('开通成功');
  163. this.refresh();
  164. });
  165. }
  166. restart(item) {
  167. asyncConfirm('提示', '是否重置', () => {
  168. Question.restart(item.paper.id).then(() => {
  169. this.refresh();
  170. });
  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. renderView() {
  184. const { tab1, tab2, tabs } = this.state;
  185. const [subject] = tabs.filter(row => row.key === tab1);
  186. const children = (subject && subject.children) ? subject.children : [];
  187. return (
  188. <div>
  189. <div className="content">
  190. <Module className="m-t-2">
  191. <Tabs
  192. type="card"
  193. active={tab1}
  194. tabs={tabs}
  195. onChange={key => {
  196. this.onChangeTab(1, key);
  197. }}
  198. />
  199. {children && children.length > 1 && (
  200. <Tabs active={tab2} tabs={children} onChange={key => this.onChangeTab(2, key)} />
  201. )}
  202. </Module>
  203. {tab1 !== TEXTBOOK && this.renderExamination()}
  204. {tab1 === TEXTBOOK && this.renderTextbook()}
  205. </div>
  206. </div>
  207. );
  208. }
  209. renderTextbook() {
  210. const { textbookProgress = [] } = this.state;
  211. return (
  212. <div>
  213. <Division col={2}>
  214. {(textbookProgress || []).map(struct => {
  215. if (struct.needService && !struct.hasService) {
  216. if (struct.unUseRecord) {
  217. return <WaitPanel
  218. title={struct.isLatest ? '最新' : '往期'}
  219. col="3"
  220. data={struct}
  221. onOpen={() => {
  222. this.open(struct.unUseRecord.id);
  223. }}
  224. />;
  225. }
  226. return <BuyPanel
  227. title={struct.isLatest ? '最新' : '往期'}
  228. onBuy={() => {
  229. this.buyTextbook();
  230. }}
  231. />;
  232. }
  233. return <Panel
  234. title={struct.isLatest ? '最新' : '往期'}
  235. col="3"
  236. data={struct}
  237. onClick={(item) => {
  238. this.textbookList(item, struct.isLatest);
  239. }}
  240. />;
  241. })}
  242. </Division>
  243. </div>
  244. );
  245. }
  246. renderExamination() {
  247. const { examinationProgress = [] } = this.state;
  248. return (
  249. <div>
  250. <Division col={3} type="compact">
  251. {(examinationProgress || []).map(struct => {
  252. if (struct.hasService) {
  253. return <SmallPanel
  254. title={struct.title}
  255. data={struct}
  256. onClick={() => {
  257. this.examinationList(struct);
  258. }}
  259. />;
  260. } if (struct.unUseRecord) {
  261. return <SmallWaitPanel
  262. title={struct.title}
  263. data={struct}
  264. onOpen={() => {
  265. this.open(struct.unUseRecord.id);
  266. }}
  267. />;
  268. }
  269. return <SmallBuyPanel
  270. title={struct.title}
  271. data={struct}
  272. onBuy={() => {
  273. this.buyQxCat();
  274. }}
  275. />;
  276. })}
  277. </Division>
  278. </div>
  279. );
  280. }
  281. }