page.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. }).catch(e => {
  111. console.log(e);
  112. });
  113. Main.listFaq({ page: 1, size: 100, channel: 'textbook' }).then(result => {
  114. this.setState({ faqs: result.list });
  115. });
  116. }
  117. refreshExamination(tab) {
  118. const { tabs, tab1 } = this.state;
  119. if (!tabs) {
  120. // 等待数据加载
  121. return;
  122. }
  123. const [subject] = tabs.filter(row => row.key === tab || row.key === tab1);
  124. Question.getExaminationProgress(subject.id).then(result => {
  125. // const exerciseProgress = getMap(r, 'id');
  126. result = result.map(row => {
  127. row.title = `${row.titleZh}`;
  128. row.info = [
  129. {
  130. title: '已做',
  131. number: row.userNumber || '-',
  132. unit: '套',
  133. },
  134. {
  135. title: '剩余',
  136. number: row.userNumber ? row.paperNumber - row.userNumber : '-',
  137. unit: '套',
  138. },
  139. ];
  140. row.pieValue = formatPercent(row.userNumber, row.paperNumber);
  141. row.pieText = formatPercent(row.userNumber, row.paperNumber, false);
  142. row.pieSubText = `共${row.paperNumber}套`;
  143. if (row.unUseRecord) User.formatCheckout([row.unUseRecord]);
  144. return row;
  145. });
  146. this.setState({ examinationProgress: result });
  147. });
  148. Main.listFaq({ page: 1, size: 100, channel: `examination-${subject.extend}` }).then(result => {
  149. this.setState({ faqs: result.list });
  150. });
  151. }
  152. onChangeTab(level, tab) {
  153. const { tab1 } = this.state;
  154. const data = {};
  155. if (level > 1) {
  156. data.tab1 = tab1;
  157. data.tab2 = tab;
  158. } else {
  159. data.tab1 = tab;
  160. }
  161. // this.refreshData(tab);
  162. this.refreshQuery(data);
  163. }
  164. onTextbook() {
  165. const { tab1, tab2, struct } = this.state;
  166. const data = {
  167. tab1, tab2, struct,
  168. };
  169. this.refreshQuery(data);
  170. }
  171. // 开通模考或者机经
  172. open(recordId) {
  173. Order.useRecord(recordId).then(() => {
  174. this.refresh();
  175. });
  176. }
  177. examinationList(item) {
  178. User.needLogin().then(() => {
  179. linkTo(`/examination/list/${item.id}`);
  180. });
  181. }
  182. textbookList(item, isLatest) {
  183. User.needLogin().then(() => {
  184. linkTo(`/textbook/list?logic=${encodeURIComponent(item.logic)}&latest=${isLatest ? 1 : 0}`);
  185. });
  186. }
  187. buyTextbook() {
  188. User.needLogin()
  189. .then(() => {
  190. return Order.speedPay({ productType: 'service', service: 'textbook' });
  191. })
  192. .then((order) => {
  193. return User.needPay(order);
  194. })
  195. .then(() => {
  196. this.refresh();
  197. });
  198. }
  199. buyQxCat() {
  200. User.needLogin()
  201. .then(() => {
  202. return Order.speedPay({ productType: 'service', service: 'qx_cat' });
  203. })
  204. .then((order) => {
  205. return User.needPay(order);
  206. })
  207. .then(() => {
  208. this.refresh();
  209. });
  210. }
  211. renderView() {
  212. const { tab1, tab2, tabs, record } = this.state;
  213. const [subject] = tabs.filter(row => row.key === tab1);
  214. const children = (subject && subject.children) ? subject.children : [];
  215. return (
  216. <div>
  217. <div className="content">
  218. <Module className="m-t-2">
  219. <Tabs
  220. type="card"
  221. active={tab1}
  222. tabs={tabs}
  223. onChange={key => {
  224. this.onChangeTab(1, key);
  225. }}
  226. />
  227. {children && children.length > 1 && (
  228. <Tabs active={tab2} tabs={children} onChange={key => this.onChangeTab(2, key)} />
  229. )}
  230. </Module>
  231. {tab1 !== TEXTBOOK && this.renderExamination()}
  232. {tab1 === TEXTBOOK && this.renderTextbook()}
  233. {this.state.faqs && <QAList data={this.state.faqs} active={'faq'} tabs={[{ key: 'faq', name: 'FAQs' }]} />}
  234. </div>
  235. <OpenConfirmModal show={record} data={record} onCancel={() => this.setState({ record: null })} onConfirm={() => this.open(record.id)} />
  236. </div>
  237. );
  238. }
  239. renderTextbook() {
  240. const { textbookProgress = [] } = this.state;
  241. return (
  242. <div>
  243. <Division col={2}>
  244. {(textbookProgress || []).map(struct => {
  245. if (struct.needService && !struct.hasService) {
  246. if (struct.unUseRecord) {
  247. return <WaitPanel
  248. title={struct.isLatest ? '最新' : '往期'}
  249. col="3"
  250. data={struct}
  251. onOpen={() => {
  252. struct.unUseRecord.title = '数学机经';
  253. this.setState({ record: struct.unUseRecord });
  254. }}
  255. />;
  256. }
  257. return <BuyPanel
  258. title={struct.isLatest ? '最新' : '往期'}
  259. onBuy={() => {
  260. this.buyTextbook();
  261. }}
  262. />;
  263. }
  264. return <Panel
  265. title={struct.isLatest ? '最新' : '往期'}
  266. col="3"
  267. data={struct}
  268. onClick={(item) => {
  269. this.textbookList(item, struct.isLatest);
  270. }}
  271. />;
  272. })}
  273. </Division>
  274. </div>
  275. );
  276. }
  277. renderExamination() {
  278. const { examinationProgress = [] } = this.state;
  279. return (
  280. <div>
  281. <Division col={3} type="compact">
  282. {(examinationProgress || []).map(struct => {
  283. if (struct.hasService) {
  284. return <SmallPanel
  285. title={struct.title}
  286. data={struct}
  287. onClick={() => {
  288. this.examinationList(struct);
  289. }}
  290. />;
  291. } if (struct.unUseRecord) {
  292. return <SmallWaitPanel
  293. title={struct.title}
  294. data={struct}
  295. onOpen={() => {
  296. struct.unUseRecord.title = struct.title;
  297. this.setState({ record: struct.unUseRecord });
  298. }}
  299. />;
  300. }
  301. return <SmallBuyPanel
  302. title={struct.title}
  303. data={struct}
  304. onBuy={() => {
  305. this.buyQxCat();
  306. }}
  307. />;
  308. })}
  309. </Division>
  310. </div>
  311. );
  312. }
  313. }