index.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { getMap, formatTreeData } from '@src/services/Tools';
  2. import detail from './detail';
  3. import search from './search';
  4. import searchHistory from './searchHistory';
  5. import { CourseModule } from '../../../Constant';
  6. import { Main } from '../../stores/main';
  7. export default [detail, search, searchHistory];
  8. export function refreshQuestionType(compontent, subject, questionType, { all, needSentence, allSubject, excludeAwa }) {
  9. return Main.getExercise().then(result => {
  10. const list = result.filter(row => (needSentence ? true : row.isExamination)).map(row => {
  11. row.title = `${row.titleZh}${row.titleEn}`;
  12. row.key = row.extend;
  13. return row;
  14. });
  15. const tree = formatTreeData(list, 'id', 'title', 'parentId');
  16. compontent.questionSubjectMap = getMap(tree, 'key', 'children');
  17. compontent.questionSubjectSelect = tree.filter(row => row.level === 1 && (allSubject ? true : row.children.length > 1) && (excludeAwa ? row.extend !== 'awa' : true));
  18. if (all) {
  19. compontent.questionSubjectSelect.forEach(row => {
  20. row.children.unshift({
  21. title: '全部',
  22. key: '',
  23. });
  24. });
  25. compontent.questionSubjectSelect.unshift({
  26. title: '全部',
  27. key: '',
  28. });
  29. }
  30. compontent.setState({
  31. questionSubjectSelect: compontent.questionSubjectSelect,
  32. questionSubjectMap: compontent.questionSubjectMap,
  33. });
  34. return {
  35. questionTypes: questionType || (subject ? compontent.questionSubjectMap[subject].map(row => row.key).filter(row => row) : null),
  36. };
  37. });
  38. }
  39. export function refreshStruct(compontent, module, one, two, { all }) {
  40. switch (module) {
  41. case 'exercise':
  42. return Main.getExerciseAll().then(result => {
  43. const tmp = result.filter(row => row.level > 2).map(row => {
  44. row.title = `${row.titleZh}`;
  45. row.key = row.titleEn;
  46. return row;
  47. });
  48. const idsMap = getMap(tmp, 'id', 'key');
  49. const map = {};
  50. tmp.forEach(row => {
  51. if (!map[row.key]) {
  52. map[row.key] = {
  53. title: row.title,
  54. key: row.key,
  55. structIds: [],
  56. parentId: row.level > 3 ? idsMap[row.parentId] : null,
  57. subject: [],
  58. questionType: [],
  59. };
  60. }
  61. const item = map[row.key];
  62. item.structIds.push(row.id);
  63. if (item.subject.indexOf(row.subject) < 0) {
  64. item.subject.push(row.subject);
  65. }
  66. if (item.questionType.indexOf(row.questionType) < 0) {
  67. item.questionType.push(row.questionType);
  68. }
  69. });
  70. const list = Object.values(map);
  71. let courseModules = null;
  72. let structIds = null;
  73. if (one === 'preview') {
  74. if (!two) {
  75. courseModules = CourseModule.map(row => row.value);
  76. } else {
  77. courseModules = [two];
  78. }
  79. } else if (one) {
  80. const resultMap = getMap(list, 'key', 'structIds');
  81. if (!two) {
  82. structIds = resultMap[one];
  83. } else {
  84. structIds = resultMap[two];
  85. }
  86. }
  87. const tree = formatTreeData(list, 'key', 'title', 'parentId');
  88. const oneSelect = tree;
  89. const twoSelectMap = getMap(tree, 'key', 'children');
  90. if (all) {
  91. oneSelect.forEach(row => {
  92. row.children.unshift({
  93. title: '全部',
  94. key: '',
  95. });
  96. });
  97. oneSelect.unshift({
  98. title: '全部',
  99. key: '',
  100. });
  101. }
  102. compontent.setState({ oneSelect, twoSelectMap });
  103. return {
  104. structIds,
  105. courseModules,
  106. };
  107. });
  108. default:
  109. return Promise.resolve({});
  110. }
  111. }