index.tsx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. instrumentId: prepareStore.getInstrumentId,
  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. instrumentNames: item.instrumentNames,
  68. fromChapterLessonCoursewareId: item.fromChapterLessonCoursewareId,
  69. name: item.name,
  70. coverImg: firstItem?.bizInfo.coverImg,
  71. type: firstItem?.bizInfo.type,
  72. isAdd: item.addFlag,
  73. coursewareType: item.coursewareType
  74. });
  75. });
  76. state.loading = false;
  77. state.tableList.push(...tempList);
  78. // console.log(result, 'result', data);
  79. state.finshed = data.pages <= data.current ? true : false;
  80. } catch {
  81. state.loading = false;
  82. }
  83. };
  84. const onSearch = async (item: any) => {
  85. state.pagination.page = 1;
  86. state.tableList = [];
  87. state.searchGroup = Object.assign(state.searchGroup, item);
  88. getList();
  89. };
  90. // 声部变化时
  91. // watch(
  92. // () => prepareStore.getSubjectId,
  93. // () => {
  94. // onSearch(state.searchGroup);
  95. // }
  96. // );
  97. const throttledFn = useThrottleFn(() => {
  98. state.pagination.page = state.pagination.page + 1;
  99. getList();
  100. }, 500);
  101. const eventChanged = () => {
  102. onSearch(state.searchGroup);
  103. };
  104. onMounted(() => {
  105. getList();
  106. eventGlobal.on('openCoursewareChanged', eventChanged);
  107. });
  108. onUnmounted(() => {
  109. eventGlobal.off('openCoursewareChanged', eventChanged);
  110. });
  111. return () => (
  112. <div>
  113. <ResourceSearchGroup onSearch={(item: any) => onSearch(item)} />
  114. <NScrollbar
  115. class={[styles.listContainer, styles.listNoMusic]}
  116. onScroll={(e: any) => {
  117. const clientHeight = e.target?.clientHeight;
  118. const scrollTop = e.target?.scrollTop;
  119. const scrollHeight = e.target?.scrollHeight;
  120. // 是否到底,是否加载完
  121. if (
  122. clientHeight + scrollTop + 20 >= scrollHeight &&
  123. !state.finshed &&
  124. !state.loading
  125. ) {
  126. throttledFn();
  127. }
  128. }}>
  129. <NSpin show={state.loading} size={'small'}>
  130. <div
  131. class={[
  132. styles.listSection,
  133. !state.loading && state.tableList.length <= 0
  134. ? styles.emptySection
  135. : ''
  136. ]}>
  137. {state.tableList.length > 0 && (
  138. <div class={styles.list}>
  139. {state.tableList.map((item: any) => (
  140. <Item
  141. item={item}
  142. onAdd={() => emit('add', item)}
  143. onLook={() => emit('look', item)}
  144. />
  145. ))}
  146. </div>
  147. )}
  148. {!state.loading && state.tableList.length <= 0 && <TheEmpty />}
  149. </div>
  150. </NSpin>
  151. </NScrollbar>
  152. </div>
  153. );
  154. }
  155. });