page.js 9.8 KB

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