index.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import {
  2. PropType,
  3. defineComponent,
  4. onMounted,
  5. onUnmounted,
  6. reactive,
  7. watch
  8. } from 'vue';
  9. import ResourceSearchGroup from './resource-search-group';
  10. import { NModal, NScrollbar, NSpin } from 'naive-ui';
  11. import styles from './index.module.less';
  12. import TheEmpty from '/src/components/TheEmpty';
  13. import { useThrottleFn } from '@vueuse/core';
  14. import { usePrepareStore } from '/src/store/modules/prepareLessons';
  15. import { api_queryOpenCoursewareByPage } from '/src/views/prepare-lessons/api';
  16. import Item from './item';
  17. import { eventGlobal } from '/src/utils';
  18. export default defineComponent({
  19. name: 'share-resources',
  20. emits: ['look', 'add'],
  21. setup(props, { emit }) {
  22. const prepareStore = usePrepareStore();
  23. const state = reactive({
  24. loading: false,
  25. finshed: false, // 是否加载完
  26. pagination: {
  27. page: 1,
  28. rows: 10
  29. },
  30. searchGroup: {
  31. keyword: ''
  32. },
  33. tableList: [] as any,
  34. editStatus: false,
  35. editItem: {} as any,
  36. show: false,
  37. item: {} as any
  38. });
  39. const getList = async () => {
  40. try {
  41. if (!prepareStore.getSelectKey) return;
  42. if (state.pagination.page === 1) {
  43. state.loading = true;
  44. }
  45. // 查询公开课件列表
  46. const { data } = await api_queryOpenCoursewareByPage({
  47. subjectId: prepareStore.getSubjectId,
  48. coursewareDetailKnowledgeId: prepareStore.getSelectKey,
  49. ...state.searchGroup,
  50. ...state.pagination
  51. });
  52. if (state.pagination.page === 1 && state.tableList.length > 0) {
  53. state.tableList = [];
  54. }
  55. const result = data.rows || [];
  56. const tempList: any = [];
  57. result.forEach((item: any) => {
  58. // const index = forms.tableList.findIndex(
  59. // (i: any) => i.fromChapterLessonCoursewareId === item.id
  60. // );
  61. const firstItem: any =
  62. item.chapterKnowledgeList[0]?.chapterKnowledgeMaterialList[0];
  63. tempList.push({
  64. id: item.id,
  65. openFlag: item.openFlag,
  66. openFlagEnable: item.openFlagEnable,
  67. subjectNames: item.subjectNames,
  68. fromChapterLessonCoursewareId: item.fromChapterLessonCoursewareId,
  69. name: item.name,
  70. coverImg: firstItem?.bizInfo.coverImg,
  71. type: firstItem?.bizInfo.type,
  72. isAdd: item.addFlag
  73. });
  74. });
  75. state.loading = false;
  76. state.tableList.push(...tempList);
  77. // console.log(result, 'result', data);
  78. state.finshed = data.pages <= data.current ? true : false;
  79. } catch {
  80. state.loading = false;
  81. }
  82. };
  83. const onSearch = async (item: any) => {
  84. state.pagination.page = 1;
  85. state.tableList = [];
  86. state.searchGroup = Object.assign(state.searchGroup, item);
  87. getList();
  88. };
  89. // 声部变化时
  90. // watch(
  91. // () => prepareStore.getSubjectId,
  92. // () => {
  93. // onSearch(state.searchGroup);
  94. // }
  95. // );
  96. const throttledFn = useThrottleFn(() => {
  97. state.pagination.page = state.pagination.page + 1;
  98. getList();
  99. }, 500);
  100. const eventChanged = () => {
  101. onSearch(state.searchGroup);
  102. };
  103. onMounted(() => {
  104. getList();
  105. eventGlobal.on('openCoursewareChanged', eventChanged);
  106. });
  107. onUnmounted(() => {
  108. eventGlobal.off('openCoursewareChanged', eventChanged);
  109. });
  110. return () => (
  111. <div>
  112. <ResourceSearchGroup onSearch={(item: any) => onSearch(item)} />
  113. <NScrollbar
  114. class={[styles.listContainer, styles.listNoMusic]}
  115. onScroll={(e: any) => {
  116. const clientHeight = e.target?.clientHeight;
  117. const scrollTop = e.target?.scrollTop;
  118. const scrollHeight = e.target?.scrollHeight;
  119. // 是否到底,是否加载完
  120. if (
  121. clientHeight + scrollTop + 20 >= scrollHeight &&
  122. !state.finshed &&
  123. !state.loading
  124. ) {
  125. throttledFn();
  126. }
  127. }}>
  128. <NSpin show={state.loading} size={'small'}>
  129. <div
  130. class={[
  131. styles.listSection,
  132. !state.loading && state.tableList.length <= 0
  133. ? styles.emptySection
  134. : ''
  135. ]}>
  136. {state.tableList.length > 0 && (
  137. <div class={styles.list}>
  138. {state.tableList.map((item: any) => (
  139. <Item
  140. item={item}
  141. onAdd={() => emit('add', item)}
  142. onLook={() => emit('look', item)}
  143. />
  144. ))}
  145. </div>
  146. )}
  147. {!state.loading && state.tableList.length <= 0 && <TheEmpty />}
  148. </div>
  149. </NSpin>
  150. </NScrollbar>
  151. </div>
  152. );
  153. }
  154. });