index.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. import { getMap, formatTreeData } from '@src/services/Tools';
  2. import main from './main';
  3. import course from './course';
  4. import tools from './tools';
  5. import error from './error';
  6. import note from './note';
  7. import answer from './answer';
  8. import report from './report';
  9. import data from './data';
  10. import collect from './collect';
  11. import order from './order';
  12. import message from './message';
  13. import { TextbookMinYear, CourseModule } from '../../../Constant';
  14. import { Main } from '../../stores/main';
  15. export default [main, course, tools, error, note, answer, report, data, collect, order, message];
  16. export function refreshQuestionType(compontent, subject, questionType, { all, needSentence, allSubject, onlyPreview }) {
  17. return Main.getExercise().then(result => {
  18. const list = result.filter(row => (needSentence ? true : row.isExamination)).filter(row => (onlyPreview ? row.isData || ['ds', 'ps'].indexOf(row.extend) >= 0 : true)).map(row => {
  19. row.title = `${row.titleZh}${row.titleEn}`;
  20. row.key = row.extend;
  21. return row;
  22. });
  23. const tree = formatTreeData(list, 'id', 'title', 'parentId');
  24. compontent.questionSubjectMap = getMap(tree, 'key', 'children');
  25. compontent.questionSubjectSelect = tree.filter(row => row.level === 1 && (allSubject ? true : row.children.length > 1));
  26. if (all) {
  27. compontent.questionSubjectSelect.forEach(row => {
  28. if (row.children.length > 1) {
  29. row.children.unshift({
  30. title: '全部',
  31. key: '',
  32. });
  33. } else if (row.key === 'sentence') {
  34. row.children.unshift({
  35. title: '长难句',
  36. key: 'sentence',
  37. });
  38. }
  39. });
  40. compontent.questionSubjectSelect.unshift({
  41. title: '全部',
  42. key: '',
  43. });
  44. compontent.questionSubjectMap[''] = [{
  45. title: '全部',
  46. key: '',
  47. }];
  48. }
  49. if (questionType === subject) {
  50. questionType = null;
  51. }
  52. compontent.setState({
  53. questionSubjectSelect: compontent.questionSubjectSelect,
  54. questionSubjectMap: compontent.questionSubjectMap,
  55. });
  56. return {
  57. questionTypes: questionType || (subject ? compontent.questionSubjectMap[subject].map(row => row.key).filter(row => row) : null),
  58. selectSentence: subject === 'sentence',
  59. };
  60. });
  61. }
  62. export function refreshStruct(compontent, questionTypes, module, one, two, { all, needTextbook, needPreview, selectSentence }) {
  63. let oneSelectPlaceholder = '全部';
  64. let twoSelectPlaceholder = '全部';
  65. switch (module) {
  66. case 'exercise':
  67. return Main.getExerciseAll().then(result => {
  68. const tmp = result.filter(row => row.level > 2).map(row => {
  69. row.title = `${row.titleZh}`;
  70. row.key = `${row.titleEn}`;
  71. row.u = `${row.titleEn}${row.level}`;
  72. return row;
  73. });
  74. const idsMap = getMap(tmp, 'id', 'key');
  75. const map = {};
  76. tmp.forEach(row => {
  77. if (!map[row.key]) {
  78. map[row.key] = {
  79. title: row.title,
  80. key: row.key,
  81. structIds: [],
  82. parentId: row.level > 3 ? idsMap[row.parentId] : null,
  83. questionTypes: [],
  84. };
  85. }
  86. const item = map[row.key];
  87. item.structIds.push(row.id);
  88. if (item.questionTypes.indexOf(row.extend) < 0) {
  89. item.questionTypes.push(row.extend);
  90. }
  91. });
  92. let list = Object.values(map);
  93. if (questionTypes && questionTypes.length > 0) {
  94. list = list.filter(row => row.questionTypes.filter((v) => questionTypes.indexOf(v) >= 0).length);
  95. // 预习作业过滤
  96. if (needPreview) {
  97. const previewList = result.filter(row => row.level === 2).filter(row => questionTypes.indexOf(row.extend) >= 0).filter(row => row.isData || ['ds', 'ps'].indexOf(row.extend) >= 0);
  98. if (previewList.length === 0) needPreview = false;
  99. }
  100. }
  101. if (needPreview) {
  102. list.push({
  103. title: '预习作业',
  104. key: 'preview',
  105. id: 'preview',
  106. });
  107. CourseModule.forEach(row => {
  108. if (!row.hasPreview) return;
  109. list.push({
  110. title: row.label,
  111. key: row.value,
  112. parentId: 'preview',
  113. });
  114. });
  115. }
  116. let courseModules = null;
  117. let structIds = null;
  118. if (one === 'preview') {
  119. if (!two) {
  120. courseModules = CourseModule.map(row => row.value);
  121. } else {
  122. courseModules = [two];
  123. }
  124. } else if (one) {
  125. const resultMap = getMap(list, 'key', 'structIds');
  126. if (!two) {
  127. structIds = resultMap[one];
  128. } else {
  129. structIds = resultMap[two];
  130. }
  131. }
  132. const tree = formatTreeData(list, 'key', 'title', 'parentId');
  133. let oneSelect = tree;
  134. const twoSelectMap = getMap(tree, 'key', 'children');
  135. if (all) {
  136. oneSelect.forEach(row => {
  137. if (row.children.length > 1) {
  138. row.children.unshift({
  139. title: '全部',
  140. key: '',
  141. });
  142. } else if (row.children.length === 0) {
  143. row.children.unshift({
  144. title: row.title,
  145. key: '',
  146. });
  147. }
  148. });
  149. if (oneSelect.length > 1) {
  150. oneSelect.unshift({
  151. title: '全部',
  152. key: '',
  153. });
  154. } else if (oneSelect.length === 1) {
  155. oneSelectPlaceholder = null;
  156. }
  157. if (!one) {
  158. twoSelectMap[''] = [{
  159. title: '全部',
  160. key: '',
  161. }];
  162. }
  163. }
  164. if (one && twoSelectMap[one]) {
  165. if (twoSelectMap[one].length === 1) {
  166. twoSelectPlaceholder = null;
  167. } else if (twoSelectMap[one].length === 0 || !twoSelectMap[one]) {
  168. twoSelectMap[one] = [{
  169. title: '全部',
  170. key: '',
  171. }];
  172. }
  173. }
  174. if (selectSentence) {
  175. oneSelect = [{
  176. title: '千行长难句',
  177. key: '',
  178. }];
  179. twoSelectMap[''] = [{
  180. title: '千行长难句',
  181. key: '',
  182. }];
  183. oneSelectPlaceholder = null;
  184. twoSelectPlaceholder = '千行长难句';
  185. }
  186. compontent.setState({ oneSelect, twoSelectMap, oneSelectPlaceholder, twoSelectPlaceholder });
  187. return {
  188. structIds,
  189. courseModules,
  190. };
  191. });
  192. case 'examination':
  193. return Main.getExamination().then(result => {
  194. const list = result.map(row => {
  195. row.title = `${row.titleZh}${row.titleEn}`;
  196. row.key = `${row.id}`;
  197. return row;
  198. });
  199. if (needTextbook) {
  200. list.push({
  201. title: '数学机经',
  202. key: 'textbook',
  203. id: 'textbook',
  204. });
  205. list.push({
  206. title: '最新',
  207. key: 'latest',
  208. parentId: 'textbook',
  209. });
  210. const nowYear = new Date().getFullYear();
  211. for (let i = TextbookMinYear; i <= nowYear; i += 1) {
  212. list.push({
  213. title: i.toString(),
  214. key: i.toString(),
  215. parentId: 'textbook',
  216. });
  217. }
  218. }
  219. let latest = null;
  220. let year = null;
  221. let structIds = null;
  222. if (one === 'textbook') {
  223. if (!two) {
  224. latest = false;
  225. } else if (two === 'latest') {
  226. latest = true;
  227. } else {
  228. year = two;
  229. }
  230. } else if (one) {
  231. if (!two) {
  232. structIds = [Number(one)];
  233. } else {
  234. structIds = [Number(two)];
  235. }
  236. }
  237. const tree = formatTreeData(list, 'key', 'title', 'parentId');
  238. let oneSelect = tree;
  239. const twoSelectMap = getMap(tree, 'key', 'children');
  240. if (all) {
  241. oneSelect.forEach(row => {
  242. if (row.children.length > 1) {
  243. row.children.unshift({
  244. title: '全部',
  245. key: '',
  246. });
  247. } else if (row.children.length === 0) {
  248. row.children.unshift({
  249. title: row.title,
  250. key: '',
  251. });
  252. }
  253. });
  254. if (oneSelect.length > 1) {
  255. oneSelect.unshift({
  256. title: '全部',
  257. key: '',
  258. });
  259. } else if (oneSelect.length === 1) {
  260. oneSelectPlaceholder = null;
  261. }
  262. if (!one) {
  263. twoSelectMap[''] = [{
  264. title: '全部',
  265. key: '',
  266. }];
  267. }
  268. }
  269. if (one && twoSelectMap[one]) {
  270. if (twoSelectMap[one].length === 1) {
  271. twoSelectPlaceholder = null;
  272. } else if (twoSelectMap[one].length === 0 || !twoSelectMap[one]) {
  273. twoSelectMap[one] = [{
  274. title: '全部',
  275. key: '',
  276. }];
  277. }
  278. }
  279. if (selectSentence) {
  280. oneSelect = [{
  281. title: '千行长难句',
  282. key: '',
  283. }];
  284. twoSelectMap[''] = [{
  285. title: '千行长难句',
  286. key: '',
  287. }];
  288. oneSelectPlaceholder = null;
  289. twoSelectPlaceholder = null;
  290. }
  291. compontent.setState({ oneSelect, twoSelectMap, oneSelectPlaceholder, twoSelectPlaceholder });
  292. return {
  293. structIds,
  294. latest,
  295. year,
  296. };
  297. });
  298. default:
  299. return Promise.resolve({});
  300. }
  301. }