page.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. this.refresh();
  170. });
  171. }
  172. examinationList(item) {
  173. User.needLogin().then(() => {
  174. linkTo(`/examination/list/${item.id}`);
  175. });
  176. }
  177. textbookList(item, isLatest) {
  178. User.needLogin().then(() => {
  179. linkTo(`/textbook/list?logic=${encodeURIComponent(item.logic)}&latest=${isLatest ? 1 : 0}`);
  180. });
  181. }
  182. buyTextbook() {
  183. User.needLogin()
  184. .then(() => {
  185. return Order.speedPay({ productType: 'service', service: 'textbook' });
  186. })
  187. .then((order) => {
  188. return User.needPay(order);
  189. })
  190. .then(() => {
  191. this.refresh();
  192. });
  193. }
  194. buyQxCat() {
  195. User.needLogin()
  196. .then(() => {
  197. return Order.speedPay({ productType: 'service', service: 'qx_cat' });
  198. })
  199. .then((order) => {
  200. return User.needPay(order);
  201. })
  202. .then(() => {
  203. this.refresh();
  204. });
  205. }
  206. renderView() {
  207. const { tab1, tab2, tabs } = this.state;
  208. const [subject] = tabs.filter(row => row.key === tab1);
  209. const children = (subject && subject.children) ? subject.children : [];
  210. return (
  211. <div>
  212. <div className="content">
  213. <Module className="m-t-2">
  214. <Tabs
  215. type="card"
  216. active={tab1}
  217. tabs={tabs}
  218. onChange={key => {
  219. this.onChangeTab(1, key);
  220. }}
  221. />
  222. {children && children.length > 1 && (
  223. <Tabs active={tab2} tabs={children} onChange={key => this.onChangeTab(2, key)} />
  224. )}
  225. </Module>
  226. {tab1 !== TEXTBOOK && this.renderExamination()}
  227. {tab1 === TEXTBOOK && this.renderTextbook()}
  228. {this.state.faqs && <QAList data={this.state.faqs} active={'faq'} tabs={[{ key: 'faq', name: 'FAQs' }]} />}
  229. </div>
  230. </div>
  231. );
  232. }
  233. renderTextbook() {
  234. const { textbookProgress = [] } = this.state;
  235. return (
  236. <div>
  237. <Division col={2}>
  238. {(textbookProgress || []).map(struct => {
  239. if (struct.needService && !struct.hasService) {
  240. if (struct.unUseRecord) {
  241. return <WaitPanel
  242. title={struct.isLatest ? '最新' : '往期'}
  243. col="3"
  244. data={struct}
  245. onOpen={() => {
  246. this.open(struct.unUseRecord.id);
  247. }}
  248. />;
  249. }
  250. return <BuyPanel
  251. title={struct.isLatest ? '最新' : '往期'}
  252. onBuy={() => {
  253. this.buyTextbook();
  254. }}
  255. />;
  256. }
  257. return <Panel
  258. title={struct.isLatest ? '最新' : '往期'}
  259. col="3"
  260. data={struct}
  261. onClick={(item) => {
  262. this.textbookList(item, struct.isLatest);
  263. }}
  264. />;
  265. })}
  266. </Division>
  267. </div>
  268. );
  269. }
  270. renderExamination() {
  271. const { examinationProgress = [] } = this.state;
  272. return (
  273. <div>
  274. <Division col={3} type="compact">
  275. {(examinationProgress || []).map(struct => {
  276. if (struct.hasService) {
  277. return <SmallPanel
  278. title={struct.title}
  279. data={struct}
  280. onClick={() => {
  281. this.examinationList(struct);
  282. }}
  283. />;
  284. } if (struct.unUseRecord) {
  285. return <SmallWaitPanel
  286. title={struct.title}
  287. data={struct}
  288. onOpen={() => {
  289. this.open(struct.unUseRecord.id);
  290. }}
  291. />;
  292. }
  293. return <SmallBuyPanel
  294. title={struct.title}
  295. data={struct}
  296. onBuy={() => {
  297. this.buyQxCat();
  298. }}
  299. />;
  300. })}
  301. </Division>
  302. </div>
  303. );
  304. }
  305. }