index.tsx 4.6 KB

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