index.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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, questionTypes, 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. questionTypes: [],
  58. };
  59. }
  60. const item = map[row.key];
  61. item.structIds.push(row.id);
  62. if (item.questionTypes.indexOf(row.extend) < 0) {
  63. item.questionTypes.push(row.extend);
  64. }
  65. });
  66. let list = Object.values(map);
  67. if (questionTypes && questionTypes.length > 0) {
  68. list = list.filter(row => row.questionTypes.filter((v) => questionTypes.indexOf(v) >= 0).length);
  69. }
  70. let courseModules = null;
  71. let structIds = null;
  72. if (one === 'preview') {
  73. if (!two) {
  74. courseModules = CourseModule.map(row => row.value);
  75. } else {
  76. courseModules = [two];
  77. }
  78. } else if (one) {
  79. const resultMap = getMap(list, 'key', 'structIds');
  80. if (!two) {
  81. structIds = resultMap[one];
  82. } else {
  83. structIds = resultMap[two];
  84. }
  85. }
  86. const tree = formatTreeData(list, 'key', 'title', 'parentId');
  87. const oneSelect = tree;
  88. const twoSelectMap = getMap(tree, 'key', 'children');
  89. if (all) {
  90. oneSelect.forEach(row => {
  91. row.children.unshift({
  92. title: '全部',
  93. key: '',
  94. });
  95. });
  96. oneSelect.unshift({
  97. title: '全部',
  98. key: '',
  99. });
  100. }
  101. compontent.setState({ oneSelect, twoSelectMap });
  102. return {
  103. structIds,
  104. courseModules,
  105. };
  106. });
  107. default:
  108. return Promise.resolve({});
  109. }
  110. }