page.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. import React from 'react';
  2. import './index.less';
  3. import Page from '@src/containers/Page';
  4. import { asyncConfirm } from '@src/services/AsyncTools';
  5. import { formatPercent, formatSeconds, formatDate } from '@src/services/Tools';
  6. import Tabs from '../../../components/Tabs';
  7. import Module from '../../../components/Module';
  8. import ListTable from '../../../components/ListTable';
  9. import ProgressText from '../../../components/ProgressText';
  10. import IconButton from '../../../components/IconButton';
  11. import { Main } from '../../../stores/main';
  12. import { Question } from '../../../stores/question';
  13. import { QuestionDifficult } from '../../../../Constant';
  14. const LOGIC_NO = 'no';
  15. const LOGIC_PLACE = 'place';
  16. const LOGIC_DIFFICULT = 'difficult';
  17. const LOGIC_ERROR = 'error';
  18. export default class extends Page {
  19. initState() {
  20. this.columns = [
  21. {
  22. title: '练习册',
  23. width: 250,
  24. align: 'left',
  25. render: (record) => {
  26. let progress = 0;
  27. if (record.report) {
  28. progress = formatPercent(record.report.userNumber, record.report.questionNumber);
  29. }
  30. return (
  31. <div className="table-row">
  32. <div className="night f-s-16">{record.title}</div>
  33. <div>
  34. <ProgressText progress={progress} size="small" />
  35. </div>
  36. </div>
  37. );
  38. },
  39. },
  40. {
  41. title: '正确率',
  42. width: 150,
  43. align: 'left',
  44. render: (record) => {
  45. let correct = '--';
  46. if (record.report) {
  47. correct = formatPercent(record.report.userCorrect, record.report.userNumber, false);
  48. }
  49. return (
  50. <div className="table-row">
  51. <div className="night f-s-16 f-w-b">{correct}</div>
  52. <div className="f-s-12">全站{formatPercent(record.stat.totalCorrect, record.stat.totalNumber, false)}</div>
  53. </div>
  54. );
  55. },
  56. },
  57. {
  58. title: '全站用时',
  59. width: 150,
  60. align: 'left',
  61. render: (record) => {
  62. let time = '--';
  63. if (record.report) {
  64. time = formatSeconds(record.report.userTime / record.report.userNumber);
  65. }
  66. return (
  67. <div className="table-row">
  68. <div className="night f-s-16 f-w-b">{time}</div>
  69. <div className="f-s-12">全站{formatSeconds(record.stat.totalTime / record.stat.totalNumber)}</div>
  70. </div>
  71. );
  72. },
  73. },
  74. {
  75. title: '最近做题',
  76. width: 150,
  77. align: 'left',
  78. render: (record) => {
  79. if (!record.report) return null;
  80. return (
  81. <div className="table-row">
  82. <div>{formatDate(record.report.updateTime, 'YYYY-MM-DD')}</div>
  83. <div>{formatDate(record.report.updateTime, 'HH:mm')}</div>
  84. </div>
  85. );
  86. },
  87. },
  88. {
  89. title: '操作',
  90. width: 180,
  91. align: 'left',
  92. render: (record) => {
  93. return (
  94. <div className="table-row p-t-1">
  95. {!record.report && <IconButton type="start" tip="Start" onClick={() => {
  96. Question.startLink('exercise', record);
  97. }} />}
  98. {(record.report && !record.report.isFinish) && <IconButton className="m-r-2" type="continue" tip="Continue" onClick={() => {
  99. Question.continueLink('exercise', record);
  100. }} />}
  101. {(record.report) && <IconButton type="restart" tip="Restart" onClick={() => {
  102. this.restart(record);
  103. }} />}
  104. </div>
  105. );
  106. },
  107. },
  108. {
  109. title: '报告',
  110. width: 30,
  111. align: 'right',
  112. render: (record) => {
  113. if (!record.report || !record.report.isFinish) return null;
  114. return (
  115. <div className="table-row p-t-1">
  116. <IconButton type="report" tip="Report" onClick={() => {
  117. Question.reportLink(record);
  118. }} />
  119. </div>
  120. );
  121. },
  122. },
  123. ];
  124. this.placeList = [];
  125. this.inited = false;
  126. return {
  127. logic: LOGIC_NO,
  128. logicExtend: '',
  129. logics: [{
  130. key: LOGIC_NO,
  131. title: '按顺序练习',
  132. }, {
  133. key: LOGIC_PLACE,
  134. title: '按考点练习',
  135. }, {
  136. key: LOGIC_DIFFICULT,
  137. title: '按难度练习',
  138. }, {
  139. key: LOGIC_ERROR,
  140. title: '按易错度练习',
  141. }],
  142. };
  143. }
  144. init() {
  145. const { id } = this.params;
  146. Main.getExerciseParent(id).then(result => {
  147. const navs = result;
  148. this.inited = true;
  149. this.setState({ navs });
  150. });
  151. }
  152. initData() {
  153. const data = Object.assign(this.state, this.state.search);
  154. this.setState(data);
  155. this.refreshData();
  156. }
  157. refreshData(newLogic) {
  158. const { logic } = this.state;
  159. let handler = null;
  160. switch (newLogic || logic) {
  161. case LOGIC_PLACE:
  162. handler = this.refreshPlace();
  163. break;
  164. case LOGIC_DIFFICULT:
  165. handler = this.refreshDifficult();
  166. break;
  167. default:
  168. handler = Promise.resolve();
  169. }
  170. handler.then(() => {
  171. this.refreshExercise();
  172. });
  173. }
  174. refreshPlace() {
  175. const { id } = this.params;
  176. let handler;
  177. if (this.placeList.length > 0) {
  178. this.setState({ logicExtends: this.placeList });
  179. handler = Promise.resolve();
  180. } else {
  181. handler = Question.getExercisePlace(id).then(result => {
  182. this.placeList = result.map(row => {
  183. return {
  184. name: row,
  185. key: row,
  186. };
  187. });
  188. this.setState({ logicExtends: this.placeList });
  189. });
  190. }
  191. return handler.then(() => {
  192. let { logicExtend } = this.state;
  193. if (logicExtend === '') {
  194. logicExtend = this.placeList[0].key;
  195. this.setState({ logicExtend });
  196. }
  197. });
  198. }
  199. refreshDifficult() {
  200. let { logicExtend } = this.state;
  201. this.setState({
  202. logicExtends: QuestionDifficult.map(difficult => {
  203. difficult.name = difficult.label;
  204. difficult.key = difficult.value;
  205. return difficult;
  206. }),
  207. });
  208. return Promise.resolve().then(() => {
  209. if (logicExtend === '') {
  210. logicExtend = QuestionDifficult[0].key;
  211. this.setState({ logicExtend });
  212. }
  213. });
  214. }
  215. refreshExercise() {
  216. const { logic, logicExtend } = this.state;
  217. Question.getExerciseList(Object.assign({ structId: this.params.id, logic, logicExtend }, this.state.search))
  218. .then((result) => {
  219. this.setState({ list: result.list, total: result.total });
  220. });
  221. }
  222. onChangeTab(key, value) {
  223. const { logic } = this.state;
  224. const data = {};
  225. if (key === 'logicExtend') {
  226. data.logic = logic;
  227. data.logicExtend = value;
  228. } else {
  229. data.logic = value;
  230. }
  231. // this.refreshData(tab);
  232. this.refreshQuery(data);
  233. }
  234. restart(item) {
  235. asyncConfirm('提示', '你打算重做本套练习,过往做题记录可至「个人中心-报告」查看。', () => {
  236. Question.restart(item.paper.id).then(() => {
  237. this.refresh();
  238. });
  239. });
  240. }
  241. renderView() {
  242. const { logic, logicExtend, logics = [], logicExtends = [], list } = this.state;
  243. return (
  244. <div>
  245. <div className="content">
  246. <Module className="m-t-2">
  247. <Tabs
  248. active={logic}
  249. border
  250. width="180px"
  251. space="0"
  252. tabs={logics}
  253. onChange={(key) => {
  254. this.onChangeTab('logic', key);
  255. }}
  256. />
  257. {logicExtends.length > 0 && <Tabs
  258. active={logicExtend}
  259. type="text"
  260. tabs={logicExtends}
  261. onChange={(key) => {
  262. this.onChangeTab('logicExtend', key);
  263. }}
  264. />}
  265. </Module>
  266. <ListTable
  267. data={list}
  268. columns={this.columns}
  269. />
  270. </div>
  271. </div>
  272. );
  273. }
  274. }